m-unit-4-1
m-unit-4-1
“id”
This is basically the name of the particular view and will be
used to refer that particular view through out the project. It
has to be unique(multiple views referencing to same id will
confuse the compiler).
“width” and “height”
As the name of these properties suggest, these
<LinearLayout xmlns:android="http://
schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/submitbutton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=“Submit” />
</LinearLayout>
Property Description Example Values
Text displayed on the "Click me", "Submit",
android:text
button. "Next"
Size of the text on the
android:textSize "12sp", "18sp", "24sp"
button.
Color of the text on the "#FF0000" (Red),
android:textColor
button. "#00FF00" (Green)
"@drawable/
android:background Background of the button. my_button_background
", "#FFFFFF"
"wrap_content",
android:layout_widt
Width of the button. "match_parent",
h
"100dp"
android:layout_heig "wrap_content",
Height of the button.
ht "100dp", "fill_parent"
android:layout_grav Gravity of the button
"center", "start", "end"
ity within its parent layout.
android:layout_mar Margins around the button
"10dp", "5dp"
gin within its parent layout.
<EditText
android:id="@+id/editTextNum1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter first number"
android:inputType="number" />
<EditText
android:id="@+id/editTextNum2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter second number"
android:inputType="number" />
<Button
android:id="@+id/buttonAdd"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Add" />
<TextView
android:id="@+id/textViewResult"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="24sp"
android:textStyle="bold" />
</LinearLayout>
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editTextNum1 = findViewById(R.id.editTextNum1);
editTextNum2 = findViewById(R.id.editTextNum2);
textViewResult = findViewById(R.id.textViewResult);
buttonAdd = findViewById(R.id.buttonAdd);
buttonAdd.setOnClickListener(new
View.OnClickListener() {
@Override
public void onClick(View view) {
int num1 =
Integer.parseInt(editTextNum1.getText().toString());
int num2 =
Integer.parseInt(editTextNum2.getText().toString());
int result = num1 + num2;
textViewResult.setText(String.valueOf(result));
}
});
}
}
Android UI Controls
3. Image Button
Image Button is a user interface control which is used
to display a button with image to perform an action
when user click or tap on it.
Generally, the Image button in android looks similar
as regular Button and perform the actions same as
regular button but only difference is for image button
we will add an image instead of text.
Create ImageButton in XML Layout File
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://
schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageButton
android:id="@+id/addBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/
add_icon" />
</LinearLayout>
Property Description Example Values
Sets the image
android:src resource for the @drawable/ic_image
ImageButton.
@color/colorPrimary,
Sets the background
android:background @drawable/backgroun
drawable or color.
d_image
wrap_content,
Defines the width of
android:layout_width match_parent,
the ImageButton.
specific dimension
wrap_content,
android:layout_heigh Defines the height of
match_parent,
t the ImageButton.
specific dimension
Adds padding around
android:padding the content of the Specific dimension
ImageButton.
Provides a description
android:contentDesc "Play Button", "Delete
of the content for
ription Image"
accessibility.
Indicates whether the
android:enabled true, false
button is clickable.
Android UI Controls
4. Toggle Button
In android, Toggle Button is a user interface control
which is used to display ON (Checked) or OFF
(Unchecked) states as a button with a light indicator.
<ToggleButton
android:id="@+id/toggleButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textOff="OFF"
android:textOn="ON"
android:layout_marginTop="100dp"
android:layout_centerHorizontal="true" />
</RelativeLayout>
public class MainActivity extends AppCompatActivity {
private ToggleButton toggleButton;
private TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toggleButton = findViewById(R.id.toggleButton);
textView = findViewById(R.id.textView);
toggleButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (toggleButton.isChecked()) {
textView.setText("Toggle button is ON");
} else {
textView.setText("Toggle button is OFF");
}
}
});
}
}
5. Radio Button & Radio
Button Group
<RadioGroup
android:id="@+id/radioGroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="50dp">
<RadioButton
android:id="@+id/radioButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Option 1" />
<RadioButton
android:id="@+id/radioButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Option 2" />
<RadioButton
android:id="@+id/radioButton3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Option 3" />
</RadioGroup>
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Submit"
android:layout_below="@+id/radioGroup"
android:layout_centerHorizontal="true"
android:layout_marginTop="50dp" />
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="You selected: "
android:textSize="24sp"
android:textColor="#000000"
android:layout_below="@+id/button"
android:layout_centerHorizontal="true"
android:layout_marginTop="50dp" />
</RelativeLayout>
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
radioGroup = findViewById(R.id.radioGroup);
radioButton1 = findViewById(R.id.radioButton1);
radioButton2 = findViewById(R.id.radioButton2);
radioButton3 = findViewById(R.id.radioButton3);
button = findViewById(R.id.button);
textView = findViewById(R.id.textView);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int selectedId = radioGroup.getCheckedRadioButtonId();
if (selectedId == radioButton1.getId()) {
textView.setText("You selected: Option 1");
} else if (selectedId == radioButton2.getId()) {
textView.setText("You selected: Option 2");
} else if (selectedId == radioButton3.getId()) {
textView.setText("You selected: Option 3");
} else {
textView.setText("Please select an option");
}
}
});
}
}
Android UI Controls
6. CheckBox
Checkbox is a two states button that can
<CheckBox
android:id="@+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Checkbox 1"/>
<CheckBox
android:id="@+id/checkBox2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Checkbox 2"/>
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Submit"/>
</LinearLayout>
public class MainActivity extends AppCompatActivity {
CheckBox checkBox1, checkBox2;
Button submitButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Initialize UI components
checkBox1 = findViewById(R.id.checkBox1);
checkBox2 = findViewById(R.id.checkBox2);
submitButton = findViewById(R.id.button);
Toast.makeText(MainActivity.this, message,
Toast.LENGTH_SHORT).show();
}
});
}
DatePicker
DatePicker is a user interface control in Android that allows
the user to select a date
<LinearLayout
xmlns:android="http://schemas.android.com/apk/re
s/android"
xmlns:app="http://schemas.android.com/apk/res-
auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Date Picker Demo"
android:textColor="@color/purple_200"
android:textSize="30dp"
android:layout_marginLeft="90dp"
android:layout_marginTop="20dp"
android:layout_marginBottom="20dp"/>
<DatePicker
android:id="@+id/datePicker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:datePickerMode="calendar"
android:layout_gravity="center_horizontal"/>
package com.example.datepickerdemo;
import
androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.DatePicker;
import android.widget.Toast;
import java.util.Calendar;
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Time Picker Demo"
android:textColor="@color/design_default_color_error"
android:textSize="30dp"
android:layout_marginLeft="90dp"
android:layout_marginTop="20dp"
android:layout_marginBottom="20dp"/>
<TimePicker
android:id="@+id/timePicker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:timePickerMode="spinner"
android:layout_gravity="center_horizontal"/
>
</LinearLayout>
package com.example.datepickerdemo;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TimePicker;
import android.widget.Toast;
import java.util.Calendar;
<Button android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="244dp"
android:layout_x="235dp" android:layout_y="327dp"
android:text="stop" />
<Button android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="244dp"
android:layout_x="65dp" android:layout_y="325dp"
android:text="Start" />
<ProgressBar android:id="@+id/progressBar"
style="?android:attr/progressBarStyle"
android:layout_width="211dp"
android:layout_height="wrap_content"
android:layout_x="103dp"
android:layout_y="220dp" />
</AbsoluteLayout>
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
progressBar = findViewById(R.id.progressBar);
progressText = findViewById(R.id.progressText);
startButton = findViewById(R.id.startButton);
stopButton = findViewById(R.id.stopButton);
public class MainActivity extends AppCompatActivity
{
Button b1, b2; ProgressBar p1;
@Override
protected void onCreate(Bundle savedInstanceState)
{ super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1=(Button) findViewById(R.id.button);
b2=(Button) findViewById(R.id.button2);
p1=(ProgressBar) findViewById(R.id.progressBar);
b1.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View view)
{
p1.setVisibility(View.VISIBLE);
}
});
b2.setOnClickListener(new
View.OnClickListener()
{ @Override public
void onClick(View view)
{ p1.setVisibility(View.INVISIBLE);
}
});
}
}
stopButton.setOnClickListener(new
View.OnClickListener() {
@Override
public void onClick(View v) {
progressStatus = 0;
progressBar.setProgress(progressStatus);
progressText.setText(progressStatus + "%");
}
});
}
}
Android UI Controls
1. ListView
<ListView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/simpleListView"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schem-
as.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/
tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ListView
android:id="@+id/userlist"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
MainActivity.java
package com.tutlane.listview;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
{
private GridView gridView;
private String[] items = {"Item 1", "Item 2",
"Item 3", "Item 4", "Item 5"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gridView = findViewById(R.id.grid_view);
ArrayAdapter<String> adapter = new
ArrayAdapter<>(this,
android.R.layout.simple_list_item_1, items);
gridView.setAdapter(adapter);
gridView.setOnItemClickListener(new
AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?
parent, View view, int position, long id) {
Toast.makeText(MainActivity.this, "Clicked
item: " + items[position],
Toast.LENGTH_SHORT).show();
}
});
}
}
Android UI Controls
3. Scroll View
ScrollView is a view group in Android that allows
its children to be scrolled vertically. It is useful
when you have a lot of content that cannot fit on
the screen at once and you want to allow the
user to scroll through it.
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/and
roid"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="This is a TextView" />
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="@drawable/my_image" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter your name" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Click me" />
</LinearLayout>
</ScrollView>
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String name = editText.getText().toString();
Toast.makeText(MainActivity.this, "Hello, " + name
+ "!", Toast.LENGTH_SHORT).show();
}
});
}
}