How to highlight/focus two EditTexts in Android? - 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);
}
}
});

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

Change TextSize of focussed EditText in android

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.

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

Hide Numeric Keyboard ONLY WHEN clicked outside any of the Edit Text

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

Get text from an EditText

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

Categories

Resources