0% found this document useful (0 votes)
12 views69 pages

Unit 4

This document provides an overview of various user interface components in Android, including EditText, AutoCompleteTextView, Button, ImageButton, and ToggleButton. It explains their functionalities, usage, and how to implement them in an Android application with code examples. Additionally, it covers the importance of specifying input types for EditText and the role of listeners for button interactions.

Uploaded by

shrutimanval104
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views69 pages

Unit 4

This document provides an overview of various user interface components in Android, including EditText, AutoCompleteTextView, Button, ImageButton, and ToggleButton. It explains their functionalities, usage, and how to implement them in an Android application with code examples. Additionally, it covers the importance of specifying input types for EditText and the role of listeners for button interactions.

Uploaded by

shrutimanval104
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

UNIT IV Designing User Interface(Weightage-6 marks)

Edit text
• EditText is a Widget of user interface (UI) used to retrieve and
modify text data from a user in an Android app.
• EditText is a subclass of TextView that inherit all the property of
TextView. Nowadays,
• EditText is represented with the PlainText element in UI, which
displays an empty text field while designing the app.
• EditText (or PlainText) is used in the app whenever you need input
from the user side and proceed with its text (or value) in your app.
• EditText (or PlainText) is not only used to get plain text in your
application, but even you can use it to get values such as email,
number, password, etc.
• To get the value of each type in EditText, you must specify its input
type in its inputType attribute. For example, to input plain text, set
the inputType attribute to "text", and to input only numeric values,
set the inputType attribute to "number".
<EditText
<?xml version="1.0" encoding="utf-8"?> android:id="@+id/editText2"
<RelativeLayout android:layout_width="wrap_content"
xmlns:android="[Link] android:layout_height="wrap_content"
xmlns:app="[Link] android:layout_alignParentStart="true"
xmlns:tools="[Link] android:layout_alignParentTop="true"
android:layout_width="match_parent" android:layout_alignParentEnd="true"
android:layout_height="match_parent" android:layout_marginStart="105dp"
tools:context=".MainActivity" android:layout_marginTop="136dp"
tools:ignore="ExtraText"> android:layout_marginEnd="97dp"
android:ems="10"
<EditText android:inputType="textPassword"
android:id="@+id/editText1" android:hint="ereshmanter password"
android:layout_width="wrap_content" tools:ignore="SpeakableTextPresentCheck,TouchTargetSizeCheck"
android:layout_height="wrap_content" tools:layout_editor_absoluteX="84dp"
android:layout_alignParentTop="true" tools:layout_editor_absoluteY="127dp" />
android:layout_centerHorizontal="true" <Button
android:layout_marginTop="61dp" android:id="@+id/button"
android:ems="10" android:layout_width="wrap_content"
android:inputType="text" android:layout_height="wrap_content"
android:hint="enter text?" android:layout_below="@+id/editText2"
tools:ignore="SpeakableTextPresentCheck,TouchTargetSizeCheck" android:layout_centerHorizontal="true"
tools:layout_editor_absoluteX="84dp" android:layout_marginTop="109dp"
tools:layout_editor_absoluteY="53dp" /> android:text="Show Value"
tools:layout_editor_absoluteX="148dp"
tools:layout_editor_absoluteY="266dp"
tools:ignore="HardcodedText" />
</RelativeLayout>
package [Link].myapplication_edittext; @Override
protected void onCreate(Bundle savedInstanceState) {
import [Link]; [Link](savedInstanceState);
[Link](this);
import [Link];
setContentView([Link].activity_main);
import [Link];
import [Link]; addListenerOnButton();
import [Link];
}
public void addListenerOnButton() {
import [Link]; edittext1 = findViewById([Link].editText1);
import [Link]; edittext2 = findViewById([Link].editText2);
import [Link]; buttonDisplay = findViewById([Link]);
import [Link];
[Link](new
import [Link]; [Link]() {
@Override
public class MainActivity extends AppCompatActivity { public void onClick(View view) {
String value1=[Link]().toString();
private EditText edittext1, edittext2;
String value2=[Link]().toString();
private Button buttonDisplay;
[Link](getApplicationContext(),value1+"\n"+value2,
Toast.LENGTH_LONG).show();
}
});
AutoCompleteTextView
• Android AutoCompleteTextView completes the word based
on the reserved words, so no need to write all the characters of
the word.
• Android AutoCompleteTextView is a editable text field, it
displays a list of suggestions in a drop down menu from which
user can select only one suggestion or value.
• Android AutoCompleteTextView is the subclass of EditText
class. The MultiAutoCompleteTextView is the subclass of
AutoCompleteTextView class.
<?xml version="1.0" encoding="utf-8"?> <AutoCompleteTextView
<[Link] android:id="@+id/autoCompleteTextView"
xmlns:android="[Link] android:layout_width="200dp"
android:layout_height="wrap_content"
xmlns:app="[Link] android:layout_marginStart="92dp"
xmlns:tools="[Link] android:layout_marginTop="144dp"
android:text=""
android:id="@+id/main"
app:layout_constraintStart_toStartOf="parent"
android:layout_width="match_parent" app:layout_constraintTop_toTopOf="parent" />
android:layout_height="match_parent"
</[Link]
tools:context=".MainActivity">
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="What is your favourite programming
language?"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.032"
package [Link].myapplication_autocomplete_textview;
@Override
void onCreate(Bundle savedInstanceState) {
import [Link]; [Link](savedInstanceState);
[Link](this);
import [Link]; setContentView([Link].activity_main);
import [Link]; //Creating the instance of ArrayAdapter containing list of
import [Link]; language names
ArrayAdapter<String> adapter = new ArrayAdapter<String>
(this,[Link].select_dialog_item,language);
import [Link]; //Getting the instance of AutoCompleteTextView
AutoCompleteTextView actv =
import [Link];
(AutoCompleteTextView)findViewById([Link]
import [Link]; iew);
import [Link]; [Link](1);//will start working from first
character
import [Link];
[Link](adapter);//setting the adapter data into
the AutoCompleteTextView
public class MainActivity extends AppCompatActivity { [Link]([Link]);
}
String[] language }
={"C","C++","Java",".NET","iPhone","Android","[Link]","PHP"};
Button
• Android Button represents a push-button.
• The [Link] is subclass of TextView class
and CompoundButton is the subclass of Button class.
• There are different types of buttons in android such as
RadioButton, ToggleButton, CompoundButton etc.
Android Button Example with Listener
• Here, we are going to create two textfields and one button for sum of two numbers.
• If user clicks button, sum of two input values is displayed on the Toast.
• We can perform action on button using different types such as calling listener on button or adding onClick
property of button in activity's xml file.
activity_main.xml <EditText
<?xml version="1.0" encoding="utf-8"?> android:id="@+id/editText2"
android:layout_width="wrap_content"
<RelativeLayout android:layout_height="wrap_content"
xmlns:android="[Link] android:layout_below="@+id/editText1"
xmlns:app="[Link] android:layout_centerHorizontal="true"
xmlns:tools="[Link] android:layout_marginTop="32dp"
android:layout_width="match_parent" android:contentDescription="Enter a number2"
android:layout_height="match_parent" android:ems="10"
tools:context=".MainActivity"> android:hint="Number2"
android:inputType="number"
<EditText android:minHeight="48dp"
android:id="@id/editText1" tools:ignore="Autofill,LabelFor,EditableContentDescCheck"
android:layout_width="wrap_content" tools:layout_editor_absoluteX="84dp"
android:layout_height="wrap_content" tools:layout_editor_absoluteY="127dp" />
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" <Button
android:layout_marginTop="61dp" android:id="@+id/button"
android:contentDescription="Enter a number1" android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10" android:layout_below="@+id/editText2"
android:hint="Number1" android:layout_centerHorizontal="true"
android:importantForAutofill="no" android:layout_marginTop="109dp"
android:inputType="number" android:text="ADD"
android:minHeight="48dp" tools:layout_editor_absoluteX="148dp"
tools:ignore="Autofill,LabelFor,EditableContentDescCheck" tools:layout_editor_absoluteY="266dp"
tools:layout_editor_absoluteX="84dp" tools:ignore="HardcodedText" />
tools:layout_editor_absoluteY="53dp" />
</RelativeLayout>
package [Link].myapplication_button;
public void addListenerOnButton() {
import [Link];
edittext1 = (EditText) findViewById([Link].editText1);
import [Link];
edittext2 = (EditText) findViewById([Link].editText2);
import [Link];
buttonSum = (Button) findViewById([Link]);
import [Link];
import [Link];
[Link](new [Link]()
import [Link];
{
import [Link];
@Override
import [Link];
public void onClick(View view) {
import [Link];
String value1=[Link]().toString();
import [Link];
String value2=[Link]().toString();
int a=[Link](value1);
public class MainActivity extends AppCompatActivity {
int b=[Link](value2);
private EditText edittext1, edittext2;
int sum=a+b;
private Button buttonSum;
@Override
[Link](getApplicationContext(),[Link](sum),
protected void onCreate(Bundle savedInstanceState) {
Toast.LENGTH_LONG).show();
[Link](savedInstanceState);
}
setContentView([Link].activity_main);
});
}
addListenerOnButton();
}
}
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.
<?xml version="1.0" encoding="utf-8"?>
<[Link]
xmlns:android="[Link]
xmlns:app="[Link]
xmlns:tools="[Link]
android:id="@+id/main" <ImageButton
android:layout_width="match_parent" android:id="@+id/imageButton"
android:layout_height="match_parent" android:layout_width="wrap_content"
tools:context=".MainActivity"> android:layout_height="wrap_content"
<TextView android:layout_centerHorizontal="true"
android:layout_width="wrap_content" android:layout_centerVertical="true"
android:layout_height="wrap_content" android:src="@drawable/download"
android:layout_alignEnd="@+id/imageButton" app:layout_constraintBottom_toBottomOf="parent"
android:layout_alignParentTop="true" app:layout_constraintEnd_toEndOf="parent"
android:layout_marginTop="140dp" app:layout_constraintHorizontal_bias="0.625"
android:text="Image Button" app:layout_constraintStart_toStartOf="parent"
android:textSize="30dp" app:layout_constraintTop_toTopOf="parent"
app:layout_constraintEnd_toEndOf="parent" tools:ignore="ContentDescription,MissingConstraints" />
app:layout_constraintHorizontal_bias="0.614" </[Link]>
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:ignore="HardcodedText,MissingConstraints,SpUsage" />
package [Link].myapplication_imagebutton;
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class MainActivity extends AppCompatActivity {
ImageButton imgButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
[Link](this);
setContentView([Link].activity_main);
[Link](findViewById([Link]), (v, insets) -> {
Insets systemBars = [Link]([Link]());
[Link]([Link], [Link], [Link], [Link]);
return insets; });
imgButton =(ImageButton)findViewById([Link]);
[Link](new [Link]() {
@Override
public void onClick(View v) {
[Link](getApplicationContext(),"Welcome to Android Image Button",Toast.LENGTH_LONG).show();
} });}}
Android ToggleButton
• Android Toggle Button can be used to display
checked/unchecked (On/Off) state on the button.
• It is beneficial if user have to change the setting between
two states. It can be used to On/Off Sound, Wifi, Bluetooth
etc.
• Since Android 4.0, there is another type of toggle button
called switch that provides slider control.
• Android ToggleButton and Switch both are the subclasses of
CompoundButton class.
<?xml version="1.0" encoding="utf-8"?>
<ToggleButton
<[Link] android:id="@+id/toggleButton2"
xmlns:android="[Link] android:layout_width="wrap_content"
" xmlns:app="[Link]
xmlns:tools="[Link] android:layout_height="wrap_content"
android:layout_marginRight="60dp"
android:id="@+id/main" android:layout_marginTop="80dp"
android:layout_width="match_parent" android:text="ToggleButton"
android:textOff="Off"
android:layout_height="match_parent" android:textOn="On"
tools:context=".MainActivity"> app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ToggleButton
android:id="@+id/toggleButton" <Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginLeft="8dp" android:layout_marginBottom="144dp"
android:layout_marginLeft="148dp"
android:layout_marginTop="80dp" android:text="Submit"
android:text="ToggleButton" app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent" />
android:textOff="Off"
android:textOn="On" </[Link]>
app:layout_constraintEnd_toStartOf="@+id/toggleButton2"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
package [Link].myapplication_toggle; addListenerOnButtonClick();
import [Link]; }
import [Link]; public void addListenerOnButtonClick(){
import [Link];
//Getting the ToggleButton and Button instance from the layout xml
import [Link]; file
import [Link]; toggleButton1=(ToggleButton)findViewById([Link]);
import [Link]; toggleButton2=(ToggleButton)findViewById([Link].toggleButton2);
import [Link];
buttonSubmit=(Button)findViewById([Link]);
import [Link];
import [Link];
//Performing action on button click
import [Link];
[Link](new [Link](){
public class MainActivity extends AppCompatActivity {
private ToggleButton toggleButton1, toggleButton2;
private Button buttonSubmit; @Override

@Override public void onClick(View view) {


protected void onCreate(Bundle savedInstanceState) { StringBuilder result = new StringBuilder();
[Link](savedInstanceState); [Link]("ToggleButton1 :
[Link](this);
").append([Link]());

setContentView([Link].activity_main); [Link]("\nToggleButton2 :
").append([Link]());
[Link](findViewById([Link]), (v, insets) -> {
//Displaying the message in toast
Insets systemBars = [Link]([Link]());
[Link](getApplicationContext(),
[Link]([Link], [Link], [Link], [Link]);
[Link](),Toast.LENGTH_LONG).show();
return insets; });
}
Checkbox
• Android CheckBox is a type of two state button either checked or
unchecked.
• There can be a lot of usage of checkboxes. For example, it can be
used to know the hobby of the user, activate/deactivate the
specific action etc.
• Android CheckBox class is the subclass of CompoundButton
class.
• Android Checkbox class
• The [Link] class provides the facility of creating the
Checkboxess.
<?xml version="1.0" encoding="utf-8"?>
<[Link]
xmlns:android="[Link]
<CheckBox
xmlns:tools="[Link]
xmlns:app="[Link]
android:id="@+id/checkBox3"
android:id="@+id/main" android:layout_width="wrap_content"
android:layout_width="match_parent" android:layout_height="wrap_content"
android:layout_height="match_parent" android:layout_marginLeft="144dp"
tools:context=".MainActivity">
android:layout_marginTop="28dp"
<CheckBox android:text="Burger"
android:id="@+id/checkBox" app:layout_constraintStart_toStartOf="parent"
android:layout_width="wrap_content" app:layout_constraintTop_toBottomOf="@+id/checkBox2"
android:layout_height="wrap_content" />
android:layout_marginLeft="144dp"
android:layout_marginTop="68dp" <Button
android:text="Pizza" android:id="@+id/button"
app:layout_constraintStart_toStartOf="parent" android:layout_width="wrap_content"
app:layout_constraintTop_toTopOf="parent" /> android:layout_height="wrap_content"
<CheckBox android:layout_marginLeft="144dp"
android:id="@+id/checkBox2"
android:layout_marginTop="184dp"
android:layout_width="wrap_content"
android:text="Order"
android:layout_height="wrap_content"
app:layout_constraintStart_toStartOf="parent"
android:layout_marginLeft="144dp"
app:layout_constraintTop_toBottomOf="@+id/checkBox3"
android:layout_marginTop="28dp"
android:text="Coffee"
/>
app:layout_constraintStart_toStartOf="parent"
</[Link]>
app:layout_constraintTop_toBottomOf="@+id/checkBox" />
package [Link].myapplication_checkbox; public void addListenerOnButtonClick(){
import [Link]; //Getting instance of CheckBoxes and Button from the activty_main.xml file
import [Link]; pizza=(CheckBox)findViewById([Link]);
coffe=(CheckBox)findViewById([Link].checkBox2);
import [Link]; burger=(CheckBox)findViewById([Link].checkBox3);
import [Link]; buttonOrder=(Button)findViewById([Link]);
import [Link]; //Applying the Listener on the Button click
[Link](new [Link](){
import [Link];
@Override
import [Link]; public void onClick(View view) {
import [Link]; int totalamount=0;
import [Link]; StringBuilder result=new StringBuilder();
[Link]("Selected Items:");
import [Link]; if([Link]()){
public class MainActivity extends AppCompatActivity { [Link]("\nPizza 100Rs");
CheckBox pizza,coffe,burger; totalamount+=100;
}
Button buttonOrder; if([Link]()){
@Override [Link]("\nCoffe 50Rs");
protected void onCreate(Bundle savedInstanceState) { totalamount+=50;
}
[Link](savedInstanceState);
if([Link]()){
[Link](this); [Link]("\nBurger 120Rs");
setContentView([Link].activity_main); totalamount+=120;
}
[Link](findViewById([Link]) [Link]("\nTotal: "+totalamount+"Rs");
, (v, insets) -> { //Displaying the message on the toast
Insets systemBars = [Link](getApplicationContext(), [Link](),
[Link]([Link]()); Toast.LENGTH_LONG).show();
[Link]([Link], [Link], [Link], }
[Link]); });
return insets; }); addListenerOnButtonClick(); } }}
Android RadioButton

• RadioButton is a two states button which is either


checked or unchecked. If a single radio button is
unchecked, we can click it to make checked radio
button.
• Once a radio button is checked, it cannot be marked as
unchecked by user.
• RadioButton is generally used with RadioGroup.
RadioGroup contains several radio buttons, marking one
radio button as checked makes all other radio buttons
as unchecked.
<?xml version="1.0" encoding="utf-8"?>
<TextView
<LinearLayout android:id="@+id/textView2"
xmlns:android="[Link] android:layout_width="fill_parent"
xmlns:tools="[Link] android:layout_height="wrap_content"
android:layout_width="match_parent" android:layout_marginTop="30dp"
android:gravity="center_horizontal"
android:layout_height="match_parent" android:textSize="22dp"
android:orientation="vertical" android:text="Radio button inside RadioGroup" />
tools:context=".MainActivity"> <!-- Customized RadioButtons -->
<TextView <RadioGroup
android:id="@+id/textView1" android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:id="@+id/radioGroup">
android:layout_height="wrap_content" <RadioButton
android:layout_marginTop="30dp" android:id="@+id/radioMale"
android:gravity="center_horizontal" android:layout_width="fill_parent"
android:textSize="22dp" android:layout_height="wrap_content"
android:text=" Male"
android:text="Single Radio Buttons" /> android:layout_marginTop="10dp"
<!-- Default RadioButtons --> android:checked="false"
<RadioButton android:textSize="20dp" />
android:id="@+id/radioButton1" <RadioButton
android:layout_width="fill_parent" android:id="@+id/radioFemale"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal" android:text=" Female"
android:text="Radio Button 1" android:layout_marginTop="20dp"
android:layout_marginTop="20dp" android:checked="false"
android:textSize="20dp" /> android:textSize="20dp" />
</RadioGroup>
<RadioButton
android:id="@+id/radioButton2" <Button
android:layout_width="fill_parent" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:text="Radio Button 2" android:text="Show Selected"
android:id="@+id/button"
android:layout_marginTop="10dp" android:onClick="onclickbuttonMethod"
android:textSize="20dp" /> android:layout_gravity="center_horizontal" />
<View
android:layout_width="fill_parent"
android:layout_height="1dp" </LinearLayout>
android:layout_marginTop="20dp"
android:background="#B8B894" />
Button button;
package [Link].myapplication_radiobutton; RadioButton genderradioButton;
import [Link]; RadioGroup radioGroup;
@Override
import [Link]; protected void onCreate(Bundle savedInstanceState) {
import [Link]; [Link](savedInstanceState);
[Link](this);
import [Link]; setContentView([Link].activity_main);
import [Link]; radioGroup=(RadioGroup)findViewById([Link]);
}
import [Link];
public void onclickbuttonMethod(View v){
int selectedId = [Link]();
import [Link]; genderradioButton = (RadioButton) findViewById(selectedId);
import [Link]; if(selectedId==-1){
[Link]([Link],"Nothing selected",
import [Link]; Toast.LENGTH_SHORT).show();
import [Link]; }
else{
import [Link]; [Link]([Link],[Link](),
public class MainActivity extends AppCompatActivity { Toast.LENGTH_LONG).show();
}

}
}
Toast
• Andorid Toast can be used to display information for the
short period of time. A toast contains message to be
displayed quickly and disappears after sometime.
• The [Link] class is the subclass of
[Link] class.
• You can also create custom toast as well for example
toast displaying image. You can visit next page to see
the code for custom toast.
• Toast class
• Toast class is used to show notification for a particular interval of
time.
• After sometime it disappears.
• It doesn't block the user interaction.
<?xml version="1.0" encoding="utf-8"?>
<[Link]
xmlns:android="[Link]
xmlns:app="[Link]
xmlns:tools="[Link]
android:id="@+id/main"
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="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

</[Link]>
package [Link].myapplication_toast;
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
[Link](this);
setContentView([Link].activity_main);
[Link](findViewById([Link]), (v,
insets) -> {
Insets systemBars = [Link]([Link]());
[Link]([Link], [Link], [Link],
[Link]);
return insets;
});

[Link](getApplicationContext(),"Hello
Javatpoint",Toast.LENGTH_SHORT).show();
Android ProgressBar
• Android progress bar dialog box to display the status
of work being done e.g. downloading file, analyzing
status of work etc.
• In this example, we are displaying the progress dialog
for dummy file download operation.
activity_main.xml

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


• <[Link]
xmlns:android="[Link]
• xmlns:app="[Link]
• xmlns:tools="[Link]
• android:id="@+id/main"
• android:layout_width="match_parent"
• android:layout_height="match_parent"
• tools:context=".MainActivity">
• </[Link]>
@Override
[Link] protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
package [Link].myapplication_progress_bar;
[Link](this);
import [Link]; setContentView([Link].activity_main);
import [Link]; ProgressDialog progressBar = new ProgressDialog(this);
import [Link]; [Link](true);//you can cancel it by
pressing back button
import [Link];
[Link]("File downloading ...");
import [Link]; [Link](ProgressDialog.STYLE_HORIZONTA
import [Link]; L); [Link](0);//initially progress is 0
import [Link]; [Link](100);//sets the maximum value 100
[Link]();//displays the progress bar
import [Link];
}
import [Link]; }
import [Link];
public class MainActivity extends AppCompatActivity
{
Button btnStartProgress;
ProgressDialog progressBar;
ListView
• Android ListView is a view which contains the group of items and displays in a
scrollable list.
• ListView is implemented by importing [Link] class. ListView
is a default scrollable which does not use other scroll view.
• ListView uses Adapter classes which add the content from data source (such as
string array, array, database etc) to ListView.
• Adapter bridges data between an AdapterViews and other Views (ListView,
ScrollView etc).
<?xml version="1.0" encoding="utf-8"?>
<[Link]
xmlns:android="[Link]
xmlns:app="[Link]
xmlns:tools="[Link]
android:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="fill_parent"
tools:ignore="MissingConstraints" />
</[Link]>
package [Link].myapplication_listview; @Override
import [Link]; protected void onCreate(Bundle savedInstanceState) {
import [Link]; [Link](savedInstanceState);
import [Link]; [Link](this);
import [Link]; setContentView([Link].activity_main);
import [Link]; listView=(ListView)findViewById([Link]);
import [Link]; listItem = getResources().getStringArray([Link].array_technology);
import [Link]; final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
import [Link];
[Link].simple_list_item_1, [Link].text1, listItem);
[Link](adapter);
import [Link];
[Link](new [Link]() {
[Link];
@Override
import [Link];
public void onItemClick(AdapterView<?> adapterView, View view, int
import [Link]; position, long l) {
import [Link]; // TODO Auto-generated method stub
public class MainActivity extends AppCompatActivity String value=[Link](position);
{
[Link](getApplicationContext(),value,Toast.LENGTH_SHORT).show();
ListView listView;
}
TextView textView; });
String[] listItem; }
@SuppressLint("MissingInflatedId") }
Android ScrollView (Vertical)

• The [Link] class provides the


functionality of scroll view.
• ScrollView is used to scroll the child elements of palette
inside ScrollView.
• Android supports vertical scroll view as default scroll
view.
• Vertical ScrollView scrolls elements vertically.
• Android uses HorizontalScrollView for horizontal
ScrollView.
<?xml version="1.0" encoding="utf-8"?> <LinearLayout <LinearLayout
<[Link] android:layout_width="fill_parent"
xmlns:android="[Link] android:layout_height="fill_parent"
xmlns:app="[Link] android:orientation="vertical" >
xmlns:tools="[Link] <Button
android:id="@+id/main" android:layout_width="fill_parent"
android:layout_width="match_parent" android:layout_height="wrap_content"
android:layout_height="match_parent" android:text="Button 1" />
tools:context=".MainActivity"> <Button
<TextView android:layout_width="fill_parent"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_height="wrap_content" android:text="Button 2" />
android:textAppearance="?android:attr/textAppearanceMedium" <Button
android:text="Vertical ScrollView example" android:layout_width="fill_parent"
android:id="@+id/textView" android:layout_height="wrap_content"
android:layout_gravity="center_horizontal" android:text="Button 3" />
android:layout_centerHorizontal="true" <Button
android:layout_alignParentTop="true" android:layout_width="fill_parent"
tools:ignore="MissingConstraints" /> android:layout_height="wrap_content"
<ScrollView android:layout_marginTop="30dp" android:text="Button 4" />
android:layout_width="fill_parent" <Button
android:layout_height="wrap_content" android:layout_width="fill_parent"
android:id="@+id/scrollView" android:layout_height="wrap_content"
tools:ignore="MissingConstraints"> android:text="Button 5" />
<Button
android:layout_width="fill_parent" <Button
android:layout_height="wrap_content" android:layout_width="fill_parent"
android:text="Button 6" /> android:layout_height="wrap_content"
<Button android:text="Button 11" />
android:layout_width="fill_parent" <Button
android:layout_height="wrap_content" android:layout_width="fill_parent"
android:text="Button 7" />
android:layout_height="wrap_content"
android:text="Button 12" />
<Button
<Button
android:layout_width="fill_parent"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_height="wrap_content"
android:text="Button 8" /> android:text="Button 13" />
<Button <Button
android:layout_width="fill_parent" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:text="Button 9" /> android:text="Button 14" />
<Button <Button android:layout_width="fill_parent"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:layout_height="wrap_content" android:text="Button 15" />
android:text="Button 10" />
<Button android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Button 16" />
[Link]
<Button
android:layout_width="fill_parent" package [Link].myapplication_scrollview;
android:layout_height="wrap_content"
android:text="Button 17" /> import [Link];
<Button
android:layout_width="fill_parent" import [Link];
android:layout_height="wrap_content" import [Link];
android:text="Button 18" /> import [Link];
<Button import [Link];
android:layout_width="fill_parent" import [Link];
android:layout_height="wrap_content"
android:text="Button 19" /> public class MainActivity extends AppCompatActivity {
<Button
android:layout_width="fill_parent" @Override
android:layout_height="wrap_content" protected void onCreate(Bundle savedInstanceState) {
android:text="Button 20" /> [Link](savedInstanceState);
</LinearLayout> [Link](this);
</ScrollView> setContentView([Link].activity_main);
</[Link]>
}
}
Android Custom Toast Example

• You are able to create custom toast in android. So, you


can display some images like congratulations or loss on
the toast. It means you are able to customize the toast
now.

activity_main.xml [Link]
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?> <LinearLayout
<[Link] xmlns:android="[Link]
xmlns:android="[Link] xmlns:tools="[Link]
xmlns:app="[Link] android:layout_width="match_parent"
xmlns:tools="[Link] android:layout_height="match_parent"
android:id="@+id/custom_toast_layout"
android:id="@+id/main"
android:orientation="vertical"
android:layout_width="match_parent" android:background="#F14E23">
android:layout_height="match_parent" <ImageView
tools:context=".MainActivity"> android:id="@+id/custom_toast_image"
<TextView android:layout_width="wrap_content"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_height="wrap_content" android:contentDescription="Hello world"
android:text="Hello World!" android:src="@drawable/download"
tools:ignore="HardcodedText" />
app:layout_constraintBottom_toBottomOf="parent"
<TextView
app:layout_constraintEnd_toEndOf="parent" android:id="@+id/custom_toast_message"
app:layout_constraintStart_toStartOf="parent" android:layout_width="wrap_content"
app:layout_constraintTop_toTopOf="parent" /> android:layout_height="wrap_content"
</[Link]> android:contentDescription="To"
android:text="JavaTpoint custom Toast"
tools:ignore="HardcodedText" />
</LinearLayout>
package [Link].myapplication_custom_toast; @Override
import [Link];
protected void onCreate(Bundle savedInstanceState) {
import [Link];
import [Link]; [Link](savedInstanceState);
import [Link]; [Link](this);
import [Link]; setContentView([Link].activity_main);
import [Link];
//Creating the LayoutInflater instance
import [Link];
import [Link]; LayoutInflater li = getLayoutInflater();
import [Link]; //Getting the View object as defined in the [Link] file
import [Link]; @SuppressLint({"MissingInflatedId", "LocalSuppress"}) View layout =
import [Link]; [Link]([Link],(ViewGroup)
import [Link]; findViewById([Link].custom_toast_layout));
public class MainActivity extends AppCompatActivity { //Creating the Toast object
Toast toast = new Toast(getApplicationContext());
[Link](Toast.LENGTH_LONG);
[Link](Gravity.CENTER_VERTICAL, 0, 0);
[Link](layout);//setting the view of custom toast layout
[Link]();
}
}
DatePicker
• Android DatePicker is a widget to select date.
• It allows you to select date by day, month and year. Like DatePicker,
android also provides TimePicker to select time.
• The [Link] is the subclass of FrameLayout class.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout <Button
xmlns:android="[Link] android:id="@+id/button1"
xmlns:app="[Link] android:layout_width="wrap_content"
xmlns:tools="[Link] android:layout_height="wrap_content"
android:layout_width="match_parent" android:layout_alignParentEnd="true"
android:layout_height="match_parent" android:layout_alignParentBottom="true"
tools:context=".MainActivity"> android:layout_marginEnd="140dp"
android:layout_marginBottom="129dp"
<TextView
android:text="Change Date"
android:id="@+id/textView1"
tools:ignore="HardcodedText" />
android:layout_width="wrap_content" <DatePicker
android:layout_height="wrap_content" android:id="@+id/datePicker"
android:layout_above="@+id/button1" android:layout_width="wrap_content"
android:layout_alignParentLeft="true" android:layout_height="wrap_content"
android:layout_alignParentStart="true" android:layout_above="@+id/textView1"
android:layout_marginBottom="102dp" android:layout_centerHorizontal="true"
android:layout_marginBottom="36dp" />
android:layout_marginLeft="30dp"
</RelativeLayout>
android:layout_marginStart="30dp"
android:text=""
tools:ignore="RtlHardcoded" />
package [Link].myapplication_date_picker;
import [Link]; @Override
import [Link];
public void onClick(View view) {
import [Link];
[Link]("Change Date: "+getCurrentDate());
import [Link];
} });
import [Link];
}
import [Link];
public String getCurrentDate(){
import [Link];
StringBuilder builder=new StringBuilder();;
import [Link];
[Link](([Link]() + 1)+"/");//month is 0 based
import [Link];
[Link]([Link]()+"/");
import [Link];
[Link]([Link]());
public class MainActivity extends AppCompatActivity {
return [Link]();
DatePicker picker;
}
Button displayDate;
}
TextView textview1;
@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
[Link](this);
setContentView([Link].activity_main);
textview1=(TextView)findViewById([Link].textView1);
picker=(DatePicker)findViewById([Link]);
displayDate=(Button)findViewById([Link].button1);
[Link]("Current Date: "+getCurrentDate());
[Link](new [Link](){
Android TimePicker
• Android TimePicker widget is used to select date.
• It allows you to select time by hour and minute.
• You cannot select time by seconds.
• The [Link] is the subclass of FrameLayout
class.
<?xml version="1.0" encoding="utf-8"?> <Button
<RelativeLayout android:id="@+id/button1"
xmlns:android="[Link] android:layout_width="wrap_content"
xmlns:app="[Link] android:layout_height="wrap_content"
xmlns:tools="[Link] android:layout_alignParentEnd="true"
android:layout_width="match_parent" android:layout_alignParentBottom="true"
android:layout_height="match_parent" android:layout_marginEnd="133dp"
tools:context=".MainActivity"> android:layout_marginBottom="88dp"
<TextView android:text="Change Time" />
android:id="@+id/textView1"
android:layout_width="wrap_content" <TimePicker
android:layout_height="wrap_content" android:id="@+id/timePicker"
android:layout_above="@+id/button1" android:layout_width="wrap_content"
android:layout_alignParentLeft="true" android:layout_height="wrap_content"
android:layout_alignParentStart="true" android:layout_above="@+id/textView1"
android:layout_marginBottom="102dp" android:layout_centerHorizontal="true"
android:layout_marginLeft="30dp" android:layout_marginBottom="36dp" />
android:layout_marginStart="30dp" </RelativeLayout>
android:text="" />
@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
package [Link].myapplication_time_picker; [Link](this);
import [Link]; setContentView([Link].activity_main);
import [Link]; textview1=(TextView)findViewById([Link].textView1);
import [Link]; timepicker=(TimePicker)findViewById([Link]);
import [Link]; //Uncomment the below line of code for 24 hour view
import [Link]; timepicker.setIs24HourView(true);
import [Link]; changetime=(Button)findViewById([Link].button1);
import [Link]; [Link](getCurrentTime());
import [Link]; [Link](new
import [Link]; [Link](){
import [Link]; @Override
public class MainActivity extends AppCompatActivity { public void onClick(View view) {
TextView textview1; [Link](getCurrentTime());
TimePicker timepicker; } }); }
Button changetime; public String getCurrentTime(){
String currentTime="Current Time:
"+[Link]()+":"+[Link]
e();
return currentTime;
}
}
Create a login form with all necessary validations (On success
or unsuccessful login, display appropriate toast Message )
<?xml version="1.0" encoding="utf-8"?> <TextView
<RelativeLayout <EditText android:layout_width="wrap_content"
xmlns:android="[Link] android:layout_width = "wrap_content" android:layout_height="wrap_content"
/apk/res/android" android:layout_height = "48dp" android:text="Attempts Left:"
xmlns:app="[Link] android:id = "@+id/editText" android:id="@+id/textView2"
xmlns:tools="[Link] android:hint = "Enter Name" android:layout_below="@+id/editText2"
android:id="@+id/main" android:focusable = "true" android:layout_alignParentLeft="true"
android:layout_width="match_parent" android:textColorHighlight = "#ff7eff15" android:layout_alignParentStart="true"
android:layout_height="match_parent" android:textColorHint = "#ffff25e6" android:textSize="25dp" />
tools:context=".MainActivity"> android:layout_marginTop = "46dp" <TextView
android:layout_below = "@+id/imageView" android:layout_width="wrap_content"
<TextView android:text = "Login" android:layout_alignParentLeft = "true" android:layout_height="wrap_content"
android:layout_width="wrap_content" android:layout_alignParentStart = "true" android:text="New Text"
android:layout_height = "wrap_content" android:layout_alignParentRight = "true" android:id="@+id/textView3"
android:id = "@+id/textview" android:layout_alignParentEnd = "true" android:layout_alignTop="@+id/textView2"
android:textSize = "35dp" tools:ignore="Autofill,HardcodedText,TextFields" /> android:layout_alignParentRight="true"
android:layout_alignParentTop = "true" android:layout_alignParentEnd="true"
android:layout_centerHorizontal = "true" <ImageView android:layout_alignBottom="@+id/textView2"
tools:ignore="HardcodedText,SpUsage" /> android:layout_width="wrap_content" android:layout_toEndOf="@+id/textview"
android:layout_height="wrap_content" android:textSize="25dp"
<TextView android:id="@+id/imageView" android:layout_toRightOf="@+id/textview"
android:layout_width = "wrap_content" android:src="@drawable/download" tools:ignore="RtlHardcoded" />
android:layout_height = "wrap_content" android:layout_below="@+id/textView" <Button
android:text = "Credentials" android:layout_centerHorizontal="true" /> android:layout_width="wrap_content"
android:id = "@+id/textView" android:layout_height="wrap_content"
android:layout_below = "@+id/textview" <EditText android:text="login"
android:layout_centerHorizontal = "true" android:layout_width="wrap_content" android:id="@+id/button"
android:textColor = "#ff7aff24" android:layout_height="48dp" android:layout_alignParentBottom="true"
android:textSize = "35dp" android:inputType="textPassword" android:layout_toLeftOf="@+id/textview"
tools:ignore="HardcodedText,SpUsage" /> android:ems="10" android:layout_toStartOf="@+id/textview" />
android:id="@+id/editText2" <Button
android:layout_below="@+id/editText" android:id="@+id/button2"
android:layout_alignParentLeft="true" android:layout_width="wrap_content"
android:layout_alignParentStart="true" android:layout_height="wrap_content"
android:layout_alignRight="@+id/editText" android:layout_alignParentBottom="true"
android:layout_alignEnd="@+id/editText" android:layout_toEndOf="@+id/textview"
android:textColorHint="#ffff299f" android:layout_toRightOf="@+id/textview"
android:hint="Password" android:text="Cancel"
tools:ignore="Autofill" /> tools:layout_editor_absoluteX="160dp"
tools:layout_editor_absoluteY="258dp" />
</RelativeLayout>
package [Link].myapplication_loginform;
[Link](new [Link]() {
import [Link];
@SuppressLint("SetTextI18n")
import [Link];
@Override
import [Link];
public void onClick(View v) {
import [Link];
if([Link]().toString().equals("admin") &&
import [Link];
[Link]().toString().equals("admin")) {
import [Link];
[Link](getApplicationContext(),
import [Link];
"Valid Credentials ....
import [Link];
Redirecting...",Toast.LENGTH_SHORT).show();
import [Link];
}else{
import [Link];
[Link](getApplicationContext(), "Wrong
import [Link];
Credentials",Toast.LENGTH_SHORT).show();
import [Link];
[Link]([Link]);
import [Link];
[Link]([Link]);
public class MainActivity extends AppCompatActivity {
counter--;
Button b1,b2;
[Link]([Link](counter));
EditText ed1,ed2;
TextView tx1;
if (counter == 0) {
int counter = 3;
[Link](false);
@Override
}
protected void onCreate(Bundle savedInstanceState) {
}
[Link](savedInstanceState);
}
[Link](this);
});
setContentView([Link].activity_main);
[Link](new [Link]() {
b1 = (Button)findViewById([Link]);
@Override
ed1 = (EditText)findViewById([Link]);
public void onClick(View v) {
ed2 = (EditText)findViewById([Link].editText2);
finish();
b2 = (Button)findViewById([Link].button2);
}
tx1 = (TextView)findViewById([Link].textView3);
});
[Link]([Link]);
}
}

You might also like