0% found this document useful (0 votes)
27 views6 pages

Android UI Widgets

The document discusses various Android UI widgets including TextView, Button, RadioButton, CheckBox, and Toast. It provides descriptions of each widget, commonly used attributes, XML syntax to add them, and example Java code for initialization and interaction. Key points covered are setting text, styles, and colors for TextView, adding click listeners for Buttons, handling radio groups for RadioButtons, setting checked states for CheckBoxes, and displaying short messages with Toasts.

Uploaded by

Growth Options
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views6 pages

Android UI Widgets

The document discusses various Android UI widgets including TextView, Button, RadioButton, CheckBox, and Toast. It provides descriptions of each widget, commonly used attributes, XML syntax to add them, and example Java code for initialization and interaction. Key points covered are setting text, styles, and colors for TextView, adding click listeners for Buttons, handling radio groups for RadioButtons, setting checked states for CheckBoxes, and displaying short messages with Toasts.

Uploaded by

Growth Options
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Unit 3

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.

Commonly used attributes for TextView widget are as follows:

Attribute Description
android:text Text to display.
android:textSize Size of the text. Recommended dimension type for text is
scale independent pixel ("sp“).

android:textStyle Style (bold, italic, bolditalic) for the text.


android:typeFace Typeface (normal, sans, serif, monospace) for the text
android:fontFamily Font family for the text.

Commonly used methods for TextView widget are as follows:

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"/>

Then create an instance of the control in java code as follows:

TextView myText = (TextView) findViewById(R.id.text_id);

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.

android:onClick Style (bold, italic, bolditalic) for the text.


Unit 3

Commonly used methods for Button are as follows:

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"/>

Then create an instance of the control in java code as follows:

Button myButton = (Button) findViewById(R.id.button);

myButton.setOnClickListener(new View.onClickListener() {

@Override

public void onClick(View v)

//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" />

Use the following code in java file to implement RadioButton:

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

Commonly used methods for CheckBox are as follows:

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"

android:text="Do you like Unit”/>

checkbox.isChecked() method will return the Boolean value of the checked status on CheckBox.

checkbox.setChecked(true); Use this code to programmatically check or uncheck the 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();

Following are the commonly used methods of Toast class:

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.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy