Force Edit Text to request focus programmatically - android

I want to show a custom keypad layout if EditText has focus and after filling the data on button click hide the keypad layout. I'm using FocusChangeListener for this, but once the focus is lost, unable to recreate it.Is there any other way to attain this? My code for button event and editText focus change listener is as follows:
//Handling Focus change listener for edit Text
editText.setOnFocusChangeListener(new OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
linearKeypadLayout.setVisibility(View.VISIBLE);
} else {
linearKeypadLayout.setVisibility(View.INVISIBLE);
}
}
});
//Button click event
btn.setOnClickListener( new View.OnClickListener() {
#Override
public void onClick(View v) {
editText.setFocusable(false);
editText.setText(null);
}
});
}

Related

How can I clear the EditText with onClick when the cursor is focussed on another EditText?

I have two EditText widgets(editText1 and editText2) both has values populated by a button and the cursor is focussed on editText1. I have the listeners on both editText as shown in the code snippet below. With the cursor on editText1, it takes two clicks/touch to clear editText2. How can I clear editText2 with a single click/touch?
tipPercent.setOnClickListener(new EditText.OnClickListener(){
public void onClick(View view){
try{
tipPercent.setText("");
}
splitTip.setOnClickListener(new EditText.OnClickListener(){
public void onClick(View view){
try{
splitTip.setText("");
}
Screenshot of the EditText and click event
try this..
editText.setOnFocusChangeListener(new OnFocusChangeListener() {
#Override
public void onFocusChange(View view, boolean hasFocus) {
if (hasFocus) {
//clear the edittext
} else {
//set the value when hasFocus is not
}
}
});
Try this:
tipPercent.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if(hasFocus){
splitTip.setText("");
}
}
});
splitTip.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if(hasFocus){
tipPercent.setText("");
}
}
});
If i understand your question
you need to clear editText2 when its focus? then you need to use setOnFocusChangeListener when editText get focus or lost focus you may clear editText
editText2.setOnFocusChangeListener(new OnFocusChangeListener() {
#Override
public void onFocusChange(View view, boolean hasFocus) {
if (hasFocus) {
//clear editText when editext get focus
Toast.makeText(getApplicationContext(), "Got the focus", Toast.LENGTH_LONG).show();
} else {
//clear editText when editext lost focus
Toast.makeText(getApplicationContext(), "Lost the focus", Toast.LENGTH_LONG).show();
}
}
});
Try this //Kotlin
private fun editTextClear(currentET: EditText, targetET: EditText) {
currentET.setFocusableInTouchMode(true)
currentET.setFocusable(true)
targetET.clearComposingText()
targetET.setFocusable(false)
}
//Now call this method onClick()
first_et.setOnClickListener {
editTextClear(first_et, second_et)
}
second_et.setOnClickListener {
editTextClear(second_et, first_et)
}
If you try to set focusable="false" for second_et you can single click on second_et from first time only to empty first_et
Hope this solves your problem
Try This:
/*EditText1*/
tipPercent.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
tipPercent.setText("");
}
});
/*EditText2*/
splitTip.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
splitTip.setText("");
}
});
onFocusListener will work as this will allow you to clear the text in one click, as onClickListener is not made for this kind of purpose
Hope it helps

highlight in edittext when click Android

Now i have a problem about highlight in EditText.
- When i click the second time the highlight it will not work.
This my code
tv2.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
tv2.performLongClick();
}
});
I want to highlight every Onclick.
tv2.performLongClick();
pic2 highlight it not work
If you want to highlight text inside editText
Insted of tv2.performLongClick()
you can simply use setSelectAllOnFocus(true)
Set the TextView so that when it takes focus, all the text is
selected.
editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
editText.setSelectAllOnFocus(true);
if (!hasFocus) {
// noFocous
}
}
});

onClickListener for disabled edittext not called

I am trying to capture when an edittext field which is disabled gets pressed.
(I only want to change it, I dont want text inserted into it).
I assigned an onClickListener to it but it is not called.. Any suggestions ?
Is there any other event I can listen to ?
final EditText field = column.get(i);
field.setEnabled(false);
field.setClickable(true);
field.setFocusable(false);
field.setFocusableInTouchMode(false);
field.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
((EditText)v).setText("a");
}
});
Thanks !
You cant pass a click-event when a View is disabled.
Therefore, try changing:
field.setEnabled(false);
To:
field.setEnabled(true);
final EditText field = column.get(i);
field.setCursorVisible(false);
field.setOnFocusChangeListener(new OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
field.setText("something");
field.clearFocus();
}
}
});

Show keyboard in Dialog

I have this piece of code (RelativeLayout is a just one row inside my main layout, not important).
RelativeLayout cellphoneNumberLayout = (RelativeLayout) findViewById(R.id.cellphone_number);
cellphoneNumberLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
SettingsDialog myDialog = new SettingsDialog(Main.this);
myDialog.show();
}
});
Inside my custom Dialog (SettingsDialog) I have EditText and a Button. How can I force a keyboard to open immidiatelly when dialog is shown and focus on my (single) EditText field?
I tried with classic "forcing" which I found here on SO but this isn't activity, it's a dialog.
EDIT: I tried this but it's not working. Declared myDialog as class variable and added below myDialog.show();
myDialog.myEditTextField.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
myDialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
}
}
});
Nothing happens.
The following will bring up the keyboard for the editText when it is focused:
EditText editText;
editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View view, boolean focused)
{
if (focused)
{
dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
}
}
});
Then just set editText to focused:
editText.setFocusable(true);
editText.requestFocus();
The following will bring up the keyboard for the editText when it is focused particularly when you have custom Dialog/DialogFragment:
myDialog.myEditTextField.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
getDialog().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
}
}
});
In AndroidManifest.xml, you can add android:windowSoftInputMode="stateVisible" to the activity tag to automatically show the keyboard.

Android: Dialog box show soft keyboard automatically when focus is on an EditText not working

Android: show soft keyboard automatically when focus is on an EditText
I've read this post that automatically shows the virtual keyboard when a dialog box is shown. however, it's not working for me. any ideas why? eventhough the edit text is automatically on focus when the dialogbox appear, the event doesn't trigger. I've also read the onpostresume answer but I don't know how to apply it. any help is appreciated.
final Dialog dialog = new Dialog(ThesisI.this);
dialog.setContentView(R.layout.budget_dialog);
final EditText et = (EditText) dialog.findViewById(R.id.textComments);
final Button enter = (Button) dialog.findViewById(R.id.buttonEnter);
final Button cancel = (Button) dialog.findViewById(R.id.buttonCancel);
enter.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
/**cancel */
cancel.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
dialog.show();
et.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
}
}
});
however, i noticed that if I change focus to the button then focus again to the edit text. this event works, using this code below.
et.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
InputMethodManager inputMgr = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
inputMgr.toggleSoftInput(0, 0);
}
}
});
any idea on how to apply it?
What you can do is try using postDelayed(Runnable) for EditText as below,
ettext.requestFocus();
ettext.postDelayed(new Runnable() {
#Override
public void run() {
InputMethodManager keyboard = (InputMethodManager)
getSystemService(Context.INPUT_METHOD_SERVICE);
keyboard.showSoftInput(ettext, 0);
}
},200);
Just try adding the below line before "et.setOnFocusChangeListener"
((InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE))
.showSoftInput(et, 2);

Categories

Resources