0% found this document useful (0 votes)
1 views

User Interface With View

The document provides an overview of various user interface components in Android, including TextView, Button, ImageButton, EditText, and CheckBox, along with their attributes and example code. It explains how to define and customize these components using XML layout files and Java programming. Each component is described with its key attributes and functionalities, demonstrating how to implement them in an Android application.

Uploaded by

sonetshaji2004
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views

User Interface With View

The document provides an overview of various user interface components in Android, including TextView, Button, ImageButton, EditText, and CheckBox, along with their attributes and example code. It explains how to define and customize these components using XML layout files and Java programming. Each component is described with its key attributes and functionalities, demonstrating how to implement them in an Android application.

Uploaded by

sonetshaji2004
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 43

User Interface with View

TextView in Android

TextView is the user interface which displays the text message on the screen to the user. It is based
on the layout size, style, and color, etc. TextView is used to set and display the text according to our
specifications.

In Android, when we create a new project with a specific name Android provides us a TextView in
our activity_main.xml file, and the second file is the MainActivity.java file for the coding of Java
programming language.

Properties of TextView in Android

android:id-ID is an attribute used to define a TextView uniquely.

android:id="@+id/text_view"

android: layout_height- Height is the attribute which is used to set the height of the TextView. It can
be like wrap_content that means TextView will be the same size as the text that is given by the user,
match_parent that means TextView will consume the size of the whole screen. It is not related to
the text of the user, and we can also define the size of the text according to the user in DPs.

android:layout_height="wrap_content"

android:layout_width- Width is the attribute which is used to set the width of the TextView. Like
height attribute, it is also can be like wrap_content, which means TextView will be the same size as
the text that is given by the user, match_parent which means TextView will consume the size of the
whole screen. It is not related to the text of the user, and we can also define the size of the text
according to the user in DPs.

android:layout_width="wrap_content"

android:layout_centerInParent- centerInParent is the attribute which is used to set the text in the
center of the screen. It is an optional attribute of the TextView. It is set as true and false by the user.

android:layout_centerInParent="true"

android: text- This attribute handles the text that is given by the user in the form of the string.

android:text="Hello World!"

android: hint- This attribute is used when there is no text is given by the user, in this hint attribute
will show how the text will display on the screen.

android:hint="Hello Android"

android:inputType- This attribute is used to set the type of the TextView. The type of text can be
phone number, eMail, and Password, etc.

android:inputType="text"

android:textSize- This attribute is used to set the size of the TextView. The size of the TextView
given in the size of sp.

android:textSize="30sp"
android:textStyle- This attribute is used to set the style of the TextView. The style of the TextView
given as bold, italic, etc.

android:textStyle="bold"

android:textColorHighlight- This attribute is used to set the color of the text selection highlight.

android:textColorHighlight="@color/colorPrimaryDark"

Program

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/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">

<TextView

android:id="@+id/text_view"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Hello World!" />

</RelativeLayout>

Buttons

A Button is a Push-button which can be pressed, or clicked, by the user to perform an action.

Button Attributes

Following are the important attributes related to Button control.

Sr.No Attribute & Description

1 android:autoText

If set, specifies that this TextView has a textual input method and automatically corrects
some common spelling errors.
2 android:drawableBottom

This is the drawable to be drawn below the text.

3 android:drawableRight

This is the drawable to be drawn to the right of the text.

4 android:editable

If set, specifies that this TextView has an input method.

5 android:text

This is the Text to display.

Inherited from android.view.View Class −

Attribute Description

1 android:background

This is a drawable to use as the background.

2 android:contentDescription

This defines text that briefly describes content of the view.

3 android:id

This supplies an identifier name for this view.

4 android:onClick

This is the name of the method in this View's context to invoke when the
view is clicked.

5 android:visibility

This controls the initial visibility of the view.

Program

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/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">

<Button

android:id="@+id/b1"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Click" />

</RelativeLayout>

ImageButton

An ImageButton is an AbsoluteLayout which enables you to specify the exact location of its children.
This shows a button with an image (instead of text) that can be pressed or clicked by the user.

ImageButton Attributes

Following are the important attributes related to ImageButton control.

Sr.No Attribute & Description

android:adjustViewBounds
1 Set this to true if you want the ImageView to adjust its bounds to preserve the aspect
ratio of its drawable.

android:baseline
2
This is the offset of the baseline within this view.

android:baselineAlignBottom
3
If true, the image view will be baseline aligned with based on its bottom edge.

android:cropToPadding
4
If true, the image will be cropped to fit within its padding.

5 android:src
This sets a drawable as the content of this ImageView.

Inherited from android.view.View Class −

Sr.No Attribute & Description

1 android:background

This is a drawable to use as the background.

2 android:contentDescription

This defines text that briefly describes content of the view.

3 android:id

This supplies an identifier name for this view

4 android:onClick

This is the name of the method in this View's context to invoke when the view is clicked.

5 android:visibility

This controls the initial visibility of the view.

Program

<?xml version="1.0" encoding="utf-8"?>


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<!--Make sure to save two images home and youtube in drawable folder-->

<ImageButton
android:id="@+id/simpleImageButtonHome"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:background="#005"
android:padding="20dp"
android:src="@drawable/home" />
<ImageButton
android:id="@+id/simpleImageButtonYouTube"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/simpleImageButtonHome"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:background="#005"
android:padding="20dp"
android:src="@drawable/youtube" />

</RelativeLayout>

package com.example.imagebutton;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// initiate view's
ImageButton simpleImageButtonHome =
(ImageButton)findViewById(R.id.simpleImageButtonHome);
ImageButton simpleImageButtonYouTube =
(ImageButton)findViewById(R.id.simpleImageButtonYouTube);

// perform click event on button's


simpleImageButtonHome.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(getApplicationContext(),"Home Button",Toast.LENGTH_LONG).show();//
display the toast on home button click
}
});
simpleImageButtonYouTube.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(getApplicationContext(),"YouTube Button",Toast.LENGTH_LONG).show();//
display the toast on you tube button click
}
});
}
}

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

Following are the important attributes related to EditText control.

Sr.No Attribute & Description

1 android:autoText

If set, specifies that this TextView has a textual input method and automatically corrects
some common spelling errors.

2 android:drawableBottom

This is the drawable to be drawn below the text.

3 android:drawableRight

This is the drawable to be drawn to the right of the text.

4 android:editable

If set, specifies that this TextView has an input method.

5 android:text

This is the Text to display.

Inherited from android.view.View Class −

Sr.No Attribute & Description

1 android:background

This is a drawable to use as the background.

2 android:contentDescription
This defines text that briefly describes content of the view.

3 android:id

This supplies an identifier name for this view.

4 android:onClick

This is the name of the method in this View's context to invoke when the view is clicked.

5 android:visibility

This controls the initial visibility of the view.

Program

<?xml version="1.0" encoding="utf-8"?>


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/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">

<!-- Text view for result view-->


<TextView
android:id="@+id/textView_answer"
android:layout_width="100dp"
android:layout_height="25dp"
android:layout_marginLeft="130dp"
android:layout_marginTop="300dp"
android:text="0"
android:textSize="20dp"
android:textStyle="bold" />

<!--take the input first number-->


<EditText
android:id="@+id/editText_first_no"
android:layout_width="150dp"
android:layout_height="40dp"
android:layout_marginLeft="200dp"
android:layout_marginTop="40dp"
android:inputType="number" />
<!-- for message input first number-->
<TextView
android:id="@+id/textView_first_no"
android:layout_width="150dp"
android:layout_height="25dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="50dp"
android:text="First number"
android:textSize="20dp" />

<!--view message -->


<TextView
android:id="@+id/textView_second_no"
android:layout_width="150dp"
android:layout_height="25dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="100dp"
android:text="Second number"
android:textSize="20dp" />

<!-- take input for second number -->

<EditText
android:id="@+id/editText_second_no"
android:layout_width="150dp"
android:layout_height="40dp"
android:layout_marginLeft="200dp"
android:layout_marginTop="90dp"
android:inputType="number"
/>

<!-- button for run add logic and view result -->

<Button
android:id="@+id/add_button"
android:layout_width="100dp"
android:layout_height="50dp"
android:layout_marginLeft="110dp"
android:layout_marginTop="200dp"
android:text="ADD" />

<Button
android:id="@+id/add_button"
android:layout_width="100dp"
android:layout_height="50dp"
android:layout_marginLeft="220dp"
android:layout_marginTop="200dp"
android:text="SUB" />

<Button
android:id="@+id/add_button"
android:layout_width="100dp"
android:layout_height="50dp"
android:layout_marginLeft="110dp"
android:layout_marginTop="200dp"
android:text="MUL" />

<Button
android:id="@+id/add_button"
android:layout_width="100dp"
android:layout_height="50dp"
android:layout_marginLeft="110dp"
android:layout_marginTop="200dp"
android:text="DIV" />

<Button
android:id="@+id/add_button"
android:layout_width="100dp"
android:layout_height="50dp"
android:layout_marginLeft="110dp"
android:layout_marginTop="200dp"
android:text="MOD" />
</RelativeLayout>

package com.example.addapp;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

EditText number1;
EditText number2;
Button Add_button;
TextView result;
int ans=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

// by ID we can use each component which id is assign in xml file


number1=(EditText) findViewById(R.id.editText_first_no);
number2=(EditText) findViewById(R.id.editText_second_no);
Add_button=(Button) findViewById(R.id.add_button);
result = (TextView) findViewById(R.id.textView_answer);

// Add_button add clicklistener


Add_button.setOnClickListener(new View.OnClickListener() {

public void onClick(View v) {

// num1 or num2 double type


// get data which is in edittext, convert it to string
// using parse Double convert it to Double type
double num1 = Double.parseDouble(number1.getText().toString());
double num2 = Double.parseDouble(number2.getText().toString());
// add both number and store it to sum
double sum = num1 + num2;
// set it ot result textview
result.setText(Double.toString(sum));
}
});
}
}

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.

CheckBox Attributes

Following are the important attributes related to CheckBox control.

Sr.No Attribute & Description

android:autoText
1 If set, specifies that this TextView has a textual input method and automatically corrects
some common spelling errors.

android:drawableBottom
2
This is the drawable to be drawn below the text.

android:drawableRight
3
This is the drawable to be drawn to the right of the text.

4 android:editable
If set, specifies that this TextView has an input method.

android:text
5
This is the Text to display.

Inherited from android.view.View Class −

Sr.No Attribute & Description

android:background
1
This is a drawable to use as the background.

android:contentDescription
2
This defines text that briefly describes content of the view.

android:id
3
This supplies an identifier name for this view.

android:onClick
4
This is the name of the method in this View's context to invoke when the view is clicked.

android:visibility
5
This controls the initial visibility of the view.

Program

<?xml version="1.0" encoding="utf-8"?>


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Select Your Programming language: "
android:textColor="#f00"
android:textSize="20sp"
android:textStyle="bold" />
<LinearLayout
android:id="@+id/linearLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:background="#e0e0e0"
android:orientation="vertical">

<CheckBox
android:id="@+id/androidCheckBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:checked="false"
android:padding="20dp"
android:text="@string/android"
android:textColor="#44f"
android:textSize="20sp"
android:textStyle="bold|italic" />

<CheckBox
android:id="@+id/javaCheckBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:checked="false"
android:padding="20dp"
android:text="@string/java"
android:textColor="#f44"
android:textSize="20sp"
android:textStyle="bold|italic" />

<CheckBox
android:id="@+id/phpCheckBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:checked="false"
android:padding="20dp"
android:text="@string/php"
android:textColor="#444"
android:textSize="20sp"
android:textStyle="bold|italic" />

<CheckBox
android:id="@+id/pythonCheckBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:checked="false"
android:padding="20dp"
android:text="@string/python"
android:textColor="#888"
android:textSize="20sp"
android:textStyle="bold|italic" />

<CheckBox
android:id="@+id/unityCheckBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:checked="false"
android:padding="20dp"
android:text="@string/unity"
android:textColor="#101010"
android:textSize="20sp"
android:textStyle="bold|italic" />
</LinearLayout>

</RelativeLayout>

package com.example.checkbox;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {


CheckBox android, java, python, php, unity3D;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// initiate views
android = (CheckBox) findViewById(R.id.androidCheckBox);
android.setOnClickListener(this);
java = (CheckBox) findViewById(R.id.javaCheckBox);
java.setOnClickListener(this);
python = (CheckBox) findViewById(R.id.pythonCheckBox);
python.setOnClickListener(this);
php = (CheckBox) findViewById(R.id.phpCheckBox);
php.setOnClickListener(this);
unity3D = (CheckBox) findViewById(R.id.unityCheckBox);
unity3D.setOnClickListener(this);
}

@Override
public void onClick(View view) {

switch (view.getId()) {
case R.id.androidCheckBox:
if (android.isChecked())
Toast.makeText(getApplicationContext(), "Android", Toast.LENGTH_LONG).show();
break;
case R.id.javaCheckBox:
if (java.isChecked())
Toast.makeText(getApplicationContext(), "Java", Toast.LENGTH_LONG).show();
break;
case R.id.phpCheckBox:
if (php.isChecked())
Toast.makeText(getApplicationContext(), "PHP", Toast.LENGTH_LONG).show();
break;
case R.id.pythonCheckBox:
if (python.isChecked())
Toast.makeText(getApplicationContext(), "Python", Toast.LENGTH_LONG).show();
break;
case R.id.unityCheckBox:
if (unity3D.isChecked())
Toast.makeText(getApplicationContext(), "Unity 3D", Toast.LENGTH_LONG).show();
break;
}
}
}

ToggleButton

A ToggleButton displays checked/unchecked states as a button. It is basically an on/off button with a


light indicator.

ToggleButton Attributes

Following are the important attributes related to ToggleButton control.

Sr.No. Attribute & Description

1 android:disabledAlpha
This is the alpha to apply to the indicator when disabled.

2 android:textOff

This is the text for the button when it is not checked.

3 android:textOn

This is the text for the button when it is checked.

Inherited from android.widget.TextView Class −

Sr.No. Attribute & Description

1 android:autoText

If set, specifies that this TextView has a textual input method and automatically corrects
some common spelling errors.

2 android:drawableBottom

This is the drawable to be drawn below the text.

3 android:drawableRight

This is the drawable to be drawn to the right of the text.

4 android:editable

If set, specifies that this TextView has an input method.

5 android:text

This is the Text to display.

Inherited from android.view.View Class −

Sr.No. Attribute & Description

1 android:background

This is a drawable to use as the background.

2 android:contentDescription

This defines text that briefly describes content of the view.


3 android:id

This supplies an identifier name for this view,

4 android:onClick

This is the name of the method in this View's context to invoke when the view is clicked.

5 android:visibility

This controls the initial visibility of the view.

Program

<?xml version="1.0" encoding="utf-8"?>


<LinearLayout xmlns:android="http://schemas.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"
tools:context=".MainActivity">

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:orientation="horizontal">

<ToggleButton
android:id="@+id/simpleToggleButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:checked="false"
android:drawablePadding="5dp"
android:drawableRight="@drawable/ic_launcher"
android:textColor="#000" />

<ToggleButton
android:id="@+id/simpleToggleButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginLeft="20dp"
android:checked="true"
android:drawableLeft="@drawable/ic_launcher"
android:drawablePadding="5dp"
android:textColor="#000" />
</LinearLayout>

<Button
android:id="@+id/submitButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="50dp"
android:background="#0f0"
android:padding="10dp"
android:text="Submit"
android:textColor="#fff"
android:textSize="20sp"
android:textStyle="bold" />
</LinearLayout>

package com.example.togglebutton;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import android.widget.ToggleButton;
public class MainActivity extends AppCompatActivity {
ToggleButton simpleToggleButton1, simpleToggleButton2;
Button submit;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// initiate toggle button's
simpleToggleButton1 = (ToggleButton) findViewById(R.id.simpleToggleButton1);
simpleToggleButton2 = (ToggleButton) findViewById(R.id.simpleToggleButton2);
submit = (Button) findViewById(R.id.submitButton);
submit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String status = "ToggleButton1 : " + simpleToggleButton1.getText() + "\n" + "ToggleButton2 :
" + simpleToggleButton2.getText();
Toast.makeText(getApplicationContext(), status, Toast.LENGTH_SHORT).show(); // display
the current state of toggle button's
}
});
}
}

RadioButton and RadioGroup

A RadioGroup class is used for set of radio buttons.

If we check one radio button that belongs to a radio group, it automatically unchecks any previously
checked radio button within the same group.

RadioGroup Attributes

Following are the important attributes related to RadioGroup control.

Attribute Description

android:checkedButton This is the id of child radio button that should be checked by default
within this radio group.

Inherited from android.view.View Class −

Sr.No. Attribute & Description

1 android:background

This is a drawable to use as the background.

2 android:contentDescription

This defines text that briefly describes content of the view.

3 android:id

This supplies an identifier name for this view

4 android:onClick

This is the name of the method in this View's context to invoke when the view is clicked.

5 android:visibility

This controls the initial visibility of the view.

Program

<?xml version="1.0" encoding="utf-8"?>


<LinearLayout xmlns:android="http://schemas.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"
tools:context=".MainActivity">

<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#e0e0e0"
android:orientation="vertical">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Select your favourite wwe SuperStar :: "
android:textColor="#000"
android:textSize="20sp"
android:textStyle="bold" />

<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content">

<RadioButton
android:id="@+id/johnCena"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginTop="10dp"
android:checked="true"
android:text="@string/johnCena"
android:textColor="#154"
android:textSize="20sp"
android:textStyle="bold" />

<RadioButton
android:id="@+id/randyOrton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginTop="10dp"
android:checked="false"
android:text="@string/randyOrton"
android:textColor="#154"
android:textSize="20sp"
android:textStyle="bold" />
<RadioButton
android:id="@+id/romanReigns"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginTop="10dp"
android:checked="false"
android:text="@string/romanReigns"
android:textColor="#154"
android:textSize="20sp"
android:textStyle="bold" />

<RadioButton
android:id="@+id/goldBerg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginTop="10dp"
android:checked="false"
android:text="@string/goldBerg"
android:textColor="#154"
android:textSize="20sp"
android:textStyle="bold" />

<RadioButton
android:id="@+id/sheamus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginTop="10dp"
android:checked="false"
android:text="@string/sheamus"
android:textColor="#154"
android:textSize="20sp"
android:textStyle="bold" />

</RadioGroup>

<Button
android:id="@+id/submitButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="20dp"
android:background="#0f0"
android:padding="10dp"
android:text="Submit"
android:textColor="#fff"
android:textSize="20sp"
android:textStyle="bold" />
</LinearLayout>

</LinearLayout>

package com.example.radiobuttonradiogroup;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {


RadioButton johnCena, randyOrton, goldBerg, romanReigns, sheamus;
String selectedSuperStar;
Button submit;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
johnCena = (RadioButton) findViewById(R.id.johnCena);
randyOrton = (RadioButton) findViewById(R.id.randyOrton);
goldBerg = (RadioButton) findViewById(R.id.goldBerg);
romanReigns = (RadioButton) findViewById(R.id.romanReigns);
sheamus = (RadioButton) findViewById(R.id.sheamus);
submit = (Button) findViewById(R.id.submitButton);
submit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (randyOrton.isChecked()) {
selectedSuperStar = randyOrton.getText().toString();
} else if (sheamus.isChecked()) {
selectedSuperStar = sheamus.getText().toString();
} else if (johnCena.isChecked()) {
selectedSuperStar = johnCena.getText().toString();
} else if (romanReigns.isChecked()) {
selectedSuperStar = romanReigns.getText().toString();
} else if (goldBerg.isChecked()) {
selectedSuperStar = goldBerg.getText().toString();
}
Toast.makeText(getApplicationContext(), selectedSuperStar,
Toast.LENGTH_LONG).show(); // print the value of selected super star
}
});
}
}

ProgressBar

Progress bars are used to show progress of a task. For example, when you are uploading or
downloading something from the internet, it is better to show the progress of download/upload to
the user.

Following are the methods provided by the ProgressDialog class.

Sr. Title & description


No

1 getMax()

This method returns the maximum value of the progress.

2 incrementProgressBy(int diff)

This method increments the progress bar by the difference of value passed as a
parameter.

3 setIndeterminate(boolean indeterminate)

This method sets the progress indicator as determinate or indeterminate.

4 setMax(int max)

This method sets the maximum value of the progress dialog.

5 setProgress(int value)

This method is used to update the progress dialog with some specific value.

6 show(Context context, CharSequence title, CharSequence message)

This is a static method, used to display progress dialog.

Program
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000"
tools:context=".MainActivity">

<ProgressBar
android:id="@+id/simpleProgressBar"
style="@style/Widget.AppCompat.ProgressBar.Horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="70dp"
android:max="100"
android:progress="0"
android:progressDrawable="@drawable/custom_progress" />

<Button
android:id="@+id/startButton"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="120dp"
android:background="#0f0"
android:padding="10dp"
android:text="Start"
android:textColor="#fff"
android:textSize="20sp"
android:textStyle="bold" />

</RelativeLayout>

package com.example.progressbar1;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;

import android.view.View;
import android.widget.ProgressBar;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
int progress = 0;
ProgressBar simpleProgressBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// initiate progress bar and start button
simpleProgressBar = (ProgressBar) findViewById(R.id.simpleProgressBar);
Button startButton = (Button) findViewById(R.id.startButton);
// perform click event on button
startButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// call a function
setProgressValue(progress);

}
});
}

private void setProgressValue(final int progress) {

// set the progress


simpleProgressBar.setProgress(progress);
// thread is used to change the progress value
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
setProgressValue(progress + 10);
}
});
thread.start();
}
}

AutoComplete TextView

A AutoCompleteTextView is a view that is similar to EditText, except that it shows a list of


completion suggestions automatically while the user is typing.

AutoCompleteTextView Attributes
Following are the important attributes related to AutoCompleteTextView control.

Sr.No Attribute & Description

android:completionHint
1
This defines the hint displayed in the drop down menu.

android:completionHintView
2
This defines the hint view displayed in the drop down menu.

android:completionThreshold
3 This defines the number of characters that the user must type before completion
suggestions are displayed in a drop down menu.

android:dropDownAnchor
4
This is the View to anchor the auto-complete dropdown to.

android:dropDownHeight
5
This specifies the basic height of the dropdown.

android:dropDownHorizontalOffset
6
The amount of pixels by which the drop down should be offset horizontally.

android:dropDownSelector
7
This is the selector in a drop down list.

android:dropDownVerticalOffset
8
The amount of pixels by which the drop down should be offset vertically.

android:dropDownWidth
9
This specifies the basic width of the dropdown.

android:popupBackground
10
This sets the background.
Program

<?xml version="1.0" encoding="utf-8"?>


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
>

<AutoCompleteTextView
android:id="@+id/simpleAutoCompleteTextView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#000"
android:hint="Enter Your Name Here"
android:padding="15dp"
android:textColorHint="#fff"
android:textStyle="bold|italic" />
</RelativeLayout>

package com.example.autocompletetextview;

import androidx.appcompat.app.AppCompatActivity;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;

import android.os.Bundle;

public class MainActivity extends AppCompatActivity {


String[] countryNameList = {"India", "Ireland", "China", "Australia", "New Zealand", "England",
"Pakistan"};
@Override
protected void onCreate(Bundle savedInstanceState) {
try {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//initiate an auto complete text view
AutoCompleteTextView simpleAutoCompleteTextView = (AutoCompleteTextView)
findViewById(R.id.simpleAutoCompleteTextView);
ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1,
countryNameList);
simpleAutoCompleteTextView.setAdapter(adapter);
simpleAutoCompleteTextView.setThreshold(1);//start searching from 1 character
simpleAutoCompleteTextView.setAdapter(adapter); //set the adapter for displaying country
name list
}
catch(Exception e){}
}
}

Spinner

Spinner allows you to select an item from a drop down menu

Program

<?xml version="1.0" encoding="utf-8"?>


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"

tools:context=".MainActivity">

<Spinner
android:id="@+id/simpleSpinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="100dp" />

</RelativeLayout>

package com.example.spinner1;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements


AdapterView.OnItemSelectedListener{
String[] bankNames={"BOI","SBI","HDFC","PNB","OBC"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Getting the instance of Spinner and applying OnItemSelectedListener on it
Spinner spin = (Spinner) findViewById(R.id.simpleSpinner);
spin.setOnItemSelectedListener(this);

//Creating the ArrayAdapter instance having the bank name list


ArrayAdapter aa = new ArrayAdapter(this,android.R.layout.simple_spinner_item,bankNames);
aa.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
//Setting the ArrayAdapter data on the Spinner
spin.setAdapter(aa);
}

//Performing action onItemSelected and onNothing selected


@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int position,long id) {
Toast.makeText(getApplicationContext(), bankNames[position], Toast.LENGTH_LONG).show();
}

@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub

}
}

ListView

Android ListView is a view which groups several items and display them in vertical scrollable list. The
list items are automatically inserted to the list using an Adapter that pulls content from a source
such as an array or database.

ListView Attributes

Following are the important attributes specific to GridView −

Sr.No Attribute & Description

1 android:id

This is the ID which uniquely identifies the layout.

2 android:divider
This is drawable or color to draw between list items.

3 android:dividerHeight

This specifies height of the divider. This could be in px, dp, sp, in, or mm.

4 android:entries

Specifies the reference to an array resource that will populate the ListView.

5 android:footerDividersEnabled

When set to false, the ListView will not draw the divider before each footer view. The
default value is true.

6 android:headerDividersEnabled

When set to false, the ListView will not draw the divider after each header view. The
default value is true.

Program

<?xml version="1.0" encoding="utf-8"?>


<LinearLayout xmlns:android="http://schemas.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>

package com.example.listview;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class MainActivity extends AppCompatActivity {
private ListView mListView;
private ArrayAdapter aAdapter;
private String[] users = { "Joshua Mathai Sabu", "Aaron Aby", "Prannoy Mathew", "Gokul Krishna",
"Austin Premod Varghese", "Abhirami P Kumar"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mListView = (ListView) findViewById(R.id.userlist);
aAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, users);
mListView.setAdapter(aAdapter);
}
}

GridView

Android GridView shows items in two-dimensional scrolling grid (rows & columns) and the grid items
are not necessarily predetermined but they automatically inserted to the layout using a ListAdapter.

GridView Attributes

Following are the important attributes specific to GridView −

Sr.No Attribute & Description

android:id
1
This is the ID which uniquely identifies the layout.

android:columnWidth
2
This specifies the fixed width for each column. This could be in px, dp, sp, in, or mm.

android:gravity
3 Specifies the gravity within each cell. Possible values are top, bottom, left, right, center,
center_vertical, center_horizontal etc.

android:horizontalSpacing
4 Defines the default horizontal spacing between columns. This could be in px, dp, sp, in, or
mm.

android:numColumns
5 Defines how many columns to show. May be an integer value, such as "100" or auto_fit
which means display as many columns as possible to fill the available space.

6 android:stretchMode
Defines how columns should stretch to fill the available empty space, if any. This must be
either of the values −

none − Stretching is disabled.

spacingWidth − The spacing between each column is stretched.

columnWidth − Each column is stretched equally.

spacingWidthUniform − The spacing between each column is uniformly stretched..

android:verticalSpacing
7
Defines the default vertical spacing between rows. This could be in px, dp, sp, in, or mm.

Program

<?xml version="1.0" encoding="utf-8"?>

<GridView xmlns:android="http://schemas.android.com/apk/res/android"

android:id="@+id/gridview"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:columnWidth="90dp"

android:numColumns="auto_fit"

android:verticalSpacing="10dp"

android:horizontalSpacing="10dp"

android:stretchMode="columnWidth"

android:gravity="center"

/>

package com.example.helloworld;

import android.os.Bundle;

import android.app.Activity;

import android.view.Menu;
import android.widget.GridView;

public class MainActivity extends Activity {

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

GridView gridview = (GridView) findViewById(R.id.gridview);

gridview.setAdapter(new ImageAdapter(this));

ImageView

It is used to display images.

Program

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:orientation="vertical"

tools:context=".MyAndroidAppActivity">

<ImageView

android:id="@+id/imageView1"

android:layout_width="wrap_content"

android:layout_height="wrap_content"/>

<Button

android:id="@+id/btnChangeImage"

android:layout_width="wrap_content"

android:layout_height="wrap_content"
android:text="Change Image" />

</LinearLayout>

ackage com.example.sample;

import android.support.v7.app.AppCompatActivity;

import android.app.Activity;

import android.os.Bundle;

import android.widget.Button;

import android.widget.ImageView;

import android.view.View;

import android.view.View.OnClickListener;

public class MyAndroidAppActivity extends AppCompatActivity {

Button button;

ImageView image;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

addListenerOnButton();

public void addListenerOnButton() {

image=(ImageView) findViewById(R.id.imageView1);

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

button.setOnClickListener(new OnClickListener() {

@Override

public void onClick(View arg0) {

image.setImageResource(R.drawable.ic_launcher_background);

});

}
<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.sample">

<application

android:allowBackup="true"

android:icon="@mipmap/ic_launcher"

android:label="@string/app_name"

android:roundIcon="@mipmap/ic_launcher_round"

android:supportsRtl="true"

android:theme="@style/AppTheme">

<activity android:name=".MyAndroidAppActivity">

<intent-filter>

<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />

</intent-filter>

</activity>

</application>

</manifest>

ScrollView

A ScrollView is a view group that is used to make vertically scrollable views. A scroll view contains a
single direct child only. In order to place multiple views in the scroll view, one needs to make a view
group(like LinearLayout) as a direct child and then we can define many views inside it.

Program

<?xml version="1.0" encoding="utf-8"?>


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<HorizontalScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scrollbars="horizontal">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal">

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="20dp"
android:background="#f00"
android:padding="10dp"
android:text="Button 1"
android:textColor="#fff"
android:textSize="20sp" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="20dp"
android:background="#0f0"
android:padding="10dp"
android:text="Button 2"
android:textColor="#fff"
android:textSize="20sp" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="20dp"
android:background="#00f"
android:padding="10dp"
android:text="Button 3"
android:textColor="#fff"
android:textSize="20sp" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="20dp"
android:background="#ff0"
android:padding="10dp"
android:text="Button 4"
android:textColor="#fff"
android:textSize="20sp" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="20dp"
android:background="#f0f"
android:padding="10dp"
android:text="Button 5"
android:textColor="#fff"
android:textSize="20sp" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="20dp"
android:background="#f90"
android:padding="10dp"
android:text="Button 6"
android:textColor="#fff"
android:textSize="20sp" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="20dp"
android:background="#f00"
android:padding="10dp"
android:text="Button 7"
android:textColor="#ff9"
android:textSize="20sp" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="20dp"
android:background="#444"
android:padding="10dp"
android:text="Button 8"
android:textColor="#fff"
android:textSize="20sp" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="20dp"
android:background="#ff002211"
android:padding="10dp"
android:text="Button 9"
android:textColor="#fff"
android:textSize="20sp" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="20dp"
android:background="#0f0"
android:padding="10dp"
android:text="Button 10"
android:textColor="#fff"
android:textSize="20sp" />

</LinearLayout>

</HorizontalScrollView>

</RelativeLayout>

CustomToastAlert

In android, Toast is a small popup notification that is used to display information about the operation
which we performed in our app. The Toast will show the message for a small period of time and it
will disappear automatically after a timeout.

Generally, the size of Toast will be adjusted based on the space required for the message and it will
be displayed on the top of the main content of activity for a short period of time.

Generally, the Toast notification in android will be displayed with simple text like as shown in above
image. In android, we can customize the layout of our toast notification to change the appearance of
based on requirements like include images in toast notification or change the background color of
toast notification, etc.

Program

<?xml version="1.0" encoding="utf-8"?>


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<Button
android:id="@+id/btnShow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Show Custom Toast"
android:layout_marginTop="150dp" android:layout_marginLeft="110dp"/>
</LinearLayout>

<?xml version="1.0" encoding="utf-8"?>


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/custom_toast_layout"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:background="#80CC28">
<ImageView android:src="@drawable/ic_notification"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="10dp" />
<TextView android:id="@+id/txtvw"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="13dp"
android:textColor="#FFF"
android:textStyle="bold"
android:textSize="15dp" />
</LinearLayout>

package com.example.toast1;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn = (Button)findViewById(R.id.btnShow);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.custom_toast, (ViewGroup)
findViewById(R.id.custom_toast_layout));
TextView tv = (TextView) layout.findViewById(R.id.txtvw);
tv.setText("Custom Toast Notification");
Toast toast = new Toast(getApplicationContext());
toast.setGravity(Gravity.CENTER_VERTICAL, 0, 100);
toast.setDuration(Toast.LENGTH_LONG);
toast.setView(layout);
toast.show();
}
});
}
}

DateTimePicker

Android Date Picker allows you to select the date consisting of day, month and year in your custom
user interface.

Android Time Picker allows you to select the time of day in either 24 hour or AM/PM mode. The time
consists of hours, minutes and clock format.

Program

<?xml version="1.0" encoding="utf-8"?>


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/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:padding="20dp"
android:background="#ffffff"
android:id="@+id/main"
android:orientation="vertical">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center">

<Button
android:id="@+id/date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="select date"
android:textColor="#ffffff"
android:textStyle="bold"
android:textSize="18sp"
android:layout_margin="20dp"
android:padding="10dp"/>

<Button
android:id="@+id/time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="select time"
android:textColor="#ffffff"
android:textStyle="bold"
android:textSize="18sp"
android:layout_margin="20dp"
android:padding="10dp"/>

</LinearLayout>

<TextView
android:id="@+id/tv_date"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="16sp"
android:textColor="#000000"
android:text="Date"
android:gravity="center"
android:layout_marginTop="40dp"/>

<TextView
android:id="@+id/tv_time"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="16sp"
android:textColor="#000000"
android:text="Time"
android:gravity="center"
android:layout_marginTop="20dp"/>
</LinearLayout>

package com.example.datepicker;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.app.DatePickerDialog;
import android.app.TimePickerDialog;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import java.util.Calendar;
import android.widget.DatePicker;
import android.widget.TimePicker;

public class MainActivity extends AppCompatActivity {


private int year, month, day, hour, minute;
TextView tv_date, tv_time;
Button date, time;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv_date = findViewById(R.id.tv_date);
tv_time = findViewById(R.id.tv_time);
date = findViewById(R.id.date);
time = findViewById(R.id.time);

date.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {

final Calendar c = Calendar.getInstance();


year = c.get(Calendar.YEAR);
month = c.get(Calendar.MONTH);
day = c.get(Calendar.DAY_OF_MONTH);

DatePickerDialog picker = new DatePickerDialog(MainActivity.this, new


DatePickerDialog.OnDateSetListener() {

@Override
public void onDateSet(DatePicker datePicker, int year, int month, int day) {
tv_date.setText(day+"/"+(month+1)+"/"+year);
}
}, year, month,day);
picker.show();
}
});

time.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {

final Calendar c = Calendar.getInstance();


hour = c.get(Calendar.HOUR_OF_DAY);
minute = c.get(Calendar.MINUTE);

TimePickerDialog timePickerDialog = new TimePickerDialog(MainActivity.this, new


TimePickerDialog.OnTimeSetListener()
{
@Override
public void onTimeSet(TimePicker timePicker, int hour, int min) {
tv_time.setText(hour+":"+min);
}
},hour,minute,false);
timePickerDialog.show();
}
});
}
}

You might also like