0% found this document useful (0 votes)
18 views

Android Component

The document explains the TextView component in Android, which allows displaying and editing text. It can be single or multi-line, and different input types can be specified. Event handling for text changes and focus changes is also described. An example shows how to use a TextView and add a text change listener.

Uploaded by

Dhruvil Bhuva
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)
18 views

Android Component

The document explains the TextView component in Android, which allows displaying and editing text. It can be single or multi-line, and different input types can be specified. Event handling for text changes and focus changes is also described. An example shows how to use a TextView and add a text change listener.

Uploaded by

Dhruvil Bhuva
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
You are on page 1/ 19

1.Explain TextView Component in android.

 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.

 In addition to typing, text fields allow for a variety of other activities,


such as text selection (cut, copy, paste) and data look-up via auto-
completion.

 You can add a text view to you layout with the EditText object.

 You should usually do so in your XML layout with a <EditText>


element

 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" />

 Specifying the Keyboard Type:

 There are several different input types available for different situations.

 Here are some of the more common values for android:inputType:


 – "text“: Normal text keyboard.

 - "textEmailAddress“: Normal text

 keyboard with the @ character.

 "textUri“: Normal text keyboard with the / character.

 "number“: Basic number keypad.

 "phone" Phone-style keypad

 The android:inputType also allows you to specify certain keyboard


behaviors, such as whether to capitalize all new words or use features like
auto-complete and spelling suggestions.

 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" />

 textCapSentences“: Normal text keyboard that capitalizes the first letter


for each new sentence.

 "textCapWords“: Normal text keyboard that capitalizes every word.


Good for titles or person names.

 “textAutoCorrect" :Normal text keyboard that corrects commonly


misspelled words.

 "textPassword“: Normal text keyboard, but the characters entered turn


into dots.

 "textMultiLine" Normal text keyboard that allow users to input long


strings of text that include line breaks (carriage returns).


 Event Handling

 (a)TextChange:Called when text is changed in EditText.

 (b)OnFocusChange:Called when component lose its focus.

Implement Event using TextView

 Step-1.Create EditText in .XML file in res/layout folder.

 Code Sample:

 <EditText

 android:layout_width=“match_parent”

 android:layout_height=“wrap_parent”

 android:id=“@id/editText”

 android:inputType=“text”/>

 Step-2:

 implement TextWatcher in java file

 public class MainActivity extends


AppCompatActivity implements TextWatcher

//retrieve layout object


editText = (EditText) findViewById(R.id.editText);

 Step-3

 Register for event using addTextChangedListener()

 editText.addTextChangedListener(this);

 Implements different method of the TextWatcher

 Public void afterTextChanged()

 Public void beforeTextChanged()

 Public void onTextChanged()


 Example Code:

 public void beforeTextChanged(CharSequence s,


int start, int count, int after)
{

public void onTextChanged(CharSequence s, int


start, int before, int count)
{
v1.setText(editText.getText());
}

public void afterTextChanged(Editable s)


{

Complete Example with Code:


1.Layout XML File

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


<android.widget.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=".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

public class MainActivity extends AppCompatActivity


implements TextWatcher
{
EditText editText=null;

protected void onCreate(Bundle b)


{
super.onCreate(b);
setContentView(R.layout.activity_main);
editText=(EditText)findViewById(R.id.editText);

//Register for event

editText.addTextChangedListener(this);

}
//implement methods given in TextWatcher Interface

public void beforeTextChanged(CharSequence s, int


start, int count, int after)
{

@Override
public void onTextChanged(CharSequence s, int
start, int before, int count)
{

v1.setText(editText.getText());
}

@Override
public void afterTextChanged(Editable s)
{

2.Explain Button Component with Example


 A button consists of text or an icon (or both text and an icon) that
communicates what action occurs when the user touches it.

 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.
 you can create the button in your layout in following ways:

 With text, using the Button class:

 <Button

 android:layout_width="wrap_content"

 android:layout_height="wrap_content"

 android:text="@string/button_text"

 />

 Image Button

 With an icon, using the ImageButton class:

 <ImageButton

 android:layout_width="wrap_content"

 android:layout_height="wrap_content"

 android:src="@drawable/button_icon" />

 Handling Click Event on Button

 To declare the event handler programmatically, create an


View.OnClickListener object and assign it to the button by calling
setOnClickListener(View.OnClickListener).

 For example:

 Button button = (Button) findViewById(R.id.button_send);

 button.setOnClickListener(new View.OnClickListener() {

 public void onClick(View v) {

 // Do something in response to button click

 }});
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.}

4. Explain CheckBox Control in android.


 Checkboxes allow the user to select one or more options from a set.
Typically, you should present each checkbox option in a vertical list.

 To create each checkbox option, create a CheckBox in your layout.

 Because a set of checkbox options allows the user to select multiple


items, each checkbox is managed separately and you must register a click
listener for each one.

 Android CheckBox class is the subclass of CompoundButton class.

 The android.widget.CheckBox class provides the facility of creating the


CheckBoxes.

Methods of CheckBox class


Method Description

public boolean isChecked() Returns true if it is checked otherwise


false.
public void setChecked(boolean Changes the state of the CheckBox.
status)

Handling CheckBox Event:

Steps:

1.Create checkbox control in GUI XML File

<CheckBox
android:id="@+id/checkBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Tea" />

2.In Java file Implements OnCheckedChangeListener interface

public class Sample extends AppCompatActivity


implements CompoundButton.OnCheckedChangeListener
{}

3.Register each CheckBox for an event using setOnCheckedChangeListener(this)

c1=(CheckBox)findViewById(R.id.checkBox);
c1.setOnCheckedChangeListener(this);

4.implement following method in JAVA file

public void onCheckedChanged(CompoundButton


buttonView, boolean isChecked)
Example Code:

public class Sample extends AppCompatActivity


implements CompoundButton.OnCheckedChangeListener
{

CheckBox c1=null;
CheckBox c2=null;

protected void onCreate(Bundle savedInstanceState)


{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sample);

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.}

6.Explain Android RadioButton With Example.


 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.

 RadioButton is generally used with RadioGroup. RadioGroup contains several


radio buttons, marking one radio button as checked makes all other radio
buttons as unchecked.
 Add RadioButton in XML File
 <RadioGroup
android:id="@+id/rg"
android:layout_width=" wrap_content "
android:layout_height=" wrap_content "

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" />

 android:checkedButton:tells that which button is


initially checked.
 android:text:indicate caption for RadioButton

Handling Event On RadioButton

 When we click on RadioButton onCheckedChanged event is generated.

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>

3.In java file implements


OnCheckedChangeListener in Activity

4.Register RadioGroup to event using


setOnCheckedChengeListener() method.

5.Implement onCheckChanged()of listener


public void onCheckedChanged(RadioGroup group,
int checkedId)

Example Code:

import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.*;
import android.view.*;

public class Sample extends AppCompatActivity


implements RadioGroup.OnCheckedChangeListener
{
//Define Object of Radiogroup

RadioGroup rg=null;

//retrieve component from UI-XML File.


protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);

rg=(RadioGroup)findViewById(R.id.rg);

//Register for event


rg.setOnCheckedChangeListener(this);
}

public void onCheckedChanged(RadioGroup group, int


checkedId) {

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;
}}}

You might also like