tofira tofira - 2 months ago 17
Android Question

Android - Text is Pushed to the Left in a Spinner

I'm building an Android app, and I have a question -

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

Answer

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.

Comments