Reputation: 775
I'm trying to create a pop-up window in Android by clicking button in action bar. Like this:
In my idea I realised it with two fragments in one container, where 1 (pop-up) is in View.GONE
state and becomes visible when I click button.
Is there an easier way to solve my problem?
Upvotes: 2
Views: 8095
Reputation: 2753
EDIT:Now you Can use showAsDropDown(findViewbyId(R.id.menuitem),0,0) to show as simple dropdown for that actionbar button.
I used showAtLocation() because you can display in any location of the screen
but with small bug which I will talk about later,
this is kinda good cause it takes gravity as one of the parameter along with X and Y positions,so use this to place where ever you want on the screen
Oh and the action button wont work yet after click action,so use these values to make popup window react to actions outside the screenpopupWindow.setBackgroundDrawable(new BitmapDrawable());
popupWindow.setOutsideTouchable(true);
now launch the window from OnOptionsItemSelected()
inside your activity
The catch here is with showatlocation(), your window works fine for actionbar clicks , but setOutsideTouchable() makes the window also to react for touches outside the window area,meaning it dismisses the window if u touch outside,
there is a method called isShowing() which would have helped in that case but it's buggy
Upvotes: 4
Reputation: 5077
Basically you can do that with very small amount of code and you will end up like this
but if you want to customize you have to design a custom layout
To Achieve that, create a xml menu file like below
<?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>
Now, write PopupMenu1 Activity.java file
package com.example.popuptest;
import android.app.Activity;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.PopupMenu;
import android.widget.Toast;
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();
}
}
Upvotes: 10
Reputation: 502
Take a look at official PopupMenu support from android. You can launch a popup window on button click and inflate your own UI for that window.
Link: http://developer.android.com/reference/android/widget/PopupMenu.html
If you want to support older versions of android, you can use PopupMenuCompat
Link: http://developer.android.com/reference/android/support/v7/widget/PopupMenu.html
Upvotes: 0