Created
July 16, 2015 17:19
-
-
Save RowlandOti/721f70ed0f311c4ab6ba to your computer and use it in GitHub Desktop.
An example pf how to create twitter-like custom menu overflow listview using ListPopupWindow
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.rowland.magent; | |
import android.support.v4.app.FragmentManager; | |
import android.support.v4.app.FragmentTransaction; | |
import android.support.v7.app.ActionBarActivity; | |
import android.os.Bundle; | |
import android.support.v7.widget.ListPopupWindow; | |
import android.support.v7.widget.Toolbar; | |
import android.util.Log; | |
import android.view.Menu; | |
import android.view.MenuItem; | |
import android.view.View; | |
import android.widget.AdapterView; | |
import android.widget.Toast; | |
import com.readystatesoftware.systembartint.SystemBarTintManager; | |
import com.rowland.adapters.ListPopupWindowAdapter; | |
import com.rowland.fragments.DetailsFragment; | |
import com.rowland.fragments.MainFragment; | |
import com.rowland.objects.Person; | |
import java.util.ArrayList; | |
public class MainActivity extends ActionBarActivity { | |
private final String LOG_TAG = MainActivity.class.getSimpleName(); | |
private boolean mTwoPane; | |
private Toolbar mToolbar; | |
private SystemBarTintManager mTintManager; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) | |
{ | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
// Initialize the mToolbar and set it as an ActionBar | |
this.mToolbar = (Toolbar) findViewById(R.id.toolbar); | |
setSupportActionBar(mToolbar); | |
getSupportActionBar().setDisplayShowTitleEnabled(false); | |
getSupportActionBar().setIcon(R.drawable.ic_logo_48px); | |
this.mToolbar.setLogo(R.drawable.ic_logo_48px); | |
// Create our manager instance after the content view is set & enable status bar tint | |
this.mTintManager = new SystemBarTintManager(this); | |
this.mTintManager.setStatusBarTintEnabled(true); | |
this.mTintManager.setTintColor(R.attr.colorPrimaryDark); | |
if (findViewById(R.id.detail_container) != null) | |
{ | |
// The detail container view will be present only in the large-screen layouts | |
// (res/layout-sw600dp). If this view is present, then the activity should be | |
// in two-pane mode. | |
mTwoPane = true; | |
// If we're being restored from a previous state, don't need to do anything | |
// and should return or else we could end up with overlapping fragments. | |
if (savedInstanceState != null) | |
{ | |
return; | |
} | |
else | |
{ | |
// In two-pane mode, show the detail view in this activity by | |
// adding or replacing the detail fragment using a fragment transaction. | |
showDetailFragment(null); | |
} | |
} | |
else | |
{ | |
mTwoPane = false; | |
} | |
showMainFragment(null); | |
} | |
private void showDetailFragment(Bundle args) | |
{ | |
FragmentManager fm = getSupportFragmentManager(); | |
FragmentTransaction ft = fm.beginTransaction(); | |
DetailsFragment fragment = DetailsFragment.newInstance(args); | |
ft.replace(R.id.detail_container, fragment); | |
ft.commit(); | |
} | |
private void showMainFragment(Bundle args) | |
{ | |
FragmentManager fm = getSupportFragmentManager(); | |
FragmentTransaction ft = fm.beginTransaction(); | |
MainFragment fragment = MainFragment.newInstance(args); | |
ft.add(R.id.fragment_container, fragment); | |
ft.commit(); | |
} | |
@Override | |
public boolean onCreateOptionsMenu(Menu menu) | |
{ | |
// Inflate the menu; this adds items to the action bar if it is present. | |
getMenuInflater().inflate(R.menu.menu_main, menu); | |
return super.onCreateOptionsMenu(menu); | |
} | |
@Override | |
public boolean onOptionsItemSelected(MenuItem item) | |
{ | |
switch (item.getItemId()) | |
{ | |
case R.id.action_search: | |
Log.w(LOG_TAG, "You called me Search"); | |
return true; | |
case R.id.action_overflow: | |
// Works as long as list item is always visible and does not go into the menu overflow | |
final View menuItemView = findViewById(R.id.action_overflow); | |
onListPopUp(menuItemView); | |
Log.w(LOG_TAG, "You called me OverFlow"); | |
return true; | |
default: | |
{ | |
return super.onOptionsItemSelected(item); | |
} | |
} | |
} | |
public void onListPopUp(View anchor) | |
{ | |
// This a sample dat to fill our ListView | |
ArrayList<Person> personItem = new ArrayList<Person>(); | |
personItem.add(new Person(R.drawable.remove_placeholder_userpic, "Mamluki", "@DigitalSurgeonR")); | |
personItem.add(new Person(0, "Lists", "@Lists")); | |
personItem.add(new Person(0, "Drafts", "@Drafts")); | |
personItem.add(new Person(0, "Accounts", "@Accounts")); | |
// Initialise our adapter | |
ListPopupWindowAdapter mListPopUpAdapter = new ListPopupWindowAdapter(this, personItem); | |
//Initialise our ListPopupWindow instance | |
final ListPopupWindow pop = new ListPopupWindow(this); | |
// Configure ListPopupWindow properties | |
pop.setAdapter(mListPopUpAdapter); | |
// Set the view below/above which ListPopupWindow dropdowns | |
pop.setAnchorView(anchor); | |
// Setting this enables window to be dismissed by click outside ListPopupWindow | |
pop.setModal(true); | |
// Sets the width of the ListPopupWindow | |
pop.setContentWidth(150); | |
// Sets the Height of the ListPopupWindow | |
pop.setHeight(ListPopupWindow.WRAP_CONTENT); | |
// Set up a click listener for the ListView items | |
pop.setOnItemClickListener(new AdapterView.OnItemClickListener() { | |
@Override | |
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) { | |
// Dismiss the LisPopupWindow when a list item is clicked | |
pop.dismiss(); | |
Toast.makeText(MainActivity.this, "Clicked ListPopUp item " + ((Person) adapterView.getItemAtPosition(position)).getName(), Toast.LENGTH_LONG).show(); | |
} | |
}); | |
pop.show(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I am having a issue. The menu runs to the right of the screen https://www.dropbox.com/s/jensnnjox7urlhi/g8Glr.jpg?dl=0