Chapter 4 Notes
Chapter 4 Notes
• 4.1 Text View , Edit Text , Button , Image Button , Toggle Button , Radio Button and Radio Group ,
Checkbox , Progress Bar
• 4.2 List View , Grid View , Image View , Scroll View , Custom Toast alert
• 4.3 Time And Date picker
Textview
A TextView displays text to the user and optionally allows them to edit it. A TextView is a complete text
editor, however the basic class is configured to not allow editing.
TextView Attributes
Following are the important attributes related to TextView control.
1
android:id
This is the ID which uniquely identifies the control.
2
android:capitalize
If set, specifies that this TextView has a textual input method and should automatically
capitalize what the user types.
3
android:cursorVisible
Makes the cursor visible (the default) or invisible. Default is false.
4
android:editable
If set to true, specifies that this TextView has an input method.
5
android:fontFamily
Font family (named by string) for the text.
6
android:gravity
7
android:hint
Hint text to display when the text is empty.
8
android:inputType
The type of data being placed in a text field. Phone, Date, Time, Number, Password etc.
9
android:maxHeight
Makes the TextView be at most this many pixels tall.
10
android:maxWidth
Makes the TextView be at most this many pixels wide.
11
android:minHeight
Makes the TextView be at least this many pixels tall.
12
android:minWidth
Makes the TextView be at least this many pixels wide.
13
android:password
Whether the characters of the field are displayed as password dots instead of themselves.
Possible value either "true" or "false".
14
android:phoneNumber
If set, specifies that this TextView has a phone number input method. Possible value either
"true" or "false".
15
android:text
Text to display.
16
android:textAllCaps
Present the text in ALL CAPS. Possible value either "true" or "false".
17
android:textColor
18
android:textColorHighlight
Color of the text selection highlight.
19
android:textColorHint
Color of the hint text. May be a color value, in the form of "#rgb", "#argb", "#rrggbb", or
"#aarrggbb".
20
android:textIsSelectable
Indicates that the content of a non-editable text can be selected. Possible value either "true" or
"false".
21
android:textSize
Size of the text. Recommended dimension type for text is "sp" for scaled-pixels (example:
15sp).
22
android:textStyle
Style (bold, italic, bolditalic) for the text. You can use or more of the following values separated
by '|'.
● normal - 0
● bold - 1
● italic - 2
23
android:typeface
Typeface (normal, sans, serif, monospace) for the text. You can use or more of the following
values separated by '|'.
● normal - 0
● sans - 1
● serif - 2
● monospace - 3
Example
This example will take you through simple steps to show how to create your own Android
application using Linear Layout and TextView.
1 You will use Android studio to create an Android application and name it as demo under a
package com.example.demo as explained in the Hello World Example chapter.
3 No need to change default string constants at string.xml file. Android studio takes care of default
string constants.
4 Run the application to launch Android emulator and verify the result of the changes done in the
application.
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
<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:layout_alignParentEnd="true"
android:textSize="50dp"/>
</RelativeLayout>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.demo.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
Let's try to run your demo application. I assume you had created your AVD while doing
environment setup. To run the app from Android studio, open one of your project's activity
files and click Run icon from the toolbar. Android studio installs the app on your AVD and
starts it and if everything is fine with your setup and application, it will display following
Emulator window −
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.
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
Example
This example will take you through simple steps to show how to create your own Android
application using Linear Layout and EditText.
Ste Description
p
1 You will use Android studio IDE to create an Android application and name it as demo under a
package com.example.demo as explained in the Hello World Example chapter.
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
eText = (EditText) findViewById(R.id.edittext);
btn = (Button) findViewById(R.id.button);
btn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
String str = eText.getText().toString();
Toast msg =
Toast.makeText(getBaseContext(),str,Toast.LENGTH_LONG);
msg.show();
}
});
}
}
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="14dp"
android:layout_marginTop="18dp"
android:text="@string/example_edittext" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_below="@+id/textView1"
android:layout_marginTop="130dp"
android:text="@string/show_the_text" />
<EditText
android:id="@+id/edittext"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button"
android:layout_below="@+id/textView1"
android:layout_marginTop="61dp"
android:ems="10"
android:text="@string/enter_text" android:inputType="text" />
</RelativeLayout>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
<activity
android:name="com.example.demo.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Let's try to run your demo application. I assume you had created your AVD while doing
environment setup. To run the app from Android studio, open one of your project's activity
files and click Run icon from the toolbar. Android Studio installs the app on your AVD and
starts it and if everything is fine with your setup and application, it will display following
Emulator window −
android:adjustViewBounds
1
Set this to true if you want the ImageView to adjust its bounds to preserve the aspect ratio of its
drawable.
2 android:baseline
This is the offset of the baseline within this view.
3 android:baselineAlignBottom
If true, the image view will be baseline aligned with based on its bottom edge.
5 android:src
This sets a drawable as the content of this ImageView.
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.
Example
This example will take you through simple steps to show how to create your own Android
application using Linear Layout and ImageButton.
Ste Description
p
4 No need to define default constants in android, Android studio takes care of default constants.
5 Run the application to launch Android emulator and verify the result of the changes done in the
application.
package com.example.myapplication;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.Toast;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imgButton =(ImageButton)findViewById(R.id.imageButton);
imgButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(),"You download is
resumed",Toast.LENGTH_LONG).show();
}
});
}
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageButton"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:src="@drawable/abc"/>
</RelativeLayout>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Let's try to run your myapplication application. I assume you had created your AVD while
doing environment setup. To run the app from Android Studio, open one of your project's
activity files and click Run icon from the toolbar. Android Studio installs the app on your
AVD and starts it and if everything is fine with your setup and application, it will display
following Emulator window −
The following screen will appear after ImageButton is clicked,It shows a toast message.
ToggleButton Attributes
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.
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.
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.
Example
This example will take you through simple steps to show how to create your own Android
application using Linear Layout and ToggleButton.
Ste Description
p
1 You will use Android studio IDE to create an Android application and name it as My
Application under a package com.example.saira_000.myapplication as explained in the Hello
World Example chapter.
3 No need to declare default constants.Android studio takes care of default constants at string.xml
Following is the content of the modified main activity file src/MainActivity.java. This file can
include each of the fundamental lifecycle methods.
In the below example abc indicates the image of tutorialspoint
package com.example.saira_000.myapplication;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import android.widget.ToggleButton;
tg1=(ToggleButton)findViewById(R.id.toggleButton);
tg2=(ToggleButton)findViewById(R.id.toggleButton2);
b1=(Button)findViewById(R.id.button2);
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
StringBuffer result = new StringBuffer();
result.append("You have clicked first ON Button-:)
").append(tg1.getText());
result.append("You have clicked Second ON Button -:)
").append(tg2.getText());
Toast.makeText(MainActivity.this,
result.toString(),Toast.LENGTH_SHORT).show();
}
});
}
}
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Tutorials point"
android:textColor="#ff87ff09"
android:textSize="30dp"
android:layout_above="@+id/imageButton"
android:layout_centerHorizontal="true"
android:layout_marginBottom="40dp" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageButton"
android:src="@drawable/abc"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
<ToggleButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="On"
android:id="@+id/toggleButton"
android:checked="true"
android:layout_below="@+id/imageButton"
android:layout_toEndOf="@+id/button2/>
<ToggleButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Off"
android:id="@+id/toggleButton2"
android:checked="true"
android:layout_alignTop="@+id/toggleButton" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/button2"
android:text="ClickMe"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.My Application.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Let's try to run your My Application application. I assume you had created your AVD while
doing environment setup. To run the app from Android studio, open one of your project's
activity files and click Run icon from the toolbar. Android studio installs the app on your
AVD and starts it and if everything is fine with your setup and application, it will display
following Emulator window −
The following screen will appear −
A RadioButton has two states: either checked or unchecked.This allows the user to select one option
from a set.
Radio Button
Example
This example will take you through simple steps to show how to create your own Android
application using Linear Layout and RadioButton.
Ste Description
p
3 Android studio takes care of default constants so no need to declare default constants at
string.xml file
4 Run the application to launch Android emulator and verify the result of the changes done in the
application.
Following is the content of the modified main activity file src/MainActivity.java. This file can
include each of the fundamental lifecycle methods.
In the below example abc indicates the image of tutorialspoint
package com.example.saira_000.myapplication;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;
Toast.makeText(MainActivity.this,rb1.getText(),Toast.LENGTH_LONG).show();
}
});
}
}
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Example of Radio Button"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:textSize="30dp" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Tutorials point"
android:textColor="#ff87ff09"
android:textSize="30dp"
android:layout_above="@+id/imageButton"
android:layout_centerHorizontal="true"
android:layout_marginBottom="40dp" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageButton"
android:src="@drawable/abc"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
<RadioGroup
android:id="@+id/radioGroup"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="@+id/imageButton"
android:layout_alignLeft="@+id/textView2"
android:layout_alignStart="@+id/textView2">
<RadioButton
android:layout_width="142dp"
android:layout_height="wrap_content"
android:text="JAVA"
android:id="@+id/radioButton"
android:textSize="25dp"
android:textColor="@android:color/holo_red_light"
android:checked="false"
android:layout_gravity="center_horizontal" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ANDROID"
android:id="@+id/radioButton2"
android:layout_gravity="center_horizontal"
android:checked="false"
android:textColor="@android:color/holo_red_dark"
android:textSize="25dp" />
<RadioButton
android:layout_width="136dp"
android:layout_height="wrap_content"
android:text="HTML"
android:id="@+id/radioButton3"
android:layout_gravity="center_horizontal"
android:checked="false"
android:textSize="25dp"
android:textColor="@android:color/holo_red_dark" />
</RadioGroup>
</RelativeLayout>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.My Application.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Let's try to run your My Application application. I assume you had created your AVD while
doing environment setup. To run the app from Android Studio, open one of your project's
activity files and click Run icon from the toolbar. Android Studio installs the app on your
AVD and starts it and if everything is fine with your setup and application, it will display
following Emulator window −
If User selected any of a Radio Button, It should give same name on Toast message. for
suppose, if User selected JAVA, it gives a toast message as JAVA.
A RadioButton has two states: either checked or unchecked.This allows the user to select one option
from a set.
Radio Button
Example
This example will take you through simple steps to show how to create your own Android
application using Linear Layout and RadioButton.
1 You will use Android studio to create an Android application and name it as My Application under
a package com.example.saira_000.myapplication as explained in the Hello World
Example chapter.
3 Android studio takes care of default constants so no need to declare default constants at
string.xml file
4 Run the application to launch Android emulator and verify the result of the changes done in the
application.
Following is the content of the modified main activity file src/MainActivity.java. This file can
include each of the fundamental lifecycle methods.
In the below example abc indicates the image of tutorialspoint
package com.example.saira_000.myapplication;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;
Toast.makeText(MainActivity.this,rb1.getText(),Toast.LENGTH_LONG).show();
}
});
}
}
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Example of Radio Button"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:textSize="30dp" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Tutorials point"
android:textColor="#ff87ff09"
android:textSize="30dp"
android:layout_above="@+id/imageButton"
android:layout_centerHorizontal="true"
android:layout_marginBottom="40dp" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/button2"
android:text="ClickMe"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true" />
<RadioGroup
android:id="@+id/radioGroup"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="@+id/imageButton"
android:layout_alignLeft="@+id/textView2"
android:layout_alignStart="@+id/textView2">
<RadioButton
android:layout_width="142dp"
android:layout_height="wrap_content"
android:text="JAVA"
android:id="@+id/radioButton"
android:textSize="25dp"
android:textColor="@android:color/holo_red_light"
android:checked="false"
android:layout_gravity="center_horizontal" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ANDROID"
android:id="@+id/radioButton2"
android:layout_gravity="center_horizontal"
android:checked="false"
android:textColor="@android:color/holo_red_dark"
android:textSize="25dp" />
<RadioButton
android:layout_width="136dp"
android:layout_height="wrap_content"
android:text="HTML"
android:id="@+id/radioButton3"
android:layout_gravity="center_horizontal"
android:checked="false"
android:textSize="25dp"
android:textColor="@android:color/holo_red_dark" />
</RadioGroup>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.My Application.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Let's try to run your My Application application. I assume you had created your AVD while
doing environment setup. To run the app from Android Studio, open one of your project's
activity files and click Run icon from the toolbar. Android Studio installs the app on your
AVD and starts it and if everything is fine with your setup and application, it will display
following Emulator window −
RadioGroup Attributes
Following are the important attributes related to RadioGroup control. You can check Android
official documentation for complete list of attributes and related methods which you can use
to change these attributes are run time.
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.
Example
This example will take you through simple steps to show how to create your own Android
application using Linear Layout and RadioGroup.
Ste Description
p
1 You will use Android studio IDE to create an Android application and name it as My
Application under a package com.example.saira_000.myapplication; as explained in the Hello
World Example chapter.
4 Run the application to launch Android emulator and verify the result of the changes done in the
application.
Following is the content of the modified main activity file src/MainActivity.java. This file can
include each of the fundamental lifecycle methods.
In the below example abc indicates the image of tutorialspoint
package com.example.saira_000.myapplication;
import android.app.Activity;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
radioSexGroup=(RadioGroup)findViewById(R.id.radioGroup);
btnDisplay=(Button)findViewById(R.id.button);
btnDisplay.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int selectedId=radioSexGroup.getCheckedRadioButtonId();
radioSexButton=(RadioButton)findViewById(selectedId);
Toast.makeText(MainActivity.this,radioSexButton.getText(),Toast.LENGTH_SH
ORT).show();
}
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Radio button"
android:id="@+id/textView"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:textSize="35dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Tutorialspoint"
android:id="@+id/textView2"
android:layout_below="@+id/textView"
android:layout_alignRight="@+id/textView"
android:layout_alignEnd="@+id/textView"
android:textSize="35dp"
android:textColor="@android:color/holo_green_dark" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageView"
android:src="@drawable/abc"
android:layout_below="@+id/textView2"
android:layout_alignLeft="@+id/textView"
android:layout_alignStart="@+id/textView"
android:layout_alignRight="@+id/textView"
android:layout_alignEnd="@+id/textView" />
<RadioGroup
android:layout_width="fill_parent"
android:layout_height="90dp"
<RadioButton
android:layout_width="wrap_content"
android:layout_height="55dp"
android:text="Male"
android:id="@+id/radioButton"
android:layout_gravity="center_horizontal"
android:checked="false"
android:textSize="25dp" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Female"
android:id="@+id/radioButton2"
android:layout_gravity="center_horizontal"
android:checked="false"
android:textSize="25dp"
android:layout_weight="0.13" />
</RadioGroup>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" Are you?"
android:id="@+id/textView3"
android:textSize="35dp"
android:layout_below="@+id/imageView"
android:layout_alignRight="@+id/textView2"
android:layout_alignEnd="@+id/textView2"
android:layout_alignLeft="@+id/imageView"
android:layout_alignStart="@+id/imageView" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="@+id/button"
android:layout_gravity="center_horizontal"
android:layout_below="@+id/radioGroup"
android:layout_centerHorizontal="true" />
</RelativeLayout>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.My Application.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Let's try to run your My Application application. I assume you had created your AVD while
doing environment setup. To run the app from Android studio, open one of your project's
activity files and click Run icon from the toolbar. Android studio installs the app on your
AVD and starts it and if everything is fine with your setup and application, it will display
following Emulator window −
The following screen will appear, here we have a RadioGroup.
android:autoText
1
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
5 android:text
This is the Text to display.
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.
Example
This example will take you through simple steps to show how to create your own Android
application using Linear Layout and CheckBox.
Ste Description
p
1 You will use Android Studio IDE to create an Android application and name it
as myapplication under a package com.example.myapplication as explained in the Hello World
Example chapter.
4 No need to declare default string constants. Android studio takes care of default constants at
string.xml
5 Run the application to launch Android emulator and verify the result of the changes done in the
application.
Following is the content of the modified main activity file src/MainActivity.java. This file can
include each of the fundamental lifecycle methods.
package com.example.myapplication;
import android.os.Bundle;
import android.app.Activity;
import android.widget.Button;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CheckBox;
import android.widget.Toast;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ch1=(CheckBox)findViewById(R.id.checkBox1);
ch2=(CheckBox)findViewById(R.id.checkBox2);
b1=(Button)findViewById(R.id.button);
b2=(Button)findViewById(R.id.button2);
b2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity">
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Example of checkbox"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:textSize="30dp" />
<CheckBox
android:id="@+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Do you like Tutorials Point"
android:layout_above="@+id/button"
android:layout_centerHorizontal="true" />
<CheckBox
android:id="@+id/checkBox2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Do you like android "
android:checked="false"
android:layout_above="@+id/checkBox1"
android:layout_alignLeft="@+id/checkBox1"
android:layout_alignStart="@+id/checkBox1" />
<TextView
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Ok"
android:id="@+id/button"
android:layout_alignParentBottom="true"
android:layout_alignLeft="@+id/checkBox1"
android:layout_alignStart="@+id/checkBox1" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Cancel"
android:id="@+id/button2"
android:layout_alignParentBottom="true"
android:layout_alignRight="@+id/textView2"
android:layout_alignEnd="@+id/textView2" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageButton"
android:src="@drawable/abc"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
<activity
android:name="com.example.myapplication.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Let's try to run your MyApplication application. I assume you had created your AVD while
doing environment setup. To run the app from Android studio, open one of your project's
activity files and click Run icon from the toolbar. Android studio installs the app on your
AVD and starts it and if everything is fine with your setup and application, it will display
following Emulator window −
Apart from these methods, there are other methods that are provided by the ProgressDialog
class
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.
Example
This example demonstrates the horizontal use of the progress dialog which is in fact a
progress bar. It display a progress bar on pressing the button.
To experiment with this example, you need to run this on an actual device after developing
the application according to the steps below.
Step Description
s
1 You will use Android studio to create an Android application under a package
com.example.sairamkrishna.myapplication.
2 Modify src/MainActivity.java file to add progress code to display the progress dialog.
4 Run the application and choose a running android device and install the application on it and
verify the results.
import android.app.ProgressDialog;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textView"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:textSize="30dp"
android:text="Progress bar" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Download"
android:onClick="download"
android:id="@+id/button2"
android:layout_marginLeft="125dp"
android:layout_marginStart="125dp"
android:layout_centerVertical="true" />
</RelativeLayout>
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Let's try to run your application. We assume, you have connected your actual Android
Mobile device with your computer. To run the app from Android studio, open one of your
project's activity files and click Run icon from the toolbar. Before starting your application,
Android studio will display following window to select an option where you want to run your
Android application.
ListView Attributes
Following are the important attributes specific to GridView −
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.
ArrayAdapter
You can use this adapter when your data source is an array. By default, ArrayAdapter
creates a view for each array item by calling toString() on each item and placing the
contents in a TextView. Consider you have an array of strings you want to display in a
ListView, initialize a new ArrayAdapter using a constructor to specify the layout for each
string and the string array −
ArrayAdapter adapter = new
ArrayAdapter<String>(this,R.layout.ListView,StringArray);
You will define your list view under res/layout directory in an XML file. For our example we
are going to using activity_main.xml file.
Example
Following is the example which will take you through simple steps to show how to create
your own Android application using ListView. Follow the following steps to modify the
Android application we created in Hello World Example chapter −
Ste Description
p
1 You will use Android Studio IDE to create an Android application and name it as ListDisplay under
a package com.example.ListDisplay as explained in the Hello World Example chapter.
2 Modify the default content of res/layout/activity_main.xml file to include ListView content with the
self explanatory attributes.
3 No need to change string.xml, Android studio takes care of default string constants.
4 Create a Text View file res/layout/activity_listview.xml. This file will have setting to display all the
list items. So you can customize its fonts, padding, color etc. using this file.
6 Run the application to launch Android emulator and verify the result of the changes done in the
application.
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.ArrayAdapter;
import android.widget.ListView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
<ListView
android:id="@+id/mobile_list"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/label"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dip"
android:textSize="16dip"
android:textStyle="bold" >
</TextView>
SimpleCursorAdapter
You can use this adapter when your data source is a database Cursor. When
using SimpleCursorAdapter, you must specify a layout to use for each row in
the Cursor and which columns in the Cursor should be inserted into which views of the
layout.
For example, if you want to create a list of people's names and phone numbers, you can
perform a query that returns a Cursor containing a row for each person and columns for the
names and numbers. You then create a string array specifying which columns from the
When you instantiate the SimpleCursorAdapter, pass the layout to use for each result, the
Cursor containing the results, and these two arrays −
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
R.layout.person_name_and_number, cursor, fromColumns, toViews, 0);
The SimpleCursorAdapter then creates a view for each row in the Cursor using the provided
layout by inserting each from Columns item into the corresponding toViews view.
GridView Attributes
Following are the important attributes specific to GridView −
1 android:id
This is the ID which uniquely identifies the layout.
2 android:columnWidth
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.
4 android:horizontalSpacing
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.
android:stretchMode
Defines how columns should stretch to fill the available empty space, if any. This must be either
of the values −
6 ● 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..
Example
This example will take you through simple steps to show how to create your own Android
application using GridView. Follow the following steps to modify the Android application we
created in Hello World Example chapter −
Ste Description
p
1 You will use Android studio IDE to create an Android application and name it as HelloWorld under
a package com.example.helloworld as explained in the Hello World Example chapter.
2 Modify the detault content of res/layout/activity_main.xml file to include GridView content with the
self explanatory attributes.
3 No need to change string.xml, Android studio takes care of defaults strings which are placed at
string.xml
4 Let's put few pictures in res/drawable-hdpi folder. I have put sample0.jpg, sample1.jpg,
sample2.jpg, sample3.jpg, sample4.jpg, sample5.jpg, sample6.jpg and sample7.jpg.
5 Create a new class called ImageAdapter under a package com.example.helloworld that extends
BaseAdapter. This class will implement functionality of an adapter to be used to fill the view.
6 Run the application to launch Android emulator and verify the result of the changes done in the
application.
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.GridView;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
// Constructor
public ImageAdapter(Context c) {
mContext = c;
if (convertView == null) {
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(8, 8, 8, 8);
}
else
{
imageView = (ImageView) convertView;
}
imageView.setImageResource(mThumbIds[position]);
return imageView;
}
Let's try to run our modified Hello World! application we just modified. I assume you had
created your AVD while doing environment setup. To run the app from Android Studio, open
one of your project's activity files and click Run icon from the toolbar. Android studio
Sub-Activity Example
Let's extend the functionality of above example where we will show selected grid image in
full screen. To achieve this we need to introduce a new activity. Just keep in mind for any
activity we need perform all the steps like we have to implement an activity class, define that
activity in AndroidManifest.xml file, define related layout and finally link that sub-activity with
the main activity by it in the main activity class. So let's follow the steps to modify above
example −
Ste Description
p
3 Create new layout file for the new activity under res/layout/ folder. Let's name this XML file as
single_view.xml.
4 Define your new activity in AndroidManifest.xml file using <activity.../> tag. An application can
have one or more activities without any restrictions.
5 Run the application to launch Android emulator and verify the result of the changes done in the
application.
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
gridview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent,
View v, int position, long id){
// Send intent to SingleViewActivity
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.ImageView;
// Selected image id
int position = i.getExtras().getInt("id");
ImageAdapter imageAdapter = new ImageAdapter(this);
<ImageView android:id="@+id/SingleView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
</LinearLayout>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.helloworld.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".SingleViewActivity"></activity>
</application>
</manifest>
Let's try to run our modified Hello World! application we just modified. I assume you had
created your AVD while doing environment setup. To run the app from Android studio, open
one of your project's activity files and click Run icon from the toolbar. Android studio
installs the app on your AVD and starts it and if everything is fine with your setup and
application, it will display following Emulator window −
<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_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"
</LinearLayout>
package 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;
Button button;
ImageView image;
@Override
setContentView(R.layout.activity_main);
addListenerOnButton();
image=(ImageView) findViewById(R.id.imageView1);
button=(Button) findViewById(R.id.btnChangeImage);
button.setOnClickListener(new OnClickListener() {
@Override
image.setImageResource(R.drawable.ic_launcher_background);
});
<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>
</intent-filter>
</activity>
</application>
</manifest>
Let's try to run your application. I assume you have connected your actual Android Mobile
device with your computer. To run the app from android studio, open one of your project's
activity files and click Run icon from the toolbar. Select your mobile device as an option
and then check your mobile device which will display your default screen −
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="MainActivity">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/buttonShowCustomDialog"
android:onClick="exit"
android:textStyle="normal|bold"
style="@android:style/Widget.DeviceDefault.Button.Inset"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="131dp"
android:text="Click"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/buttonShowCustomDialog"
android:layout_marginBottom="86dp"
android:id="@+id/textView"
android:gravity="center"
android:textStyle="normal|bold"
android:textSize="18sp" />
</RelativeLayout>
Step 3 – Create a layout resourse file and add the following code in customdialog.xml
<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:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:text="Alert...!"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/text"
android:textStyle="normal|bold"
android:textSize="18sp"
android:layout_alignParentTop="true"
android:layout_centerInParent="true"/>
<ImageButton
android:layout_width="35dp"
android:layout_height="35dp"
app:srcCompat="@drawable/ic_block"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
tools:ignore="ContentDescription,RtlHardcoded" />
<Button
android:id="@+id/dialogButtonOK"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:text="Dismiss"
android:layout_marginTop="23dp"
android:paddingRight="5dp"
android:layout_below="@+id/image"
style="@style/Widget.AppCompat.Button.Colored"
android:textSize="18sp" />
</RelativeLayout>
import android.app.Dialog;
import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
@Override
setContentView(R.layout.activity_main);
button = findViewById(R.id.buttonShowCustomDialog);
button.setOnClickListener(new View.OnClickListener() {
@Override
dialog.setContentView(R.layout.customdialog);
Button dialogButton =
dialog.findViewById(R.id.dialogButtonOK);
dialogButton.setOnClickListener(new View.OnClickListener()
{
@Override
dialog.dismiss();
Toast.makeText(getApplicationContext(),"Dismissed..!!",Toast.LENGTH_SHORT
).show();
});
dialog.show();
});
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="app.com.sample">
<application
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=".MainActivity">
<intent-filter>
</intent-filter>
</activity>
</application>
</manifest>
Let's try to run your application. I assume you have connected your actual Android Mobile
device with your computer. To run the app from android studio, open one of your project's
activity files and click Run icon from the toolbar. Select your mobile device as an option
and then check your mobile device which will display your default screen –
In the last step, you have to register the DatePickerDialog listener and override its
onDateSet method. This onDateSet method contains the updated day, month and year. Its
syntax is given below −
private DatePickerDialog.OnDateSetListener myDateListener = new
DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker arg0, int arg1, int arg2, int arg3) {
// arg1 = year
// arg2 = month
// arg3 = day
}
};
Apart form date attributes, DatePicker object is also passed into this function. You can use
the following methods of the DatePicker to perform further operation.
2
getMonth()
This method gets the selected month
3
getYear()
This method gets the selected year
4
setMaxDate(long maxDate)
This method sets the maximal date supported by this DatePicker in milliseconds since January
1, 1970 00:00:00 in getDefault() time zone
5
setMinDate(long minDate)
This method sets the minimal date supported by this NumberPicker in milliseconds since
January 1, 1970 00:00:00 in getDefault() time zone
6
setSpinnersShown(boolean shown)
This method sets whether the spinners are shown
7
updateDate(int year, int month, int dayOfMonth)
This method updates the current date
8
getCalendarView()
This method returns calendar view
9
getFirstDayOfWeek()
This Method returns first day of the week
Example
Here is an example demonstrating the use of DatePickerDialog class. It creates a basic
Date Picker application that allows you to set the Date using DatePicker Widget
To experiment with this example , you can run this on an actual device or in an emulator.
Step Description
s
5 Run the application and choose a running android device and install the application on it and
verify the results.
import java.util.Calendar;
import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.DatePicker;
import android.widget.TextView;
import android.widget.Toast;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
@SuppressWarnings("deprecation")
public void setDate(View view) {
showDialog(999);
Toast.makeText(getApplicationContext(), "ca",
Toast.LENGTH_SHORT)
.show();
}
@Override
protected Dialog onCreateDialog(int id) {
// TODO Auto-generated method stub
if (id == 999) {
return new DatePickerDialog(this,
myDateListener, year, month, day);
}
return null;
}
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="70dp"
android:onClick="setDate"
android:text="@string/date_button_set" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="24dp"
android:text="@string/date_label_set"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/button1"
android:layout_marginTop="66dp"
android:layout_toLeftOf="@+id/button1"
android:text="@string/date_view_set"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/button1"
android:layout_below="@+id/textView2"
android:layout_marginTop="72dp"
android:text="@string/date_selected"
android:textAppearance="?android:attr/textAppearanceMedium" />
</RelativeLayout>
Let's try to run our DatePicker application we just modified. I assume you had created
your AVD while doing environment set-up. To run the app from Eclipse, open one of your
project's activity files and click Run icon from the tool bar. Eclipse installs the app on your
AVD and starts it and if everything is fine with your set-up and application, it will display
following Emulator window −
Now set the required date, and after setting the date, press the Done button. This dialog will
disappear and your newly setted date will start showing at the screen. This is shown below.