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);
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 wanna change the style of my spinner (make a bigger text and align it to the centre).
Usually, I created layout file spinner_item.xm
<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:textSize="20sp"
android:gravity="center" />
And change the declaration of the spinner:
ArrayAdapter adapter = ArrayAdapter.createFromResource(this, R.array.planets_array, R.layout.spinner_item);
spinner.setAdapter(adapter);
However, how should I achieve that when I'm using databinding.
As you said, you're using a xml file as adapter, so you have a TextView under the xml, you can try to change the property android:textSize to a value above the current and the property android:textAlignment to center.
Example:
<TextView
android:id="#+id/text_view_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAlignment="center"
android:textSize="30sp" />
I hope it will help you.
i am a beginner to android i tried in please help
my code is
<AutoCompleteTextView
android:id="#+id/autoCompleteTextView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="top"
android:hint="#string/receipt_account_name" >
i tried with android:dropDownVerticalOffset="5dp", android:dropDownHeight="5dp", android:height="5dp", android:layout_height="5dp" etc. No font size is defined.
in java class
adapter = new ArrayAdapter(this, android.R.layout.simple_dropdown_item_1line, COUNTRIES);
please help.
create your own xml like this
layout/item.xml
<TextView
android:id="#+id/text1"
android:layout_width="wrap_content"
android:layout_height="5dp"/>
then try following
adapter = new ArrayAdapter<T>(this, R.layout.item,R.id.text1, COUNTRIES);
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