I have a form with a lot of ViewSwitcher that change a TextView to an EditText with a clic.
The thing is I must clic 2 times to be able to insert value : Here are the screenshots :
The good thing would be not passing by the second state....
Here is my code.
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|right"
android:text="Colonia:"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#color/android_blue" />
<ViewSwitcher
android:id="#+id/vs2"
android:layout_width="match_parent"
android:layout_height="50dp"
android:gravity="center_vertical" >
<TextView
android:id="#+id/txt_colonia"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:clickable="true"
android:onClick="TextViewClicked"
android:paddingLeft="12dp"
android:text="Colonia"
android:textAppearance="?android:attr/textAppearanceLarge" />
<EditText
android:id="#+id/et_colonia"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:focusableInTouchMode="true"
android:text="Colonia 13"
android:textAppearance="?android:attr/textAppearanceLarge" />
</ViewSwitcher>
</TableRow>
And here is the java part :
switcher = (ViewSwitcher) findViewById(switchId);
View newView = switcher.getNextView();
if (newView instanceof TextView) {
((TextView) newView).setText(valor);
} else if (newView instanceof EditText) {
((EditText) newView).setText(valor);
((EditText) newView).setFocusableInTouchMode(true);
((EditText) newView).requestFocus();
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
}
switcher.showNext();
Try switching to the edittext and then formatting it,
viewswitcher.showNext();
edittext.requestFocus();
edittext.setSelection(edittext.getText().length());
Try this:
If you are in a fragment
switcher.showNext();
edittext.requestFocus();
InputMethodManager inputMethodManager = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.showSoftInput(edittext, 1);
If you are in an Activity
switcher.showNext();
edittext.requestFocus();
InputMethodManager inputMethodManager = (InputMethodManager) getApplicationContext().getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.showSoftInput(edittext, 1);
A little late, but might help someone else...
Related
This layout is used for popup window to change name of a textview.But the edittext doesnt show keyboard even on clicking the edittext. I'm using it in a fragment. fragment has a namefield which is meant to be changed on clicking OK in this popup.
This is the xml file for popup window.
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#fff">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_alignParentBottom="true"
android:id="#+id/name_status_btns"
>
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:elevation="2dp"
android:background="#fff"
android:text="CANCEL"
android:textSize="18sp"
android:textColor="#000"
android:id="#+id/cancel_change_name"/>
<TextView
android:layout_width="1dp"
android:layout_height="match_parent"
android:background="#b1b0b0"/>
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="OK"
android:textColor="#000"
android:textSize="18sp"
android:elevation="2dp"
android:background="#fff"
android:id="#+id/ok_change_name"
/>
</LinearLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#b1b0b0"
android:layout_above="#id/name_status_btns"
android:id="#+id/name_status_horizontal_liner"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_marginTop="15dp"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:id="#+id/name_status_edit_field">
<EditText
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="5"
android:hint="write your name here"
android:scrollHorizontally="true"
android:id="#+id/change_name"
android:focusable="true"
android:focusableInTouchMode="true"/>
</RelativeLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#color/mainColor"
android:layout_below="#id/name_status_edit_field"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
/>
</RelativeLayout>
This is the java file for above xml.
public class Name_Status extends AppCompatActivity implements View.OnClickListener {
EditText name_change;
RelativeLayout name_status_edit_field;
String name;
Button cancel_name_change , ok_name_change;
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.name_status);
DisplayMetrics dm=new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
int width=dm.widthPixels;
int height=dm.heightPixels;
getWindow().setLayout((int)(width*.8),(int)(height*.3));
name_change=(EditText)findViewById(R.id.change_name);
name_status_edit_field=(RelativeLayout)findViewById(R.id.name_status_edit_field);
name=name_change.getText().toString();
cancel_name_change=(Button)findViewById(R.id.cancel_change_name);
ok_name_change=(Button)findViewById(R.id.ok_change_name);
ok_name_change.setOnClickListener(this);
name_change.setOnClickListener(this);
name_status_edit_field.clearFocus();
name_change.setFocusable(true);
//name_change.update();
}
#Override
public void onClick(View v) {
if(v.getId()==R.id.ok_change_name)
{FragActivity1 obj=new FragActivity1();
obj.changeName(name);
Intent intent=new Intent();
intent.setClass(Name_Status.this , MainActivity.class);
startActivity(intent);}
if(v.getId()==R.id.change_name)
{
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInputFromWindow(name_change.getApplicationWindowToken(), InputMethodManager.SHOW_FORCED, 0);
}
}
}
The main problem is your EditText has a 0dp width and a layout_weight attribute in a RelativeLayout, which does not make any sense.
Either make the parent of the EditText a LinearLayout or remove the layout_weight attribute from the EditText and give it a proper width:
<EditText
android:id="#+id/change_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="write your name here"
android:scrollHorizontally="true"
android:focusable="true"
android:focusableInTouchMode="true"/>
The following code is completely unnecessary, you should just remove it from onClick():
if(v.getId()==R.id.change_name)
{
InputMethodManager imm = (InputMethodManager) getSystemService(Context.
INPUT_METHOD_SERVICE);
imm.toggleSoftInputFromWindow(name_change.getApplicationWindowToken(),
InputMethodManager.SHOW_FORCED, 0);
}
You can also try the following for EditText
editText.setOnFocusChangeListener(new OnFocusChangeListener() {
#Override
public void onFocusChange(final View v, final boolean hasFocus) {
if (hasFocus && editText.isEnabled() && editText.isFocusable())
editText.post(new Runnable() {
#Override
public void run() {
final InputMethodManager imm = (InputMethodManager) context
.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT);
}
});
}
});
For more details refer Keyboard not shown when i click on edittextview in android?
EditText is not suppose to have 0dp width and layout_weight inside a RelativeLayout. Weight works inside LinearLayout. So change your EditText to below:
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="write your name here"
android:scrollHorizontally="true"
android:id="#+id/change_name"
android:focusable="true"
android:focusableInTouchMode="true"/>
And change your OnClick to because EditText will take care of click by itself:
#Override
public void onClick(View v) {
if(v.getId()==R.id.ok_change_name){
FragActivity1 obj=new FragActivity1();
obj.changeName(name);
Intent intent=new Intent();
intent.setClass(Name_Status.this , MainActivity.class);
startActivity(intent);
}
}
And finally remove the below line from your OnCreate() method:
name_change.setOnClickListener(this);
Because EditText will open keyboard by itself when clicked.
This question already has answers here:
Android: softkeyboard not showing up
(5 answers)
Closed 6 years ago.
I am newbie to android and working on a demo app,In that i am having an activity contains an edittext at the top of the screen and want to show the keyboard on the start of that activity i have tried many ways but none of this works,Can anybudy help me to sort it ot?
layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/linearLayout"
android:orientation="vertical" >
<RelativeLayout
android:id="#+id/rl_hdr"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#16BBA1"
android:gravity="center_vertical"
android:padding="10dp">
<ImageView
android:id="#+id/iv_back"
android:layout_width="25dp"
android:layout_height="22dp"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="0dp"
android:src="#drawable/ic_back_white" />
<EditText
android:layout_width="fill_parent"
android:layout_height="40dp"
android:textColorHint="#939393"
android:singleLine="true"
android:layout_centerInParent="true"
android:hint="Search"
android:layout_marginLeft="5dp"
android:padding="5dp"
android:gravity="center_vertical"
android:drawablePadding="5dp"
android:layout_marginRight="5dp"
android:textSize="16dp"
android:imeOptions="actionDone"
android:cursorVisible="true"
android:layout_toRightOf="#+id/iv_back"
android:drawableRight="#drawable/ic_search"
android:background="#drawable/bg_et_actionbar"
android:id="#+id/et_search" />
</RelativeLayout>
<FrameLayout
android:id="#+id/fragmentTimeline"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:layout_marginTop="0dp" />
</LinearLayout>
manifest.xml
<activity
android:name="one.tusk.stush.SearchPostActivity"
android:label="Back"
android:parentActivityName="one.tusk.stush.activities.MainActivity"
android:screenOrientation="portrait"
android:windowSoftInputMode="stateAlwaysVisible" >
>
</activity>
SerachPostActivity.java
public class SearchPostActivity extends FragmentActivity implements OnQueryTextListener, OnItemClickListener {
public EditText et_search;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.search_post);
.
.
.
.
et_search = (EditText)findViewById(R.id.et_search);
InputMethodManager inputMethodManager=(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.toggleSoftInputFromWindow(linearLayout.getApplicationWindowToken(), InputMethodManager.SHOW_FORCED, 0);
et_search.setOnEditorActionListener(new TextView.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if ((event != null && (event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) || (actionId == EditorInfo.IME_ACTION_DONE)) {
Log.i("Done pressed", "Enter pressed");
search.searchPost(et_search.getText().toString().trim());
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
}
return false;
}
});
et_search.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus){
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(et_search, InputMethodManager.SHOW_FORCED);
} else {
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
}
}
});
You have to give focus to EditText.Try
android:focusable="true"
in your EditText and it will open the soft keyboard.
Try changing your manifest to
android:windowSoftInputMode="stateVisible"
instead of
android:windowSoftInputMode="stateAlwaysVisible".
If problem persists,try opening it programatically using
InputMethodManager imm = (InputMethodManager)getSystemService(
Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(mEditText, 0);
You may also refer This answer on SO
In my form activity, I've 3 fields. After confirming the form to the server it finishes and go to the previous activity. But the problem , After finish the keyboard is still pop up and the previous activity has no input field.
Here is the layout file.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:background="#color/colorWhite"
android:layout_width="match_parent"
android:layout_height="match_parent">
<include layout="#layout/toolbar"/>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_gravity="center"
android:layout_margin="20dp"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<sslwireless.com.easycorporate.Fonts.LoraBold
android:layout_marginBottom="20dp"
android:text="#string/change_password_title"
android:textSize="25sp"
android:textColor="#color/colorAccent"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<EditText
android:id="#+id/oldPassword"
android:background="#drawable/super_white_style"
android:padding="12dp"
android:layout_marginTop="15dp"
android:singleLine="true"
android:inputType="textPassword"
android:hint="#string/hints_old_password"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<EditText
android:id="#+id/newPassword"
android:background="#drawable/super_white_style"
android:padding="12dp"
android:layout_marginTop="15dp"
android:singleLine="true"
android:inputType="textPassword"
android:hint="#string/hints_new_password"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<EditText
android:id="#+id/confirmPassword"
android:background="#drawable/super_white_style"
android:padding="12dp"
android:layout_marginTop="15dp"
android:singleLine="true"
android:inputType="textPassword"
android:hint="#string/hints_confirm_password"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="#+id/changePassword"
android:text="#string/change_password"
android:layout_marginTop="20dp"
android:textColor="#color/colorWhite"
android:background="#drawable/primary_dark_color_style"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</ScrollView>
</LinearLayout>
Here what actually happening.
i also set the android:windowSoftInputMode="stateHidden" for both activity in the manifest file. is there any way I can remove focus after go back to the previous activity ?
In onStop() of the finishing Activity, disable all the EditText fields like this in order to make the soft keyboard disappear:
EditText et = (EditText)findViewById(R.id.confirmPassword);
et.setEnabled(false);
You may force IME to close (before you finish() your activity):
public void hideSoftKeyboard(#NonNull Activity activity) {
IBinder windowToken = activity.getWindow().getDecorView().getRootView().getWindowToken();
InputMethodManager imm = (InputMethodManager)activity.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(windowToken, 0);
}
Here's something that actually fixed an identical bug today.
#Override
public void closeKeyboard(View view) {
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
//Log.d("RKZ", "isKeyboardShown("+((ViewGroup) findViewById(android.R.id.content)).getChildAt(0)+") = "+isKeyboardShown(((ViewGroup) findViewById(android.R.id.content)).getChildAt(0)));
if(view != null && Util.isSoftKeyboardShown(imm, view)) {
Util.hideKeyboard(ActivityASG.this, view);
}
}
And in your CHANGE PIN Button's onClick() just call it like this:
//close keyboard
callback.closeKeyboard(noteInput);
callback.closeKeyboard(mailInput);
//callback.closeKeyboard(yourCHANGE_PINview);
call below code for close keybord , call when click on button
InputMethodManager imk = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
if (imk.isActive()){
imk.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0); // hide key board
}
I have an edittext view and button , Edit text has some default value.
when i click on button i give focus to the edit text. but problem is cursor is at the start of edit text. how to place cursor after the default text.
what i did.
mobile_text =(EditText)findViewById(R.id.mobile_text);
mobile_edit = (ImageView)findViewById(R.id.mobile_edit_icon);
mobile_edit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mobile_text.setEnabled(true);
mobile_text.requestFocus();
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
}
});
My layout.xml file
<RelativeLayout
android:paddingTop="#dimen/pad_3dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="#dimen/pad_5dp"
android:paddingBottom="#dimen/pad_5dp"
>
<TextView
android:id="#+id/mobile"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="MOBILE"
android:paddingLeft="2dp"
android:textSize="#dimen/txt_12sp"
/>
<EditText
android:paddingTop="#dimen/pad_5dp"
android:layout_below="#+id/mobile"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#android:color/transparent"
android:id="#+id/mobile_text"
android:text="9581129423"
/>
<ImageView
android:clickable="true"
android:id="#+id/mobile_edit_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/edit"
android:layout_below="#+id/mobile"
android:layout_alignParentRight="true"
android:gravity="right|end"
/>
</RelativeLayout>
Google Docs here : EditText - setSelection, Selection - setSelection
add this line
mobile_text.setSelection(mobile_text.getText().length());
I can't seem to get this work. I already set popWindow focusable as to what I read on other forums but still no luck.
xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:adjustViewBounds="true"
android:background="#drawable/popbg"
android:orientation="vertical" >
<Button
android:id="#+id/cancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_marginRight="10dp"
android:layout_marginTop="30dp"
android:background="#drawable/zcancel" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginLeft="10dp"
android:text="SSID"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="#+id/editText3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName" />
java
case(R.id.settings):
switch (event.getAction())
{
case MotionEvent.ACTION_DOWN:
v.setBackgroundResource(R.drawable.cpanel2);
return true;
case MotionEvent.ACTION_UP:
v.setBackgroundResource(R.drawable.cpanel1);
LayoutInflater layoutInflater =
(LayoutInflater)getBaseContext().getSystemService(LAYOUT_INFLATER_SERVICE);
View popSwitchView = layoutInflater.inflate(R.layout.settings_xml, null);
final PopupWindow popWindow = new PopupWindow(popSwitchView);
popWindow.setWidth(LayoutParams.MATCH_PARENT);
popWindow.setHeight(LayoutParams.MATCH_PARENT);
popWindow.showAtLocation(popSwitchView, Gravity.CENTER, 0, 0);
popWindow.setOutsideTouchable(false);
popWindow.setFocusable(true);
Drawable d = getResources().getDrawable(R.drawable.popbg);
popWindow.setBackgroundDrawable(d);
Button CancelButton = (Button)popSwitchView.findViewById(R.id.cancel);
CancelButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
popWindow.dismiss();
}
});
popWindow.showAsDropDown(v, 50, -30);
return true;
default:
return false;
}
I'm planning on creating a popwindow of setting for network configurations. I can't seem to fix my code for a good view for you guys .
ha, .found the answer.,
just did
popWindow.setFocusable(true);
popWindow.update();
thanks for the support!
credits from this topic
Keyboard not shown when i click on edittextview in android?
This worked for me:
popWindow.setFocusable(true);
popWindow.update();
Try adding this line before you show the popup:
popupWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
Nothing helped me, it still didn't appear on some devices, so I had to force showing it like that (kotlin):
val editText = customView.findViewById<EditText>(numberFieldId)
editText.onClick {
showKeyboard(editText, context)
}
private fun showKeyboard(mEtSearch: EditText, context: Context) {
mEtSearch.requestFocus()
val imm = context.getSystemService(Activity.INPUT_METHOD_SERVICE) as InputMethodManager
imm.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0)
}
Try this::
Programatically:
edittext.requestFocus();
Through xml:
<EditText
android:id="#+id/editText3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName">
<requestFocus />
</EditText>
Hope it Helps!!