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.
Related
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'm using an ArrayAdapter to populate my listView:
val adapter = ArrayAdapter(applicationContext, R.layout.listview_text, emailAddresses)
listView.adapter = adapter
and this layout for custom textView:
<?xml version="1.0" encoding="utf-8"?>
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
xmlns:android="http://schemas.android.com/apk/res/android"
android:background="#FFFFFF"
android:textSize="16sp"
android:paddingTop="35dp"
android:fontFamily="#font/familiar_pro_bold"
android:textColor="#212121"
android:id="#+id/listText"/>
Now, I want to display an icon (as a "view full item") at right of each items (which are added by user).
I want to avoid hundred of lines of code just for an image, what can I do?
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
xmlns:android="http://schemas.android.com/apk/res/android"
android:background="#FFFFFF"
android:textSize="16sp"
android:paddingTop="35dp"
android:fontFamily="#font/familiar_pro_bold"
android:textColor="#212121"
android:id="#+id/listText"
android:drawableRight="#drawable/view_full_item"/>
Also you can add some padding with android:drawablePadding=xdp
I am a beginner in android. I have a question why the spinner is not showing in design window?. For your help I have added a code below
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Text View!"
android:paddingRight="100dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:id="#+id/brand"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:layout_below="#+id/color"
android:layout_alignLeft="#+id/color"
android:id="#+id/find_beer"
/>
<Spinner
android:id="#+id/color"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="72dp"
>
</Spinner>
enter image description here
The Constraints you are using are incorrect. Change your layout as follows:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
>
<Spinner
android:id="#+id/color"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"></Spinner>
<Button
android:id="#+id/find_beer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
app:layout_constraintBottom_toBottomOf="#+id/color"
app:layout_constraintStart_toEndOf="#+id/color"
app:layout_constraintTop_toBottomOf="#+id/color" />
<TextView
android:id="#+id/brand"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="57dp"
android:layout_marginRight="57dp"
android:paddingRight="100dp"
android:text="Text View!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
Setting following attribute:
android:layout_width="wrap_content"
android:layout_height="wrap_content"
Means following:
The view should be only big enough to enclose its content
Even if you correct your constraints, remember to add some content to the Spinner otherwise you will just see drop down arrow on the screen.
change the layout type like a linearlayout with orientation vertical or relativelayout set layout after you set value with using java code or xml using resources
<resources>
<string-array name="planets_array">
<item>Mercury</item>
<item>Venus</item>
<item>Earth</item>
<item>Mars</item>
<item>Jupiter</item>
<item>Saturn</item>
<item>Uranus</item>
<item>Neptune</item>
</string-array>
java code
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.simple_spinner_item);
// Specify the layout to use when the list of choices appears
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the spinner
spinner.setAdapter(adapter);
using layout xml into spinner tag
android:entries="#array/planets_array"
XML code.
<Spinner
android:id="#+id/spCountry"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="25dp"
android:layout_centerVertical="true"
android:layout_gravity="center"
android:background="#android:color/transparent"
android:gravity="center"
android:spinnerMode="dropdown" />
Code for activity
Spinner spCountry;
spRole = (Spinner)getView().findViewById(R.id.spCountry);
String[] arRoleSpinner = {"India","USA","NWZ","SA", "WI","ENG"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),android.R.layout.simple_spinner_item, arRoleSpinner);
adapter.setDropDownViewResource(R.layout.spinner_item);
spRole.setAdapter(adapter);
spRole.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
((TextView) adapterView.getChildAt(0)).setTextColor(Color.WHITE);
((TextView) adapterView.getChildAt(0)).setTextSize(12);
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
use this code
<Spinner
android:id="#+id/color"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="72dp" ></Spinner>
because in spinner (drop down list) we add items at run time (in java code) so it show when we run app. and also every change in java code like color change , Visibility etc show when we run the app.
and also visit this https://www.javatpoint.com/android-spinner-example
I'm trying to set attributes to a spinner on my android app and one of the attributes in textAlignment = "right" (the problem being I've a spinner that layout_width="match_parent" so there's a lot a space and I'd like to have it to the right) but this is only supported in API 17 and up whereas I want to make an app for API 16 - Is there a work-around?
My attributes are:
<Spinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:textAlignment="gravity"
android:id="#+id/MainSpinner"
tools:listitem="#layout/support_simple_spinner_dropdown_item"/>
The current linearLayout looks like this:
And I want it to look like this:
Where:
LinearLayout (horizontal) =
RelativeLayout =
TextView =
LinearLayout (Vertical) =
Spinner =
Button =
This ans work for me...
<Spinner
android:id="#+id/example_spinner"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_gravity="right"
android:textAlignment="right"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingTop="2dp"
android:paddingBottom="2dp" />
I've followed this : http://nevescheng.blogspot.fr/2013/05/spinner-with-item-text-aligned-to-center.html & this worked fine...
You can create an adapter like this :
SpinnerAdapter spinnerAdapter = new ArrayAdapter<String>(this, R.layout.spinner_item, new String[]{"tata", "toto", "titi"});
spriner.setAdapter(spinnerAdapter );
and in your layout (res/layout/spinner_item.xml):
<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:gravity="center_vertical|end"
android:paddingStart="?android:attr/listPreferredItemPaddingStart"
android:paddingEnd="?android:attr/listPreferredItemPaddingEnd"
android:background="?android:attr/activatedBackgroundIndicator"
android:minHeight="?android:attr/listPreferredItemHeightSmall" />
Heyy Jeremy, Try to add android:textDirection="rtl" attribute in your spinner. This attribute will give the text a direction from right to left.
<Spinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:textAlignment="gravity"
android:textDirection="rtl"
android:id="#+id/MainSpinner"
tools:listitem="#layout/support_simple_spinner_dropdown_item"/>
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.