I have the following items displayed on the screen:
EditText Forename
Spinner gender selection (Male | Female)
EditText Email
When the application is first launched, I want the focus to be set on the Forename EditText. Then after "Male" or "Female" has been selected in the Spinner, I want the focus to be set on the Email EditText that is located below the spinner.
I have used setOnItemSelectedListener
to set the requestFocus
on the email EditText, but the problem is that it automatically sets the focus on
this EditText whenever I launch the application.
This happens because by default the spinner displays the first selection which in this case is "Male" and therefore it thinks
that a selection has already been made and sets the focus on the Email field.
I don't mind the first selection to be already selected in the spinner by default but if I could somehow override the requestFocus
to be set on the
Forename EditText initially that would be great.
XML:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/LinearLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<EditText
android:id="@+id/ForenameForm"
android:layout_width="285dp"
android:layout_height="65dp"
android:hint="@string/forenameHint"
android:lines="1"
android:singleLine="true"
android:textSize="20sp" >
</EditText>
<Spinner
android:id="@+id/SpinnerGender"
android:spinnerMode="dialog"
android:textSize="30sp"
android:layout_width="285dp"
android:layout_height="60dp"
android:prompt="@string/spinnerGender"
android:entries="@array/genderList">
</Spinner>
<EditText
android:id="@+id/EmailForm"
android:layout_width="285dp"
android:layout_height="65dp"
android:hint="@string/emailHint"
android:lines="1"
android:singleLine="true"
android:textSize="20sp" >
</EditText>
</LinearLayout>
Activity class:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final EditText forename=(EditText)findViewById(R.id.ForenameForm);
forename.requestFocus();
final EditText email=(EditText)findViewById(R.id.EmailForm);
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) {
email.requestFocus();
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
}
How about this solution.
define a boolean flag and set it default false.
in oncreate set the focus of forname
in setOnItemSelectedListener
if flag is false then set flag true
else focus email
So your code will be like
boolean flag = false;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final EditText forename=(EditText)findViewById(R.id.ForenameForm);
forename.requestFocus();
final EditText email=(EditText)findViewById(R.id.EmailForm);
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) {
// changes here
if(flag == false)
flag = true;
else
email.requestFocus();
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With