Samvid Kulkarni
Samvid Kulkarni

Reputation: 129

Spinner text center align both vertically and horizontally

I want to align text inside the spinner both vertically and horizontally centered. I am following the tutorial from this site(first tutorial only).

My problem is that android studio not able to find the spinner_center_item even though it is stored at location \layout\spinner_center_item.xml.

here is my code

menu = (Spinner)findViewById(R.id.spinner1);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.optionmenu, android.R.layout.spinner_center_item);
adapter.setDropDownViewResource(android.R.layout.spinner_center_item);
menu.setAdapter(adapter);
menu.setSelection(0);

here is my main xml code

 <Spinner
        android:layout_width="wrap_content"
        android:layout_height="60dp"
        android:id="@+id/spinner1"
        android:entries="@array/optionmenu"
        android:layout_alignParentTop="true"

        android:layout_alignParentStart="true"
        android:layout_toLeftOf="@+id/setting"
        android:background="@drawable/button_border"
        android:textColor="@android:color/white"  />

here is my code from \layout\spinner_center_item.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="match_parent"
    android:layout_height="wrap_content"
    android:paddingBottom="5dp"
    android:paddingTop="5dp"
    android:gravity="center_vertical|center_horizontal"
    />

here is my code from string.xml

 <string-array name="optionmenu">
        <item>Categories</item>
        <item>Calculator</item>
        <item>unit Converter</item>

    </string-array>

here is the error

C:\Users\Samvid\AndroidStudioProjects\SamsUltimateAllPurposeCalculator\app\src\main\java\com\sams\ultimateallpurpose\calculator\Main_Calculator.java
Error:(67, 120) error: cannot find symbol variable spinner_center_item
Error:(68, 57) error: cannot find symbol variable spinner_center_item
Error:Execution failed for task ':app:compileDebugJava'.
> Compilation failed; see the compiler error output for details.

Upvotes: 2

Views: 1892

Answers (2)

Avinash Kumar Pankaj
Avinash Kumar Pankaj

Reputation: 1720

change your this code

ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.optionmenu, android.R.layout.spinner_center_item);
    adapter.setDropDownViewResource(android.R.layout.spinner_center_item);

from

ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.optionmenu, android.R.layout.spinner_center_item);
    adapter.setDropDownViewResource(R.layout.spinner_center_item);

Upvotes: 3

Hamid Shatu
Hamid Shatu

Reputation: 9700

Change the resource id of spinner_center_item.xml from this

android.R.layout.spinner_center_item

to this

R.layout.spinner_center_item

Here, android.R means you are trying find the spinner_center_item.xml from android package but its located in the layout folder.

Upvotes: 4

Related Questions