Friday, January 23, 2015

ActionBarCompat-ListPopupMenu Example in Android

Download eclipse project from here : ActionBarCompat_listPopupMenu.zip

you need add latest AppCompat-v7 library to your project.

Get icons from attached projects drawable folders.

Screen Shots:


Create menu folder under res folder and create popup.xml under menu folder and include following code.
popup.xml:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >

    <item
        android:id="@+id/menu_remove"
        android:title="@string/menu_remove"/>

</menu>


strings.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">ActionBarCompat-ListPopupMenu</string>
    <string name="hello_world">Hello world!</string>
    <string name="menu_remove">Remove</string>
    <string name="content_open_popup">Open Popup Menu</string>
    <string name="intro_message">
<![CDATA[
       
           
            This sample shows you how to use {@link android.support.v7.widget.PopupMenu PopupMenu}
            from ActionBarCompat to create a list, with each item having a dropdown menu.
           
       
        ]]>
    </string>

</resources>


styles.xml:
<resources>

    <!-- Activity themes -->

    <style name="Theme.Base" parent="android:Theme.Light" />

    <style name="Theme.Sample" parent="Theme.Base" />

    <style name="AppTheme" parent="Theme.Sample" />
    <!-- Widget styling -->

    <style name="Widget" />

    <style name="Widget.SampleMessage">
        <item name="android:textAppearance">?android:textAppearanceMedium</item>
        <item name="android:lineSpacingMultiplier">1.1</item>
    </style>

    <style name="Widget.SampleMessageTile">
        <item name="android:background">@drawable/tile</item>
        <item name="android:shadowColor">#7F000000</item>
        <item name="android:shadowDy">-3.5</item>
        <item name="android:shadowRadius">2</item>
    </style>

</resources
>

template-dimens.xml:
<resources>

    <!-- Define standard dimensions to comply with Holo-style grids and rhythm. -->

    <dimen name="margin_tiny">4dp</dimen>
    <dimen name="margin_small">8dp</dimen>
    <dimen name="margin_medium">16dp</dimen>
    <dimen name="margin_large">32dp</dimen>
    <dimen name="margin_huge">64dp</dimen>

    <!-- Semantic definitions -->

    <dimen name="horizontal_page_margin">@dimen/margin_medium</dimen>
    <dimen name="vertical_page_margin">@dimen/margin_medium</dimen>

</resources>


activity_main.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <LinearLayout
        style="@style/Widget.SampleMessageTile"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <TextView
            style="@style/Widget.SampleMessage"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="@dimen/vertical_page_margin"
            android:layout_marginLeft="@dimen/horizontal_page_margin"
            android:layout_marginRight="@dimen/horizontal_page_margin"
            android:layout_marginTop="@dimen/vertical_page_margin"
            android:text="@string/intro_message" />
    </LinearLayout>

</LinearLayout>


list_item.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="?attr/listPreferredItemHeight"
    android:orientation="horizontal" >

    <TextView
        android:id="@android:id/text1"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:ellipsize="end"
        android:gravity="center_vertical"
        android:maxLines="1"
        android:paddingLeft="8dp"
        android:paddingRight="8dp"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <ImageView
        android:id="@+id/button_popup"
        android:layout_width="56dip"
        android:layout_height="match_parent"
        android:background="?attr/selectableItemBackground"
        android:contentDescription="@string/content_open_popup"
        android:src="@drawable/ic_overflow" />

</LinearLayout>


sample_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
    android:name="com.ram.actionbarcompat_listpopupmenu.PopupListFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />



Cheeses.java:
package com.ram.actionbarcompat_listpopupmenu;

public class Cheeses
{
    public static final String[] CHEESES =
    { "Abbaye de Belloc", "Abbaye du Mont des Cats", "Abertam", "Abondance", "Ackawi",
            "Acorn", "Adelost", "Affidelice au Chablis", "Afuega'l Pitu", "Airag",
            "Airedale", "Aisy Cendre", "Allgauer Emmentaler", "Alverca", "Ambert",
            "American Cheese", "Ami du Chambertin", "Anejo Enchilado",
            "Anneau du Vic-Bilh", "Anthoriro", "Appenzell", "Aragon", "Ardi Gasna",
            "Ardrahan", "Armenian String", "Aromes au Gene de Marc", "Asadero", "Asiago",
            "Aubisque Pyrenees", "Autun", "Avaxtskyr", "Baby Swiss", "Babybel",
            "Baguette Laonnaise", "Bakers", "Baladi", "Balaton", "Bandal", "Banon",
            "Barry's Bay Cheddar", "Basing", "Basket Cheese", "Bath Cheese",
            "Bavarian Bergkase", "Baylough", "Beaufort", "Beauvoorde", "Beenleigh Blue"
             };
}


PopupListFragment.java:
package com.ram.actionbarcompat_listpopupmenu;

import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.support.v7.widget.PopupMenu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

import java.util.ArrayList;

/**
 * This ListFragment displays a list of cheeses, with a clickable view on each
 * item whichs displays a {@link android.support.v7.widget.PopupMenu PopupMenu}
 * when clicked, allowing the user to remove the item from the list.
 */
public class PopupListFragment extends ListFragment implements View.OnClickListener
{

    @Override
    public void onActivityCreated(Bundle savedInstanceState)
    {
        super.onActivityCreated(savedInstanceState);

        // We want to allow modifications to the list so copy the dummy data
        // array into an ArrayList
        ArrayList<String> items = new ArrayList<String>();
        for (int i = 0, z = Cheeses.CHEESES.length; i < z; i++)
        {
            items.add(Cheeses.CHEESES[i]);
        }

        // Set the ListAdapter
        setListAdapter(new PopupAdapter(items));
    }

    @Override
    public void onListItemClick(ListView listView, View v, int position, long id)
    {
        String item = (String) listView.getItemAtPosition(position);

        // Show a toast if the user clicks on an item
        Toast.makeText(getActivity(), "Item Clicked: " + item, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onClick(final View view)
    {
        // We need to post a Runnable to show the popup to make sure that the
        // PopupMenu is
        // correctly positioned. The reason being that the view may change
        // position before the
        // PopupMenu is shown.
        view.post(new Runnable()
        {
            @Override
            public void run()
            {
                showPopupMenu(view);
            }
        });
    }

    // BEGIN_INCLUDE(show_popup)
    private void showPopupMenu(View view)
    {
        final PopupAdapter adapter = (PopupAdapter) getListAdapter();

        // Retrieve the clicked item from view's tag
        final String item = (String) view.getTag();

        // Create a PopupMenu, giving it the clicked view for an anchor
        PopupMenu popup = new PopupMenu(getActivity(), view);

        // Inflate our menu resource into the PopupMenu's Menu
        popup.getMenuInflater().inflate(R.menu.popup, popup.getMenu());

        // Set a listener so we are notified if a menu item is clicked
        popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener()
        {
            @Override
            public boolean onMenuItemClick(MenuItem menuItem)
            {
                switch (menuItem.getItemId())
                {
                case R.id.menu_remove:
                    // Remove the item from the adapter
                    adapter.remove(item);
                    return true;
                }
                return false;
            }
        });

        // Finally show the PopupMenu
        popup.show();
    }

    // END_INCLUDE(show_popup)

    /**
     * A simple array adapter that creates a list of cheeses.
     */
    class PopupAdapter extends ArrayAdapter<String>
    {

        PopupAdapter(ArrayList<String> items)
        {
            super(getActivity(), R.layout.list_item, android.R.id.text1, items);
        }

        @Override
        public View getView(int position, View convertView, ViewGroup container)
        {
            // Let ArrayAdapter inflate the layout and set the text
            View view = super.getView(position, convertView, container);

            // BEGIN_INCLUDE(button_popup)
            // Retrieve the popup button from the inflated view
            View popupButton = view.findViewById(R.id.button_popup);

            // Set the item as the button's tag so it can be retrieved later
            popupButton.setTag(getItem(position));

            // Set the fragment instance as the OnClickListener
            popupButton.setOnClickListener(PopupListFragment.this);
            // END_INCLUDE(button_popup)

            // Finally return the view to be displayed
            return view;
        }
    }

}


MainActivity.java:
package com.ram.actionbarcompat_listpopupmenu;

import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;

/**
 * This sample shows you how to use {@link android.support.v7.widget.PopupMenu
 * PopupMenu} from ActionBarCompat to create a list, with each item having a
 * dropdown menu.
 * <p>
 * The interesting part of this sample is in {@link PopupListFragment}.
 *
 * This Activity extends from {@link ActionBarActivity}, which provides all of
 * the function necessary to display a compatible Action Bar on devices running
 * Android v2.1+.
 */
public class MainActivity extends ActionBarActivity
{

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        // Set content view (which contains a PopupListFragment)
        setContentView(R.layout.sample_main);
      
      
    }

}
 

ActionBarCompat-ListPopupMenu Example in Android Rating: 4.5 Diposkan Oleh: Unknown

0 comments:

Post a Comment