I have a small problem with Edittext, I have two edittext and when I press an edittext the other must be cleaned, try to do it with isFocusable () but I can't, who can help me?
public void loadED(View view) {
if (edt4.isFocusable()) {
edt3.setText("");
} else {
if (edt3.isFocusable()) {
edt4.setText("");
}
}
}
Use an OnFocusChangeListener applied to each EditText:
edt4.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
edt3.getText().clear();
}
}
});
edt3.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
edt4.getText().clear();
}
}
});
Related
JAVA CODE:
mEdtTextPickup.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if(hasFocus){
mLayoutDrop.invalidate();
mEdtTextPickup.bringToFront();
}
}
});
mEdtTextDrop.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if(hasFocus){
mLayoutPickup.invalidate();
mEdtTextDrop.bringToFront();
}
}
});
`
I have two layouts in my application Source and Destination in which one overlaps the other inside a FrameLayout.(just like the attached image)
Now how do i bring destination layout on top of source layout animated when it is focused. I googled a lot but found no solutions.
You can use a focus change listener to identify when the field is focused and then bringToFront to bring the view above the others.
destinationEditText.setOnFocusChangeListener(new OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
edittext.bringToFront();
}
}
});
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.
Admob is blocking EditText when user wants to enter a value. I introduced this code but it is still not working.
iAge = (EditText) v.findViewById(R.id.edAge);
iIncome= (EditText) v.findViewById(R.id.edIncome);
iAge.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View view, boolean hasFocus) {
if (hasFocus) {
mAdView.setVisibility(View.GONE);
} else {
mAdView.setVisibility(View.VISIBLE);
}
}
});
iIncome.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View view, boolean hasFocus) {
if (hasFocus) {
mAdView.setVisibility(View.GONE);
} else {
mAdView.setVisibility(View.VISIBLE);
}
}
});
I have several EditText fields which I want to save to the SQLiteDatabase with setOnFocusChangeListener. Do I have to set an onFocusChangeListener on each one individually, or is there a catch-all of some sort? (getActivity().findViewByID because this is a fragment)
final TextView txtName = (TextView)getActivity().findViewById(R.id.clientHeader);
final TextView txtCompany = (TextView)getActivity().findViewById(R.id.txtContactCompany);
final TextView txtPosition = (TextView)getActivity().findViewById(R.id.txtContactPosition);
txtName.setOnFocusChangeListener(new OnFocusChangeListener() {
public void onFocusChange(View v, boolean hasFocus) {
if(!hasFocus) {
saveThisItem(txtClientID.getText().toString(), "name", txtName.getText().toString());
}
}
});
txtCompany.setOnFocusChangeListener(new OnFocusChangeListener() {
public void onFocusChange(View v, boolean hasFocus) {
if(!hasFocus) {
saveThisItem(txtClientID.getText().toString(), "company", txtCompany.getText().toString());
}
}
});
txtPosition.setOnFocusChangeListener(new OnFocusChangeListener() {
public void onFocusChange(View v, boolean hasFocus) {
if(!hasFocus) {
saveThisItem(txtClientID.getText().toString(), "position", txtPosition.getText().toString());
}
}
});
Like... is there some way to have a ArrayList < EditText > of EditText Views, assign a pointer (sorry, not sure how) to the existing editTexts and set the onFocusChangeListener to the whole arraylist? Or, even, iterate through the ArrayList and set the onFocusChangeListener to each member?
Or a way to detect ANY onFocusChangeListener events, and just save all data to the database, regardless of what EditText the even occurred on?
Well, you could have your activity implement OnFocusChangeListener. That way, all your changes will be on that one metho,d but you will have to check which view changed focus by getting the view id with v.getId() and handle accordingly.
#Override
public void onFocusChange(View v, boolean hasFocus) {
switch(v.getId()){
case r.id.editText1:
break;
...etc
}
}
There's plenty of ways you could make this simpler. For one, how about abstracting this into a method and calling it for each TextView you want to add the event for:
private void setOnFocusChangeListener(TextView textView, String name){
textView.setOnFocusChangeListener(new OnFocusChangeListener() {
public void onFocusChange(View v, boolean hasFocus) {
if(!hasFocus) {
saveThisItem(txtClientID.getText(), name,
textView.getText());
}
}
});
}
Then you can call this method for each of your TextView instances:
setOnFocusChangeListener(txtName, "name");
setOnFocusChangeListener(txtCompany, "company");
setOnFocusChangeListener(txtPosition, "position");
My suggestion would be to use a custom EditText to handle this for you:
public class SaveEditText extends EditText implements View.OnFocusChangeListener {
private String mDataKey;
private String mClient;
public SaveEditText (Context context) {
super(context);
setOnFocusChangeListener(this);
}
public SaveEditText (Context context, AttributeSet attrs) {
super(context, attrs);
setOnFocusChangeListener(this);
}
public void setDataKey (String dataKey) {
mDataKey = dataKey;
}
public void setClient (String client) {
mClient = client;
}
#Override
public void onFocusChange (View v, boolean hasFocus) {
saveThisItem(mClient, mDataKey, getText().toString());
}
}
You might decide to handle the client and data keys differently, but the idea is that you use a custom base class that will handle it automatically.
Or even simpler
OnFocusChangeListener listener;
listener = new OnFocusChangeListener() {
public void onFocusChange(View v, boolean hasFocus) {
//Do something
}
};
textbox1. setOnFocusChangeListener(listener);
textbox2. setOnFocusChangeListener(listener);
textbox3. setOnFocusChangeListener(listener);
Since onFocusChange(View v, boolean hasFocus) has the parameter View you can use that to tell which view called it