edittext focused on visible in android - 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 !

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>

Switch to the next field when autocompleted

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

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.

Is there a cleaner approach to click listeners in Android?

So I have been developing in Android for quite some time and I am natively a .Net developer. The problem I am facing is that my code looks awful, and I really dislike the way that Java\Android not sure which, makes click events appear in code. In this example below I have a List View with a click event.
list_view.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
// TODO Auto-generated method stub
}
});
In .Net I would have set my click event in my Initialize Components method and then implemented it in my main code page. How do I tell android to use a click event method below in my code instead of referencing it right there. Maybe something like this?
list_view.setOnItemClickListener(onItemClick());
Later on in my code page.
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
// TODO Auto-generated method stub
}
});
All in all I am just looking for a cleaner approach to wiring my events up to my controls so I can read the code better. It is a struggle moving to Java and Android when originally being a .Net developer.
Thanks!
Edit
Thank you for the answer below I posted my work here.
list_view.setOnItemClickListener(list_view_onItemClickListener);
//below in my code
private OnItemClickListener list_view_onItemClickListener = new OnItemClickListener (){
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
// TODO Auto-generated method stub
}
};
you can try this!
you may like or not but what approach i found best for me is for example this is your layout.xml
<Button1 android:id="#+id/btn1" android:onClick="btnHandle">
</Button1>
<Button2 android:id="#+id/btn2 android:onClick="btnHandle">
</Button2>
and in your activity.Java
public void btnHandle(View v)
{
if(v.getId()==R.id.btn1){
}else if(v.getId()==R.id.btn2){
}
by this approach you dont have to implement any interface and even if you change your Button view to ImageView still you dont have to do any thing in code. but in case of setOnClickListener you have to change your instance variable type from Button to ImageView.
}

Categories

Resources