Switch to the next field when autocompleted - android

I have got a lot of AutoCompleteTextViews and EditText in my app, and i wanted to make user input a bit more easy :
I would like to go to the next field when the user hit the dropdown list.
Thank you if you have any infos about how to do that !
Here is what i finally did and which worked :
AutoCompleteField.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
TextView nextField = (TextView)AutoCOmpleteField.focusSearch(View.FOCUS_FORWARD);
nextField.requestFocus();
}
});

hey add this to your edittext xml
android:imeOptions="actionNext"
and add
android:imeOptions="actionDone"
in your last edit text to finish

Related

Moving cursor to the next field

How do I move the cursor to the next line? I have two spinners, then a title edit text box and then two edit text boxes in which I am setting the date using a date picker. When setting the date in the "To"-field, the cursor is moved back to the Title field. After selecting the date I want it move to the next field which is "Send By", but it keeps returning to the previous field which is title field. Can anybody help?
android:nextFocusDown="#+id/.."
android:nextFocusLeft="#+id/.."
android:nextFocusRight="#+id/.."
android:nextFocusUp="#+id/.."
or
Spinner spinner=(Spinner)findViewById(R.id.SpinnerGender);
spinner.setFocusable(true);
spinner.setFocusableInTouchMode(true);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
{
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
if(flag == false)
flag = true;
else
edtTitle.requestFocus();
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
You can try to call requestFocus() on the corresponding EditText in the DatePicker.OnDateChangedListener method.
Kindly add this line to xml file Spinner/EditText Control..
android:imeOptions="actionNext"
or
android:imeOptions="actionNext"
android:singleLine="true"
Hope, this might help you..

change color of button when list item is selected

i'm working on a listview in an activity and i want when ever i longpressed any listitem the Button on the action bar changes its color like as get highlihted as suppose " i select
list item the button on the action bar changes from white to gray" .
listView.setOnItemLongClickListener(new OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// changes the color button ;
return false;
}
});
or is there any other way that i can do this.
I dont have enough reputation to post a pictures Firstly it looks like Before selecting any item and its looks like thisWhen item is long pressed or selected hope you understand my probleum ,
Thanks in advance
Edit: as misunderstood original question:
int mPosition = -1;
lv.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
int pos, long id) {
if(mPosition!=-1) {
//reset color of previously pressed item to white
lv.getChildAt(mPosition).setBackgroundColor(Color.WHITE);
}
//set current item to fray.
lv.getChildAt(pos).setBackgroundColor(Color.GRAY);
//put value of currently selected item to mPosition.
mPosition = pos;
return true;
}
});
Old answer below:
Add this into your method.
Button button1 = (Button)findViewById(R.id.button1);
if(this condition){
button1.setBackgroundColor(Color.GRAY);
}else{
//whatever..
}
This answer covers the same principle, but in more detail using the resources.
you can give color to list item when selected with
android:listSelector="#color/AnyColor" in .xml file .. see coding
<ListView
android:id="#+id/nameOfYourListview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_margin="10dp"
android:background="AnyYouWant"
<!--code for color the selected item --> android:listSelector="ColorHereWhichYouWant" >
</ListView>

keyboard bug with onClickListener in EditText

I have a problem with an editText. I want to put a default text in this editText and when you click on the text disappear and you can wirte a number. Every thing is going well with that code for the layout:
<EditText
android:id="#+id/yearEdit"
android:layout_width="145dp"
android:layout_height="45dp"
android:layout_marginBottom="40dp"
android:gravity="center"
android:ems="10"
android:inputType="number|numberDecimal"
android:text="#string/chooseDate"
android:textSize="13sp"
/>
and in the activity the edit text have as textWatcher and onClickListener :
private class ChooseYear implements TextWatcher, OnClickListener {
#Override
public void afterTextChanged(Editable arg0) {
if (!yearEdit.getText().toString().equals("")) {
yearChoose = Integer.parseInt(yearEdit.getText()
.toString());
}
}
#Override
public void beforeTextChanged(CharSequence arg0, int arg1,
int arg2, int arg3) {
}
#Override
public void onTextChanged(CharSequence arg0, int arg1,
int arg2, int arg3) {
}
#Override
public void onClick(View arg0) {
if (yearChoose == -1) {
yearEdit.setText("");
}
}
}
But my problem is : When i had it as the onClickListener, the android keyboard doesn't close anymore when i click on its "Done" button. Does someone know why ? Does it exist a proper way to fix it ?
Thanks a lot for your help,
Eliott
For people who are interest, this is my conclusion on my own problem.
It looks like the keyBoard doesn't work proprely (The "Done" button is not closing the keyboard) when you add an OnClickListener on the EditText. To solve the problem I have to add the line :
yearEdit.setOnClickListener(null);
in the code. When the OnClickListener is set to null, The "Done" button restart to work normally. You have to re-set the OnClickListener if you need it again after (not my case).
My opinion is that could be an Android bug, because there is no logic than an OnClickListener have an effect on the keyboard. If you want to share your opinion about it, i will be interest.

edittext focused on visible in android

I have one edittext.It is INVISIBLE by defalut.On click of list item it is visible.I want to focust edit text when it is visible.and opens soft keyboard.i have searches alot but it is no working on visible and Invisible.
CODE:
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
list_selection=((TextView)arg1).getText().toString();
if(list_selection.equals("Other"))
{
edit_other.setVisibility(View.VISIBLE);
edit_other.setFocusable(true);
edit_other.setCursorVisible(true);
other.setVisibility(View.VISIBLE);
image.setVisibility(View.VISIBLE);
((InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE)).toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);
}
try to use
edit_other.requestFocus();
You could give this a try and see :
if(edit_other.requestFocus()) {
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
}
Hope it helps !

EditText in multi-color

I am working on an Android application.
Now I want take input from user into an EditText.
I want display the text after the last fullstop in black color and text before last full stop in red color.
For example, If user types below sentence in EditText:
'my name is john.I am from India.I Love Android'
I want to show the 'I love Android ' in black and first parts of the sentence in red.
Is there any way to do that?
Use SpannableString to apply attributes to your text. Here blog entry you may want to read: http://www.chrisumbel.com/article/android_textview_rich_text_spannablestring.
Listen to changes in text by:
editText.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {
if (!colorHasSet) {
makeColorText();
}
colorHasSet = false;
}
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {}
public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {}
});
then declare a function to colorize text by using the tutorial which WebnetMobile linked to.
public void makeColorText() {
SpannableString ss = new SpannableString(textEdit.getText());
// customize ss here
// ...
colorHasSet = true;
editText.setText(ss);
}
flag boolean variable colorHasSet should be defined to prevent stackOverflowException.
this is not a complete WYSIWYG editor with instant colored text, and you should do some hacks to make it complete and suitable to your needs, that is left to be done by yourself.

Categories

Resources