Шах Шах - 5 months ago 95
Android Question

Toolbar Spinner doesn't show a text

UPDATED

I need to make a

Spinner
in the
Toolbar
. Below
AppBarLayout
xml:

<android.support.design.widget.AppBarLayout
android:id="@+id/appBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">

<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:fitsSystemWindows="true"
app:layout_collapseMode="pin"
app:layout_scrollFlags="scroll|exitUntilCollapsed"
app:popupTheme="@style/AppTheme.PopupOverlay"
app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

<Spinner
android:id="@+id/toolbarSpinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:popupTheme="@style/AppTheme.PopupOverlay"/>

</android.support.v7.widget.Toolbar>

<android.support.v7.widget.RecyclerView
android:id="@+id/path"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:layout_gravity="bottom"
android:clipToPadding="false"
app:layout_scrollFlags="snap"/>

</android.support.design.widget.AppBarLayout>


App style:

<resources>

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/primary</item>
<item name="colorPrimaryDark">@color/primaryDark</item>
<item name="colorAccent">@color/accent</item>
</style>

<style name="AppTheme.SolidStatusBar" parent="AppTheme" />

<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar">
<item name="colorControlHighlight">@color/black_30</item>
</style>

<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />

</resources>


Dropdown title xml:

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:orientation="vertical">

<TextView
android:id="@android:id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawablePadding="8dp"
android:drawableRight="@drawable/ic_spinner_triangle"
android:drawableEnd="@drawable/ic_spinner_triangle"
android:fontFamily="sans-serif"
android:paddingLeft="16dp"
android:paddingRight="4dp"
android:paddingStart="16dp"
android:paddingEnd="4dp"
android:textColor="@color/white"
android:textSize="18sp"
android:textStyle="bold"/>

</LinearLayout>


Dropdown item:

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:orientation="vertical">

<TextView
android:id="@android:id/text1"
android:layout_width="match_parent"
android:layout_height="48dp"
android:drawablePadding="8dp"
android:gravity="center_vertical|start"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:textColor="#ff333333"
android:textSize="16sp"/>

</LinearLayout>


Here current result (
Spinner
doesn't show a text). What do I doing wrong?

Non-dropdown
Dropdown


Adapter

public class ToolbarSpinnerAdapter extends BaseAdapter {

private static final String TAG = ToolbarSpinnerAdapter.class.getSimpleName();

private List<Item> mItems = new ArrayList<>();
private LayoutInflater mInflater;

public ToolbarSpinnerAdapter(Context context) {
mInflater = LayoutInflater.from(context);
}

public void clear() {
mItems.clear();
}

public void add(final Item item) {
mItems.add(item);
}

public void addAll(List<Item> items) {
mItems.addAll(items);
}

@Override
public int getCount() {
return mItems.size();
}

@Override
public Object getItem(final int position) {
return mItems.get(position);
}

@Override
public long getItemId(final int position) {
return position;
}

@Override
public View getDropDownView(final int position, View view, ViewGroup parent) {

if (view == null || !view.getTag().toString().equals("DROPDOWN")) {
view = mInflater.inflate(R.layout.toolbar_si_dropdown, parent, false);
view.setTag("DROPDOWN");
}

TextView textView = (TextView) view.findViewById(android.R.id.text1);
textView.setText(getTitle(position));

return view;
}

@Override
public View getView(final int position, View view, ViewGroup parent) {

if (view == null || !view.getTag().toString().equals("NON_DROPDOWN")) {
view = mInflater.inflate(R.layout.toolbar_si, parent, false);
view.setTag("NON_DROPDOWN");
}

TextView textView = (TextView) view.findViewById(android.R.id.text1);
textView.setText("Test");
return view;
}

private String getTitle(final int position) {
return position >= 0 && position < mItems.size() ? mItems.get(position).mTitle : "";
}

public static final class Item {
private String mTitle;
private Object mTag;

public Item(final String title) {
this.mTitle = title;
}

public String getTitle() {
return mTitle;
}

public Item setTitle(final String title) {
mTitle = title;
return this;
}

@Nullable
public Object getTag() {
return mTag;
}

public Item setTag(@Nullable Object tag) {
mTag = tag;
return this;
}
}
}


ADDED

I understood my bug. Method
public View getView(final int position, View view, ViewGroup parent)
is not called in the adapter (only for dropdown views). What could be behind the magic? (I checked it in the LogCat).

Answer

I don't think you want to set the theme for you Spinner, as it should inherit its attributes from the theme overlay you specified in your Toolbar. But, I don't think it inherits the popup theme you specified, so you should set that in your Spinner as well. Also, there is no need for an extra LinearLayout to wrap your Spinner. You can either use the Spinner as the root object in your xml:

<Spinner xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/spinner"
    android:layout_width="wrap_content" android:layout_height="wrap_content"
    app:popupTheme="@style/AppTheme.PopupOverlay"/>

Or place it directly in your Toolbar:

<android.support.v7.widget.Toolbar android:id="@+id/toolbar"
    android:layout_width="match_parent" android:layout_height="?attr/actionBarSize"
    android:background="?attr/colorPrimary" android:fitsSystemWindows="true"
    app:layout_scrollFlags="scroll|exitUntilCollapsed" app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    app:layout_collapseMode="pin" app:popupTheme="@style/AppTheme.PopupOverlay">

    <Spinner android:id="@+id/spinner"
        android:layout_width="wrap_content" android:layout_height="wrap_content"
        app:popupTheme="@style/AppTheme.PopupOverlay" />

</android.support.v7.widget.Toolbar>

Lastly, make sure your are not displaying the title in your Toolbar:

setSupportActionBar(mToolbar);
getSupportActionBar().setDisplayShowTitleEnabled(false);