How do I insert a layout into an Android menu item row?
I don't know how to create a layout like this.
Here's what I want:
Create a custom layout like this:
<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"
tools:context=".MainActivity"
android:padding="5dp">
<Switch
android:id="#+id/mySwitch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="20dp"
android:text="Play with the Switch" />
<TextView
android:id="#+id/switchStatus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/mySwitch"
android:layout_marginTop="22dp"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium" />
</RelativeLayout>
In your Activity, implement the following code:
public void showPopup(View v) {
LayoutInflater layoutInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View popupView = layoutInflater.inflate(R.layout.popup_filter_layout, null);
popupWindow = new PopupWindow(
popupView,
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
popupWindow.setBackgroundDrawable(new BitmapDrawable());
popupWindow.setOutsideTouchable(true);
popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
#Override
public void onDismiss() {
//TODO do sth here on dismiss
}
});
popupWindow.showAsDropDown(v);
}
Related
I'm trying to create a simple Pop up on this project, while the code works on some device, but there seems to be a bug on some devices.
This is the XML
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/popup_bg"
android:gravity="center"
android:orientation="vertical"
android:padding="10dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Silakan pilih yang ingin di rating"
android:textColor="#000" />
<Button
android:id="#+id/btn_atasan"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="3dp"
android:layout_marginTop="5dp"
android:background="#drawable/button_bg"
android:text="Atasan"
android:textColor="#fff" />
<Button
android:id="#+id/btn_sekawan"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="3dp"
android:background="#drawable/button_bg"
android:text="Sekawan"
android:textColor="#fff" />
<Button
android:id="#+id/btn_bawahan"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="3dp"
android:background="#drawable/button_bg"
android:text="Bawahan"
android:textColor="#fff" />
</LinearLayout>
This is the java code on the fragment
private void popUpRatingType() {
//instantiate the popup.xml layout file
LayoutInflater layoutInflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View customView = layoutInflater.inflate(R.layout.popup_rate_karyawan, null);
//casting buttons
btnAtasan = customView.findViewById(R.id.btn_atasan);
btnBawahan = customView.findViewById(R.id.btn_bawahan);
btnSekawan = customView.findViewById(R.id.btn_sekawan);
//instantiate popup window
popupWindow = new PopupWindow(customView, FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.WRAP_CONTENT);
//display the popup window
popupWindow.showAtLocation(contentFrame, Gravity.CENTER, 0, 0);
Remove popup on outside click
// Removes default background.
popupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
// Closes the popup window when touch outside.
popupWindow.setOutsideTouchable(true);
popupWindow.setFocusable(true);
popupWindow.update();
}
On some devices it looks okay, like this :
But I don't know how on some devices (this is on Nougat) looks like this :
private void popUpRatingType() {
//instantiate the popup.xml layout file
LayoutInflater layoutInflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View customView = layoutInflater.inflate(R.layout.item_temp, null);
//casting buttons
//instantiate popup window
PopupWindow popupWindow = new PopupWindow(customView, FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.WRAP_CONTENT);
//display the popup window
popupWindow.showAtLocation(customView, Gravity.CENTER, 0, 0);
popupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
// Closes the popup window when touch outside.
popupWindow.setOutsideTouchable(true);
popupWindow.setFocusable(true);
popupWindow.update();
}
Like this
If you click the button a menu will drop down. I use ListView to implement, but I don't know how to attach the three-dot-button and add a pop-up menu.
Here is my list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="?android:attr/listPreferredItemHeight" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="4dp"
android:layout_marginLeft="6dp"
android:layout_marginRight="6dp"
android:layout_marginTop="4dp"
android:background="#drawable/card_background" >
<TextView
android:id="#+id/tvMain"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_gravity="left|center_vertical"
android:layout_weight="1"
android:textColor="#android:color/primary_text_light" />
</LinearLayout>
</FrameLayout>
And the screenshot of my app:
https://www.dropbox.com/s/h2ko2ctfn5ohmf9/Screenshot_2013-09-10-15-43-57.png
It's just a ImageView with the correct PNG (you'll have to draw the image yourself on photoshop, paint, GIMP or any other graphical editor) and use a PopupWindow to make the popup.
here is a simple example on how to do a PopupWindow (got it from here http://android-er.blogspot.de/2012/03/example-of-using-popupwindow.html)
#Override
public void onClick(View view) {
LayoutInflater layoutInflater = (LayoutInflater)getBaseContext().getSystemService(LAYOUT_INFLATER_SERVICE);
View popupView = layoutInflater.inflate(R.layout.popup, null);
final PopupWindow popupWindow = new PopupWindow(popupView, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
Button btnDismiss = (Button)popupView.findViewById(R.id.dismiss);
btnDismiss.setOnClickListener(new Button.OnClickListener(){
#Override
public void onClick(View v) {
popupWindow.dismiss();
}});
popupWindow.showAsDropDown(view, 0, -0);
}
I have a problem when creating a popup for my app. I'm trying to show a popup that fills the total size of my screen. The problem is that i'm getting 1px line at the left side that isn't filled.
my popup xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:id="#+id/popup"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginBottom="20dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:background="#drawable/popup_round_corners"
android:orientation="vertical"
android:padding="5dp" >
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="5dp"
android:text="#string/zm_main_uvod" />
<Button
android:id="#+id/close"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_marginTop="10dp"
android:paddingLeft="3dp"
android:paddingRight="3dp"
android:text="Ok" />
</LinearLayout>
</LinearLAyout>
and my function to show popup:
private void showPopup(final Activity context) {
// Inflate the popup_layout.xml
LinearLayout viewGroup = (LinearLayout)findViewById(R.id.popup);
LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = layoutInflater.inflate(R.layout.startpopup, viewGroup);
// Creating the PopupWindow
final PopupWindow popup = new PopupWindow(context);
popup.setContentView(layout);
Display display = getWindowManager().getDefaultDisplay();
int width = display.getWidth();
int height=display.getHeight();
popup.setWidth(width+5);
popup.setHeight(height+5);
popup.setFocusable(true);
popup.showAtLocation(layout, Gravity.FILL, 0, 0);
Button close = (Button) layout.findViewById(R.id.close);
close.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
popup.dismiss();
}
});
}
I tried with gravity.center and try setting offsets to -30,-30 but nothing happend. Any ideas?
I have a popup window that should be displaying two button and a textview with this 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:id="#+id/pe"
android:paddingTop="100dp"
android:paddingBottom="100dp"
android:paddingLeft="180dp"
android:paddingRight="180dp"
android:background="#444444"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textSize="26dp"
android:text="Are you sure?" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Button
android:id="#+id/bYesClear"
android:layout_width="86dp"
android:layout_height="wrap_content"
android:text="Yes" />
<Button
android:id="#+id/bNoClear"
android:layout_width="86dp"
android:layout_height="wrap_content"
android:text="No" />
</LinearLayout>
</LinearLayout>
And this code is calling it:
private PopupWindow pw;
private void initiatePopupWindow() {
try {
LayoutInflater inflater = (LayoutInflater) Options.this
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.popupareyousure, (ViewGroup) findViewById(R.id.pe));
pw = new PopupWindow(layout, 470, 300, true);
pw.showAtLocation(layout, Gravity.CENTER, 0, 0);
yesClearButton = (Button) layout.findViewById(R.id.bYesClear);
noClearButton = (Button) layout.findViewById(R.id.bNoClear);
yesClearButton.setOnClickListener(this);
noClearButton.setOnClickListener(this);
} catch (Exception e) {
e.printStackTrace();
}
}
It does display the popup window with the correct size, however, it does not display the text view, nor the buttons within.
I'm trying to use a PopupWindow with a Gallery inside, but when inflating the popup, I get the following error:
ERROR/AndroidRuntime(31817): java.lang.UnsupportedOperationException: addView(View, LayoutParams) is not supported in AdapterView
Does it mean there's no way to use a Gallery in a PopupWindow?
Thanks
btn_open_popup.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
LayoutInflater inflater = (LayoutInflater) HomeActivity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View popup_container = inflater.inflate(R.layout.popup_container,null, false);
final PopupWindow pw = new PopupWindow(popup_container, width, height, true);
pw.showAtLocation(findViewById(R.id.home), Gravity.CENTER, 0,0);
ImageView btn_close_popup = (ImageView) popup_container.findViewById(R.id.btn_close_popup);
btn_close_popup.setAlpha(120);
btn_close_popup.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
pw.dismiss();
}
});
}
});
popup_container.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="295dp"
android:layout_height="307dp" android:background="#drawable/bg_popup">
<ImageView android:id="#+id/btn_close_popup"
android:layout_width="25dp" android:layout_height="25dp"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true" android:src="#983742" />
<Gallery android:id="#+id/popup_gallery"
android:layout_width="fill_parent" android:layout_height="fill_parent">
<ImageView android:layout_width="wrap_content"
android:layout_height="wrap_content" android:background="#drawable/popup_contact" />
<ImageView android:layout_width="wrap_content"
android:layout_height="wrap_content" android:background="#drawable/popup_welcome" />
<ImageView android:layout_width="wrap_content"
android:layout_height="wrap_content" android:background="#drawable/popup_useraccount" />
<ImageView android:layout_width="wrap_content"
android:layout_height="wrap_content" android:background="#drawable/popup_geoloc" />
</Gallery>
</RelativeLayout>
This doesn't necessarily answer your question, but why don't you use a dialog?. It's prettier and easier to use.
Edit 1: Yes you should be able to use a gallery in a popup window, I'm not sure why you can't get it to work.