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="[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
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
}
}
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