Android Component
Android Component
A text view allows the user to type text into your app.
It can be either single line or multi-line. Touching a text field places the
cursor and automatically displays the keyboard.
You can add a text view to you layout with the EditText object.
Text fields can have different input types, such as number, date,
password, or email address.
The type determines what kind of characters are allowed inside the field,
and may prompt the virtual keyboard to optimize its layout for frequently
used characters.
You can specify the type of keyboard you want for your EditText object
with the android:inputType attribute.
For example, if you want the user to input an email address, you should
use the textEmailAddress input type:
<EditText android:id="@+id/email_address"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="@string/email_hint"
android:inputType="textEmailAddress" />
There are several different input types available for different situations.
For example, here's how you can collect a postal address, capitalize each
word, and disable text suggestions:
<EditText
android:id="@+id/postal_address"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="@string/postal_address_hint"android:inputType="textPost
alAddress|textCapWords| textNoSuggestions" />
Event Handling
Code Sample:
<EditText
android:layout_width=“match_parent”
android:layout_height=“wrap_parent”
android:id=“@id/editText”
android:inputType=“text”/>
Step-2:
Step-3
editText.addTextChangedListener(this);
app:layout_behavior="@string/appbar_scrolling_view_
behavior"
tools:context=".MainActivity"
tools:showIn="@layout/activity_main">
<EditText
android:id="@+id/editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:text="Name" />
</android.widget.RelativeLayout>
2.java file
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.*;
import android.widget.*;
//implement TextWatcher Interface
editText.addTextChangedListener(this);
}
//implement methods given in TextWatcher Interface
@Override
public void onTextChanged(CharSequence s, int
start, int before, int count)
{
v1.setText(editText.getText());
}
@Override
public void afterTextChanged(Editable s)
{
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button_text"
/>
Image Button
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/button_icon" />
For example:
button.setOnClickListener(new View.OnClickListener() {
}});
3.Write android application take input and find sum of it
,display on the screen.
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.
File: activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/an
droid"
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="example.javatpoint.com.sumoftwonumber.MainActivit
y">
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="61dp"
android:ems="10"
android:inputType="number"
tools:layout_editor_absoluteX="84dp"
tools:layout_editor_absoluteY="53dp" />
<EditText
android:id="@+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/editText1"
android:layout_centerHorizontal="true"
android:layout_marginTop="32dp"
android:ems="10"
android:inputType="number"
tools:layout_editor_absoluteX="84dp"
tools:layout_editor_absoluteY="127dp" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/editText2"
android:layout_centerHorizontal="true"
android:layout_marginTop="109dp"
android:text="ADD"
tools:layout_editor_absoluteX="148dp"
tools:layout_editor_absoluteY="266dp" />
</RelativeLayout>
ile: MainActivity.java
1.import android.support.v7.app.AppCompatActivity;
2.import android.os.Bundle;
3.import android.view.*;
4.import android.widget.*;
5.
6. public class MainActivity extends AppCompatActivity {
7. private EditText edittext1, edittext2;
8. private Button buttonSum;
9.
10. @Override
11. protected void onCreate(Bundle savedInstanceState) {
12. super.onCreate(savedInstanceState);
13. setContentView(R.layout.activity_main);
14.
15. addListenerOnButton();
16. }
17.
18. public void addListenerOnButton() {
19. edittext1 = (EditText) findViewById(R.id.editText1);
20. edittext2 = (EditText) findViewById(R.id.editText2);
21. buttonSum = (Button) findViewById(R.id.button);
22.
23. buttonSum.setOnClickListener(new View.OnClickListener() {
24. @Override
25. public void onClick(View view) {
26. String value1=edittext1.getText().toString();
27. String value2=edittext2.getText().toString();
28. int a=Integer.parseInt(value1);
29. int b=Integer.parseInt(value2);
30. int sum=a+b;
31. Toast.makeText(getApplicationContext(),String.valueOf(sum), Toast.
LENGTH_LONG).show();
32. }
33. });
34. }
35.}
Steps:
<CheckBox
android:id="@+id/checkBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Tea" />
c1=(CheckBox)findViewById(R.id.checkBox);
c1.setOnCheckedChangeListener(this);
CheckBox c1=null;
CheckBox c2=null;
c1=(CheckBox)findViewById(R.id.checkBox);
c2=(CheckBox)findViewById(R.id.checkBox2);
c1.setOnCheckedChangeListener(this);
c2.setOnCheckedChangeListener(this);
}
public void onCheckedChanged(CompoundButton
buttonView, boolean isChecked)
{
if(buttonView.getId()==R.id.checkBox)
{
if(isChecked)
{
Toast.makeText(this,"1st button is
clicked",Toast.LENGTH_SHORT).show();
}
}
if(buttonView.getId()==R.id.checkBox2)
{
if(isChecked)
{
Toast.makeText(this,"2ND button is
clicked",Toast.LENGTH_SHORT).show();
}
}
}
}
Sample.xml
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/a
ndroid"
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"
app:layout_behavior="@string/appbar_scrolling_view_
behavior"
tools:context=".Sample"
tools:showIn="@layout/activity_sample">
<CheckBox
android:id="@+id/checkBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentBottom="true"
android:layout_marginStart="49dp"
android:layout_marginBottom="80dp"
android:text="Tea" />
<CheckBox
android:id="@+id/checkBox2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/checkBox"
android:layout_alignParentEnd="true"
android:layout_marginEnd="87dp"
android:text="Coffee" />
</RelativeLayout>
5.Write android application to give two option to user
"Tea" and " coffee". Display selected Item as Toast
message. user can select multiple item.
Drag the two checkboxes and one button for the layout. Now the
activity_main.xml file will look like this:
File: activity_main.xml
1. <?xml version="1.0" encoding="utf-8"?>
2. <android.support.constraint.ConstraintLayout xmlns:android="http://schem
as.android.com/apk/res/android"
3. xmlns:app="http://schemas.android.com/apk/res-auto"
4. xmlns:tools="http://schemas.android.com/tools"
5. android:layout_width="match_parent"
6. android:layout_height="match_parent"
7. tools:context="example.javatpoint.com.checkbox.MainActivity">
8.
9.
10. <CheckBox
11. android:id="@+id/checkBox"
12. android:layout_width="wrap_content"
13. android:layout_height="wrap_content"
14. android:layout_marginLeft="144dp"
15. android:layout_marginTop="68dp"
16. android:text="Pizza"
17. app:layout_constraintStart_toStartOf="parent"
18. app:layout_constraintTop_toTopOf="parent" />
19.
20. <CheckBox
21. android:id="@+id/checkBox2"
22. android:layout_width="wrap_content"
23. android:layout_height="wrap_content"
24. android:layout_marginLeft="144dp"
25. android:layout_marginTop="28dp"
26. android:text="Coffee"
27. app:layout_constraintStart_toStartOf="parent"
28. app:layout_constraintTop_toBottomOf="@+id/checkBox" />
29.
30.
31. <Button
32. android:id="@+id/button"
33. android:layout_width="wrap_content"
34. android:layout_height="wrap_content"
35. android:layout_marginLeft="144dp"
36. android:layout_marginTop="184dp"
37. android:text="Order"
38. app:layout_constraintStart_toStartOf="parent"
39. app:layout_constraintTop_toBottomOf="@+id/checkBox3" />
40.
41.</android.support.constraint.ConstraintLayout>
File: MainActivity.java
1. import android.support.v7.app.AppCompatActivity;
2. import android.os.Bundle;
3. import android.view.*;
4. import android.widget.*;
5.
6.
7.
8. public class MainActivity extends AppCompatActivity {
9. CheckBox Tea,Coffe;
10. Button buttonOrder;
11. @Override
12. protected void onCreate(Bundle savedInstanceState) {
13. super.onCreate(savedInstanceState);
14. setContentView(R.layout.activity_main);
15. addListenerOnButtonClick();
16. }
17. public void addListenerOnButtonClick(){
18. //Getting instance of CheckBoxes and Button from the activty_main.xml fil
e
19. Tea=(CheckBox)findViewById(R.id.checkBox);
20. Coffe=(CheckBox)findViewById(R.id.checkBox2);
21.
22. buttonOrder=(Button)findViewById(R.id.button);
23.
24. //Applying the Listener on the Button click
25. buttonOrder.setOnClickListener(new View.OnClickListener(){
26.
27. @Override
28. public void onClick(View v) {
29.
30. StringBuilder result=new StringBuilder();
31. result.append("Selected Items:");
32. if(Tea.isChecked()){
33. result.append("\n tea");
34. }
35.}
36. if(Coffe.isChecked()){
37. result.append("\nCoffe ");
38.
39. }
40.
41.
42. //Displaying the message on the toast
43. Toast.makeText(getApplicationContext(), result.toString(), Toast.LE
NGTH_LONG).show();
44. }
45.
46. });
47. }
48.}
android:checkedButton="@+id/radioButton">
<RadioButton
android:id="@+id/radioButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Tea" />
<RadioButton
android:id="@+id/radioButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Coffee" />
Steps:
1. Create RadioButton in XML File as shown in syntax
2.<RadioGroup
android:id="@+id/rg"
android:layout_width="159dp"
android:layout_height="113dp"
<RadioButton
android:id="@+id/radioButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Tea" />
<RadioButton
android:id="@+id/radioButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Coffee" />
</RadioGroup>
Example Code:
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.*;
import android.view.*;
RadioGroup rg=null;
rg=(RadioGroup)findViewById(R.id.rg);
switch(checkedId)
{
case R.id.radioButton:
Toast.makeText(this,"1st button is
clicked",Toast.LENGTH_SHORT).show();
break;
case R.id.radioButton2:
Toast.makeText(this,"2nd button is
clicked",Toast.LENGTH_SHORT).show();
break;
}}}