7arooney 7arooney -4 years ago 225
Android Question

How to create filter icon in ActionBar Toolbar programatically

I just Need to create a tool bar like this
Example

Have a filter icon but I want dynamically add items taken from data base as if I Click item it will be sorted in this menu fliter icon
I hope that's gonna be clear
here's my menu xml code

<menu xmlns:android="http://schemas.android.com/apk/res/android" >

<item
android:id="@+id/menu_spinner"
android:actionViewClass="android.widget.Spinner"
android:visible="false"
android:showAsAction="always"/>

</menu>


and this my MainActivity

package abtech.waiteriano.com.actionbartest;

import android.app.ActionBar;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Spinner;

public class MainActivity extends Activity {

private MenuItem mSpinnerItem = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu( Menu menu )
{
mSpinnerItem = menu.findItem( R.id.menu_spinner );
getMenuInflater().inflate( R.menu.main, menu );
mSpinnerItem = menu.findItem( R.id.menu_spinner );
setupSpinner( mSpinnerItem );
return true;
}
private void setupSpinner( MenuItem item )
{
item.setVisible( getActionBar().getNavigationMode() == ActionBar.NAVIGATION_MODE_LIST );
View view = item.getActionView();
if (view instanceof Spinner)
{
Spinner spinner = (Spinner) view;
spinner.setAdapter( ArrayAdapter.createFromResource( this,
R.array.spinner_data,
android.R.layout.simple_spinner_dropdown_item ) );
}
}
}


Error

Answer Source

you can achieve your desired view as follows .

activity_main.xml

<LinearLayout
    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”
    tools:context=”.MainActivity”>
    <android.support.v7.widget.Toolbar
        android:id=”@+id/toolbar”
        android:layout_width=”match_parent”
        android:layout_height=”wrap_content”
        android:background=”?attr/colorPrimary”
        android:minHeight=”?attr/actionBarSize” />
</LinearLayout>

create a spinner_dropdown_item.xml which contains TextView which represents view for each items of spinner

spinner_dropdown_item.xml

<?xml version=”1.0″ encoding=”utf-8″?>
<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:background=”@color/colorPrimary”
    android:gravity=”center_vertical”
    android:minHeight=”?android:attr/listPreferredItemHeightSmall”
    android:paddingLeft=”12dp”
    android:paddingRight=”12dp”
    android:textAppearance=”?android:attr/textAppearanceListItemSmall” />

MainActivity.java

public class MainActivity extends AppCompatActivity {
    private Toolbar toolbar=null;
    private String[] category={"hello","this","is","me"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        category = getResources().getStringArray(R.array.category);

        toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayShowTitleEnabled(false);
        toolbar.setLogo(R.drawable.ic_drawer);

        SpinnerAdapter spinnerAdapter = ArrayAdapter.createFromResource(getApplicationContext(), R.array.category, R.layout.spinner_dropdown_item);
        Spinner navigationSpinner = new Spinner(getSupportActionBar().getThemedContext());
        navigationSpinner.setAdapter(spinnerAdapter);
        toolbar.addView(navigationSpinner, 0);

        navigationSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                Toast.makeText(MainActivity.this,
                        “you selected: ” + category[position],
                        Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });

    }
   @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu, menu);
        return true;

    }
}

this your menu.xml file

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context="com.na.quiz.activity.WelcomeActivity">

    <item
        android:id="@+id/action_logout"
        android:orderInCategory="100"
        android:title="@string/action_logout"
        app:showAsAction="always"
        android:icon="@android:drawable/ic_menu_search" />
    <item
        android:title="LIke"
        android:orderInCategory="101"
        android:id="@+id/like"
        app:showAsAction="always"
        android:icon="@drawable/com_facebook_button_like_icon_selected" />

</menu>

I hope it works for you.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download