Android create view programmatically

romannurik / AndroidManifest.xml

This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters

manifest . >
.
Make sure your app (or individual activity) uses the
theme with the custom attribute defined. —>
application android : theme = » @style/AppTheme » . >
.
application >
manifest >

This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters

public class MyActivity extends Activity <
protected void onCreate ( Bundle savedInstanceState ) <
super . onCreate(savedInstanceState);
.
// Pass in the theme attribute that’ll resolve to the
// desired button style resource. The current theme
// must have a value set for this attribute.
Button myButton = new Button ( this , null , R . attr . myButtonStyle);
myButton . setText( » Hello world » );
ViewGroup containerView = ( ViewGroup ) findViewById( R . id . container);
containerView . addView(myButton);
>
>

This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters

The convention is to put the

And TextView initialization does:

But it doesn’t recognize fontFamily attribute :S Is it a normal behaviour?

This comment has been minimized.

Copy link Quote reply

jrub commented Jun 24, 2015

Very useful indeed, thanks Roman. This should definitely be included into the official documentation for Themes & Styles (http://developer.android.com/guide/topics/ui/themes.html)

This comment has been minimized.

Copy link Quote reply

ursusursus commented Feb 19, 2016

For me this does not apply layout params like layout_width, layout_height when I have them in my style. Only default wrap_content LayoutParams gets created in both dimensions

Источник

Tutorialwing

Hello Readers! In this post, you will learn how to create TextView programmatically in android. You will go through different steps that explains to create Textview dynamically and adding it in java file.

Output

Tutorialwing Dynamic TextView Tutorial Output

Tutorialwing Dynamic TextView Tutorial Output

Video Output

First step is to create New Project. Follow the steps below to create new project. Please ignore the steps if you have already created project.

1. Creating New Project

Step Description
1. Open Android Studio.
2. Go to File => New => New Project. Write application name as DynamicTextView. Then, click next button.
3. Select minimum SDK you need. However, we have selected 17 as minimum SDK. Then, click next button
4. Then, select Empty Activity => click next => click finish.
5. If you have followed above process correctly, you will get a newly created project successfully. However, you can also visit post to create a new project to know steps in detail.

Now, we will modify the xml and java files to create TextView programmatically and add it in xml file. Please follow the steps below.

2. Modify Values Folder

Open res/values/strings.xml file and add below code into it.

Other values folders have not been changed. So, we are not going to mention it here.

3. Modify Layout Folder

Open res/layout/activity_main.xml file. Add below code into it.

Note that LinearLayout has id rootLayout. In Java file, we will create TextView dynamically and add it into this LinearLayout having id rootLayout.

4.Create Android TextView Programmatically / Dynamically

Open src/main/java/com.tutorialwing.dynamictextview/MainActivity.java file. Then, add below code into it. You may also visit post to use Textview widget in xml file in android

Note that we are creating TextView Dynamically. Then, We have added it’s layoutParams, gravity and other attributes dynamically. Finally, added this TextView in LinearLayout having id rootLayout.

AndroidManifest.xml file

Since AndroidManifest file is very important for project. We are also going to mention it here. Code inside main/AndroidManifest.xml file is as below.

Finally, when you run the application, you will get output as shown above.

Источник

Tutorialwing

In this article, we will learn how to create android ImageView programmatically in Kotlin. We will go through various steps that explains how to create ImageView and add it in kotlin file, use different attributes to customise it etc. in any android application. For example, how to set text in ImageView programmatically, how to set id of ImageView, how to capitalise text of ImageView dynamically etc. We will get answer to all such questions in this post.

Output

Tutorialwing Kotlin Dynamic ImageView Output

Tutorialwing Kotlin Dynamic ImageView Output

Getting Started

We can define android ImageView widget as below –

ImageView is subclass of view that displays image. It is also used to handle image tinting and scaling.

Now, how do we use ImageView in android application ?

Creating New Project

Follow steps below to create any android project in Kotlin –

Step Description
1. Open Android Studio (Ignore if already done).
2. Go to File => New => New Project. This will open a new window. Then, under Phone and Tablet section, select Empty Activity. Then, click Next.
3. In next screen, select project name as DynamicImageView. Then, fill other required details.
4. Then, clicking on Finish button creates new project.

Newbie in Android ?
Some very important concepts (Recommended to learn before you move ahead)

Before we move ahead, we need to setup for viewBinding to access ImageView in Kotlin file without using findViewById() method.

Setup ViewBinding

Add viewBinding true in app/build.gradle file.

Now, set content in activity using view binding.
Open MainActivity.kt file and write below code in it.

Now, we can access view in Kotlin file without using findViewById() method.

Since we have a new project, we will modify the xml and class file to use ImageView programmatically in kotlin. Please follow the steps below.

2. Modify Values Folder

Open res/values/strings.xml file. Add below code into it.

Other values folders have not been changed. So, we are not going to mention it here.

3. Modify Layout Folder

Open res/layout/activity_main.xml file. Add below code into it.

Note that LinearLayout has id rootContainer. In Kotlin file, we will create ImageView Dynamically and add it into this LinearLayout having id rootContainer.

4. Create Android ImageView programmatically in Kotlin

Open src/main/java/com.tutorialwing.dynamicimageview/MainActivity.kt file. Then, add below code into it.

Finally, when you run the application, you will get output as shown above.

Tutorialwing Kotlin Dynamic ImageView Output

Tutorialwing Kotlin Dynamic ImageView Output

Now, Let’s check how to use different attributes of ImageView to customize it dynamically –

Set Id of ImageView

Follow steps below to set id of ImageView programmatically –

  • Create ids.xml file in res/values folder. Then, add below code into it –
  • Now, we can set id of ImageView dynamically, in MainActivity.kt file, as –

Here, we have set id of ImageView using property access syntax – imageView.id

Set Width and Height of ImageView

We use layoutParams to set width and height of any View programmatically. In this article, we have added ImageView in LinearLayout. So, we will define LayoutParams as below –

Here, we have set width and height as WRAP_CONTENT. Some of possible values for width and height are –

  • WRAP_CONTENT: Sets value of width or height depending on text inside it.
  • MATCH_PARENT: Sets value of width of height depending on width or height of parent layout . i.e. width or height of ImageView will be same as width or height of parent layout.
  • Fixed Value: Sets width or height as per value provided.

Set Padding of ImageView

Follow steps below to set padding of ImageView Dynamically –

  • If there is no dimens.xml file, create dimens.xml file in res/values folder. Then, add below code in it –
  • Now, we can set padding of ImageView dynamically, in MainActivity.kt file, as –

Here, we have accessed dimension defined in dimens.xml using getDimension() method. Then, set padding of ImageView using setPadding() method.

Set Margin of ImageView

Follow steps below to set margin of ImageView Dynamically –

  • If there is no dimens.xml file, create dimens.xml file in res/values folder. Then, add below code in it –
  • Now, we can set margin of ImageView dynamically, in MainActivity.kt file, as –

Here, we have accessed dimension defined in dimens.xml using getDimension() method. Then, we have defined layoutParams, set margin to layoutParams. After that, set layoutParams to ImageView.

Set Background of ImageView

Follow steps below to set background of ImageView programmatically –

  • If there is no colors.xml file, create colors.xml file in res/values folder. Then, add below code in it –
  • Now, we can set background of ImageView dynamically, in MainActivity.kt file, as –

Here, we used setBackgroundColor() method to set background color in imageView.

Set Visibility of ImageView

We can set visibility of ImageView programmatically as –

Here, we have set visibility of ImageView using imageView.visibility attribute. Visibility can be of three types – gone, visible and invisible.
Learn to Set Visibility of ImageView Using XML Attribute

That’s end of tutorial on ImageView Programmatically in Kotlin With Example.

Источник

Tutorialwing

Hello Readers! In this post, we are going to learn how to create and use android imageView programmatically in any android application. We will also learn to add imageView in linearLayout programmatically in any application.

Output

Tutorialwing Android Dynamic ImageView Output

Tutorialwing Android Dynamic ImageView Output

Getting Started

At first, we will create an android application. Then, we will use imageView widget in the application.

1. Creating New Project

Follow the steps below to create a new project. Please ignore the steps if you have already created a new project.

Step Description
1. Open Android Studio.
2. Go to File => New => New Project. Write application name as DynamicImageView. Then, click next button.
3. Select minimum SDK you need. However, we have selected 17 as minimum SDK. Then, click next button
4. Then, select Empty Activity => click next => click finish.
5. If you have followed above process correctly, you will get a newly created project successfully. However, you can also visit post to create a new project to know steps in detail.

Now, we will modify xml and java file to use android imageView programmatically.

2. Modify values folder

No values folders have been modified. So, we are not going to mention them here.

3. Modify Layout Folder

Open res/layout/activity_main.xml file. Then, add below code into it.

In activity_main.xml file, we have defined linearLayout and button widget. Now, we will create imageView programmatically in android application. Then, we will add this imageView in linearLayout.

4. Create Android ImageView Programmatically / Dynamically

Open app/src/main/java/com.tutorialwing.dynamicimageview/MainActivity.java file and add below code into it.

In MainActivity.java file, we have created imageView programmatically. Then, we have set it’s layout params and image resource. After that, we have set click listener of button to change image in imageView. At last, we have added this widget in linearLayout, with id rootContainer.

Since AndroidManifest.xml file is very important in any android project. We are also going to see the content inside this file.

AndroidManifest.xml file

Code inside src/main/AndroidManifest.xml file would look like below –

When we run the application, we will get output as shown above.

That’s the end of tutorial on Creating Android ImageView Programmatically.

Источник

Читайте также:  What the doodle android
Оцените статью