how to remove the default White border of dropdown of spinner - android

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

Related

Spinner selected text no resizing when selected

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

Spinner style and databinding

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.

How to shorten space between triangle and text in spinner?

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" />

Setting the text to be in the center works for one spinner, but not for other

In my application I've got 3 spinners - one on the top, which when used provides the user with 2 more aligned below it.
I looked at this question where I found the answer I was looking for, but only half-way.. Now the two supplementary spinners have their text shown in the center, but the first one - no.
Looking at the .xml for the spinners I can see only one difference regarding to centering and it is that the two supplementary ones have the android:layout_gravity="center" attribute, which for some reason is not applicable to the first spinner. Why is this so? Could this be the reason? By not applicable I mean it is not in the properties for the spinner in the design view and when I add it manually in the .xml nothing changes.
So ideally the answer which I am looking for is why the 2 supplementary spinners have their text centered, but the first one - no? Note I am not interested in centering the elements in the popup, this is purely optional. I just want to have the selected option to be in the center.
Here is a screenshot of how it looks now with the problem in red:
Here is the .xml I am using:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/some_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="packagename"
tools:showIn="#layout/specific_activity_layout"
android:gravity="center_horizontal"
android:background="#FF9800">
<Spinner
android:layout_width="fill_parent"
android:layout_height="50dp"
android:id="#+id/spinner1"
android:entries="#array/spinner1entries"
android:layout_centerHorizontal="true"
android:layout_marginBottom="15dp"
android:gravity="center|top"
android:background="#drawable/rounded_corners"
android:paddingTop="4dp"
android:paddingBottom="4dp"
android:popupBackground="#FFB74D"
android:layout_alignParentEnd="false"
android:textAlignment="center" />
<LinearLayout
android:id="#+id/horizontal_layout_spinners"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentEnd="false"
android:layout_alignParentStart="false"
android:visibility="invisible"
android:layout_below="#+id/spinner1">
<Spinner
android:layout_width="wrap_content"
android:layout_height="50dp"
android:id="#+id/spinner2"
android:visibility="visible"
android:background="#drawable/rounded_corners"
android:layout_marginRight="5dp"
android:popupBackground="#FFB74D"
android:textAlignment="center"
android:gravity="center|top"
android:layout_weight="0.5"
android:layout_gravity="center" />
<Spinner
android:layout_width="wrap_content"
android:layout_height="50dp"
android:id="#+id/spinner3"
android:visibility="visible"
android:layout_marginLeft="5dp"
android:background="#drawable/rounded_corners"
android:popupBackground="#FFB74D"
android:textAlignment="center"
android:gravity="center|top"
android:layout_weight="0.5"
android:layout_gravity="center" />
</LinearLayout>
</RelativeLayout>
And the rounded_corners file:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#FFB74D"/>
<stroke android:width="0dip" android:color="#B1BCBE" />
<corners android:radius="10dip"/>
<padding android:left="0dip" android:top="0dip" android:right="0dip" android:bottom="0dip" />
</shape>
Try adding :
android:dropDownWidth="wrap_content" to the spinner.
You can try to set the text style programatically after an item is selected from the spinner. Set an setOnItemSelectedListener and inside it set TextView to be centred.
Spinner spinner1 = (Spinner) findViewById(R.id.spinner1);
spinner1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View view, int arg2, long arg3) {
// Set the text style after item is selected.
setSpinnerSelectedItemParams(spinner1, getActivity());
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
/**
* Sets the spinner text style to a different one after one value has been chosen.
* #param spinner Spinner currently in use
*/
public void setSpinnerSelectedItemParams(Spinner spinner, Context context) {
if (spinner.getChildCount() > 0){
TextView tvSpinner = ((TextView) spinner.getChildAt(0));
tvSpinner.setPadding(0, 0, 0, 0);
tvSpinner.setGravity(Gravity.CENTER);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
params.gravity = Gravity.CENTER;
params.setMargins(0, 0, 0, 0);
tvSpinner.setLayoutParams(params);
}
}
If you want the text before selecting it in centre as well, try setting a custom resource when creating the adapter.
In Activity or Fragment:
ArrayAdapter<String> yourAdapter = new ArrayAdapter(activity, R.layout.default_spinner_adapter_view, yourDataToFillTheSpinner);
"default_spinner_adapter_view.xml" file:
<?xml version="1.0" encoding="utf-8"?>
<!-- This is the view for each individual item of the spinner-->
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/tv_default_spinner"
style="?android:attr/spinnerItemStyle"
android:layout_width="match_parent
android:layout_height="wrap_content"
android:layout_gravity="center"
android:ellipsize="marquee"
android:gravity="center"
android:singleLine="true"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#android:color/black" />
If you want to customize the dropdown list items you will need to create a new layout file. Let’s call it spinner_dropdown_item.xml:
<?xml version="1.0" encoding="utf-8"?>
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/text1"
style="?android:attr/spinnerDropDownItemStyle"
android:singleLine="true"
android:layout_width="match_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:ellipsize="marquee"
android:textColor="#aa66cc"/>
Another change in the declaration of the spinner:
ArrayAdapter adapter = ArrayAdapter.createFromResource(this,R.array.planets_array, R.layout.spinner_item);
adapter.setDropDownViewResource(R.layout.spinner_dropdown_item);
spinner.setAdapter(adapter);
Make your custom list item of spinner and set as you want and that should have a TextView id text1 and use this list_item.xml like below
Spinner spinner = (Spinner) findViewById(R.id.spinner);
// Create an ArrayAdapter using the string array and a default spinner
layout
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
R.array.planets_array, android.R.layout.your_list_item);
// Specify the layout to use when the list of choices appears
adapter.setDropDownViewResource(android.R.layout.your_drop_down_list_item);
// Apply the adapter to the spinner
spinner.setAdapter(adapter);
change relative layout to linear layout vertical and width of the spinner should be match_parent.
Make sure all spinners are using the same type of adapter—or at least another adapter using the same item layout—the adapter provides the layout.

Remove padding in spinner

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);

Categories

Resources