Edittext requestFocus() dynamically not working - android

I have multiple Edittexts on action next I have added nextFocus values in the xml.
But one edit text is either visible or invisible based on the state of under age checkbox value. So I need to set focus on it afterr YYYY editext dynammically. But dynamically the requestFocus() is not working.
What I need to do on click of Next
This Email view is visible only when under 18 check box is unchecked.
What happends on next button click after YYYY edittext : control goes
to username Edittext. I cant use FocusNext in xml as the app crashes
whenever the email textview is GONE
dobYearEt.setOnEditorActionListener(new EditText.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
Log.i("focus","on yyyy");
if (actionId == EditorInfo.IME_ACTION_DONE ||
actionId == EditorInfo.IME_ACTION_NEXT) {
Log.i("focus",isUnderAge+"");
if(isUnderAge){
username_reg_ev_dob_row.setFocusableInTouchMode(true);
username_reg_ev_dob_row.requestFocus();
}else{
// NOTHING HAPPENS
// email_ev.setFocusableInTouchMode(true);
// dobYearEt.clearFocus();
email_ev.requestFocus();
// InputMethodManager mgr = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
// mgr.showSoftInput(email_ev, InputMethodManager.SHOW_IMPLICIT);
}
return false;
}
Xml
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/register"
android:layout_marginLeft="#dimen/sign_up_left_right_margin"
android:layout_marginRight="#dimen/sign_up_left_right_margin"
android:layout_marginTop="#dimen/sign_up_user_name_top_margin">
<!--<RelativeLayout-->
<!--android:layout_width="match_parent"-->
<!--android:layout_height="wrap_content">-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:id="#+id/name_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="3">
<EditText
android:id="#+id/firstname_ev"
style="#style/RegisterViewStyle"
android:hint=" FIRST NAME:"
android:textCursorDrawable="#null"
android:nextFocusForward="#+id/middle_ev"
android:nextFocusRight="#+id/middle_ev"
android:nextFocusDown="#+id/middle_ev"
android:nextFocusLeft="#+id/middle_ev"
android:nextFocusUp="#+id/middle_ev"/>
<View
android:layout_width="#dimen/sign_up_names_sep_width"
android:layout_height="wrap_content"/>
<EditText
android:id="#+id/middle_ev"
style="#style/RegisterViewStyle"
android:hint=" MIDDLE INITIAL:"
android:textCursorDrawable="#null"
android:nextFocusForward="#+id/lastname_ev"
android:nextFocusRight="#+id/lastname_ev"
android:nextFocusDown="#+id/lastname_ev"
android:nextFocusLeft="#+id/lastname_ev"
android:nextFocusUp="#+id/lastname_ev"/>
<View
android:layout_width="#dimen/sign_up_names_sep_width"
android:layout_height="wrap_content"/>
<EditText
android:id="#+id/lastname_ev"
style="#style/RegisterViewStyle"
android:hint=" LAST NAME:"
android:textCursorDrawable="#null"
android:nextFocusForward="#+id/dob_mm_et"
android:nextFocusRight="#+id/dob_mm_et"
android:nextFocusDown="#+id/dob_mm_et"
android:nextFocusLeft="#+id/dob_mm_et"
android:nextFocusUp="#+id/dob_mm_et"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<CheckBox
android:id="#+id/under18_check_box"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:buttonTint="#color/raffle_blue_color"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/under18_tv"
android:text="I'm under 18 and/or don't have an email?"/>
</LinearLayout>
<LinearLayout
android:id="#+id/dob_parent_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<!--android:weightSum="2"-->
<LinearLayout
style="#style/RegisterViewStyle2"
android:id="#+id/dob_container"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginRight="2dp"
android:layout_marginTop="#dimen/sign_up_field_sep_margin"
android:layout_weight="1"
android:orientation="horizontal">
<!--style="#style/RegisterViewStyle2"-->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="DOB:"
android:textColor="#color/edit_text_hint_color"
android:id="#+id/dob_label_tv"
/>
<!--<EditText-->
<!--android:id="#+id/dob_dd_et"-->
<!--android:layout_width="wrap_content"-->
<!--android:layout_height="wrap_content"-->
<!--android:background="#android:color/transparent"-->
<!--android:hint="_ _"-->
<!--android:textCursorDrawable="#null"-->
<!--android:inputType="number"-->
<!--android:maxLength="2"-->
<!--android:nextFocusForward="#+id/dob_mm_et"-->
<!--android:nextFocusRight="#+id/dob_mm_et"-->
<!--android:nextFocusDown="#+id/dob_mm_et"-->
<!--android:nextFocusLeft="#+id/dob_mm_et"-->
<!--android:nextFocusUp="#+id/dob_mm_et"/>-->
<EditText
android:id="#+id/dob_mm_et"
style="#style/RegisterViewStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#android:color/transparent"
android:hint="MM"
android:textCursorDrawable="#null"
android:inputType="number"
android:maxLength="2"
android:nextFocusForward="#+id/dob_dd_et"
android:nextFocusRight="#+id/dob_dd_et"
android:nextFocusDown="#+id/dob_dd_et"
android:nextFocusLeft="#+id/dob_dd_et"
android:nextFocusUp="#+id/dob_dd_et"/>
<TextView
android:textColor="#color/edit_text_hint_color"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" / "/>
<!--<EditText-->
<!--android:id="#+id/dob_mm_et"-->
<!--android:layout_width="wrap_content"-->
<!--android:layout_height="wrap_content"-->
<!--android:background="#android:color/transparent"-->
<!--android:hint="_ _"-->
<!--android:inputType="number"-->
<!--android:textCursorDrawable="#null"-->
<!--android:maxLength="2"-->
<!--android:nextFocusForward="#+id/dob_yyyy_et"-->
<!--android:nextFocusRight="#+id/dob_yyyy_et"-->
<!--android:nextFocusDown="#+id/dob_yyyy_et"-->
<!--android:nextFocusLeft="#+id/dob_yyyy_et"-->
<!--android:nextFocusUp="#+id/dob_yyyy_et"/>-->
<EditText
android:id="#+id/dob_dd_et"
style="#style/RegisterViewStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#android:color/transparent"
android:hint="DD"
android:inputType="number"
android:textCursorDrawable="#null"
android:maxLength="2"
android:nextFocusForward="#+id/dob_yyyy_et"
android:nextFocusRight="#+id/dob_yyyy_et"
android:nextFocusDown="#+id/dob_yyyy_et"
android:nextFocusLeft="#+id/dob_yyyy_et"
android:nextFocusUp="#+id/dob_yyyy_et"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" / "
android:textColor="#color/edit_text_hint_color"/>
<EditText
style="#style/RegisterViewStyle"
android:id="#+id/dob_yyyy_et"
android:layout_width="wrap_content"
android:textCursorDrawable="#null"
android:layout_height="wrap_content"
android:background="#android:color/transparent"
android:hint="YYYY"
android:inputType="number"
android:maxLength="4"
/>
<!--android:nextFocusForward="#+id/email_ev"-->
<!--android:nextFocusRight="#+id/email_ev"-->
<!--android:nextFocusDown="#+id/email_ev"-->
<!--android:nextFocusLeft="#+id/email_ev"-->
<!--android:nextFocusUp="#+id/email_ev"-->
</LinearLayout>
<EditText
android:id="#+id/email_ev"
style="#style/RegisterViewStyle"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginTop="#dimen/sign_up_field_sep_margin"
android:layout_weight="1"
android:hint=" EMAIL:"
android:textCursorDrawable="#null"
/>
<!--android:nextFocusForward="#+id/password_reg_ev"-->
<!--android:nextFocusRight="#+id/password_reg_ev"-->
<!--android:nextFocusDown="#+id/password_reg_ev"-->
<!--android:nextFocusLeft="#+id/password_reg_ev"-->
<!--android:nextFocusUp="#+id/password_reg_ev"-->
<!--style="#style/RegisterViewStyle2"-->
<EditText
android:id="#+id/username_reg_ev_dob_row"
style="#style/RegisterViewStyle"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginTop="#dimen/sign_up_field_sep_margin"
android:layout_weight="1"
android:hint=" USERNAME:"
android:textCursorDrawable="#null"
android:visibility="gone"
android:nextFocusForward="#+id/password_reg_ev"
android:nextFocusRight="#+id/password_reg_ev"
android:nextFocusDown="#+id/password_reg_ev"
android:nextFocusLeft="#+id/password_reg_ev"
android:nextFocusUp="#+id/password_reg_ev"/>
</LinearLayout>
<EditText
android:id="#+id/username_reg_ev"
style="#style/RegisterViewStyle2"
android:layout_below="#+id/email_ev"
android:layout_marginTop="#dimen/sign_up_field_sep_margin"
android:hint=" USERNAME:"
android:textCursorDrawable="#null"
android:nextFocusForward="#+id/password_reg_ev"
android:nextFocusRight="#+id/password_reg_ev"
android:nextFocusDown="#+id/password_reg_ev"
android:nextFocusLeft="#+id/password_reg_ev"
android:nextFocusUp="#+id/password_reg_ev"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<EditText
android:id="#+id/password_reg_ev"
style="#style/RegisterViewStyle2"
android:layout_below="#+id/username_reg_ev"
android:layout_marginTop="#dimen/sign_up_field_sep_margin"
android:hint=" PASSWORD:"
android:layout_marginRight="2dp"
android:textCursorDrawable="#null"
android:inputType="textPassword"
android:nextFocusForward="#+id/confirm_password_reg_ev"
android:nextFocusRight="#+id/confirm_password_reg_ev"
android:nextFocusDown="#+id/confirm_password_reg_ev"
android:nextFocusLeft="#+id/confirm_password_reg_ev"
android:nextFocusUp="#+id/confirm_password_reg_ev"/>
<EditText
android:id="#+id/confirm_password_reg_ev"
style="#style/RegisterViewStyle2"
android:layout_below="#+id/password_reg_ev"
android:layout_marginTop="#dimen/sign_up_field_sep_margin"
android:hint=" CONFIRM PASSWORD:"
android:textCursorDrawable="#null"
android:inputType="textPassword"
android:nextFocusForward="#+id/group_code_reg_ev"
android:nextFocusRight="#+id/group_code_reg_ev"
android:nextFocusDown="#+id/group_code_reg_ev"
android:nextFocusLeft="#+id/group_code_reg_ev"
android:nextFocusUp="#+id/group_code_reg_ev"/>
</LinearLayout>
<EditText
android:id="#+id/group_code_reg_ev"
style="#style/RegisterViewStyle2"
android:layout_below="#+id/confirm_password_reg_ev"
android:layout_marginTop="#dimen/sign_up_field_sep_margin"
android:textCursorDrawable="#null"
android:hint=" GROUP CODE(OPTIONAL):"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<CheckBox
android:id="#+id/terms_conditions_signup_checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:buttonTint="#color/raffle_blue_color"
/>
<TextView
android:id="#+id/terms_conditions_register_message"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/terms_conditions"/>
</LinearLayout>
<RelativeLayout
android:id="#+id/register_btn_rl"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/registration_message"
android:layout_marginBottom="6dp"
android:layout_marginTop="#dimen/sign_up_btn_top_margin"
android:background="#color/raffle_blue_color">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:id="#+id/register_tv"
android:padding="6dp"
android:text="REGISTER"
android:textColor="#android:color/white"
android:textSize="#dimen/sign_up_edit_text_size"/>
</RelativeLayout>
</LinearLayout>
</ScrollView>
I have referred to various solutions but nothing is working
Set next EditText focused and editable on KEY_DOWN
requestFocus not working
EditText request focus not working
requestFocus not working

What was happening is the I was returning false from onEditorAction- Hence the default editor action was also performed back to back and the focus instantly moved away from the email editText. Now I return true and the deafult action is not performed.Its working correctly now.
dobYearEt.setOnEditorActionListener(new EditText.OnEditor ActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
Log.i("focus","on yyyy");
if (actionId == EditorInfo.IME_ACTION_DONE ||
actionId == EditorInfo.IME_ACTION_NEXT) {
Log.i("focus",isUnderAge+"");
if(isUnderAge){
username_reg_ev_dob_row.setFocusableInTouchMode(true);
username_reg_ev_dob_row.requestFocus();
}else{
email_ev.requestFocus();
}
return true ; // NOW WORKS
// return false;
}

U need two xml attributes also to achieve this:
android:focusable="true"
android:focusableInTouchMode="true"
Add them to the EditText as well as the parent layouts. By default these are false, so the focus is not given to the requested view.
Source: https://developer.android.com/reference/android/view/View.html#attr_android:focusable
After u show the EditText based on the checkbox selection, add the next and previous focus points dynamically in code.
Hope this helps.

Related

EditText OnFocusChangeListener() doesn't hide cursor and view

I made an EditText OnFocusChangeListener that supposedly hides the cursor and another view when it's not in focus (i.e. clicking anywhere on the screen aside from the EditText will hide those items). Clicking on the EditText is the only time that will display them.
This is my OnFocusChangeListener for my EditText pageTitle:
pageTitle.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View view, boolean b) {
if(b) {
pageTitle.setCursorVisible(true);
saveCancelBar.setVisibility(View.VISIBLE);
} else {
pageTitle.setCursorVisible(false);
saveCancelBar.setVisibility(View.GONE);
}
}
});
I also made an OnclickListener:
pageTitle.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
pageTitle.setCursorVisible(true);
saveCancelBar.setVisibility(View.VISIBLE);
}
});
The OnClickListener works but not the OnFocusListener. Ideally, pageTitle's cursor and saveCancelBar are initially hidden and will only appear when pageTitle is clicked.
This my XML code:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:ignore="missingPrefix"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/white"
tools:context="app.wanderast.activity.AddPhotoActivity">
<LinearLayout
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/white"
android:orientation="horizontal">
<TextView
android:id="#+id/back_arrow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/arrow_left"
android:textSize="20sp"
android:clickable="true"
android:textColor="#color/black" />
</LinearLayout>
<LinearLayout
android:id="#+id/header"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:layout_marginStart="10dp"
android:layout_marginEnd="10dp"
android:layout_below="#id/toolbar"
android:orientation="vertical">
<EditText
android:id="#+id/title"
android:focusable="true"
android:focusableInTouchMode="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="36dp"
android:background="#color/transparent"
android:maxLength="64"
android:privateImeOptions="nm"
android:inputType="textNoSuggestions|textMultiLine"
android:cursorVisible="false"
app:autoSizeTextType="uniform"
app:autoSizeMinTextSize="16sp"
app:autoSizeMaxTextSize="24sp"
app:autoSizeStepGranularity="4sp"
android:textColor="#color/grey700"/>
<View
android:layout_width="100dp"
android:layout_height="2dp"
android:layout_marginTop="3dp"
android:background="#color/grey700"
/>
<LinearLayout
android:id="#+id/sort"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/header"
android:layout_marginTop="20dp"
android:clickable="true"
android:gravity="bottom"
android:orientation="horizontal"
android:visibility="gone">
<TextView
android:id="#+id/sort_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="3dp"
android:text="List View"
android:textColor="#color/grey700"
android:textSize="14sp"/>
<TextView
android:id="#+id/sort_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/chevron_circle_down"
android:textColor="#color/grey700"
android:textSize="14sp"/>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="#+id/linearLayout">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/add_photo_layout"
android:layout_marginTop="150dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center"
android:layout_centerInParent="true">
<TextView
android:id="#+id/placeholder"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="40dp"
android:layout_marginEnd="40dp"
android:layout_marginBottom="30dp"
android:background="#null"
android:minLines="0"
android:text="Add your first travel moment to your story"
android:textAlignment="center"
android:textColor="#color/black"
android:textSize="20sp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center">
<LinearLayout
android:id="#+id/capture_photo_button"
android:layout_marginBottom="15dp"
android:paddingStart="5dp"
android:paddingEnd="5dp"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:layout_width="170dp"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center|center_vertical"
android:background="#drawable/green_pill_thick">
<TextView
android:id="#+id/capture_photo_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
android:text="#string/camera"
android:layout_gravity="center_horizontal|center_vertical"
android:textColor="#color/green500"
android:layout_marginEnd="5dp"/>
<TextView
android:id="#+id/capture_photo_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Capture Moment"
android:layout_gravity="center_horizontal|center_vertical"
android:textColor="#color/green500"
android:textAlignment="center"
android:textSize="14sp"/>
</LinearLayout>
<LinearLayout
android:id="#+id/add_photo_button"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:paddingStart="5dp"
android:paddingEnd="5dp"
android:layout_width="170dp"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center|center_vertical"
android:background="#drawable/blue_pill_thick">
<TextView
android:id="#+id/add_photo_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
android:text="#string/photo"
android:layout_gravity="center_horizontal|center_vertical"
android:textColor="#color/blue500"
android:layout_marginEnd="5dp"/>
<TextView
android:id="#+id/add_photo_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add from Gallery"
android:layout_gravity="center_horizontal|center_vertical"
android:textColor="#color/blue500"
android:textAlignment="center"
android:textSize="14sp"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
</RelativeLayout>
<RelativeLayout
android:id="#+id/list_view_layout"
android:visibility="gone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="150dp"
android:paddingBottom="50dp">
<ListView
android:id="#+id/list_view_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:stackFromBottom="true"
android:transcriptMode="alwaysScroll">
</ListView>
</RelativeLayout>
</LinearLayout>
<!--Navbar-->
<LinearLayout
android:id="#+id/photo_story_navbar"
android:layout_width="match_parent"
android:layout_height="56dp"
android:background="#drawable/navbar"
android:layout_alignParentBottom="true"
android:orientation="horizontal"
android:visibility="gone">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:gravity="right"
android:paddingStart="15dp"
android:orientation="horizontal"
android:layout_marginEnd="10dp">
<!-- Take Photo Button -->
<LinearLayout
android:id="#+id/nav_capture_button"
android:layout_width="84dp"
android:layout_height="32dp"
android:paddingStart="10dp"
android:orientation="horizontal"
android:background="#drawable/green_pill_button"
android:layout_gravity="center_vertical"
android:gravity="center|center_vertical"
android:paddingEnd="10dp"
android:clickable="true">
<TextView
android:id="#+id/nav_capture_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="14sp"
android:text="#string/camera"
android:textColor="#color/green500"
android:layout_gravity="center_horizontal|center_vertical"
android:paddingEnd="5dp"/>
<TextView
android:id="#+id/nav_capture_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Capture"
android:layout_gravity="center_horizontal|center_vertical"
android:textColor="#color/green500"
android:textAlignment="center"
android:textSize="12sp"/>
</LinearLayout>
<!-- Add from Gallery Button -->
<LinearLayout
android:id="#+id/nav_gallery_button"
android:layout_width="84dp"
android:layout_height="32dp"
android:paddingStart="10dp"
android:layout_marginLeft="5dp"
android:orientation="horizontal"
android:background="#drawable/blue_pill"
android:layout_gravity="center_vertical"
android:gravity="center|center_vertical"
android:paddingEnd="10dp"
android:clickable="true">
<TextView
android:id="#+id/nav_gallery_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="14sp"
android:text="#string/photo"
android:textColor="#color/blue500"
android:layout_gravity="center_horizontal|center_vertical"
android:paddingEnd="5dp"/>
<TextView
android:id="#+id/nav_gallery_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Gallery"
android:layout_gravity="center_horizontal|center_vertical"
android:textColor="#color/blue500"
android:textAlignment="center"
android:textSize="12sp"/>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:gravity="right"
android:paddingEnd="15dp"
android:orientation="horizontal">
<!-- Review button -->
<LinearLayout
android:id="#+id/nav_review_button"
android:layout_width="84dp"
android:layout_height="32dp"
android:paddingStart="10dp"
android:orientation="horizontal"
android:background="#drawable/red_500_pill"
android:layout_gravity="right"
android:gravity="center|center_vertical"
android:paddingEnd="10dp"
android:clickable="true">
<TextView
android:id="#+id/nav_review_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Review"
android:layout_gravity="center_horizontal|center_vertical"
android:textColor="#color/red500"
android:textAlignment="center"
android:textSize="12sp"/>
<TextView
android:id="#+id/nav_review_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="14sp"
android:text="#string/angle_double_right"
android:textColor="#color/red500"
android:layout_gravity="center_horizontal|center_vertical"
android:paddingStart="5dp"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
<RelativeLayout
android:id="#+id/save_cancel_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/linearLayout"
android:layout_alignParentStart="true"
android:background="#drawable/black_border_top"
android:visibility="gone">
<RelativeLayout
android:id="#+id/save_title_btn"
android:clickable="true"
android:layout_width="84dp"
android:layout_height="32dp"
android:layout_alignParentRight="true"
android:background="#drawable/blue_pill"
android:layout_marginRight="15dp"
android:layout_marginTop="10dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_gravity="fill"
android:text="Save Title"
android:textSize="12sp"
android:textColor="#2196F3"/>
</RelativeLayout>
<RelativeLayout
android:id="#+id/cancel_title_btn"
android:layout_width="84dp"
android:layout_height="32dp"
android:clickable="true"
android:layout_alignParentRight="true"
android:background="#drawable/grey_700_pill"
android:layout_marginRight="105dp"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Cancel"
android:layout_gravity="fill"
android:textSize="12sp"
android:textColor="#616161" />
</RelativeLayout>
</RelativeLayout>
</RelativeLayout>
If you check the definition of onFocusChange(View v, boolean hasFocus) you will notice that your code must change like this:
pageTitle.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View view, boolean hasFocus) {
if(!hasFocus) {
pageTitle.setCursorVisible(true);
saveCancelBar.setVisibility(View.VISIBLE);
} else {
pageTitle.setCursorVisible(false);
saveCancelBar.setVisibility(View.GONE);
}
}
});
This is the code for checking the soft keyboard visibility in Android
public class MainActivity extends AppCompatActivity
{
public static String TAG = MainActivity.class.getSimpleName();
private TextView saveCancelBar;
private EditText pageTitle;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setListenerToRootView();
saveCancelBar = (TextView) findViewById(R.id.cancel_bar);
pageTitle = (EditText) findViewById(R.id.title);
pageTitle.setOnFocusChangeListener(new View.OnFocusChangeListener()
{
#Override
public void onFocusChange(View view, boolean hasFocus)
{
...
}
});
}
public void setListenerToRootView()
{
final View activityRootView = getWindow().getDecorView().findViewById(android.R.id.content);
activityRootView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener()
{
#Override
public void onGlobalLayout()
{
Rect r = new Rect();
activityRootView.getWindowVisibleDisplayFrame(r);
int screenHeight = activityRootView.getRootView().getHeight();
// r.bottom is the position above soft keypad or device button.
// if keypad is shown, the r.bottom is smaller than that before.
int keypadHeight = screenHeight - r.bottom;
Log.e(TAG, "keypadHeight = " + keypadHeight);
if (keypadHeight > screenHeight * 0.15) {
// 0.15 ratio is perhaps enough to determine keypad height.
Toast.makeText(getApplicationContext(), "Gotcha!!! softKeyboard open", Toast.LENGTH_SHORT).show();
} else {
// keyboard is closed
Toast.makeText(getApplicationContext(), "Gotcha!!! softKeyboard closed", Toast.LENGTH_SHORT).show();
saveCancelBar.setVisibility(View.GONE);
}
}
});
}
}
I think you forgot to add focusableInTouchMode. Add focusableInTouchMode to your editText to get focused on touching the view.
android:focusableInTouchMode="true"

How to change the focus from last edit text field to a button on clicking done in soft input?

I want to change the focus from edit-text field to a submit button when done is clicked on the soft input.
I've the following code :
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingTop="5sp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Driver + Conductor :"
android:textSize="17sp"
android:textStyle="bold" />
<EditText
android:id="#+id/bus_driver_above60"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_weight="1"
android:hint="Driver"
android:inputType="numberDecimal"
android:textAlignment="center"
android:textColor="#000"
android:nextFocusDown="#+id/bus_conductor_above60"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="2dp"
android:text="+"
android:textSize="17sp"
android:textStyle="bold" />
<EditText
android:id="#+id/bus_conductor_above60"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="2dp"
android:layout_weight="1"
android:hint="Conductor"
android:inputType="numberDecimal"
android:textAlignment="center"
android:textColor="#000"
android:nextFocus="#+id/bus_above60_btn"/>
</LinearLayout>
<Button
android:id="#+id/bus_above60_btn"
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="Calculate"
android:textSize="17sp"
android:textColor="#ffffff"
android:textStyle="bold"
android:background="#3498DB"/>
Here in the code,
android:nextFocusDown="#+id/bus_conductor_above60"
changes the focus from Driver Edit text field to Conductor Edit text field.
But after Conductor Edit text field, there is a Calculate button - bus_above60_btn.
android:nextFocusDown="#+id/bus_above60_btn"
is not changing the focus from Conductor Edit text field to the button below that field.
How do I achieve this?
If you just want to make button focused on Done action, first you must set your buttons focusableInTouchMode as true
<Button
android:id="#+id/bus_above60_btn"
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="Calculate"
android:focusableInTouchMode="true"
android:textSize="17sp"
android:textColor="#ffffff"
android:textStyle="bold"
android:background="#3498DB"/>
Now in your EditText's OnEditorActionListener, make button focused when done clicked:
bus_conductor_above60.setOnEditorActionListener(new TextView.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView textView, int action, KeyEvent keyEvent) {
if(action == EditorInfo.IME_ACTION_DONE)
bus_above60_btn.requestFocus();
return false;
}
});
<EditText
android:id="#+id/bus_conductor_above60"
android:imeOptions="actionDone"
/>
In your code
bus_above60_btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
sameFunction();
}
});
bus_conductor_above60 = findViewById(R.id.bus_conductor_above60);
bus_conductor_above60 .setOnEditorActionListener(new TextView.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_DONE) {
sameFunction();
}
return false;
}
});
Your code will work if all components are in same layout. So firstly include your button in same layout where other editTexts placed then try.
Afaik there are two ways for getting your goal
1. My layout is working exactly as you want:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:gravity="center"
android:orientation="vertical"
android:padding="15dp">
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="#+id/etUsername"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:maxLines="1"
android:hint="#string/username"
android:paddingLeft="10dp"
android:inputType="text"
android:paddingRight="10dp"
android:paddingStart="10dp"
android:paddingEnd="10dp" />
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:passwordToggleEnabled="true"
>
<EditText
android:id="#+id/etPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:maxLines="1"
android:hint="#string/password"
android:inputType="textPassword"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingStart="10dp"
android:paddingEnd="10dp" />
</android.support.design.widget.TextInputLayout>
<Button
android:id="#+id/btnLogin"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:background="#color/colorPrimary"
android:text="#string/login"
android:textColor="#color/white"
android:textSize="17sp" />
</LinearLayout>
The reason for it, from documentation, is the following:
Focus movement is based on an algorithm which finds the nearest
neighbor in a given direction. In rare cases, the default algorithm
may not match the intended behavior of the developer. In these
situations, you can provide explicit overrides by using these XML
attributes in the layout file [...]
So if you put the button in the same layout of the edittexts, it should manage the next focus automatically.
2. Manage the "next" click in the editText
Taken from this SO post
edittext.setOnKeyListener(new View.OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
if ((event.getAction() == KeyEvent.ACTION_DOWN) && (keyCode == KeyEvent.KEYCODE_ENTER)) {
Toast.makeText(HelloFormStuff.this, edittext.getText(), Toast.LENGTH_SHORT).show();
return true;
}
return false;
}
});
Hope this helps!

Hiding keyboard in fragments

I am having a fragment and in that fragment there is an add button which triggers a dialog when clicked . In that dialog I am having two edittext and two buttons. On click of save button dialog is dismissed. Now problem is that when I click on save button edit text keyboard closes and another keyboard pop up.(Edit text keyboard type is number while that popup after is alphabetical).I want to close this keyboard therefore I tried configChanges and this method
public static void hideKeyboard(Context ctx) {
InputMethodManager inputManager = (InputMethodManager) ctx
.getSystemService(Context.INPUT_METHOD_SERVICE);
// check if no view has focus:
View v = ((Activity) ctx).getCurrentFocus();
if (v == null)
return;
inputManager.hideSoftInputFromWindow(v.getWindowToken(), 0);
}
But none of this method works and I am also not having any edit text in fragment from which dialog appears.
dialog.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/lin_add_dns">
<TextView
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="ADD DNS"
android:gravity="center"
android:background="#drawable/add_dns_title"
android:textColor="#ffffff"
android:textStyle="bold"
android:textSize="20sp"
android:id="#+id/txt_add_dns"/>
<EditText
android:layout_width="200dp"
android:layout_height="wrap_content"
android:hint="DNS NAME"
android:layout_marginLeft="20dp"
android:layout_marginTop="30dp"
android:layout_marginRight="20dp"
android:id="#+id/et_name"
android:layout_gravity="center"
android:textColorHint="#999999"
android:textColor="#000"
android:focusableInTouchMode="true"/>
<EditText
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="30dp"
android:hint="IP Address"
android:inputType="number"
android:id="#+id/et_ip"
android:layout_gravity="center"
android:digits="0123456789."
android:textColorHint="#999999"
android:textColor="#000"
android:maxLength="15"
android:focusableInTouchMode="true"/>
<Button
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="30dp"
android:layout_marginBottom="20dp"
android:text="Submit"
android:background="#drawable/textview_click"
android:textColor="#ffffff"
android:id="#+id/btn_submit"/>
</LinearLayout>
fragment.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:background="#ccc"
android:focusableInTouchMode="true">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="wrap_content">
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:id="#+id/TopHeader"
android:text="Current WIFI Info"
android:paddingRight="15dp"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_marginTop="20dp"/>
<LinearLayout
android:layout_width="fill_parent"
android:layout_marginTop="10dp"
android:orientation="horizontal"
android:layout_height="wrap_content"
android:baselineAligned="false">
<LinearLayout
android:layout_width="0dp"
android:layout_weight=".50"
android:paddingRight="5dp"
android:orientation="vertical"
android:layout_height="wrap_content">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="right"
android:textAlignment="gravity"
android:id="#+id/WiFiConnectLeft"
android:text="Wifi Name :"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="right"
android:textAlignment="gravity"
android:id="#+id/DefaultGatewayLeft"
android:text="Default Gateway :"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="right"
android:textAlignment="gravity"
android:id="#+id/DNS1Left"
android:text="Primary DNS :"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="right"
android:textAlignment="gravity"
android:id="#+id/DNS2Left"
android:text="Secondary DNS :"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="right"
android:textAlignment="gravity"
android:id="#+id/CurrentIPLeft"
android:text="IP Address :"/>
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_weight=".50"
android:paddingLeft="5dp"
android:orientation="vertical"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left"
android:id="#+id/WiFiConnectedText"
android:textAlignment="gravity"
android:text=""/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/DefaultGatewayText"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/DNS1Text"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/DNS2Text"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/CurrentIPText"/>
</LinearLayout>
</LinearLayout>
-<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CHANGE DNS"
android:layout_gravity="center"
android:textAlignment="gravity"
android:id="#+id/MiddleHeaderText"
android:layout_marginTop="10dp"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textStyle="bold"
android:textColor="#000"
android:textSize="20sp"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_below="#+id/tv_title"
android:orientation="horizontal"
android:weightSum="3"
android:layout_marginTop="10dp"
android:id="#+id/lin_primary"
android:gravity="center_vertical"
android:paddingRight="15dp">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/primary"
android:textStyle="bold"
android:textColor="#000"
android:id="#+id/PrimaryDNSText"
android:gravity="center"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:textSize="13sp"/>
<TextView
android:layout_width="0dp"
android:layout_height="30dp"
android:layout_weight="1.2"
android:background="#000"
android:textColor="#ffffff"
android:inputType="numberDecimal"
android:layout_marginRight="10dp"
android:maxLength="16"
android:id="#+id/EDITDNS1"
android:gravity="center"
android:text=""/>
<Button
android:layout_width="0dp"
android:layout_height="30dp"
android:layout_weight="0.8"
android:text="BROWSE"
android:gravity="center"
android:background="#drawable/textview_click"
android:textColor="#ffffff"
android:id="#+id/BrowseDNS1"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_below="#+id/lin_primary"
android:orientation="horizontal"
android:weightSum="3"
android:layout_marginTop="10dp"
android:id="#+id/lin_secondary"
android:gravity="center_vertical"
android:paddingRight="15dp">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/secondary"
android:textColor="#000"
android:id="#+id/SecondaryDNSText"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:textSize="12sp"
android:gravity="center"/>
<TextView
android:layout_width="0dp"
android:layout_height="30dp"
android:layout_weight="1.2"
android:background="#000"
android:textColor="#ffffff"
android:inputType="numberDecimal"
android:layout_marginRight="10dp"
android:maxLength="16"
android:id="#+id/EDITDNS2"
android:gravity="center"
android:text=""
/>
<Button
android:layout_width="0dp"
android:layout_height="30dp"
android:layout_weight="0.8"
android:text="BROWSE"
android:gravity="center"
android:background="#drawable/textview_click"
android:textColor="#ffffff"
android:id="#+id/BrowseDNS2"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_marginTop="15dp"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_weight="3">
<Button
android:layout_width="0dp"
android:layout_height="40dp"
android:text="ADD DNS"
android:id="#+id/btn_add"
android:textColor="#ffffff"
android:background="#drawable/textview_click"
android:gravity="center"
android:layout_marginLeft="15dp"
android:layout_weight="1"/>
<Button
android:layout_width="0dp"
android:layout_height="40dp"
android:id="#+id/UpdateDNS"
android:text="Update"
android:background="#drawable/textview_click"
android:textColor="#ffffff"
android:layout_marginLeft="20dp"
android:layout_weight="1"/>
<Button
android:layout_width="0dp"
android:layout_height="40dp"
android:id="#+id/ResetWifi"
android:text="RESET"
android:background="#drawable/textview_click"
android:textColor="#ffffff"
android:layout_marginLeft="20dp"
android:layout_marginRight="15dp"
android:layout_weight="1"
/>
</LinearLayout>
</LinearLayout>
</ScrollView>
</LinearLayout>
Dialog java code
addDns.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
android.support.v7.app.AlertDialog.Builder builder;
final android.support.v7.app.AlertDialog alertDialog;
TextView txt_add_dns;
Button btn_submit;
final EditText et_name, et_dns;
LayoutInflater inflater = getActivity().getLayoutInflater();
final View layout = inflater.inflate(R.layout.add_dns_dialog, (ViewGroup) getActivity().findViewById(R.id.lin_add_dns));
builder = new android.support.v7.app.AlertDialog.Builder(ctx);
builder.setView(layout);
alertDialog = builder.create();
alertDialog.show();
alertDialog.getWindow().setBackgroundDrawableResource(R.drawable.dialog_bg);
txt_add_dns = (TextView) layout.findViewById(R.id.txt_add_dns);
et_name = (EditText) layout.findViewById(R.id.et_name);
et_dns = (EditText) layout.findViewById(R.id.et_ip);
Fonts.setHelveticaFont(ctx, txt_add_dns);
btn_submit = (Button) layout.findViewById(R.id.btn_submit);
Fonts.setHelveticaFont(ctx, btn_submit);
InputFilter[] filters = new InputFilter[1];
filters[0] = new InputFilter() {
public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
if (end > start) {
String destTxt = dest.toString();
String resultingTxt = destTxt.substring(0, dstart) + source.subSequence(start, end) + destTxt.substring(dend);
if (!resultingTxt.matches("^\\d{1,3}(\\.(\\d{1,3}(\\.(\\d{1,3}(\\.(\\d{1,3})?)?)?)?)?)?")) {
return "";
} else {
String[] splits = resultingTxt.split("\\.");
for (int i = 0; i < splits.length; i++) {
if (Integer.valueOf(splits[i]) > 255) {
return "";
}
}
}
}
return null;
}
};
et_dns.setFilters(filters);
btn_submit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Fonts.setHelveticaFont(ctx, et_name);
Fonts.setHelveticaFont(ctx, et_dns);
et_dns.setRawInputType(InputType.TYPE_CLASS_NUMBER);
if (!et_name.getText().toString().equalsIgnoreCase("") && !et_dns.getText().toString().equalsIgnoreCase("")) {
String name = et_name.getText().toString();
String dns = et_dns.getText().toString();
IPAddressValidator iptester = new IPAddressValidator();
boolean valid = iptester.validate(dns);
if (valid) {
dataBaseHelper.addUserDns(name, dns);
// dnsListAdapter.notifyDataSetChanged();
alertDialog.dismiss();
hideKeyboard(getActivity());
} else {
Toast.makeText(ctx, "Invalid IP Address", Toast.LENGTH_LONG).show();
}
} else {
Toast.makeText(ctx, "Please enter IP Address and DNS Name", Toast.LENGTH_LONG).show();
}
}
});
}
});
What's the issue here??
**EDIT:**After checking for each edit text individually I found that the keyboard from first editext didn't closes itself.So I added focus change listener and when it loses focus I called hide Keyboard.
I debugged line by line and everything works perfectly, debugger also enters hide keyboard method but that code is not able to hide keyboard. Is there any other method for hiding keyboard in fragments??
Is this problem with context that is passed in hide keyboard method ??Can we get context of an alert dialog in android because when I debug it showing context of Main Activity as I am passing getActivity() as parameter .Is this the issue??
public static void hideSoftKeyboard(Activity activity) {
InputMethodManager inputMethodManager = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(), 0);
}
create this methos infragment and call this method where u want to hide keyboard in your fragment.
Hiding Keyboard using method doesn't work for me. I added the code directly in submit button and that worked for me. Here is what I added
InputMethodManager im = (InputMethodManager)getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
im.hideSoftInputFromWindow(et_name.getWindowToken(), 0);
InputMethodManager im1 = (InputMethodManager)getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
im1.hideSoftInputFromWindow(et_dns.getWindowToken(), 0);
Here et_dns and et_name are my edit text

how to scroll up layout when clicking on edit text

this is my layout with soft input mode and without soft input mode.
But main problem is when i click next from second last edit text, i want to show those two buttons and last edit text with soft input mode. Please help
my xml file is as follows
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/scroll_bg" >
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="521dp"
android:layout_marginBottom="60dp"
android:background="#drawable/bg" >
<com.deemtech.widgets.MenuLayout
android:id="#+id/menuTitle"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/txtCustomerDetails"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="140dp"
android:gravity="center"
android:text="#string/customer_details"
android:textColor="#ffffff"
android:textSize="12sp" />
<EditText
android:id="#+id/editNameCustomerDetails"
style="#style/EditText"
android:layout_below="#+id/txtCustomerDetails"
android:layout_centerHorizontal="true"
android:layout_marginTop="12dp"
android:ems="10"
android:hint="NAME"
android:inputType="text" />
<RelativeLayout
android:id="#+id/innerLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginLeft="25dp"
android:layout_marginRight="25dp"
android:layout_marginTop="190dp"
android:background="#drawable/bg_create_account1x"
android:gravity="center"
android:paddingBottom="5dp"
android:paddingLeft="25dp"
android:paddingRight="25dp"
android:paddingTop="8dp" >
<EditText
android:id="#+id/editEmailCustomerDetails"
style="#style/EditText"
android:layout_alignLeft="#+id/editPhoneCutomerDetails"
android:layout_alignParentTop="true"
android:layout_marginTop="52dp"
android:ems="10"
android:hint="EMAIL ADDRESS"
android:inputType="textEmailAddress" />
<EditText
android:id="#+id/editPhoneCutomerDetails"
style="#style/EditText"
android:layout_below="#+id/editEmailCustomerDetails"
android:layout_marginTop="8dp"
android:ems="10"
android:hint="CONTACT NUMBER"
android:maxLength="12"
android:inputType="phone" />
<!--
<EditText
android:id="#+id/editAdditionalInfo"
android:layout_width="150dp"
android:layout_height="50dp"
android:layout_alignLeft="#+id/editPhoneCutomerDetails"
android:layout_alignRight="#+id/editPhoneCutomerDetails"
android:layout_below="#+id/editPhoneCutomerDetails"
android:layout_marginBottom="60dp"
android:layout_marginTop="10dp"
android:background="#drawable/message_additional_box1x"
android:ems="10"
android:hint="ADDITIONAL INFO"
android:inputType="text"
android:padding="10dp"
>
<requestFocus />
</EditText>
-->
<EditText
android:id="#+id/editAdditionalInfo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/editPhoneCutomerDetails"
android:layout_alignParentBottom="true"
android:layout_alignRight="#+id/editPhoneCutomerDetails"
android:layout_below="#+id/editPhoneCutomerDetails"
android:layout_marginBottom="60dp"
android:layout_marginTop="8dp"
android:hint="ADDITIONAL INFO"
android:background="#drawable/message_additional_box1x"
android:ems="10"
android:singleLine="true"
android:gravity="top"
android:lines="3"
android:padding="10dp"
android:width="150dp" >
<requestFocus />
</EditText>
</RelativeLayout>
<Button
android:id="#+id/btnBackCustomerDetails"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="448dp"
android:layout_toLeftOf="#+id/btnSubmitCustomerDetails"
android:background="#drawable/btn_back"
android:contentDescription="#string/app_name" />
<Button
android:id="#+id/btnSubmitCustomerDetails"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="#+id/editNameCustomerDetails"
android:layout_marginTop="448dp"
android:background="#drawable/btn_submit"
android:contentDescription="#string/app_name" />
</RelativeLayout>
</ScrollView>
In your case you cant simply "adjustResize" the window. Also as you only want to show the buttons when the last edit text is clicked.
So you can try this approach, not truly suggested but will work for your scenario:
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mainf);
findViewById(R.id.editAdditionalInfo).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(sampleMediaPlayer.this, "text", 700).show();
findViewById(R.id.btnBackCustomerDetails).requestFocusFromTouch();
findViewById(R.id.editAdditionalInfo).requestFocusFromTouch();
}
});
}
check this link
or try this android:windowSoftInputMode="adjustResize" in your activity in manifest file.
with the help of nimish's answer i finally got wat i wanted
editPhoneCustomerDetails.setOnKeyListener(new OnKeyListener() {
#Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if ((event.getAction() == KeyEvent.ACTION_DOWN) &&
(keyCode == KeyEvent.KEYCODE_ENTER)) {
findViewById(R.id.btnBackCustomerDetails).requestFocusFromTouch();
findViewById(R.id.editAdditionalInfo).requestFocusFromTouch();
//((ScrollView) findViewById(R.id.scrollView)).fullScroll(View.FOCUS_DOWN);
//findViewById(R.id.editPhoneCutomerDetails).requestFocusFromTouch();
// findViewById(R.id.btnBackCustomerDetails).requestFocusFromTouch();
}
return false;
}
});
findViewById(R.id.editAdditionalInfo).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//findViewById(R.id.btnBackCustomerDetails).setFocusable(true);
findViewById(R.id.btnBackCustomerDetails).requestFocusFromTouch();
findViewById(R.id.editAdditionalInfo).requestFocusFromTouch();
}
});

Android: Why does my EditText keep its value, even on orientation change?

this is a question out of curiosity.
I recently just learned that i have to care about keeping my activity state on orientation changes myself.
But in one of my Layouts, the Edittext just keeps its value and i do not get why?
Obviously i would like to reproduce this behaviour for other EditTexts...
Heres the Layout, the edittext is android:id="#+id/createlistactivity_listname_edittext"
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:id="#+id/linearLayout2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#android:drawable/title_bar"
android:gravity="center"
android:text="Enter a Name"
android:textAppearance="?android:attr/textAppearanceLarge" />
<EditText
android:id="#+id/createlistactivity_listname_edittext"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
android:hint="Name..."
android:singleLine="true" >
</EditText>
</LinearLayout>
<LinearLayout
android:id="#+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical" >
<TextView
android:id="#+id/CreateListActivity_HeaderLabel"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#android:drawable/title_bar"
android:gravity="center"
android:text="Add Products to your new List"
android:textAppearance="?android:attr/textAppearanceLarge" />
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:choiceMode="multipleChoice"
android:footerDividersEnabled="true" >
</ListView>
<TextView
android:id="#android:id/empty"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingLeft="8dp"
android:paddingRight="8dp"
android:text="No Products available. Insert some Products in the Products menu first" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:id="#+id/CreateListActivity_BottomBar"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:weightSum="2"
style="#android:style/ButtonBar" >
<ImageButton
android:id="#+id/CreateListActivity_SaveButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:background="#android:color/transparent"
android:onClick="saveButtonClicked"
android:src="#drawable/tick_32" />
<ImageButton
android:id="#+id/CreateListActivity_CancelButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:background="#android:color/transparent"
android:onClick="cancelButtonClicked"
android:src="#drawable/block_32_2" />
</LinearLayout>
</LinearLayout>
and here is the onCreate for the activity
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.createlistactivity_layout);
_selectedItems = new HashMap<Integer, Integer>();
_productDAO = new ProductDAO(getApplication());
_listDAO = new ListDAO(getApplication());
_productCursor = _productDAO.getAllProductsCursor();
startManagingCursor(_productCursor);
setUpListView();
final EditText listNameEditText = (EditText)this.findViewById(R.id.createlistactivity_listname_edittext);
listNameEditText.setOnEditorActionListener(new OnEditorActionListener() {
public boolean onEditorAction(TextView v, int actionId,
KeyEvent event) {
if (event != null&& (event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) {
InputMethodManager in = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
in.hideSoftInputFromWindow(listNameEditText
.getApplicationWindowToken(),
InputMethodManager.HIDE_NOT_ALWAYS);
}
return false;
}
});
}
this is the code of another activity which contains edittexts that do not preserve their state
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/RelativeLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TableLayout
android:id="#+id/TableLayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_below="#id/productImage"
android:background="#android:drawable/alert_light_frame" >
<TableRow
android:id="#+id/productview_toprow"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="top" >
<EditText
android:id="#+id/productview_productname"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="2"
android:hint="Product Name"
android:singleLine="true" >
<requestFocus />
</EditText>
</TableRow>
<TableRow
android:id="#+id/tableRow2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/textView1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:layout_weight="1"
android:gravity="center_vertical|center_horizontal"
android:text="#string/productview_pricelabel"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#android:color/black" />
<EditText
android:id="#+id/productview_price_edittext"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="0.0"
android:singleLine="true"
android:inputType="numberDecimal" />
</TableRow>
<TableRow
android:id="#+id/tableRow3"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/textView2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center_vertical|center_horizontal"
android:text="#string/productview_unitlabel"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#android:color/black" />
<EditText
android:id="#+id/productview_unit_edittext"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="kg|ml|..." />
</TableRow>
</TableLayout>
and the onCreate...
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.productview);
_productName = (EditText)findViewById(R.id.productview_productname);
_productPrice = (EditText)findViewById(R.id.productview_price_edittext);
_productUnit = (EditText)findViewById(R.id.productview_unit_edittext);
_productImage = (ImageButton)findViewById(R.id.productImage);
_productDAO = new ProductDAO(getApplication());
//set data from database
_product = _productDAO.getProduct(getIntent().getExtras().getString(DataBaseKeys.PRODUCT_NAME));
if(_product != null)
{
_productName.setText(_product.getName());
_productImage.setImageBitmap(_product.getIcon());
_productPrice.setText(""+_product.getPrice());
_productUnit.setText(_product.getUnit());
_productIcon = _product.getIcon();
}
}
Well the Views in android do keep some of its state. Like EditText keep its text so you dont have to save it onSaveInstanceState. Other examples are ListView keep the scroll position. EditText also keeps scroll position if it multi-line text.
But some of the thing a user has to takecare like if you are having a Note App, you have to store Note ID which is being edited so that on orientation change you know what you are working on.
This is a standard way of Android, nothing unusual here. For more details refer.
Saving Activity State
Regarding one of your activity not behaving as aspected is because of code:
_product = _productDAO.getProduct(getIntent().getExtras().getString(DataBaseKeys.PRODUCT_NAME));
if(_product != null)
{
_productName.setText(_product.getName());
_productImage.setImageBitmap(_product.getIcon());
_productPrice.setText(""+_product.getPrice());
_productUnit.setText(_product.getUnit());
_productIcon = _product.getIcon();
}
You are overriding the values of the edit text. Try this log statement before if statement.
Log.i("EDIT_NOT_WORKING", "VALUE OF EDIT: " + _productName.getText());
You should see that it will print the entered value.
If you don't give an id to your editText, it won't save the value on screen change. You have to give an id so that the value is saved, even if the screen changes orientation.

Categories

Resources