User Interface With View
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.
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
<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"
</RelativeLayout>
Buttons
A Button is a Push-button which can be pressed, or clicked, by the user to perform an action.
Button Attributes
1 android:autoText
If set, specifies that this TextView has a textual input method and automatically corrects
some common spelling errors.
2 android:drawableBottom
3 android:drawableRight
4 android:editable
5 android:text
Attribute Description
1 android:background
2 android:contentDescription
3 android:id
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
Program
<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
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.
1 android:background
2 android:contentDescription
3 android:id
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
Program
<!--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;
@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);
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
1 android:autoText
If set, specifies that this TextView has a textual input method and automatically corrects
some common spelling errors.
2 android:drawableBottom
3 android:drawableRight
4 android:editable
5 android:text
1 android:background
2 android:contentDescription
This defines text that briefly describes content of the view.
3 android:id
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
Program
<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;
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);
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
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.
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
<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;
@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
ToggleButton Attributes
1 android:disabledAlpha
This is the alpha to apply to the indicator when disabled.
2 android:textOff
3 android:textOn
1 android:autoText
If set, specifies that this TextView has a textual input method and automatically corrects
some common spelling errors.
2 android:drawableBottom
3 android:drawableRight
4 android:editable
5 android:text
1 android:background
2 android:contentDescription
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
Program
<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
}
});
}
}
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
Attribute Description
android:checkedButton This is the id of child radio button that should be checked by default
within this radio group.
1 android:background
2 android:contentDescription
3 android:id
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
Program
<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;
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.
1 getMax()
2 incrementProgressBy(int diff)
This method increments the progress bar by the difference of value passed as a
parameter.
3 setIndeterminate(boolean indeterminate)
4 setMax(int max)
5 setProgress(int value)
This method is used to update the progress dialog with some specific value.
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);
}
});
}
AutoComplete TextView
AutoCompleteTextView Attributes
Following are the important attributes related to AutoCompleteTextView control.
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
<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;
Spinner
Program
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;
@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
1 android:id
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
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
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 −
android:verticalSpacing
7
Defines the default vertical spacing between rows. This could be in px, dp, sp, in, or mm.
Program
<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;
@Override
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gridview.setAdapter(new ImageAdapter(this));
ImageView
Program
<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;
Button button;
ImageView image;
@Override
super.onCreate(savedInstanceState);
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);
});
}
<?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>
</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
<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
<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>
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
<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;
date.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
@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) {