Sep 29, 2013

Request focus on a specified EditText view in startup, by calling requestFocus()

The example force a specified EditText, editText2, to be focused when the app start-up, by calling its requestFocus().

Remark: somebody advise to specify android:focusable="true" and android:focusableInTouchMode="true" in XML.

For reference, I display the focus view in onCreate(). And also implement OnGlobalLayoutListener() to monitor the focus view after layout finished, and when focus view changed.

focus on a specified EditText view
focus on a specified EditText view


package com.example.androidfocus;

import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.view.ViewTreeObserver;
import android.view.ViewTreeObserver.OnGlobalLayoutListener;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {
 
 TextView textInfo;
 EditText editText1, editText2, editText3;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  
  textInfo = (TextView)findViewById(R.id.info);
  editText1 = (EditText)findViewById(R.id.edittext1);
  editText2 = (EditText)findViewById(R.id.edittext2);
  editText3 = (EditText)findViewById(R.id.edittext3);
  
  editText2.requestFocus();
  
  View focusViewOnCreate = getWindow().getCurrentFocus();
  if(focusViewOnCreate == null){
   Toast.makeText(MainActivity.this, 
     "NO View get focus onCreate()!", Toast.LENGTH_LONG).show();
  }else{
   Toast.makeText(MainActivity.this, 
     "focus onCreate(): " + focusViewOnCreate.toString(), 
     Toast.LENGTH_LONG).show();
  }
  
  ViewTreeObserver viewTreeObserver = textInfo.getViewTreeObserver();
  viewTreeObserver.addOnGlobalLayoutListener(new OnGlobalLayoutListener(){

   @Override
   public void onGlobalLayout() {
    /*
     *  onGlobalLayout() will be called whenthe global layout state 
     *  or the visibility of views within the view tree changes.
     *  
     *  In this case, it will be called after layout displayed, or 
     *  focus view changed. 
     */
    
    View focusViewOnGlobalLayout = getWindow().getCurrentFocus();
    if(focusViewOnGlobalLayout == null){
     textInfo.setText("NO View get Focus!");
    }else{
     textInfo.setText("Focus View:\n" +  focusViewOnGlobalLayout.toString());
    }
   }});
 }

}


<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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="android-coding.blogspot.com" />

    <EditText
        android:id="@+id/edittext1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="android-coding" />

    <EditText
        android:id="@+id/edittext2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="android-coding.blogspot.com" />

    <EditText
        android:id="@+id/edittext3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
    <TextView
        android:id="@+id/info"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>


Alternatively, you can also Request focus by defining in layout XML.

No comments:

Post a Comment

Infolinks In Text Ads