In my app I have an EditText that I want to get the value out of when it looses focus. How should I do this?
Thanks!
Along the lines of this should work.
EditText.setOnFocusChangeListener(new View.OnFocusChangeListener()
{
#override
public void onFocusChange(View v, boolean hasFocus)
{
if (!hasFocus) {
string value = (EditText) v.getText().ToString();
}
}
}
Related
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
Can any of you show me the best way to change the TextSize attribute of an EditText just when the user has it selected (is writing)?
This is what i accomplished so far:
I've two EditText in a linear layout.
xml:
<EditText
android:textSize="20dp"
android:onClick="makeTextViewFocusedBigger"/>
<EditText
android:textSize="20dp"
android:onClick="makeTextViewFocusedBigger"/>
activity:
public void makeTextViewFocusedBigger(View view) {
((EditText) view).setTextSize(TypedValue.COMPLEX_UNIT_DIP, 25);
}
When the user select it it does become bigger, but obviously it does not go back to the original size when the user stop using it.
What would you do to realize this function?
Use View.OnFocusChangedListener
myEditText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
// Change size based on hasFocus boolean
}
});
Use setOnFocusChangeListener
Like This :
Your_Edittext.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if(hasFocus)
{
Your_Edittext.setTextSize(25); //increased size
}
else
{
Your_Edittext.setTextSize(15); //normal size
}
}
});
Do following :
XML:
<EditText
android:textSize="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/edit_text_first"/>
<EditText
android:textSize="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/edit_text_second"/>
Activity OnCreate:
EditText editFirst=findViewById(R.id.edit_first)
EditText editSecond=findViewById(R.id.edit_second)
editFirst.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
// Change size of text inside 1st EditText
}
});
editSecond.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
// Change size of text inside 2nd EditText
}
});
To implement it more general :
public class MainActivity extends AppCompatActivity implements View.OnFocusChangeListener{
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editFirst.setOnFocusChangeListener(this);
editSecond.setOnFocusChangeListener(this);
}
#Override
public void onFocusChange(View v, boolean hasFocus)
{
// Change text size of every EditText or for selective EditText. As You want
}
}
You need to implement View.OnFocusChangeListener(). This method is called when the focus state of a view has changed.
editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if(hasFocus)
{
// Change size when editText has focus
editText.setTextSize(30);
}else
{
// Change size when editText doesn't has focus
editText.setTextSize(15);
}
}
});
You have to implementView.OnFocusChangeListener() method , set the textview size according you want in onFocusChange()
myEditText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
// set the text size here
}
});
You need to addFocusChangedListener to your EditTexts. In them you can see if they are focused or not.
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);
}
}
});
I have three different Edit Text with restriction of entering only numeric values, thus tapping on any one of the Edit Text opens NUMERIC KEYBOARD.
I tried to implement setOnFocusChangeListener for all Edit Text which ensures that when user tap anywhere outside any of the Edit Text hide the keyboard.
editText1.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if (!hasFocus) {
hideKeyboard(v);
}
}
});
editText2.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if (!hasFocus) {
hideKeyboard(v);
}
}
});
editText3.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if (!hasFocus) {
hideKeyboard(v);
}
}
});
And here is the implementation of 'hideKeyBoard'
public void hideKeyboard(View view) {
InputMethodManager inputMethodManager =(InputMethodManager)getSystemService(Activity.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
This works fine, however, when I change focus from one EditText to another EditText, keyboard hides. This behaviour might be frustrating for user.
How can I deal with it and avoid unnecessary hiding of keyboard.
P.S I need to ensure that keyboard is numeric not alphabetical in any case.
Thank you MarianoZorrila for correcting me on my approach to the problem.Here are the changes I performed to solve this problem.
Gave an Id to my Relative Layout
<RelativeLayout
...
...
android:id="#+id/parent">
Modifications to Java File
View parent = findViewById(R.id.parent);
parent.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View view, boolean hasFocus) {
if(hasFocus){
hideKeyboard(view);
}
}
});
I have a number of EditText elements in my page along with two buttons. I want the user to touch on any one EditText field and click any button to insert a certain value into that very EditText field they touched. Giving input using the keypad is not allowed. Please help me to do this.
You can use View.OnFocusChangeListener to detect if any view (edittext) gained or lost focus.
for (EditText view : editList){
view.setOnFocusChangeListener(focusListener);
}
....
private OnFocusChangeListener focusListener = new OnFocusChangeListener() {
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus){
focusedView = v;
} else {
focusedView = null;
}
}
}
This goes in your activity or fragment or wherever you have the EditTexts. The .... is just saying that you can put it anywhere else in the class. And obviously you would need to create an array with them in it, thus the editList.
This works for me. It returns a boolean.
myEditText.hasFocus()
One thing that you can do is declare a global variable evalue which will tell you which is the last selected EditText by using onTouchListener and then based on the value of evalue, you can set the text value to the edittext by button click. hope you understood.
the code for it can be as follow:
EditText e1,e2;
Button b1,b2;
String evalue;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
e1=(EditText)findViewById(R.id.editText1);
e2=(EditText)findViewById(R.id.editText2);
b1=(Button)findViewById(R.id.button1);
b2=(Button)findViewById(R.id.button2);
e1.setOnTouchListener(new View.OnTouchListener()
{
public boolean onTouch(View arg0, MotionEvent arg1)
{
evalue="1";
return false;
}
});
e2.setOnTouchListener(new View.OnTouchListener()
{
public boolean onTouch(View arg0, MotionEvent arg1)
{
evalue="2";
return false;
}
});
b1.setOnClickListener(new View.OnClickListener()
{
public void onClick(View arg0) {
if(evalue=="1")
{
e1.setText("yes");
}
if(evalue=="2")
{
e2.setText("yes");
}
}
});
b2.setOnClickListener(new View.OnClickListener()
{
public void onClick(View arg0) {
if(evalue=="1")
{
e1.setText("No");
}
if(evalue=="2")
{
e2.setText("No");
}
}
});
}
Its a logical coding.. not upto the mark.. if you find a better one. then use it. thank you.
This worked for me.
e1 = (EditText) findViewById(R.id.editText1);
e1.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View arg0, boolean hasfocus) {
if (hasfocus) {
Log.e("TAG", "e1 focused")
} else {
Log.e("TAG", "e1 not focused")
}
}
});
With Java 8 lambdas:
EditText editTextTo = findViewById(R.id.editTextTo);
editTextTo.setOnFocusChangeListener((view, b) -> {
if (view.isFocused()) {
// Do whatever you want when the EditText is focused
// Example:
editTextFrom.setText("Focused!");
}
});
Note that the view inside the lambda function is actually an instance of an EditText.
Have your Fragment implement OnTouchListener & OnFocusChangeListener:
public class AttributesFragment extends Fragment
implements OnTouchListener, OnFocusChangeListener {
Then implement using:
#Override
public boolean onTouch(View view, MotionEvent event) {
if (view instanceof EditText) {
view.setOnFocusChangeListener(this); // User touched edittext
}
return false;
}
#Override
public void onFocusChange(View v, boolean hasFocus) {
}
Don't forget to set your listener after creating your EditText
editText.setOnTouchListener(this);
This way if an EditText box is focused by default but user has not changed the field it will not fire the onFocusChange event.
Simple Coding:
if(EditText1.isFocused()){
//EditText1 is focused
}else if(EditText2.isFocused()){
//EditText2 is focused
}
This worked for me using Java 8 (lambdas):
EditText myEditText = findViewById(R.id.editText);
myEditText.setOnFocusChangeListener((view, hasFocus) -> {
if(hasFocus){
Log.e("TAG", "myEditext has focus")
}else{
Log.e("TAG", "myEditext has no focus")
}
});
In your activity or fragment implement OnFocusChangeListener.
edittextNeme= (EditText) findViewById(R.id.edittextNeme);
edittextNeme.setOnFocusChangeListener(this);
You will have to check which view changed focus by getting the view id with view.getId() and handle accordingly.
#Override
public void onFocusChange(View view, boolean hasfocus) {
switch(view.getId()){
case R.id.edittextNeme:
//Do something
break;
...etc
}
}
val currentFocusView = activity.currentFocus
if (currentFocusView is EditText){
currentFocusView.appendText("Has Focus!")
}