Android Text Controls

Text controls include:

1. TextView.

2. EditText

3. AutoCompleteEditText

4. MultiCompleteTextView

TextView
The TextView represent an un-editable text. It resembles the Label control in C# or ASP.NET but it has an interesting feature which is the ability to highlight the text if its is an URL, an e-mail or a phone number so that when the user clicks on the textview the default intent whether it is the web browser or the dialer launches
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="Visit Http://www.android-pro.blogspot.com"
    android:autoLink="web"
    android:id="@+id/txtURL"
    />
    
    <TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="Dial 1 650-253-0000"
    android:autoLink="all"
    />
</LinearLayout>


you can see that the TextViews containing URLs or Phone numbers are highlighted, and when the user presses on them the default intent (the browser or the dialer launches)


this is done by using the property android:autoLink which can have the values:
web, email ,phone, map
or
All
this can be achieved from code by using the following code:
TextView txtURL=(TextView)findViewById(R.id.txtURL);
Linkify.addLinks(txtURL, Linkify.ALL);

EditText
The EditText is a subclass of the TextView it is like the TextBox in C#. it enables users to edit text.


We can use the autoText property to make the EditText to correct the common spelling mistakes.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:autoText="true"

/>
</LinearLayout>



We can use the capitalize property to make the text capitalized like this:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >

<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:capitalize="characters"

/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:capitalize="none"
/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:capitalize="words"
/>
</LinearLayout>



we can use the password property to make the control accepts phone numbers input:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >

<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:password="true"

/>

</LinearLayout>



We can enforce the Control to wrap all the text in a single line by setting android:singleLine property to true.

AutoCompleteTextView
The autoCompleteTextView is an EditText with auto complete functionality. The auto complete functionality can be achieved by attaching an Adapter with the auto complete values to the control like this:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >

<AutoCompleteTextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/act"

/>

</LinearLayout>



Then attach the adapter from the code like this:
AutoCompleteTextView act=(AutoCompleteTextView)findViewById(R.id.act);
        ArrayAdapter arr=new ArrayAdapter(this,android.R.layout.simple_dropdown_item_1line,new String []{"Hello","Hi","Alloha"});
        act.setAdapter(arr);

in a search program for example you can obtain the auto complete words from a web service and populate the adapter with these words.

MultiAutoCompleteTextView

The AutoCompleteTextView can suggest auto complete for the entire text in the control, meaning that if you type more than one word it would try to match the whole sentence not the single words.
The MultiAutoCompleteTextView works the same way as the AutoCompleteTextView except you can add a Tokenizer that parses the text and allows you to suggest where to start suggesting words like this:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >

<MultiAutoCompleteTextView 
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/act"

/>

</LinearLayout>


then from code:
MultiAutoCompleteTextView mact=(MultiAutoCompleteTextView)findViewById(R.id.act);
        ArrayAdapter arr=new ArrayAdapter(this,android.R.layout.simple_dropdown_item_1line,new String []{"Hello","Hi","Alloha"});
        mact.setAdapter(arr);
        mact.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer());

The tokenizer tells the control to start suggesting for words separated by a comma.

Reactions: 

Android Button Controls

Android offers three types of button controls


1. The Basic Button.

2. Image Button

3. Toggle Button.

The Basic Button:


The android standard button. It is a subclass of the TextView class so it inherits all of its properties.

<linearlayout 
android:layout_height="fill_parent" 
android:layout_width="fill_parent" 
android:orientation="vertical" xmlns:android="http://schemas.android.com/apk/res/android"
>
<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click Me"
/>
</linearlayout>


If you want to implement the OnClick event handler for a button there are three ways to do it:
First
You can implement the OnClickListner Interface for each single button in the activity like this:

Button btn=(Button)findViewById(R.id.btn);
        
        btn.setOnClickListener(new OnClickListener()
        {
   public void onClick(View v) {
    Button btn=(Button)v;
    btn.setText("You clicked on the button");
   }
        }
        );
 
But this will lead to large code blocks with lots of redundancy cause you will do it for each button in your activity.

Second
You can use an activity that implements the OnClickListner Interface and use the onClick method by switching between the buttons IDs:
public class ButtonControls extends Activity implements OnClickListener {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
    }
 public void onClick(View v) {
  // TODO Auto-generated method stub
  switch(v.getId())
  {
  case R.id.btn1:
   //Do something
   break;
  case R.id.btn2:
   // Do something
   break;
  }
 }
}


Third
Since Android 1.6 there was a new cool feature which is the ability to define the on click handlers for the bttons from the XML layout definition. Which is similar to that in ASP.NET.

<button 
android:id="@+id/btn1" 
android:layout_height="wrap_content" 
android:layout_width="wrap_content" 
android:onClick="ClickHandler" 
android:text="Click Me"
/>
<button 
android:id="@+id/btn2" 
android:layout_height="wrap_content" 
android:layout_width="wrap_content" 
android:onclick="ClickHandler" 
android:text="Click Me too" 
/>
Then you define the event handler method in your class file in the same normal way

 public void ClickHandler(View v) { 
// TODO Auto-generated method stub
  switch(v.getId())
  {
  case R.id.btn1:
   //Do something
   break;
  case R.id.btn2:
   // Do something
   break;
  }
 }

The ImageButton
The ImageButton control is similar to the Button except it represents a button with an image instead of the text
<linearlayout android:layout_height="fill_parent" android:layout_width="fill_parent" android:orientation="vertical" xmlns:android="http://schemas.android.com/apk/res/android"
/>
<imagebutton 
android:id="@+id/btn1" 
android:layout_height="wrap_content" 
android:layout_width="wrap_content" 
android:src="@drawable/globe"
/>
</linearlayout>

You can set the image source property from the code like this:
ImageButton btn=(ImageButton)findViewById(R.id.btn1);
btn.setImageResource(R.drawable.globe);


TheToggleButton:
The toggle button is like a check box or radio button, it has two states: On or Off.
The default behavior of ToggleButton is Off state, it displays a gray bar and the text Off.
When in On state it displays a green bar and has the text On.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<ToggleButton
android:id="@+id/tb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Switch On"
/>
</LinearLayout>


See that despite we specified the android:text property of the toggle button, it displays the default text “Off”.
This is because ToggleButton inherits from TextView. But practically the android:text property is useless.
Instead we define the android:textOn and android:textOff properties.

In code to check the state of the Toggle button programmatically you can define the click handler in the regular way:
<ToggleButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textOff="Switch On"
android:textOn="Switch Off"
android:id="@+id/btn"
android:onClick="ClickHandler"
/>

Then check the state of it like this:
public void ClickHandler(View v)
    {
     ToggleButton tg=(ToggleButton)v;
     if(tg.isChecked())
      //Do something
     else
      //Do something else
    }

here's what it's gonna look like:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<ToggleButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textOff="Switch On"
android:textOn="Switch Off"
/>
<ToggleButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textOff="Switch On"
android:textOn="Switch Off"
/>
</LinearLayout>


Reactions: 

Introduction to UI in Android

Layouts in android is constructed from two objects: View and ViewGroup.


The View class is the base class for many widgets sub classes such as TextView and Button classes.

The ViewGroup is a view that conatains other views. The Viewgroup class is the base class for many layouts in android.

The UI Hierarchy is described in this picture from the android online sdk documentation



As you can see the root node is a view group which can be any layout that contains child views or even other viewgroups.


The root node appears on the activity by calling setcontentview ( method). Which in turn draws its child nodes and each child node draws its child nodes

In the following posts we are going to explore the categories of android widgets which are:
 
1. Text Controls


2. Button controls

3. List controls

4. Grid controls

5. Date and time controls

6. Menus

7. Dialogs

8. Adapters

Reactions: 

Really interesting Android Application

Please take a look at this amazing Android app.
it is like a remote controller for a Robot.
it is controlled via bluetooth with a joystick interface with accelerometers
really interesting !!

Reactions: