Photos
Q1. How can I shorten space between triangle and text in Spinner?
Q2. How to extend the dropdown menu like photo(dotted line)?
Q3. How can I change the dropdown menus theme to light?
My source codes
▼Layout
<Spinner
android:id="#+id/spinner"
android:spinnerMode="dropdown"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="0" />
▼Java File
Spinner spinner = (Spinner)findViewById(R.id.spinner);
ArrayAdapter<String> adapter = new ArrayAdapter<>(this,
R.layout.listitem, new ArrayList<String>() {{
add("ENGLISH");
add("RUSSIAN");
}});
▼listitem
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceListItemSmall"
android:textColor="#android:color/white"
android:gravity="center_vertical"
android:paddingStart="?android:attr/listPreferredItemPaddingStart"
android:minHeight="?android:attr/listPreferredItemHeightSmall" />
Related
Why does using a custom text view prevent the Spinner text & items (yet not the arrow) from being clickable and yet this isn't the case with Android-prvoided Spinner text layouts?
works when used
val arrayAdapter = ArrayAdapter(view!!.context, android.R.layout.simple_dropdown_item_1line, spinnerItems)
arrayAdapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line)
XML
<Spinner
android:id="#+id/mySpinner"
style="#style/Widget.AppCompat.Spinner.Underlined"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:spinnerMode="dialog"/>
Kotlin
spinnerItems = arrayOf(
"Cathedral of the Intercession of the Most Holy Theotokos on the Moat",
"Ferapontov Monastery",
"Historic Monuments of Novgorod and Surroundings",
"Golden Mountains of Altai",
"Historic Centre of Saint Petersburg and Related Groups of Monuments",
"Bogoroditse-Smolensky Monastery",
"White Monuments of Vladimir and Suzdal"
)
val arrayAdapter = ArrayAdapter(view!!.context, R.layout.spinner_item, spinnerItems)
arrayAdapter.setDropDownViewResource(R.layout.spinner_item)
mSpinner.adapter = arrayAdapter
spinner_item.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
style="?android:attr/dropDownItemStyle"
android:id="#+id/my_SpinnerItem"
android:background="?android:attr/selectableItemBackground"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:focusable="true"
android:paddingBottom="16dp"
android:paddingTop="16dp"
android:textColor="?android:attr/textColorPrimary" />
Remove these two lines:
android:clickable="true"
android:focusable="true"
Your code works fine.
I have a spinner with the selections at max character length of 100. The text fits within the spinner but when I select the entry it doesn't autosize in the result. If I remove the android:layout_height then the app crashes.
Spinner xml with autoSizeTextType
<Spinner
android:id="#+id/spinnerCompany"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginTop="43dp"
android:paddingLeft="16dp"
android:spinnerMode="dropdown"
app:autoSizeTextType="uniform"
app:layout_constraintTop_toBottomOf="#+id/otpLabel"
tools:layout_editor_absoluteX="27dp" />
Spinner open, text fits
Spinner item selected, text is cut-off
It's not a good practice to specify the layout height for a Text, instead, use textSize and let the height and width wrap_content.
<Spinner
android:id="#+id/spinnerCompany"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="13sp" (this is just random, find the ones fits your usage)
android:layout_marginTop="43dp"
android:paddingLeft="16dp"
android:spinnerMode="dropdown"
app:autoSizeTextType="uniform"
app:layout_constraintTop_toBottomOf="#+id/otpLabel"
tools:layout_editor_absoluteX="27dp" />
activity_main.xml
<Spinner
android:id="#+id/spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
spinner_item.xml create custom spinner:
<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20dp"
android:padding="5dp"
/>
In your java class onCreate method write:
// Get reference of widgets from XML layout
Spinner spinner = (Spinner) findViewById(R.id.spinner);
// Initializing a String Array
String[] plants = new String[]{
"Black birch",
"Bolean birch",
"Canoe birch",
"Cherry birch",
"European weeping birch"
};
// Initializing an ArrayAdapter
ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(
this,R.layout.spinner_item,plants
);
spinnerArrayAdapter.setDropDownViewResource(R.layout.spinner_item);
spinner.setAdapter(spinnerArrayAdapter);
You can simply do this
<Spinner
android:id="#+id/bloodGrpSpinner"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:entries="#array/bloodGrps" />
You are giving wrap content to width in your layout simply make it match_parent and it will automatically pick the max length it can
I tried to make Spinner with white text as my theme but when Spinner popup show then it has default white background so my text isn't visible there. I also tried to set with both android:popupBackground and app:popupBackground but it doesn't reflect there.
Screenshots of Spinner :
Spinner 1
Spinner Popup 2
XML for spinner
<android.support.v7.widget.AppCompatSpinner
android:id="#+id/spinLocation"
style="#style/Base.Widget.AppCompat.Spinner.Underlined"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:popupBackground=""
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="20dp"
app:backgroundTint="#color/white" />
XML for Spinner Item
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/txtValue"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ellipsize="marquee"
android:maxLines="1"
android:paddingBottom="7dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingTop="7dp"
android:text=""
android:textColor="#color/white"
android:textSize="#dimen/_12sdp" />
I know it is because of item textColor but if I do not define it then it showing in popup but not proper in Spinner. So suggest me a best approach for this and also suggest for header if appCompatSpinner supports it. Thanks.
You need to set themes, or create a custom one.
Here is a sample code:
<android.support.v7.widget.AppCompatSpinner
style="#style/Base.Widget.AppCompat.Spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:entries="#array/array_radius"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"
app:theme="#style/ThemeOverlay.AppCompat.Light" />
Possibly you can add your own Textview for spinner
<Spinner
android:id="#+id/carMake"
android:textColor="#606060"
android:textSize="12sp"
android:layout_gravity="end"
android:layout_alignParentEnd="true"
android:layout_below="#+id/vin_border"
android:layout_marginTop="8dp"
android:gravity="center_vertical"
android:layout_width="wrap_content"
android:layout_height="14dp">
</Spinner>
java for adding items
List<String> model = new ArrayList<>();
model.add("Ford");
model.add("BMW");
model.add("Audi");
model.add("Hyundai");
model.add("Suzuki");
model.add("Porsche");
ArrayAdapter<String> dataAdapter = new ArrayAdapter<>(getContext(), R.layout.spinner_textview_layout, model);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
carMake.setAdapter(dataAdapter);
textview for spinner item
<com.tagx.view.CustomTextView
app:font="#string/monte_light"
android:layout_width="wrap_content"
android:maxLines="1"
style="?android:attr/spinnerItemStyle"
android:ellipsize="marquee"
android:paddingStart="=20dp"
android:paddingEnd="40dp"
android:textColor="#color/fragment_owner_title_color"
android:layout_gravity="end"
android:textSize="12sp"
android:layout_height="wrap_content"
android:gravity="center"
/>
Hello how can i remove this padding in spinner item? And how to remove underline? My code you can saw below.
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="DETERGENT"
android:id="#+id/textView"
android:gravity="center_horizontal"
android:layout_marginTop="18dp"
android:textSize="14dp" android:textStyle="bold"/>
<Spinner
android:layout_width="fill_parent"
android:layout_height="36dp"
android:id="#+id/spinner_detergent"/>
Java code:
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.spinner_item, spinnerArray);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerDetergent.setAdapter(adapter);
Spinner_item.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="20dp"
android:textSize="18dp"
android:textColor="#000000"
android:gravity="center"
android:text="dfdfdf"
android:paddingLeft="0dp"
android:paddingRight="0dp"/>
Hi If you want to remove that line you can write a background. For that you simply place an image in the drawable and set that by using below tag.
android:background="#drawable/spinner_background"
or
android:background="#android:color/transparent"
And the second one is your given spcific height. so remove that and give wrapcontent or try for small heights. Then that space will goes.
Spinner spinner = (Spinner) findViewById(R.id.spinner);
//simple_spinner_item Specify the spinner TextView
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
R.array.planets_array, android.R.layout.simple_spinner_item);
// simple_spinner_dropdown_item Specify the dropdown item TextView if not set , and the same as simple_spinner_item
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
and used as:
citySpinner=(Spinner) findViewById(R.id.cityspinner);
ArrayAdapter<CharSequence> cityadapter = ArrayAdapter.createFromResource(this, R.array.beijing_city, R.layout.spinner_item);
cityadapter.setDropDownViewResource(R.layout.spinner_down_item);
citySpinner.setAdapter(cityadapter);
my spinner_down_item.xml is:
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:background="#drawable/spinner_textview_rounded_corners"
android:id="#+id/text1"
android:textColor="#ffff0000"
android:gravity="center"
android:singleLine="true"
android:layout_width="fill_parent"
android:layout_height="40dip"
android:ellipsize="marquee" />
but the dropDownView as: http://i.stack.imgur.com/S2dcA.jpg
you can see there is a default White rectangle border On the outermost layer,my question is how to remove it and not show scrollbar.thank you