i would like to implement a popup menu similar to google's play store as shown below.
so basically from what i understand, i'll need an activity and a layout for this activity with a listview defined in it. i need to create my custom adapter. also, i need to create a list layout would contain the information and a view (with the 3 dots) that will serve as the button to launch the popup menu? the issue that i'm seeing here is that how do i create a listener for this view only and how do i reference the value for that specific list item in the list view.
i don't have any code available yet as i haven't started anything related to this. i'm currently getting info in theory for now but if required i will create a sample code.
thanks.
Using popup menu it's quite simple to create a menu with these three steps:
1 - Add a click listener to the menu button using OnClickListener or as i prefer from the layout xml:
<ImageButton android:id="#+id/menu_button" android:onClick="showMenu" ... />
2 - Create the menu layout menu_layout.xml:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/item_settings"
android:showAsAction="ifRoom|withText"
android:title="Settings"
android:visible="true"/>
<item
android:id="#+id/item_about"
android:showAsAction="ifRoom|withText"
android:title="About"
android:visible="true"/>
</menu>
3 - Create a popup menu, inflate the xml layout and show it:
public void showMenu (View view)
{
PopupMenu menu = new PopupMenu (this, view);
menu.setOnMenuItemClickListener (new PopupMenu.OnMenuItemClickListener ()
{
#Override
public boolean onMenuItemClick (MenuItem item)
{
int id = item.getItemId();
switch (id)
{
case R.id.item_settings: Log.i (Tag, "settings"); break;
case R.id.item_about: Log.i (Tag, "about"); break;
}
return true;
}
});
menu.inflate (R.menu.menu_layout);
menu.show();
}
ActionBarCompat List PopupMenu implementation is here (with back port available because it uses ABC)!
You can also get this sample from Github or from SDK (Mr.Morgan commented below)
/sdk/samples/android-19/ui/ActionBarCompat-ListPopupMenu. Make sure to
install Samples for SDK under Android 4.4.2 (API 19)
You can use like this:
public class MainActivity extends Activity {
ListView listView_Actions;
ArrayList<String> actionsArrayList;
Button btn_ViewPopUp;
ArrayAdapter<String> actionsAdapter;
static final int CUSTOM_DIALOG_ID1 = 1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn_ViewPopUp=(Button) findViewById(R.id.btn_ViewPopUp);
actionsArrayList=new ArrayList<String>();
actionsArrayList.add("Action 1");
actionsArrayList.add("Action 2");
}
#Override
protected void onStart() {
super.onStart();
btn_ViewPopUp.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
showDialog(CUSTOM_DIALOG_ID1);
actionsAdapter = new MyCustomBaseAdapter(getApplicationContext(), R.layout.list_actions, actionsArrayList);
listView_Actions.setAdapter(actionsAdapter);
}
});
}
#Override
protected Dialog onCreateDialog(int id) {
Dialog dialog = null;
switch (id) {
case CUSTOM_DIALOG_ID1:
dialog = new Dialog(MainActivity.this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
dialog.setContentView(R.layout.list_actions);
listView_Actions = (ListView) dialog.findViewById(R.id.listView_Actions);
break;
}
return dialog;
}
class MyCustomBaseAdapter extends ArrayAdapter<String>
{
public MyCustomBaseAdapter(Context context, int textViewResourceId, ArrayList<String> actionsArrayList) {
super(context, textViewResourceId,actionsArrayList);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflater.inflate(R.layout.action_list_cell, null);
final TextView lblContactAction;
lblContactAction = (TextView) v.findViewById(R.id.txtContactAction);
lblContactAction.append(actionsArrayList.get(position));
return v;
}
}
}
Now XML files:
action_list_cell.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="match_parent"
android:orientation="vertical"
android:background="#android:color/background_light" >
<TextView
android:id="#+id/txtContactAction"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text=""
android:textSize="18dp"
android:textColor="#android:color/black" />
</LinearLayout>
list_actions.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="match_parent"
android:orientation="vertical"
android:background="#drawable/rounded_corner_top">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#DB6A16"
android:orientation="vertical"
android:paddingBottom="2dp"
android:paddingLeft="2dp"
android:paddingRight="2dp" >
<ListView
android:id="#+id/listView_Actions"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ffffff" >
</ListView>
</LinearLayout>
</LinearLayout>
</LinearLayout>
first of all you need to make your custom adapter with a view that has the 3 dots.
then in the getView() or newView() method you set the listener to the 3 dots image.
i think that PopupMenu is what you are looking for, it's is supported since API 11.
if you want to support also earlier version of the API you can use PopupMenu class provided by the support library v7.
the usage is pretty straight forward.
you define it with the id of the view you want the menu to show next to, and then you can directly inflate a menu resource there as if it was a common menu.
Now showDialog is deprecated, use PopupMenu instead
And AppCompat PopupMenu f you want to support version before V11
public class MainActivity extends Activity {
Button button1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//Creating the instance of PopupMenu
PopupMenu popup = new PopupMenu(MainActivity.this, button1);
//Inflating the Popup using xml file
popup.getMenuInflater().inflate(R.menu.popup_menu, popup.getMenu());
//registering popup with OnMenuItemClickListener
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
public boolean onMenuItemClick(MenuItem item) {
Toast.makeText(MainActivity.this,"You Clicked : " + item.getTitle(),Toast.LENGTH_SHORT).show();
return true;
}
});
popup.show();//showing popup menu
}
});//closing the setOnClickListener method
}
}
You have to set the Listener of the Button in the getView()-Method of your List-Adapter.
In this getView()-Method you assign a Layout to one List-Item. if you have done this, you just have to set the Listener on this View (Button), and handle the onClick() Event.
Not sure if i understand you correctly but you can trigger this method to open a pop up dialog with a listview.
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle("Title if Any");
builder.setItems(R.array.listoptions, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int itemClicked) {
String[] option_array = getResources().getStringArray(R.array.listoptions);
String optionSelected = option_array[itemClicked];
}
});
return builder.create();
}
See Adding a List
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="listoption">
<item>Install</item>
<item>Add to listview</item>
</string-array>
</resources>
Hope this helps.
Related
When you click on the image button, pop up notification pops up. How do I customize the "ok" and "cancel" button to instead of using the default look of the buttons, I want to use my own custom ImageButtons as "ok" and "cancel".
Here's my code for pop up notification.
public class Notifications extends AppCompatActivity {
ImageButton Notifications;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_notifications);
Notifications = (ImageButton) findViewById(R.id.AllowNotifications);
Notifications.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
AlertDialog.Builder builder = new AlertDialog.Builder(Notifications.this);
builder.setCancelable(false); //False= ONLY way to exist notification is by clicking NO
//True= Exit notification by clicking anywhere on screen outside of notification box.
builder.setTitle("Here is the alert dialog");
builder.setMessage("Here is my message thing");
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int WhichButton) {
dialog.cancel();
}
});
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
}
});
builder.show();
}
});
}
}
Here's the default pop up notification with the above code:
So instead of there being an "ok" and "cancel" in red color, I want to put the "ok" and "cancel" as my own custom image buttons and I'd want to change the color from red to something else. How do I go about doing this inside the Pop Up notification?
As the documentation says, in the Creating a Custom Layout session, you can create a custom layout and inflate it at your Dialog.
To use another button than the one create by the AlertDialog.Builder you will need to handle the click listener of them.
This is the layout I created to test the solution:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="#android:color/white"
android:orientation="vertical"
android:padding="20dp">
<TextView
android:id="#+id/dialogTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Here is the alert dialog"
android:textColor="#android:color/black"
android:textSize="16sp"
android:textStyle="bold"/>
<TextView
android:id="#+id/dialogSubtitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Here is my message thing"/>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="32dp">
<Button
android:id="#+id/positiveButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:background="#android:color/transparent"
android:text="OK"
android:textColor="#android:color/holo_red_light"/>
<Button
android:id="#+id/negativeButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="40dp"
android:layout_toStartOf="#id/positiveButton"
android:background="#android:color/transparent"
android:text="Cancel"
android:textColor="#android:color/holo_red_light"/>
</RelativeLayout>
</LinearLayout>
And the code to make it run:
LayoutInflater layoutInflater = LayoutInflater.from(this);
View promptView = layoutInflater.inflate(R.layout.test, null);
final AlertDialog alertD = new AlertDialog.Builder(this).create();
TextView title = (TextView) promptView.findViewById(R.id.dialogTitle);
TextView subtitle = (TextView) promptView.findViewById(R.id.dialogSubtitle);
title.setText("My new Custom Dialog");
subtitle.setText("With everything that I want");
Button positive = (Button) promptView.findViewById(R.id.positiveButton);
Button negativeButton = (Button) promptView.findViewById(R.id.negativeButton);
positive.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// btnAdd1 has been clicked
}
});
negativeButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// btnAdd2 has been clicked
}
});
alertD.setView(promptView);
alertD.show();
This is an screenshot of how it looks like in my phone. Feel free to change the layout in the way it better fits your needs.
Thanks to Vikram that explains it very well in this answers for other question, but I thought that a specific code for your question would be better.
If you want to customize everything, the look of the dialog, add your own buttons, TextViews etc. - you need to make a class that extends DialogFragment and implements View.OnClickListener and you need to create your own layout with two custom made buttons for that. Give them ids and set OnClickListeners
As typed in: https://developer.android.com/reference/android/app/DialogFragment.html
public static class MyDialogFragment extends DialogFragment implements View.OnClickListener {
static MyDialogFragment newInstance() {
return new MyDialogFragment();
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.dialog_fragment, container, false);
v.findViewById(R.id.btn_ok).setOnClickListener(this);
return v;
}
#Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.btn_ok:
// do something
break;
default:
break;
}
}
}
And from your Activity you do:
void showDialog() {
// Create the fragment and show it as a dialog.
DialogFragment newFragment = MyDialogFragment.newInstance();
newFragment.show(getFragmentManager(), "dialog");
}
I have a custom ListView. Inside the layout of the custom ListView, I have an ImageButton which acts as an overflow menu (similar to how the Menu on the ActionBar works):
layout/item_list.xml
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_more_vert_black_24dp"
android:contentDescription="#string/descr_overflow_button"
android:id="#+id/overflowMenu"
android:layout_alignParentRight="true"/>
In the Activity, I configure this ImageButton as a Popup in the onCreate method:
overflowMenu = (ImageButton) findViewById(R.id.overflowMenu);
popupMenu = new PopupMenu(this, overflowMenu);
popupMenu.setOnMenuItemClickListener(this);
I also inflated it in onCreateOptionsMenu:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = popupMenu.getMenuInflater();
inflater.inflate(R.menu.popup_menu, popupMenu.getMenu());
return true;
}
I have the following in menu/popup_menu.xml folder:
<menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" tools:context=".MainActivity">
<item android:id="#+id/report" android:title="Report"
app:showAsAction="always"/>
<item android:id="#+id/share" android:title="Share"
app:showAsAction="always"/>
</menu>
And I added this:
#Override
public boolean onMenuItemClick(MenuItem item) {
return true;
}
However, when I try to click on the ImageButton on the phone, nothing happens. It does not display the menu items as it should. What am I missing?
Firstly PopupMenu is exactly what you need . From the docs
A popup menu displays a list of items in a vertical list that's
anchored to the view that invoked the menu. It's good for providing an
overflow of actions that relate to specific content or to provide
options for a second part of a command.
Now the reason why your implementation doesn't work is because onCreateOptionsMenu and onMenuItemClick are for managing the menu for the activity so inflating the overflow menu there is pointless.
What you need to do is attach an onClickListener to your ImageButton and initialize the PopupMenu and call show() inside your ListView/RecyclerView adapter
imageButton = findViewById(R.id.overflow_menu);
PopupMenu popup = new PopupMenu(this, imageButton);
MenuInflater inflater = popup.getMenuInflater();
inflater.inflate(R.menu.popup_menu, popup.getMenu());
imageButton.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
popup.show();
}
});
You can have a look at the docs linked above for examples as well.
This Questions is duplication.
For custom layouts you can't use a menu, you have to use to a PopupWindow
Android MenuItem Custom Layout
PopupWindow popupwindow_obj = popupDisplay();
popupwindow_obj.showAsDropDown(clickbtn, -40, 18); // where u want show on view click event popupwindow.showAsDropDown(view, x, y);
public PopupWindow popupDisplay()
{
final PopupWindow popupWindow = new PopupWindow(this);
// inflate your layout or dynamically add view
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.mylayout, null);
Button item = (Button) view.findViewById(R.id.button1);
popupWindow.setFocusable(true);
popupWindow.setWidth(WindowManager.LayoutParams.WRAP_CONTENT);
popupWindow.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
popupWindow.setContentView(view);
return popupWindow;
}
// Create this XML file in the res/layout folder named my layout.xml
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Window test" />
</LinearLayout>
How do I do a custom dropdown/popup menu anchored to a button?
I need it to work like the popup menu (anchored to a view), and do something when I click an item from the menu.
How do I add items to the menu by code, keeping menu's height and make it scrollable if there are more than 5 items. I don't need to add any images, just text.
Update: To create a popup menu in android with Kotlin refer my answer here.
To create a popup menu in android with Java:
Create a layout file activity_main.xml under res/layout directory which contains only one button.
Filename: activity_main.xml
<RelativeLayout 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"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="62dp"
android:layout_marginTop="50dp"
android:text="Show Popup" />
</RelativeLayout>
Create a file popup_menu.xml under res/menu directory
It contains three items as shown below.
Filename: poupup_menu.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/one"
android:title="One"/>
<item
android:id="#+id/two"
android:title="Two"/>
<item
android:id="#+id/three"
android:title="Three"/>
</menu>
MainActivity class which displays the popup menu on button click.
Filename: MainActivity.java
public class MainActivity extends Activity {
private Button button1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//Creating the instance of PopupMenu
PopupMenu popup = new PopupMenu(MainActivity.this, button1);
//Inflating the Popup using xml file
popup.getMenuInflater()
.inflate(R.menu.popup_menu, popup.getMenu());
//registering popup with OnMenuItemClickListener
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
public boolean onMenuItemClick(MenuItem item) {
Toast.makeText(
MainActivity.this,
"You Clicked : " + item.getTitle(),
Toast.LENGTH_SHORT
).show();
return true;
}
});
popup.show(); //showing popup menu
}
}); //closing the setOnClickListener method
}
}
To add programmatically:
PopupMenu menu = new PopupMenu(this, view);
menu.getMenu().add("One");
menu.getMenu().add("Two");
menu.getMenu().add("Three");
menu.show();
Follow this link for creating menu programmatically.
I know this is an old question, but I've found another answer that worked better for me and it doesn't seem to appear in any of the answers.
Create a layout xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="5dip"
android:paddingBottom="5dip"
android:paddingStart="10dip"
android:paddingEnd="10dip">
<ImageView
android:id="#+id/shoe_select_icon"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_gravity="center_vertical"
android:scaleType="fitXY" />
<TextView
android:id="#+id/shoe_select_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textSize="20sp"
android:paddingStart="10dp"
android:paddingEnd="10dp"/>
</LinearLayout>
Create a ListPopupWindow and a map with the content:
ListPopupWindow popupWindow;
List<HashMap<String, Object>> data = new ArrayList<>();
HashMap<String, Object> map = new HashMap<>();
map.put(TITLE, getString(R.string.left));
map.put(ICON, R.drawable.left);
data.add(map);
map = new HashMap<>();
map.put(TITLE, getString(R.string.right));
map.put(ICON, R.drawable.right);
data.add(map);
Then on click, display the menu using this function:
private void showListMenu(final View anchor) {
popupWindow = new ListPopupWindow(this);
ListAdapter adapter = new SimpleAdapter(
this,
data,
R.layout.shoe_select,
new String[] {TITLE, ICON}, // These are just the keys that the data uses (constant strings)
new int[] {R.id.shoe_select_text, R.id.shoe_select_icon}); // The view ids to map the data to
popupWindow.setAnchorView(anchor);
popupWindow.setAdapter(adapter);
popupWindow.setWidth(400);
popupWindow.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
switch (position){
case 0:
devicesAdapter.setSelectedLeftPosition(devicesList.getChildAdapterPosition(anchor));
break;
case 1:
devicesAdapter.setSelectedRightPosition(devicesList.getChildAdapterPosition(anchor));
break;
default:
break;
}
runOnUiThread(new Runnable() {
#Override
public void run() {
devicesAdapter.notifyDataSetChanged();
}
});
popupWindow.dismiss();
}
});
popupWindow.show();
}
The Kotlin Way
fun showPopupMenu(view: View) {
PopupMenu(view.context, view).apply {
menuInflater.inflate(R.menu.popup_men, menu)
setOnMenuItemClickListener { item ->
Toast.makeText(view.context, "You Clicked : " + item.title, Toast.LENGTH_SHORT).show()
true
}
}.show()
}
UPDATE: In the above code, the apply function returns this which is not required, so we can use run which don't return anything and to make it even simpler we can also remove the curly braces of showPopupMenu method.
Even Simpler:
fun showPopupMenu(view: View) = PopupMenu(view.context, view).run {
menuInflater.inflate(R.menu.popup_men, menu)
setOnMenuItemClickListener { item ->
Toast.makeText(view.context, "You Clicked : ${item.title}", Toast.LENGTH_SHORT).show()
true
}
show()
}
First, create a folder named “menu” in the “res” folder.
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/search"
android:icon="#android:drawable/ic_menu_search"
android:title="Search"/>
<item
android:id="#+id/add"
android:icon="#android:drawable/ic_menu_add"
android:title="Add"/>
<item
android:id="#+id/edit"
android:icon="#android:drawable/ic_menu_edit"
android:title="Edit">
<menu>
<item
android:id="#+id/share"
android:icon="#android:drawable/ic_menu_share"
android:title="Share"/>
</menu>
</item>
</menu>
Then, create your Activity Class:
public class PopupMenu1 extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.popup_menu_1);
}
public void onPopupButtonClick(View button) {
PopupMenu popup = new PopupMenu(this, button);
popup.getMenuInflater().inflate(R.menu.popup, popup.getMenu());
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
public boolean onMenuItemClick(MenuItem item) {
Toast.makeText(PopupMenu1.this,
"Clicked popup menu item " + item.getTitle(),
Toast.LENGTH_SHORT).show();
return true;
}
});
popup.show();
}
}
I'm currently using a ListFragment together with an ExpandableListView to show some data backed by a SimpleCursorTreeAdapter. Everything works fine, but I recently switched to the support.v4 package, to make use of the ViewPager class to swipe between tabs. Swiping and all the other classes that now use the support.v4.Fragment work fine, but my ListFragment has stopped working.
There are no exceptions thrown, but the ListFragment simply doesn't show any items.
This is the code for the ListFragment:
public class VisuTextFragment extends ListFragment {
private Storage mStorage;
private int mFilterSensortype;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mStorage = Storage.newSQLiteDatabase(getActivity());
mFilterSensortype = -1;
setHasOptionsMenu(true);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.l_visu_text, container, false);
}
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
fillData();
}
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.textvis, menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_filter:
final Dialog dialog = new Dialog(getActivity());
dialog.setTitle("Filter by sensor type");
dialog.setContentView(R.layout.l_dialog_filter);
Button ok = (Button) dialog.findViewById(R.id.filter_ok);
ok.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
int filter = Integer.parseInt(((EditText) dialog.findViewById(R.id.et_filter)).getText().toString());
mFilterSensortype = filter;
fillData();
dialog.dismiss();
}
});
Button cancel = (Button) dialog.findViewById(R.id.filter_cancel);
cancel.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
dialog.cancel();
}
});
dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
dialog.show();
break;
}
return true;
}
#Override
public void onResume() {
super.onResume();
fillData();
}
public void fillData() {
Log.d("VisuTextFragment", "fillData()");
Cursor cursor;
if (mFilterSensortype == -1)
cursor = mStorage.queryAllAsCursor();
else
cursor = mStorage.query(mFilterSensortype);
TextVisCursorAdapter adapter = new TextVisCursorAdapter(
getActivity(),
cursor,
R.layout.l_visu_text_group,
new String[] { Storage.ELEMENT_ID, Storage.ELEMENT_ENTRIES_DATE, Storage.ELEMENT_ENTRIES_LATITUDE, Storage.ELEMENT_ENTRIES_LONGITUDE, Storage.ELEMENT_ENTRIES_SENSORTYPE },
new int[] { R.id.id, R.id.date, R.id.latitude, R.id.longitude, R.id.sensortype },
R.layout.l_visu_text_child,
new String[] { Storage.ELEMENT_MEASUREMENTS_VALUE },
new int[] { R.id.value });
ListView lv = (ListView) getListView();
ExpandableListView elv = (ExpandableListView) lv;
elv.setGroupIndicator(null);
elv.setAdapter(adapter);
}
}
And this is the layout that I'm using (don't know if that helps):
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/id"/>
<TextView
android:gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="2"
android:text="#string/date"/>
<TextView
android:gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="2"
android:text="#string/latitude"/>
<TextView
android:gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="2"
android:text="#string/longitude"/>
<TextView
android:gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/sensortype"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ExpandableListView android:id="#+id/android:list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:transcriptMode="normal"/>
<TextView android:id="#+id/android:empty"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/no_entries"/>
</LinearLayout>
</LinearLayout>
Just FYI: The TextView's in the layout file are shown, but the list itself is just missing. Not even the TextView for an empty list is shown.
Hope you can help.
EDIT: I have checked for the ExpandableListView's width and height via their corresponding methods and both return a value of 0. Its getCount() method returns 347. So the View definitely exists and is filled properly, but it is for some weird reason just not drawn to the screen.
EDIT2: Ok I fixed the problem. The problem was that the LinearLayout that hosted the TextViews on top of the actual list had its layout_height attribute set to fill_parent, which strangely was no issue for the non-support version as well as the composer in eclipse, since they both worked that way and I didn't even notice that it was set to fill_parent.
getListView() in a ListFragment is specifically looking for a listview id of #id/android:list. I'm not sure that adding the "+" in there like you did would have an effect or not, but it's the first thing I would try.
You also note you switched to the support library... did you switch all the appropriate method calls? For example, instead of getFragmentManager you would need to use getSupportFragmentManager and instead of using an Activity to control the fragments, you would need to use FragmentActivity, etc.
I think that that its butter to use android:id="#android:id/list" instead of android:id="#+id/android:list", also, in your case its really useless to extend ListFragment, just use Fragment and use findViewById for your expandableList.
Can you change:
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
}
to:
public void onActivityCreated(Bundle aSavedInstanceState) {
super.onActivityCreated(aSavedInstanceState);
fillData();
}
make sure that your fillData method is called.
Ok I fixed the problem. The problem was that the LinearLayout that hosted the TextViews on top of the actual list had its layout_height attribute set to fill_parent, which strangely was no issue for the non-support version as well as the composer in eclipse, since they both worked that way and I didn't even notice that it was set to fill_parent.
I have this app, that I created a custom dialog for. I must of goofed something up cause while the .show call on the dialog does indeed bring it up, it looks like a whole new fragment and it is not floating but instead replacing the ui with its contents. I did see in their help for DialogFragment:
http://hi-android.info/docs/reference/android/app/DialogFragment.html#Lifecycle
that one can embed a dialog as a regular fragment or not. Though I am not doing anything to do this so I cannot figure out why its acting like an embedded fragment and not floating. After thinking on it, is it the way I defined my XML definition? The dialogfragment example above didn't really give a definition for the xml layout, so maybe that is where my issue is? (Even added the gravity to the xml file, still no dice)
My xml definition for this Dialog is here:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:gravity="center_horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:textSize="20sp"
android:text = "Location:"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left"/>
<Spinner
android:id="#+id/location_spinner"
android:layout_width = "450sp"
android:layout_height="wrap_content"/>
<!-- fill out the data on the package total cost etc -->
</LinearLayout>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button android:id="#+id/location_dlg_ok"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Okay"/>
<Button android:id="#+id/location_dlg_cancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Cancel"/>
<Button android:id="#+id/location_dlg_new"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Create new..."/>
</LinearLayout>
</LinearLayout>
Like I said displays just fine, the code for the fragment:
package com.viciousbytes.studiotab.subactivities.dialogfragments;
import ... ...
public class LocationPicker extends DialogFragment {
ArrayList<Location> mLocations;
public static LocationPicker newInstance()
{
LocationPicker loc = new LocationPicker();
return loc;
}
private void setLocations(ArrayList<Location> loc)
{
mLocations=loc;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Pick a style based on the num.
int style = DialogFragment.STYLE_NORMAL, theme = android.R.style.Theme;
setStyle(style, theme);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.location_dialog, container, false);
Spinner spinner = (Spinner)v.findViewById(R.id.location_spinner);
ArrayAdapter<Location> adapter = new ArrayAdapter<Location>(v.getContext(), android.R.layout.simple_spinner_item, mLocations);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
if(mLocations==null)
spinner.setPrompt("No Locations");
else
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new LocationSelectedListener());
// Watch for button clicks.
Button newBtn = (Button)v.findViewById(R.id.location_dlg_new);
newBtn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// When button is clicked, call up to owning activity.
//create new start that activity...
}
});
// Cancel do nothing dismissthis
Button cancelBtn = (Button)v.findViewById(R.id.location_dlg_cancel);
cancelBtn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// When button is clicked, call up to owning activity.
//create new start that activity...
}
});
// okay button means set listener with the selected location.
Button okBtn = (Button)v.findViewById(R.id.location_dlg_ok);
okBtn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// When button is clicked, call up to owning activity.
//create new start that activity...
}
});
return v;
}
}
It is called from a fragment itself? though does that matter? because I am calling a TimePIckerDialog and a DatePickerDialog and those work fine, but my calling code from my other fragment is:
void showLocationDialog() {
FragmentTransaction ft = getFragmentManager().beginTransaction();
Fragment prev = getFragmentManager().findFragmentByTag("locpicker");
if (prev != null) {
ft.remove(prev);
}
ft.addToBackStack(null);
// Create and show the dialog.
DialogFragment newFragment = LocationPicker.newInstance();
newFragment.show(ft, "locpicker");
}
Your constructors are wrong. Try to have just one static method newInstance to instantiate the fragment for all cases and use a Bundle to store the arguments that you want to use in the fragment. Refer to Basic Dialog section here and extend it to your case.