highlight in edittext when click Android - 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
}
}
});

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

How to highlight/focus two EditTexts in Android?

I have two EditTexts in an activity. I am trying to implement it so that when one EditText is focused, the second one looks like it is focused too (the underline must be bold as it is when EditText is focused).
I am trying to change underline like this when focus changes:
editText1.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if (!hasFocus) {
// here editText2 must have not state
} else {
// here editText2 must have not state
}
}
});
But how can I make the underline bold on another text without it actually receiving focus?
Try this
editText1.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if (!hasFocus) {
editText2.setTypeface(Typeface.NORMAL);
} else {
editText2.setTypeface(Typeface.DEFAULT_BOLD);
}
}
});

Force Edit Text to request focus programmatically

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);
}
});
}

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.

Categories

Resources