Android Widgets

Android widgets is having an important role in development of android application . Widget are UI elements required for developing  screens in android application. Most commonly used android widgets are TextView, ImageView,  EditText, Spinner, ListView etc. Let us see how these widgets are implemented in android.

TextView

In your layout xml file ,use the following code

<TextView 
android:id="@+id/tv_name"
android:layout_width="match_parent"
android:layout_height="wrap_content" 
android:text="Display name here" />

And in your java file you can get the reference of the above TextView using the following code

EditText

TextView tv_name = (TextView) findViewById(R.id.tv_name)

In your layout xml file ,use the following code

<EditText 
android:id="@+id/ed_name" 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:inputType="text" 
android:hint="Name" />

And in your java file you can get the reference of the above EditText using the following code

EditText ed_name = (EditText) findViewById(R.id.ed_name);

Button

In your layout xml file ,use the following code

<Button 
android:id="@+id/btn_submit" 
android:layout_width="match_parent" 
android:layout_height="wrap_content"
android:text="Submit" />

And in your java file you can get the reference of the above EditText using the following code

Button btn_submit = (Button) findViewById(R.id.btn_submit);

Now if you wan to give click action for this button ,use the following code

btn_submit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

//You code for button click action comes here
}
});

ImageView

In your layout xml file ,use the following code

<ImageView 
android:id="@+id/im_launcher" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:scaleType="fitXY" 
android:src="@mipmap/ic_launcher" />

And in your java file you can get the reference of the above ImageView using the following code

ImageView im_launcher = (ImageView) findViewById(R.id.im_launcher);

Other Important Widgets

About the author

Hi guys, i am the author of codesfor. I am a B.Tech graduate currently working as an App developer. Apart from job i am a blogger and a freelancer.

Add a Comment

Your email address will not be published. Required fields are marked *