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.



No comments:

Post a Comment