Android UI Widgets
Android UI Widgets
Android UI Widgets
TextView
Button
RadioButton
CheckBox
Toast
Unit 3
Android UI Widgets
The Widgets/controls are the interactive components in your app's user interface. Android provides
a wide variety of controls you can use in your UI, such as buttons, text fields, seek bars, check box,
toggle buttons, and many more.
A View is an object that draws something on the screen that the user can interact with and
a ViewGroup is an object that holds other View (and ViewGroup) objects in order to define the
layout of the user interface.
A project is an organizational unit that represents a complete software solution. In Android Studio a
project, can contain multiple modules. A module in Android Studio is a folder.
TextView
A TextView displays text to the user and optionally allows them to edit it. We can set the text,
change colour, change background, give padding (spacing), give margin and do much more with
TextView.
Attribute Description
android:text Text to display.
android:textSize Size of the text. Recommended dimension type for text is
scale independent pixel ("sp“).
Methods Description
public void setText(CharSequence text) Sets the string value of the textview.
public CharSequence getText() Returns the text the textview is displaying.
public void setMaxLines(int maxlines) Sets the maximum number of lines that can be
displayed in this textview.
public int getMaxLines() Returns the maximum number of lines displayed
in this textview.
public void setEnabled(boolean enabled) Sets enabled state of the view. True if this view
is enabled, false otherwise.
Unit 3
A TextView widget can be added to your app by writing the following XML syntax:
<TextView
android:id="@+id/text_id"
android:layout_width="300dp"
android:layout_height="200dp"
android:capitalize="characters"
android:text="hello_world"
android:textColor="@android:color/holo_blue_dark"
android:textColorHighlight="@android:color/primary_text_dark"
android:layout_centerVertical="true"
android:textSize="50dp"/>
Button
A Button is a push-button which can be pressed, or clicked, by the user to perform an action. You
can customize button by changing colour, give life to it by doing some action on clicking the Button,
etc. Commonly used attributes for Button are as follows:
Attribute Description
android:id This is the ID which uniquely identifies the control
android:background This is the name of the method in this View’s context to invoke
when the view is clicked.
Methods Description
public void setTextColor(int color) Sets the text colour.
public void setGravity(int gravity) Sets the horizontal alignment of the text and the
vertical gravity that will be used when there is
extra space.
A Button widget can be added to your app by writing the following XML syntax:
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
android:id="@+id/button"/>
myButton.setOnClickListener(new View.onClickListener() {
@Override
//Your code
})
RadioButton
A RadioGroup class is used for set of RadioButtons. If we check one radio button that belongs to a
radio group, it automatically unchecks any previously checked radio button within the same group. A
RadioButton widget can be added to your app by writing the following XML syntax:
<RadioButton
android:layout_width="wrap_content"
Unit 3
android:layout_height="55dp"
android:text="Male"
android:id="@+id/radioButton"
android:layout_gravity="center_horizontal"
android:checked="false"
android:textSize="25dp" />
RadioGroup radioGroup=(RadioGroup)findViewById(R.id.radioGroup);
int selectedId=radioGenderGroup.getCheckedRadioButtonId();
radioGender =(RadioButton)findViewById(selectedId);
Toast.makeText(MainActivity.this,radioGender.getText(),Toast.LENGTH_SHORT).show();
The above code will display the text of checked radio button in the RadioGroup.
CheckBox
A CheckBox is an on/off switch that can be toggled by the user. You should use check-boxes when
presenting users with a group of selectable options that are not mutually exclusive. Commonly used
attributes for CheckBox are as follows:
Attribute Description
android:checked Changes the checked state of the view. Possible
values either “true” or “false”.
android:layout_width Width of the RadioButton widget
android:layout_width Height of the RadioButton widget
Methods Description
public CharSequence getText() Returns the text the RadioButton is
displaying.
public abstract Boolean isChecked() Returns the current checked state of
the view. True if checked, false
otherwise.
public abstract void setChecked(boolean checked) Changes the checked state of the
view. RadioButton is checked if true,
unchecked if false.
Unit 3
A CheckBox widget can be added to your app by writing the following XML syntax:
<CheckBox
android:id="@+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
checkbox.isChecked() method will return the Boolean value of the checked status on CheckBox.
Toast
Toast is used to display information for the short period of time. The toast contains a message that
disappears after a short duration of its appearance on the screen.
The android.widget.Toast class is the subclass of java.lang.Object class. You can also create
custom toast as well for purposes like to display an image in Toast. A Toast can be added to your
app by writing the following java syntax:
Toast.makeText(MainActivity.this,”Message”,Toast.LENGTH_SHORT).show();
Methods Description
public static Toast makeText(Context context, CharSequence Returns the text the
text, int duration) RadioButton is displaying.
public void show() Displays toast.
public void setMargin (float horizontalMargin, float Changes the horizontal and
verticalMargin) vertical margin difference.