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) {
}
Related
I am trying to create edit text in which when user presses enter it goes to new line and allow user to enter more text.
here is my design part
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="#+id/discussion"
android:layout_width="match_parent"
android:layout_height="100dp"
android:inputType="textMultiLine"
android:maxLines="5"
android:hint="Discussion outcome"
android:digits="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890 "/>
</android.support.design.widget.TextInputLayout>
Here is my code, but the problem is cursor position is not going to the next line. i tried discussion.append("\n") too but it didnt work
discussion= (EditText) findViewById(R.id.discussion);
discussion.setOnKeyListener(new View.OnKeyListener() {
#Override
public boolean onKey(View view, int i, KeyEvent keyEvent) {
Log.e(TAG , "Key Action "+keyEvent.getAction());
if(keyEvent.getAction() == KeyEvent.ACTION_UP && keyEvent.getKeyCode() == KeyEvent.KEYCODE_ENTER){
String data =discussion.getText().toString();
discussion.setText(data+"\n");
Log.e(TAG , "Key "+keyEvent.getKeyCode());
}
return false;
}
});
You should include the newline character \n in the allowed digits in your xml:
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="#+id/discussion"
android:layout_width="match_parent"
android:layout_height="100dp"
android:inputType="textMultiLine"
android:maxLines="5"
android:hint="Discussion outcome"
android:digits="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890\n "/>
</android.support.design.widget.TextInputLayout>
Next, you don't need any key listeners if you do that.
try this
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textMultiLine"
android:ems="10"
android:id="#+id/edt_address"
android:hint="Shipping Address" />
hope it will help you
Try this solution: https://stackoverflow.com/a/20475011/13179254
Basically what you need to do:
Kotlin:
isSingleLine = false
Java:
setSingleLine(false)
Xml:
android:singleLine="false"
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.
I have a Listview content form TextView and Edittext, and I want to get the value from each Edittext in this Listview.
My problems are:
When the Listview is created, the focus is not on the first Edittext in Listview.
The second problem, is when I enter a value in Edittext and move list text down in screen the value in Edittext changed or removed, or the value moved to another Edittext on screen.
You can see image in this
link
my code is
ArrayList<HashMap<String, String>> AllStudent ;
AllStudent = new ArrayList<HashMap<String, String>>();
for (int j=0; j<15; j++){
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put("id"," " + j);
map.put("name", "A" + j );
// adding HashList to ArrayList
AllStudent.add(map);
}
listadapter = new SimpleAdapter(
sc_grade_group.this, AllStudent,
R.layout.list_item_graed, new String[] { "id","name" },
new int[] { R.id.list_itemGra_t1, R.id.list_itemGra_t2 });
setListAdapter(listadapter);
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/list_item_LinearLayout"
android:layout_width="fill_parent"
android:layout_height="60dp"
android:background="#drawable/custom_bg_1"
android:orientation="horizontal"
android:padding="2dp"
android:weightSum="1" >
<TextView
android:id="#+id/list_itemGra_t1"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:visibility="gone" />
<TextView
android:id="#+id/list_itemGra_t2"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.6"
android:background="#drawable/custom_bg_2"
android:gravity="left|center_vertical"
android:paddingLeft="6dip"
android:paddingTop="6dip"
android:textColor="#color/black"
android:textSize="17sp"
android:textStyle="bold" />
<TextView
android:id="#+id/list_itemGra_t4"
android:layout_width="180dp"
android:layout_height="match_parent"
android:layout_weight="0.28"
android:background="#drawable/custom_bg_2"
android:gravity="left|center_vertical"
android:paddingLeft="6dip"
android:paddingTop="6dip"
android:textColor="#color/gold"
android:textSize="13sp"
android:textStyle="bold"
android:visibility="gone" />
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.33"
android:background="#color/cachecolor2"
android:gravity="center_vertical|center_horizontal"
android:weightSum="1" >
<EditText
android:id="#+id/list_itemGra_Degree"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:ems="10"
android:inputType="numberDecimal" >
<requestFocus />
</EditText>
</LinearLayout>
<TextView
android:id="#+id/list_itemGra_t3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone" />
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.07"
android:background="#color/cachecolor2" >
</LinearLayout>
<TextView
android:id="#+id/list_itemGra_t5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone" />
</LinearLayout>
Ok first of all, if you want that entered information should be persistent when scrolled out and scrolled back, you have to make a model that should store what was entered. All information is cleared when you scroll, and scroll back is because ListView re-creates only those view's which are on screen and visible.
Used SimpleAdapter can hook up with ViewBinder interface, though its hard to handle storing in there. My suggestion would be to make a class model and make a custom adapter for this.
Here is a great tutorial on various ListView implementations (http://www.vogella.com/tutorials/AndroidListView/article.html).
Second problem, to prevent the view focusing once you enter the area, you can try setting
seting the attributes android:focusable="true" and android:focusableInTouchMode="true" on EditText, though this should be tested, im not sure it will work.
1, Try in onCreate:
yourEditText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if (!hasFocus) { // run when focus is lost
String value = v.getText().toString(); // get the value from the EditText
// Your code to update hashmap
}
}
});
2, When you create the ListView, add a OnFocusChangeListener, like shown below
myList.setOnFocusChangeListener(new View.OnFocusChangeListener() {
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
myHandler.postAtFrontOfQueue(new Runnable() {
public void run() {
myList.setSelection(0);
}
});
}
}
});
and if it wont work read Focusable EditText inside ListView
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;
}
i'm doing a cable length calculator, and i'm having trouble with negative numbers.
EditTexts are like this
<EditText android:layout_height="wrap_content" android:layout_weight="1" android:inputType="numberDecimal|numberSigned" android:layout_width="40px" android:id="#+id/rxmax">
</EditText>
Then i use them like this:
final EditText rxmax = (EditText) findViewById(R.id.rxmax);
double RXmax = new Double(rxmax.getText().toString());
After i do a simple calculation:
double OPBmax = TXmax - RXmax;
Somewhere the inputted negative number turns positive. i'm guessing at the toString conversation but i don't find anything on how to prevent this.
Use
android:digits="0123456789"
EX:
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:digits="0123456789"
android:gravity="center"
android:inputType="numberSigned"
android:textColor="#color/black"
android:textSize="#dimen/font_size_small" />
I've tried this, and it works...
Activity:
double RXmax;
EditText rxmax;
TextView tv;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
rxmax = (EditText) findViewById(R.id.rxmax);
tv = (TextView) findViewById(R.id.text);
}
public void click(View v) {
RXmax = new Double(rxmax.getText().toString());
tv.setText(Double.toString(RXmax));
}
main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<EditText android:layout_height="wrap_content"
android:layout_weight="1" android:inputType="numberDecimal|numberSigned"
android:layout_width="40px" android:id="#+id/rxmax">
</EditText>
<TextView android:id="#+id/text" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="#string/hello" />
<Button android:layout_height="wrap_content" android:id="#+id/button1"
android:layout_weight="1" android:text="Button" android:layout_width="wrap_content"
android:onClick="click"></Button>
</LinearLayout>
If I type -2, after clicking the TextView displays -2.0
If you're using Eclipse, try Project --> Clean..., and select your project. Maybe this will help.