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.
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 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"/>
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);
In TextView control, I can set android:singleLine="true" and android:ellipsize="end" to truncate a long string,
but I don't know how to set the property for ListView control, could you help me ? Thanks!
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:background="#DDDDDD"
android:layout_weight="1" />
<TextView
android:id="#+id/textView5"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ellipsize="end"
android:singleLine="true"
android:text="This is a web This is a web This is a web This is a web I will go to school" />
Ellipsizing a ListView doesn't even make any sense. Rather, you'll want to set the ellipsize attribute on any TextViews in the layout you're inflating for the list item in your Adapter.
When you create the adapter by doing
new SimpleCursorAdapter(this, android.R.layout.simple_list_item_multiple_choice,
cur, cols, views);
you're passing in the layout file that it's going to use to display the text.
As of SDK v17, that layout file looks like this:
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/text1"
android:layout_width="match_parent"
android:layout_height="?android:attr/listPreferredItemHeightSmall"
android:textAppearance="?android:attr/textAppearanceListItemSmall"
android:gravity="center_vertical"
android:checkMark="?android:attr/listChoiceIndicatorMultiple"
android:paddingStart="?android:attr/listPreferredItemPaddingStart"
android:paddingEnd="?android:attr/listPreferredItemPaddingEnd"
/>
Note that the text is not ellipsized.
Instead of passing in that layout, create your own similar layout file, with android:ellipsize set, and then pass in that layout to the adapter.
Make sure to use #android:id/text1 as the android:id for the TextView, as that's what the adapter uses to find it.
I am using a spinner in several places in my program, but I will stick with one case.
I have two xml files -- small_new_system and new_system. They both have the a spinner that is named state_spinner.
The odd thing is that when I use this code on a tablet running 3.2, which uses new_system, they are displayed but when I put the app on my phone that is running 2.1, which uses small_new_system, they do not show up. The items are in the spinner list, but there is no text being displayed. I have tried naming the spinners differently, as well as not using a custom spinner layout.
The other odd thing is that when I use the identical layouts, which do not look very good, on the small device they are not populated with the text either.
Thank you for any help! My code is as follows:
Code to populate the spinner:
states = (Spinner) findViewById(R.id.state_spinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter
.createFromResource(this, R.array.states,
R.layout.spinner_layout);
adapter.setDropDownViewResource(R.layout.spinner_layout);
states.setAdapter(adapter);
states.setOnItemSelectedListener(new MyItemsOnSelectListener());`
Spinner in small_new_system:
<Spinner
android:id="#+id/state_spinner"
android:layout_width="200dp"
android:layout_height="55dp"
android:layout_below="#+id/city_edit"
android:layout_margin="5dp"
android:layout_alignParentRight="true"
android:inputType="textPersonName"
android:textSize="40dp" >
</Spinner>
Spinner in new_system:
<Spinner
android:id="#+id/state_spinner"
android:layout_width="500dp"
android:layout_height="75dp"
android:layout_below="#+id/city_edit"
android:layout_margin="10dp"
android:layout_alignParentRight="true"
android:textSize="60dp" >
</Spinner>`
Code in custom spinner:
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/spinnerTarget"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="left"
android:textColor="#00000000"
android:textSize="40dp" >
</TextView>`
Try removing:
android:inputType="textPersonName"
from small_new_system. Then it would look like:
<Spinner
android:id="#+id/state_spinner"
android:layout_width="200dp"
android:layout_height="55dp"
android:layout_below="#+id/city_edit"
android:layout_margin="5dp"
android:layout_alignParentRight="true"
android:textSize="40dp" >
</Spinner>
Or you might try adding a background color:
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/spinnerTarget"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="left"
android:textColor="#00000000"
android:background="#FFFFFFFF"
android:textSize="40dp" />
The solution I came up with was to use android.R.layout.simple_dropdown_item for my spinner when an sdk less than 11 is detected, and when the sdk is greater than 11 I use my custom spinner. This seems to work ok. Not my favorite solution, but it does work.
Try this one ..
spnPlan = (Spinner) findViewById(R.id.set_spinner1);
ArrayAdapter<CharSequence> planAdapter = ArrayAdapter.createFromResource(
this, R.array.plan_size, android.R.layout.simple_spinner_item);
planAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spnPlan.setAdapter(planAdapter);
spnPlan.setOnItemSelectedListener(new MyOnItemSelectedListener());