Android - Text is Pushed to the Left in a Spinner - android

I have a spinner with a style. The style only contains a background for the spinner. The problem is,that no matter which image I use for the background, the text of the spinner is always pushed to the left.
This is the declaration of the Spinner in the XML -
<Spinner
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/minus"
android:layout_width="wrap_content"
android:layout_below="#+id/female"
android:id="#+id/spin"
android:gravity="center"
android:background="#drawable/spin"
android:layout_marginTop="10dip">
</Spinner>
Also, I get a warning under the android:gravity attribute that says it's an unknown XML attribute.
I can't figure why it does that.
Thanks

Continuing from my last comment above...
The following code modifies the Hello Spinner tutorial application to display the spinner text contents centered horizontally, as seen in the following two screenshots.
res/layout/my_spinner_textview.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
style="?android:attr/spinnerItemStyle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center" />
public class HelloSpinner extends Activity
{
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Spinner spinner = (Spinner) findViewById(R.id.spinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.planets_array,
// android.R.layout.simple_spinner_item);
R.layout.my_spinner_textview);
// adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
adapter.setDropDownViewResource(R.layout.my_spinner_textview);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new MyOnItemSelectedListener());
}
//No other modification needed.
This hopefully provides enough direction to fix the problem.

Use android:textAlignment="center" tag on spinner
<android.support.v7.widget.AppCompatSpinner
android:id="#+id/state"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAlignment="center"/>

Add gravity in your xml file...
<Spinner
android:gravity="center">
</Spinner>
Or add gravity in your java code...
Spinner spinner = (Spinner)findViewById(R.id.spinner);
spinner.setGravity(17);
//17 = center

I had this problem as well and the accepted answer did not work to me.
If this also applies to you, make sure that no parent layout overrides the gravity of the spinner item. In my case, I had to set the gravity of the entire Spinner to right.

If you want to see the checkboxes in the dropdown menu while using your own layout:
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
mContext, R.array.room_list, R.layout.spinner_layout);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
yourSpinner.setAdapter(adapter);
Please notice the "android" before "R.layout...." in the line where you put the drop down view resource!

Related

android Setting style to programmatically created spinner

I have a spinner which is created programmatically like this:
spinnerLogger = new Spinner(context);
spinnerLogger.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
SpinnerAdapter spinnerAdapter = new ArrayAdapter<>(context, android.R.layout.simple_spinner_dropdown_item, spinnerNames);
spinnerLogger.setAdapter(spinnerAdapter);
in my code I also use xml created spinners which use a style to make sure the user understands they are spinners:
<Spinner
android:layout_width="90dp"
android:layout_height="wrap_content"
android:id="#+id/spinnerHour"
android:spinnerMode="dropdown"
style="#style/Widget.AppCompat.Spinner.Underlined" />
I want to apply the Widget.AppCompat.Spinner.Underlined style to the programmatically created spinner but can't seem to find how I can achieve this.
I tried by creating an xml containing only a spinner with the right style (xml's name is spinner_underlined and it's located in the layout folder):
<?xml version="1.0" encoding="utf-8"?>
<Spinner android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/spinner"
xmlns:android="http://schemas.android.com/apk/res/android"
style="#style/Widget.AppCompat.Spinner.Underlined"/>
when i try to apply this using
SpinnerAdapter spinnerAdapter = new ArrayAdapter<>(context, android.R.layout.spinner_underlined, spinnerNames);
it gives me a cannot resolve symbol 'spinner_underlined' error
You need change to R.layout.... not android.R.layout....

Android spinner changing width causing drop down item to not display

I have a spinner which contains a list of weeks (1->52) plus an additional 'All Weeks' option. When ever I select a week number, such as '1', the spinner width decreases to the size needed to display '1'. This causes problems when trying to select 'All Weeks' again as the spinner drop down width is too small for the 'All Weeks' option to display, causing it to be a blank entry at the top. You can still select the blank entry however, and doing so causes the spinner to act as I would expect it (where the width is at least wide enough for the 'All Weeks' text).
Is there a way around this? I have come across android:dropDownWidth attribute, which setting it to 100dp makes this problem go away. If I set it to wrap_content, it displays the same behaviour when there is no attribute, and this is what I would like ideally. I have tested on API 23 (6.0) and 22 (5.1), both have the same problem.
I have set up a basic new activity which has this problem, and the relevant spinner code is:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
protected void onResume() {
super.onResume();
Spinner spinner = (Spinner) findViewById(R.id.spinner);
ArrayAdapter<CharSequence> arrayAdapter = ArrayAdapter.createFromResource(this, R.array.week_spinner_array, android.R.layout.simple_spinner_item);
arrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(arrayAdapter);
}
Style xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp" >
<Spinner
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/spinner" />
</RelativeLayout>
Try Better Spinner library for an android. In this library you don't have to use that All Weeks tag. It also has cool animations.
BetterSpinner

How to remove radio button in spinner only in layout view android

I have strange problem with spinner.
My spinner display radio button when i don't use it, check this out:
Image 1 Radio when not use
Image 1 Radio when use spinner
Someone know how fix it? I want radio button when i click on spinner and dropdown list shows, but now when i dont use spinner.
Thanks in advance.
when you set adapter for your spinner you can set it's layout like this:
String[] arr = new String[]{"a","b","c"};
selectionSpinner = (Spinner) findViewById(
R.id.dr);
ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, arr);
spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
selectionSpinner.setAdapter(spinnerArrayAdapter);
main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000" >
<Spinner android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/dr" />
</RelativeLayout>
result:

How to change Spinner drop down image icon?

I want to change the default spinner drop down icon also want to change the background of it, by using which property I can do that?
I am having following spinner code:
<Spinner
android:id="#+id/security_ques"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:drawSelectorOnTop="true"
style="#style/spinner_style"
android:prompt="#string/prompt" >
</Spinner>
please give any solution on it
See this link for custom spinner drop down box.
They have created customized xml layout for spinner and called that layout to the spinner using these code
Spinner mySpinner = (Spinner) findViewById(R.id.spinner_show);
mySpinner.setAdapter(new MyAdapter(this, R.layout.custom_spinner, spinnerValues));
use it in your xml
android:background="#drawable/your_imagename"

How do I change the font color of the selection list in a spinner?

I've created a spinner, from where a user can select a value. I CAN change the textcolor of the spinner itself like shown in this picture:
Unfortunately when I press the spinner a list of items, to be selected, is shown, but these have a white font color on white background, and I just haven't been able to change this:
I've googled the problem and others have experienced the same problem. None of the fixes suggested worked for me.
As far as I understand I have to make a custom list of some kind, but well.. I'm not able to make it work. Any suggestions?
My code looks like this (array_spinner is a array of strings):
ArrayAdapter adapter = new ArrayAdapter(this, R.layout.row, array_spinner);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
And my row.xml:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/spinnerText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:textColor="#000000"
android:textSize="14dp" />
Try this (untested). I'm basically reproducing simple_spinner_dropdown_item.xml but with a black text color:
Replace the line
ArrayAdapter adapter = new ArrayAdapter(this, R.layout.row, array_spinner);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
with
ArrayAdapter adapter = new ArrayAdapter(this, R.layout.row, array_spinner);
adapter.setDropDownViewResource(R.layout.spinner_dropdown_item);
Add this file in res/layout, called spinner_dropdown_item.xml:
<CheckedTextView
android:id="#android:id/text1"
style="?android:attr/spinnerDropDownItemStyle"
android:singleLine="true"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:textColor="#FF000000"
/>
Simple and Crisp
private OnItemSelectedListener your_spinner _name = new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view, int pos,
long id) {
((TextView) parent.getChildAt(0)).setTextColor(Color.BLUE);
}
public void onNothingSelected(AdapterView<?> parent) {
}
};
I was having this same issue. My answer isn't the most proper answer, but it's a very quick solution for those using a basic app theme.
If you are creating the arrayadapter for the spinner using
createfromResource()
Do NOT use getApplicationContext(). Declare Myclass.this instead. My text now stays the same as the basic app theme. Hopefully this will help someone else.

Categories

Resources