Showing posts with label EditText. Show all posts
Showing posts with label EditText. Show all posts

Sunday, September 22, 2013

Edit Text - its Features & Key Event handling

activity_practice.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
android:orientation="vertical"
android:background="#d0d0d0"
    tools:context=".PracticeActivity" >

<EditText 
   android:id="@+id/edittxt1"
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:layout_margin="20dp"
   />

</LinearLayout>


Practiceactivity.java

package com.agilissystems.practicework;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;

public class PracticeActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_practice);
        
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.practice, menu);
        return true;
    }

  
}

Strings.xml

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

    <string name="app_name">Practicework</string>
    <string name="action_settings">Settings</string>

</resources>

Above code will provide the simple edit box to enter the text. There is no heading for the text which might misguide the use. so Android provides a attributed called hint. Add below line in "Strings.xml" file.

<string name="txt1">Enter Value</string>

and below line to edittext element of activity_practice.xml file.

        android:hint="@string/txt1"

It will show you "Enter Value" text in the edit text field while it is run.

When user runs the application, it will show the default keyboard. although Android provides different types of keyboards to fulfill user's requirements like to enter numberic values, user should have numberic keyboard rather than text same as for phone number, email, web site URL etc. to achieve, Android provides a properties like inputType. Add below line to edittext element in activity_practice.xml file.

android:inputType="number"

It will show only number keyboard and android:inputType="phone" will show phone keyboard. 

Below source code explains about the handling of Key event. There are no changes in layout and string files only in the code base file. the updated code is given below.

PracticeActivity.java

package com.agilissystems.practicework;

import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnKeyListener;
import android.widget.EditText;
import android.widget.Toast;

public class PracticeActivity extends Activity implements OnKeyListener {

EditText oET1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_practice);
        oET1 = (EditText) findViewById(R.id.edittxt1);
        oET1.setOnKeyListener(this);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.practice, menu);
        return true;
    }


@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
// TODO Auto-generated method stub

if (v.getId() == R.id.edittxt1){
if (event.getAction() == KeyEvent.ACTION_DOWN && keyCode == KeyEvent.KEYCODE_ENTER)
Toast.makeText(this, oET1.getText().toString() , Toast.LENGTH_LONG).show();
return true;
}
else
return false;
}
   
}

Above code shows that whatever the text entered into the edit text field will be toasted while user taps on "Enter" button from key pad.



Sunday, August 19, 2012

Android - EditText Example


Below is example of EditText, it can be used to enter text. Example demonstrates usage of other attributes and methods.

I used Eclipse as development tool and tested the sample in Emulator for Android 4.0.3.

There are resources are used to develop android app like image files etc. but for this example, we are using three basis components like code base file, layout file and string file.

Source of Layout File(.XML):

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

<EditText 
   android:id="@+id/etText"
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:hint="@string/ettext"
   />
    <Button
        android:id="@+id/btnselectall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/btnselectall"
        />
    
    <Button
        android:id="@+id/btnsetselection"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/btnsetselection"
        />
    <Button
        android:id="@+id/btnsettext"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/btnsettext"
        />
    <Button
        android:id="@+id/btnclose"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/btnclose"
        />
</LinearLayout >



Source of Strings file(.XML):

<resources>

    <string name="app_name">EditTextExample</string>
    <string name="hello_world">Hello world!</string>
    <string name="menu_settings">Settings</string>
    <string name="title_activity_edit_text_example">Edit Text Example</string>
<string name="ettext">Enter Text</string>
<string name="btnselectall">Select All</string>
<string name="btnsetselection">Set Selection</string>
<string name="btnsettext">Set Text</string>
<string name="btnclose">Close</string>
</resources>


Source code file (.JAVA):

package com.example.edittextexample;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class EditTextExample extends Activity {

private Button btnSelectAll, btnClose, btnSetSelection, btnSetText;
private EditText etText;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_edit_text_example);
        
    btnSelectAll = (Button) findViewById(R.id.btnselectall);
    etText = (EditText) findViewById(R.id.etText);
    btnClose = (Button) findViewById(R.id.btnclose);
    btnSetSelection = (Button) findViewById(R.id.btnsetselection);
    btnSetText = (Button) findViewById(R.id.btnsettext);
   
    etText.setSingleLine(); //allows to be text in single line if it is not there then longer text will be in next lines. you can use "setHorizontallyScrolling" method as well
        addOnOkClickListener();
        addOnCloseListener();
        addOnSetSelectionListener();
        addOnClickSetTextListener();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_edit_text_example, menu);
        return true;
    }
    
    public void addOnOkClickListener (){

   
    btnSelectAll.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
etText.selectAll(); //select all text of EditText
}
});
    }
    
    public void addOnCloseListener(){
    btnClose.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
// TODO Auto-generated method stub
finish();
}
});
    }
    
    public void addOnSetSelectionListener(){
    btnSetSelection.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
etText.setSelection(0,4); //method is used to select specific character from EditText, start and end range is required, start = 0 means first character. and "End" parameter allows how many characters to be selected.
}
});
    }
    
    public void addOnClickSetTextListener(){
    btnSetText.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
etText.setText("Hello World!!!"); //setText method is used to set Text to EditText
}
});
    }
}


Note:
  • "Select All"  selects text entered in text field
  • "Set Selection" selects first 4 characters of entered text
  • "Set Text" sets value like "Hello World!!!" to edit text 
  • "Close" will close the application