Say "I want users to select from a dropdown list while filling a form, e.g Sex". Can I achieve exactly what is in the image attached? If yes, how?
Asked
Active
Viewed 1,177 times
0
-
Why not a spinner? – Rohit5k2 Apr 08 '19 at 10:51
-
use spinner and add background same as edittext. you will get the same effect. – Molly Apr 08 '19 at 10:53
-
`Spinner` is idle solution for this Since its gender and you do not enter it manually . [You can change Spinner apperience](https://stackoverflow.com/questions/11188398/how-to-change-the-spinner-background-design-and-color-for-android) OR You can use use `ListPopUpWindow` other than `Spinner` – ADM Apr 08 '19 at 10:53
-
You can use spinner with custom style https://stackoverflow.com/a/37859442/5815673 or set drawable as per your requirement on EditText and on click open popup Menu or any list as per your requirement – Farmer Apr 08 '19 at 10:53
-
Spinner is the best solution. – Prafulla Nayak Apr 08 '19 at 10:59
-
@Rohit5k2, I thought I won't be able to achieve it using spinner. – developerTobi Apr 09 '19 at 09:46
-
@Tee-Boi - Spinner is for the exact same scenario you are looking for. – Rohit5k2 Apr 09 '19 at 09:48
-
2@ADM That reference was just perfect for me... Thanks a lot – developerTobi Apr 09 '19 at 09:48
-
@Rohit5k2, Yes, I just realized that now – developerTobi Apr 09 '19 at 09:49
2 Answers
1
You can add an arrow image as drawable-right/drawable-end. Set focusable properties and cursor visibility false and on click of Edit text open a dialog with list. For the background use same background you are using for email-address edittext. Below is Sample XML Code
<EditText
android:id="@+id/etCustomerType"
android:layout_width="match_parent"
android:layout_height="@dimen/_40sdp"
android:layout_marginStart="@dimen/_15sdp"
android:layout_marginEnd="@dimen/_15sdp"
android:backgroundTint="@color/editTextTextColor"
android:cursorVisible="false"
android:drawableEnd="@drawable/ic_dropdown"
android:drawableRight="@drawable/ic_dropdown"
android:drawablePadding="@dimen/_10sdp"
android:focusable="false"
android:focusableInTouchMode="false"
android:paddingStart="@dimen/_10sdp"
android:paddingEnd="@dimen/_10sdp"
android:textColor="@color/editTextTextColor"
android:textColorHint="@color/editTextTextColor"
android:textCursorDrawable="@color/editTextTextColor"
android:textSize="@dimen/textSizeXSmall" />

User
- 692
- 2
- 11
- 29
0
I guess this should do:
The XML files should be:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<AutoCompleteTextView
android:id="@+id/sex"
android:layout_width="fill_parent"
android:layout_height="wrap_content"></AutoCompleteTextView>
</LinearLayout>
And the MainActivity.java
should be
import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
public class MainActivity extends Activity {
String[] sex = { "Male", "Female" };
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//Create Array Adapter
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.select_dialog_singlechoice, sex);
//Find TextView control
AutoCompleteTextView acTextView = (AutoCompleteTextView) findViewById(R.id.sex);
//Set the number of characters the user must type before the drop down list is shown
acTextView.setThreshold(1);
//Set the adapter
acTextView.setAdapter(adapter);
}
}

Keyur Kariya
- 69
- 2
- 12
-
Thanks for this, I'm really grateful. But I don't just want the user to type anything. I want the user to click on the edittext and select his/her gender – developerTobi Apr 09 '19 at 09:28
-