Unit 4
Unit 4
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="http://schemas.android.com/apk/res/android" android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_alignParentStart="true"
xmlns:tools="http://schemas.android.com/tools" 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 com.example.myapplication_edittext; @Override
protected void onCreate(Bundle savedInstanceState) {
import android.os.Bundle; super.onCreate(savedInstanceState);
EdgeToEdge.enable(this);
import android.view.View;
setContentView(R.layout.activity_main);
import android.widget.Button;
import android.widget.EditText; addListenerOnButton();
import android.widget.Toast;
}
public void addListenerOnButton() {
import androidx.activity.EdgeToEdge; edittext1 = findViewById(R.id.editText1);
import androidx.appcompat.app.AppCompatActivity; edittext2 = findViewById(R.id.editText2);
import androidx.core.graphics.Insets; buttonDisplay = findViewById(R.id.button);
import androidx.core.view.ViewCompat;
buttonDisplay.setOnClickListener(new
import androidx.core.view.WindowInsetsCompat; View.OnClickListener() {
@Override
public class MainActivity extends AppCompatActivity { public void onClick(View view) {
String value1=edittext1.getText().toString();
private EditText edittext1, edittext2;
String value2=edittext2.getText().toString();
private Button buttonDisplay;
Toast.makeText(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
<androidx.constraintlayout.widget.ConstraintLayout android:id="@+id/autoCompleteTextView"
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="200dp"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_marginStart="92dp"
xmlns:tools="http://schemas.android.com/tools" 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"
</androidx.constraintlayout.widget.ConstraintLayout
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 com.example.myapplication_autocomplete_textview;
@Override
void onCreate(Bundle savedInstanceState) {
import android.graphics.Color; super.onCreate(savedInstanceState);
EdgeToEdge.enable(this);
import android.os.Bundle; setContentView(R.layout.activity_main);
import android.widget.ArrayAdapter; //Creating the instance of ArrayAdapter containing list of
import android.widget.AutoCompleteTextView; language names
ArrayAdapter<String> adapter = new ArrayAdapter<String>
(this,android.R.layout.select_dialog_item,language);
import androidx.activity.EdgeToEdge; //Getting the instance of AutoCompleteTextView
AutoCompleteTextView actv =
import androidx.appcompat.app.AppCompatActivity;
(AutoCompleteTextView)findViewById(R.id.autoCompleteTex
import androidx.core.graphics.Insets; iew);
import androidx.core.view.ViewCompat; actv.setThreshold(1);//will start working from first
character
import androidx.core.view.WindowInsetsCompat;
actv.setAdapter(adapter);//setting the adapter data into
the AutoCompleteTextView
public class MainActivity extends AppCompatActivity { actv.setTextColor(Color.RED);
}
String[] language }
={"C","C++","Java",".NET","iPhone","Android","ASP.NET","PHP"};
Button
• Android Button represents a push-button.
• The android.widget.Button 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="http://schemas.android.com/apk/res/android" android:layout_below="@+id/editText1"
xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_centerHorizontal="true"
xmlns:tools="http://schemas.android.com/tools" 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 com.example.myapplication_button;
public void addListenerOnButton() {
import android.os.Bundle;
edittext1 = (EditText) findViewById(R.id.editText1);
import android.view.View;
edittext2 = (EditText) findViewById(R.id.editText2);
import android.widget.Button;
buttonSum = (Button) findViewById(R.id.button);
import android.widget.EditText;
import android.widget.Toast;
buttonSum.setOnClickListener(new View.OnClickListener()
import androidx.activity.EdgeToEdge;
{
import androidx.appcompat.app.AppCompatActivity;
@Override
import androidx.core.graphics.Insets;
public void onClick(View view) {
import androidx.core.view.ViewCompat;
String value1=edittext1.getText().toString();
import androidx.core.view.WindowInsetsCompat;
String value2=edittext2.getText().toString();
int a=Integer.parseInt(value1);
public class MainActivity extends AppCompatActivity {
int b=Integer.parseInt(value2);
private EditText edittext1, edittext2;
int sum=a+b;
private Button buttonSum;
@Override
Toast.makeText(getApplicationContext(),String.valueOf(sum),
protected void onCreate(Bundle savedInstanceState) {
Toast.LENGTH_LONG).show();
super.onCreate(savedInstanceState);
}
setContentView(R.layout.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"?>
<androidx.constraintlayout.widget.ConstraintLayout
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: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" </androidx.constraintlayout.widget.ConstraintLayout>
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:ignore="HardcodedText,MissingConstraints,SpUsage" />
package com.example.myapplication_imagebutton;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
import android.widget.Toast;
import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
public class MainActivity extends AppCompatActivity {
ImageButton imgButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EdgeToEdge.enable(this);
setContentView(R.layout.activity_main);
ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
return insets; });
imgButton =(ImageButton)findViewById(R.id.imageButton);
imgButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(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
<androidx.constraintlayout.widget.ConstraintLayout android:id="@+id/toggleButton2"
xmlns:android="http://schemas.android.com/apk/res/android android:layout_width="wrap_content"
" xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" 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" </androidx.constraintlayout.widget.ConstraintLayout>
app:layout_constraintEnd_toStartOf="@+id/toggleButton2"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
package com.example.myapplication_toggle; addListenerOnButtonClick();
import android.os.Bundle; }
import android.view.View; public void addListenerOnButtonClick(){
import android.widget.Button;
//Getting the ToggleButton and Button instance from the layout xml
import android.widget.Toast; file
import android.widget.ToggleButton; toggleButton1=(ToggleButton)findViewById(R.id.toggleButton);
import androidx.activity.EdgeToEdge; toggleButton2=(ToggleButton)findViewById(R.id.toggleButton2);
import androidx.appcompat.app.AppCompatActivity;
buttonSubmit=(Button)findViewById(R.id.button);
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
//Performing action on button click
import androidx.core.view.WindowInsetsCompat;
buttonSubmit.setOnClickListener(new View.OnClickListener(){
public class MainActivity extends AppCompatActivity {
private ToggleButton toggleButton1, toggleButton2;
private Button buttonSubmit; @Override
setContentView(R.layout.activity_main); result.append("\nToggleButton2 :
").append(toggleButton2.getText());
ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
//Displaying the message in toast
Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
Toast.makeText(getApplicationContext(),
v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
result.toString(),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 android.widget.CheckBox class provides the facility of creating the
Checkboxess.
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
<CheckBox
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
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"
</androidx.constraintlayout.widget.ConstraintLayout>
app:layout_constraintTop_toBottomOf="@+id/checkBox" />
package com.example.myapplication_checkbox; public void addListenerOnButtonClick(){
import android.os.Bundle; //Getting instance of CheckBoxes and Button from the activty_main.xml file
import android.view.View; pizza=(CheckBox)findViewById(R.id.checkBox);
coffe=(CheckBox)findViewById(R.id.checkBox2);
import android.widget.Button; burger=(CheckBox)findViewById(R.id.checkBox3);
import android.widget.CheckBox; buttonOrder=(Button)findViewById(R.id.button);
import android.widget.Toast; //Applying the Listener on the Button click
buttonOrder.setOnClickListener(new View.OnClickListener(){
import androidx.activity.EdgeToEdge;
@Override
import androidx.appcompat.app.AppCompatActivity; public void onClick(View view) {
import androidx.core.graphics.Insets; int totalamount=0;
import androidx.core.view.ViewCompat; StringBuilder result=new StringBuilder();
result.append("Selected Items:");
import androidx.core.view.WindowInsetsCompat; if(pizza.isChecked()){
public class MainActivity extends AppCompatActivity { result.append("\nPizza 100Rs");
CheckBox pizza,coffe,burger; totalamount+=100;
}
Button buttonOrder; if(coffe.isChecked()){
@Override result.append("\nCoffe 50Rs");
protected void onCreate(Bundle savedInstanceState) { totalamount+=50;
}
super.onCreate(savedInstanceState);
if(burger.isChecked()){
EdgeToEdge.enable(this); result.append("\nBurger 120Rs");
setContentView(R.layout.activity_main); totalamount+=120;
}
ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main) result.append("\nTotal: "+totalamount+"Rs");
, (v, insets) -> { //Displaying the message on the toast
Insets systemBars = Toast.makeText(getApplicationContext(), result.toString(),
insets.getInsets(WindowInsetsCompat.Type.systemBars()); Toast.LENGTH_LONG).show();
v.setPadding(systemBars.left, systemBars.top, systemBars.right, }
systemBars.bottom); });
return insets; }); addListenerOnButtonClick(); } }}
Android RadioButton
}
}
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 android.widget.Toast class is the subclass of
java.lang.Object 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"?>
<androidx.constraintlayout.widget.ConstraintLayout
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: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" />
</androidx.constraintlayout.widget.ConstraintLayout>
package com.example.myapplication_toast;
import android.os.Bundle;
import android.widget.Toast;
import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EdgeToEdge.enable(this);
setContentView(R.layout.activity_main);
ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v,
insets) -> {
Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
v.setPadding(systemBars.left, systemBars.top, systemBars.right,
systemBars.bottom);
return insets;
});
Toast.makeText(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