Uidesign
Uidesign
•xml
• <TextView
android:id="@+id/text_id"
android:layout_width="wrap_cont
ent"
android:layout_height="wrap_cont
ent" android:text="I am a
TextView" />
•create an instance of the Control object and capture it
from the layout
•TextView myText = (TextView) findViewById(R.id.text_id);
EditText()
•A EditText is an overlay over TextView that
configures itself to be editable.
• It is the predefined subclass of TextView that includes
rich editing capabilities.
EditText Attributes
•It inherits all the attributes of TextView
•inputType: to add the input method for text fields.
• Values : none(text is not editable),text, textAutoCorrect,
textAutoComplete,textPassword,textEmailAddress,phone
•gravity : The gravity attribute is an optional attribute which
is used to control the alignment of the text like left, right,
center, top, bottom, center_vertical , center_horizontal etc.
•textStyle : textStyle attribute is used to set the text style of
a edit text. The possible text styles are bold, italic and
normal
<EditText android:id="@+id/simpleEditText“
android:layout_width=“match_parent“
android:layout_height="wrap_content“
android:hint="Enter Your Name Here"
android:padding="15dp" android:textColorHint="#fff"
android:textStyle="bold|italic"
android:background="#000"/><!--set background color black-->
Retrieving / Getting the Value From EditText In Java Class:
EditText editText =
(EditText)findViewById(R.id.simpleEditText);
String editTextValue = editText.getText().toString();
AutoCompleteTextView
•The AutoCompleteTextView control is a TextView with
auto- complete functionality.
•In other words, as the user types in the TextView, the
control can display suggestions for selection
•To implement autocomplete ,we have to specify an
adapter that provides text suggestions
• Steps
•Add AutoCompleteTextView to the layout
•<?xml version=“1,0” encoding=“utf-8”?>
<AutoCompleteTextView
android:id=“@+id/autocomplete_country”
android:layout_width=“fill_parent”
android:layout_height=“wrap_content”/>
•Define the array that contains all text suggestions
AutoCompleteTextView textView=
(AutoCompleteTextView)findViewById(R.id.autocomplete_coun
try);
textView.setAdapter(adapter);
• Assign the adapter to the AutoCompleteTextView by calling setAdapter()
Button ControlsBasic Button,Image Button
Toggle
•Types of button controls:
•The basic button, the image button, and the toggle button
The Button Control
• The basic button class in Android is android.widget.Button
Xml code
•<Button
android:id="@+id/button1"
android:text="@string/basicBtnLabel"
android:layout_width=“match_parent"
android:layout_height="wrap_content" />
Button Controls
•onClick:- when user clicks a button.
•Add android:onClick attribute to button in xml
•Value of attribute is the name of method to call on click
android:onClick=“sendMessage”
//called when user touches the button
Java
•ImageButton imageButton2 =
(ImageButton)this.findViewById(R.id.imageButton2);
imageButton2.setImageResource(R.drawable.icon);
•Button With text and icon
•<Button
android:id="@+id/button1"
android:layout_width=“match_parent"
android:layout_height="wrap_content“
android:text="@string/button_text“
android:drawableLeft="@drawable/icon" />
•The ToggleButton Control
• ToggleButton control, is a two-state button
•This button can be in either the On or Off state.
• ToggleButton’s default behavior is to show a green bar when
in the On state and a grayed-out bar when in the Off state.
•Default behavior also sets the button’s text to On when it’s in
the On state and Off when it’s in the Off state.
•You can modify the text for the ToggleButton if On/Off is
not appropriate for your application
• <ToggleButton
android:id="@+id/cctglBtn"
android:layout_width="wrap_conte
nt"
android:layout_height="wrap_cont
ent" android:textOn="Stop"
android:textOff="Run"/>
The CheckBox Control Radio Button
Controls Image View DateandTime
picker
•Check box allows user to select one or more options from a set
•It is a type of 2 state button either checked or unchecked
•Check box class in Android is android.widget.CheckBox
•Methods
•public Boolean isChecked() returns true if it is checked
•public void setChecked(Boolean status) changes state of
checkbox
The CheckBox Control
• <CheckBox
android:id="@+id/chickenCB"
android:text="Chicken"
android:checked="true"
android:layout_width=“wrap_content“
android:layout_height="wrap_content" />
<CheckBox
android:id="@+id/fishCB“
android:text="Fish"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
•//initiate a check box
CheckBox simpleCheckBox = (CheckBox) findViewById
(R.id.simpleCheckBox);
//check current state of a check box (true or false)
Boolean checkBoxState = simpleCheckBox.isChecked();
Event listener
•CheckBox checkBoxState= (CheckBox) findViewById
(simpleCheckBox);
checkBoxState.setOnClickListener(new OnClickListener()
{ public void onClick(View v) {
//do something
}
}
Radio button
• A radio button gives the users several choices and forces them
to select a single item
•To enforce this single-selection model, radio buttons generally
belong to a group, and each group is forced to have only one
item selected at a time.
• To create a group of radio buttons in Android, first create a
RadioGroup, and then populate the group with radio
buttons
•Implement a radio group using android.widget.RadioGroup
and a radio button using android.widget.RadioButton.
<RadioGroup
android:id="@+id/rBtnGrp"
android:layout_width="wrap_content"
android:layout_height="wrap_content“
android:orientation="vertical" >
<RadioButton
android:id="@+id/chRBtn" android:text="Chicken" android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</RadioGroup>
To retrieve value using java code
• RadioButton food=(RadioButton )findViewById(R.id.chRBtn);
• Boolean state=chRBtn.isChecked();
• Click events can be handled using
onClick()
ImageView
• This is used to display an image, where the image can come
from a file, a content provider, or a resource such as a
drawable.
• we can even specify a color, and the ImageView will
display that color.
• Implement using android.widget.ImageView;
• <ImageView
android:id="@+id/image1"
android:layout_width="wrap_c
ontent"
android:layout_height="wrap_c
ontent"
android:src="@drawable/icon"
/>
• ImageView
image=(ImageView)findViewById(R.id.imageView
1); image.setImageResource(R.drawable.capture);
• Click events can be handled using onClick()
Date and Time Controls
•Android provides controls for the user to pick a time or pick
a date as ready-to-use dialogs
Android Date Picker
•Android Date Picker allows to select the date consisting
of day, month and year in your custom user interface.
•For this functionality android provides
DatePicker. Attributes of DatePicker
1. id: id is an attribute used to uniquely identify a date picker.
2. datePickerMode: This attribute is used to set the
Date Picker in mode either spinner or calendar.
3. background: background attribute is used to set
the background of a date picker.
4. padding: padding attribute is used to set the padding
from left, right, top or bottom for a date picker.
Methods of DatePicker
1.setSpinnersShown(boolean shown):used to set whether
the spinner of the date picker in shown or not.
2. GetDayOfMonth()
3. getMonth()
4. getYear()
• <DatePicker
android:id="@+id/simpleDatePicker“
android:layout_width="wrap_content“
android:layout_height="wrap_content"
android:datePickerMode="spinner"/>