Android EditText with icon/button - android

How can I obtain an EditText as those bellow?
The x button removes the text inserted and the eye button displays the clear password as long as it's pressed. Note that I refer to them as buttons because I don't really know what they actually are nor if they are part of the EditText itself or if they are independent views.

You can just add a button at the right of your EditText
<FrameLayout
android:id="#+id/layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="#style/ScreenLoginContainer">
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="#+id/edit_text_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:imeOptions="actionDone"
android:hint="#string/enter_password_hint"
android:paddingRight="30dp"
style="#style/ScreenPasswordEditText"/>
</android.support.design.widget.TextInputLayout>
<Button
android:id="#+screen_card_ean_enter/show_password_button"
style="#style/ShowHidePasswordButton"/>
</FrameLayout>

You can use android:drawableRight
Set a Drawable to the right of the text, in EditText, in XML .
android:drawableRight="#drawable/Your_drawable"

You can use this :
android:drawableRight="#drawable/your_icon"
Also for click this answer
may help

This is the prefect way to create an EditText with cross button at the end:
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="9dp"
android:padding="5dp">
<EditText
android:id="#+id/calc_txt_Prise"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="numberDecimal"
android:layout_marginTop="20dp"
android:textSize="25dp"
android:textColor="#color/gray"
android:textStyle="bold"
android:hint="#string/calc_txt_Prise"
android:singleLine="true" />
<Button
android:id="#+id/calc_clear_txt_Prise"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:layout_gravity="right|center_vertical"
android:background="#drawable/delete" />
And now to clear the EditText upon click you can use
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
EditTextID.setText("");
}
});
And in order to make the password visible till the button is pressed, you can implement it this way:
yourButton.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
switch ( event.getAction() ) {
case MotionEvent.ACTION_DOWN:
editText.setInputType(InputType.TYPE_CLASS_TEXT);
break;
case MotionEvent.ACTION_UP:
editText.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
break;
}
return true;
}
});

The design support library has a setPasswordVisibilityToggleEnabled method: https://developer.android.com/reference/android/support/design/widget/TextInputLayout.html#setPasswordVisibilityToggleEnabled(boolean)
This would be good for your password edittext, but not the username one.

Related

onFocus function not working properly in Android ?

Hi I am newbie in android, I have 3 edit box and I have set 3 setOnFocusChangeListener based on edit box wise, When I focus mobile no edit box ,I am getting sim no id instead mobile no Id. onFocus function getting called with wrong id's. If I did any any mistake please let us know or else solve the solution.
Source code :
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.content_temp);
objMobileNo = (EditText) findViewById(R.id.mobile_no);
objSimNo = (EditText) findViewById(R.id.sim_no);
objImsiNo = (EditText) findViewById(R.id.imsi_no);
View.OnFocusChangeListener a = new View.OnFocusChangeListener() {
public void onFocusChange(View view, boolean hasFocus) {
if (!hasFocus) {
//this if condition is true when edittext lost focus...
//check here for number is larger than 10 or not
// focusText = "MOBILE";
// Log.d("BKS", "onFocusChange: " + focusText);
Log.d("BKS", "onFocusChange: " + view.getId());
Log.d("BKS", "onFocusChange: " + R.id.mobile_no);
}
}
};
objMobileNo.setOnFocusChangeListener(a);
objSimNo.setOnFocusChangeListener(a);
objImsiNo.setOnFocusChangeListener(a);
XML Code :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<EditText
android:layout_marginTop="100dp"
android:id="#+id/mobile_no"
style="#style/Material_Action"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/hint_mob_no"
android:inputType="number"
android:maxLength="10" />
<EditText
android:id="#+id/sim_no"
style="#style/Material_Action"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/hint_sim_no"
android:inputType="number"
android:maxLength="20" />
<EditText
android:id="#+id/imsi_no"
style="#style/Material_Action"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/hint_imsi_no"
android:inputType="number"
android:maxLength="15" />
<EditText
android:id="#+id/status"
style="#style/Material_Action"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="text"
android:visibility="gone" />
<EditText
android:id="#+id/scanning_type"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="text"
android:visibility="gone" />
<LinearLayout
android:id="#+id/mLlayoutBottomButtons"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:orientation="horizontal">
<Button
android:id="#+id/verify"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/holo_green_light"
android:text="Verify"
android:textColor="#android:color/white">
</Button>
</LinearLayout>
Thanks in Advance,
Kalanidhi.
You are setting same OnFocusChangeListener for all EditText. so when you focus a new EditText, the current one loses its focus. So for both the events onFocusChange() will be called. But your if condition filters only the view that loses the focus and not the one which got the focus.
So replace
if (!hasFocus) {
}
with this to make it work as expected
if (hasFocus) {
}

Change ImageView source for some time after button click

I have two buttons and one imageview:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads" >
<ImageView
android:id="#+id/dog"
android:contentDescription="#string/desc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:src="#drawable/dogsec" />
<ImageButton
android:id="#+id/barkk"
android:contentDescription="#string/desc"
android:background="#android:color/transparent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/dog"
android:layout_centerHorizontal="true"
android:layout_marginTop="45dp"
android:src="#drawable/bark" />
<ImageButton
android:id="#+id/rrrr"
android:background="#android:color/transparent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="#+id/barkk"
android:layout_below="#+id/barkk"
android:layout_marginRight="41dp"
android:layout_marginTop="15dp"
android:contentDescription="#string/desc"
android:src="#drawable/rrr" />
</RelativeLayout>
a) I need to make Imageview change its' source to another one(I mean the image's default state is picture1, it should show picture2 and go back to picture1) on button "barkk" click. It should not show picture2 as long as barkk is pressed.
b) and I need to display picture3 as long as button rrrr is pressed.
I use exactly ImageView which should change its' source depending on case a) or b) as it is described above.
Help me please. Thanks in advance
Use this code.
myButton.setOnClickListener(new OnClickListener() {
#Override
public boolean onTouch(View v, MotionEvent event)
{
if(event.getAction() == MotionEvent.ACTION_DOWN) {
myImageView.setImageResource(R.drawable.myNewImage);
} else if (event.getAction() == MotionEvent.ACTION_UP) {
myImageView.setImageResource(R.drawable.myNewImage);
}
}
};

EditText in PopupWindow not showing keyboard even if setFocusable(true)

I can't seem to get this work. I already set popWindow focusable as to what I read on other forums but still no luck.
xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:adjustViewBounds="true"
android:background="#drawable/popbg"
android:orientation="vertical" >
<Button
android:id="#+id/cancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_marginRight="10dp"
android:layout_marginTop="30dp"
android:background="#drawable/zcancel" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginLeft="10dp"
android:text="SSID"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="#+id/editText3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName" />
java
case(R.id.settings):
switch (event.getAction())
{
case MotionEvent.ACTION_DOWN:
v.setBackgroundResource(R.drawable.cpanel2);
return true;
case MotionEvent.ACTION_UP:
v.setBackgroundResource(R.drawable.cpanel1);
LayoutInflater layoutInflater =
(LayoutInflater)getBaseContext().getSystemService(LAYOUT_INFLATER_SERVICE);
View popSwitchView = layoutInflater.inflate(R.layout.settings_xml, null);
final PopupWindow popWindow = new PopupWindow(popSwitchView);
popWindow.setWidth(LayoutParams.MATCH_PARENT);
popWindow.setHeight(LayoutParams.MATCH_PARENT);
popWindow.showAtLocation(popSwitchView, Gravity.CENTER, 0, 0);
popWindow.setOutsideTouchable(false);
popWindow.setFocusable(true);
Drawable d = getResources().getDrawable(R.drawable.popbg);
popWindow.setBackgroundDrawable(d);
Button CancelButton = (Button)popSwitchView.findViewById(R.id.cancel);
CancelButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
popWindow.dismiss();
}
});
popWindow.showAsDropDown(v, 50, -30);
return true;
default:
return false;
}
I'm planning on creating a popwindow of setting for network configurations. I can't seem to fix my code for a good view for you guys .
ha, .found the answer.,
just did
popWindow.setFocusable(true);
popWindow.update();
thanks for the support!
credits from this topic
Keyboard not shown when i click on edittextview in android?
This worked for me:
popWindow.setFocusable(true);
popWindow.update();
Try adding this line before you show the popup:
popupWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
Nothing helped me, it still didn't appear on some devices, so I had to force showing it like that (kotlin):
val editText = customView.findViewById<EditText>(numberFieldId)
editText.onClick {
showKeyboard(editText, context)
}
private fun showKeyboard(mEtSearch: EditText, context: Context) {
mEtSearch.requestFocus()
val imm = context.getSystemService(Activity.INPUT_METHOD_SERVICE) as InputMethodManager
imm.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0)
}
Try this::
Programatically:
edittext.requestFocus();
Through xml:
<EditText
android:id="#+id/editText3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName">
<requestFocus />
</EditText>
Hope it Helps!!

EditText Scrollable with keyborad active

Well friend ..
I have a design so:
The main Scroll is very necessary when appear the keyboard... If i dont make that, the button can't see...
The problem is when i try move inside the Edittext with the keyboard active...
I tried this:
android:scrollbars="vertical"
and this:
setMovementMethod(new ScrollingMovementMethod());
but not solve this yet
Anyone help me?
grax advance!
Check this without using scroll bar:
add permission in menifest file for corresponding activity.
android:windowSoftInputMode="adjustPan"
Please try this:
Your layout should be similar to example:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<EditText
android:id="#+id/edittext"
android:layout_width="200dp"
android:layout_height="500dp"
android:text="some long text"
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
</ScrollView>
and in your java try this:
EditText edit = (EditText)findViewById(R.id.edittext);
edit.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if (v.getId() == R.id.edittext) {
v.getParent().requestDisallowInterceptTouchEvent(true);
switch (event.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_UP:
v.getParent().requestDisallowInterceptTouchEvent(false);
break;
}
}
return false;
}
});
It could be helpful.

Android: EditText NextFocusDown not trigger Spinner

In my application I have one registration screen, There are lot of EditTexts and Spinners.
I want to go through the Registrations field one by one.
So I applied android:imeOptions="actionNext" . But It ignores all the Spinners. It will give focus only to EditText. I have also tried setNextFocusDownId(). This is also ignore the spinners.
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/reportentry11"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<EditText
android:id="#+id/numbers"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:imeOptions="actionNext"
android:inputType="phone"
>
<requestFocus/>
</EditText>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/reportentry12"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<TextView
android:id="#+id/txt_exp"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="EXP:"
android:textColor="#000000"
android:textSize="12dp" />
<Spinner
android:id="#+id/spin_date"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="3"
android:focusable="true"
android:focusableInTouchMode="true"
android:nextFocusDown="#+id/spin_year"
android:text="date"
android:textSize="12dp" />
<Spinner
android:id="#+id/spin_year"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginRight="5dp"
android:nextFocusDown="#+id/cvc"
android:text="year"
android:textSize="12dp" />
</LinearLayout>
<EditText
android:id="#+id/cvc"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:hint="#string/reg_label_cvc"
android:imeOptions="actionNext"
android:inputType="phone" />
<EditText
android:id="#+id/fname"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="#string/reg_label_fname"
android:imeOptions="actionNext" />
<EditText
android:id="#+id/address"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="#string/reg_label_address"
android:imeOptions="actionNext" />
<EditText
android:id="#+id/city"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="#string/reg_label_city"
android:imeOptions="actionNext"
android:nextFocusDown="#+id/pr_spin" />
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/reportentry13"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="#+id/txt_pr"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="PROV:"
android:textColor="#000000"
android:textSize="12dp" />
<Spinner
android:id="#+id/pr_spin"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="date"
android:imeOptions="actionNext"
android:textSize="14dp" />
<EditText
android:id="#+id/pcode"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:hint="#string/reg_label_pcode"
android:imeOptions="actionDone" />
</LinearLayout>
<Button
android:id="#+id/register_register_button"
android:layout_width="wrap_content"
android:background="#drawable/green_button_bg"
android:onClick="completeClicked"
android:text="#string/reg_label_complete"
android:textSize="28dp"
android:textStyle="bold" />
</LinearLayout>
Please provide me the best way to trigger the spinners.
On your edit texts, override onEditorAction and give focus (or do whatever like open your spinner)...
yourEditTXT.setOnEditorActionListener(new OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView view, int actionID, KeyEvent event) {
if (actionID == EditorInfo.IME_ACTION_NEXT) {
//do your stuff here...
return true;
}
return false;
}
});
Edit 12/4:
I see you were still struggling with this as of last night, if you didn't find a solution (and didn't post one) or to help others who may read this, this may help to get from spinner to edit text.
mySpinner.setOnItemSelectedListener(new MyOnItemSelectedListener());
public class MyOnItemSelectedListener implements OnItemSelectedListener {
public void onItemSelected(AdapterView<?> parentview, View v, int position, long id) {
// your spinner proces code
// then when you are done,
yourEditText.setFocusableInTouchMode(true); //if this is not already set
yourEditText.requestFocus(); //to move the cursor
final InputMethodManager inputMethodManager = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.showSoftInput(yourEditText, InputMethodManager.SHOW_IMPLICIT); // this line and the one above will open the soft keyboard if it doesn't already open
}
public void onNothingSelected(AdapterView<?> arg0) { }
};
do the following in your xml file:
<Spinnner
android:focusable="true"
android:focusableInTouchMode="true"
android:nextFocusDown="#+id/youedittextid"
/>
Apart from the solutions above to focus on Spinners, here are further troubles with ime options:
If the spinners are between the EditTexts, then the EditTexts that are below the spinners ignore imeOption="actionDone" and lines="1" attributes. In both the cases they accept enter keys and move to next line, when, it should not allow the enter key event.
I found that though the listener for ime was still intact, the ime option set to whatever (next or done), it changed to 0.
So my answer is for second part while I am still trying to make spinners focusable:
xml:
<EditText
android:id="#+id/et_1"
android:layout_width="match_parent"
android:layout_height="#dimen/dp_40"
android:layout_marginLeft="#dimen/dp_8"
android:layout_marginStart="#dimen/dp_8"
android:gravity="start|bottom"
android:hint="#string/et_1"
android:imeOptions="actionNext"
android:inputType="textCapWords" />
<!--This ime option works fine-->
<!--lots of spinners in between these Edit texts-->
<EditText
android:id="#+id/et_2"
android:layout_gravity="start|bottom"
android:layout_marginLeft="#dimen/dp_8"
android:layout_marginStart="#dimen/dp_8"
android:gravity="start|bottom"
android:hint="#string/et_2"
android:imeOptions="actionDone" />
<!--this actionDone is ignored and set to 0-->
[value of actionNext = 5 while that of actionDone = 0, but for edit text et2 reports as 0 in the listener]
set listener:
...
EditText et_1 = view.findViewById(R.id.et_1);
fet_1.setOnEditorActionListener(this);
//==
EditText et_2 = view.findViewById(R.id.et_2);
et_2.setOnEditorActionListener(this);
...
The handling of listeners where the ime actions are ignored too:
#Override
public boolean onEditorAction(TextView textView, int actionID, KeyEvent keyEvent) {
FontEditText et_1 = getView().findViewById(R.id.et_1);
//==
EditText et_2 = getView().findViewById(R.id.et_2);
final int id = textView.getId();
switch (id) {
case R.id.et_1: {
if (actionID == EditorInfo.IME_ACTION_NEXT) {
// works actionID = 5.
//inherited method
activity.hideKeyboard(textView);
fet_bus_entity.clearFocus();
et_business_add.requestFocus();
et_business_add.performClick();
return true;
}
}
break;
case R.id.et_2: {
//following line doesn't work hence commented, value of actionID = 0 here.
// if (actionID == EditorInfo.IME_ACTION_DONE) {
et_business_add.clearFocus();
//inherited method
activity.hideKeyboard(textView);
return true;
// }
}
// break;
}
return false;
}

Categories

Resources