Android: Popup window will not display contents - android

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.

Related

How to remove empty white space in the custom dialog android?

How to remove the empty space in the dialog. My code-
<?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="wrap_content"
android:orientation="vertical"
android:layout_gravity="center_horizontal"
android:padding="10dp">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton
android:id="#+id/quality_dialog_2x_radio_btn_view_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="#+id/quality_dialog_2x_view_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2X"
android:textSize="16sp"
android:textColor="#003FBC"
android:padding="5dp"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton
android:id="#+id/quality_dialog_1_5x_radio_btn_view_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="#+id/quality_dialog_1_5x_view_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1.5X"
android:textSize="16sp"
android:textColor="#003FBC"
android:padding="5dp"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton
android:id="#+id/quality_dialog_1_point_25_x_radio_btn_view_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="#+id/quality_dialog_1_point_25_x_view_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1.25X"
android:textSize="16sp"
android:textColor="#003FBC"
android:padding="5dp"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton
android:id="#+id/quality_dialog_normal_radio_btn_view_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="#+id/quality_dialog_normal_view_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Normal"
android:textSize="16sp"
android:textColor="#003FBC"
android:padding="5dp"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton
android:id="#+id/quality_dialog_normal_75x_radio_btn_view_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="#+id/quality_dialog_point_75x_view_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0.75X"
android:textSize="16sp"
android:textColor="#003FBC"
android:padding="5dp"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton
android:id="#+id/quality_dialog_5x_radio_btn_view_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="#+id/quality_dialog_point_5x_view_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0.5X"
android:textSize="16sp"
android:textColor="#003FBC"
android:padding="5dp"/>
</LinearLayout>
</LinearLayout>
My Java Code
View alertLayout = View.inflate(this, R.layout.quality_dialog_layout, null);
TextView quality2XView = alertLayout.findViewById(R.id.quality_dialog_2x_view_id);
TextView quality1_5View = alertLayout.findViewById(R.id.quality_dialog_1_5x_view_id);
TextView quality1_Point25View = alertLayout.findViewById(R.id.quality_dialog_1_point_25_x_view_id);
TextView qualityNormalView = alertLayout.findViewById(R.id.quality_dialog_normal_view_id);
quality_point75View = alertLayout.findViewById(R.id.quality_dialog_point_75x_view_id);
quality_point5View = alertLayout.findViewById(R.id.quality_dialog_point_5x_view_id);
quality2XRadioBtn = alertLayout.findViewById(R.id.quality_dialog_2x_radio_btn_view_id);
quality1_5RadioBtn = alertLayout.findViewById(R.id.quality_dialog_1_5x_radio_btn_view_id);
quality1_Point25RadioBtn = alertLayout.findViewById(R.id.quality_dialog_1_point_25_x_radio_btn_view_id);
qualityNormalRadioBtn = alertLayout.findViewById(R.id.quality_dialog_normal_radio_btn_view_id);
quality_point75RadioBtn = alertLayout.findViewById(R.id.quality_dialog_normal_75x_radio_btn_view_id);
quality_point5RadioBtn = alertLayout.findViewById(R.id.quality_dialog_5x_radio_btn_view_id);
AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setView(alertLayout);
alert.setCancelable(true);
qualityAlertDialog = alert.create();
qualityAlertDialog.show();
Suggestion 1:
In order to customize the AlertDialog theme you have to create a new theme for it, which is a bit complicated and quite hard as you have also to create proper images etc.
Regarding this option you can check this topic here
Suggestion 2:
Include the button in the xml:
<Button android:text="OK"
style="?android:attr/borderlessButtonStyle" />
Suggestion 3:
Use the AlertDialog options and don't change the theme, this will add proper spacing in all options and follow the android theming so it will not look strange, but will not remove the spacing you mentioned:
AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setSingleChoiceItems(R.array.your_array_options, null/* no item preselected*/,
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// handle the item selection here
}
}
);
alert.setCancelable(true);
qualityAlertDialog = alert.create();
qualityAlertDialog.show();
and the your_array_options will be in a file res/values/array.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array
name="your_array_options">
<item>2X</item>
<item>1.5X</item>
<item>1.25X</item>
...
</string-array>
</resources>
Suggestion 4:
In order to have full customized theme then do not use AlertDialog at all but DialogFragment:
public class MyDialogFragment extends DialogFragment {
// ...
// Empty constructor required for DialogFragment
public MyDialogFragment() {}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.quality_dialog_layout, container);
TextView quality2XView = alertLayout.findViewById(R.id.quality_dialog_2x_view_id);
TextView quality1_5View = alertLayout.findViewById(R.id.quality_dialog_1_5x_view_id);
TextView quality1_Point25View = alertLayout.findViewById(R.id.quality_dialog_1_point_25_x_view_id);
TextView qualityNormalView = alertLayout.findViewById(R.id.quality_dialog_normal_view_id);
quality_point75View = alertLayout.findViewById(R.id.quality_dialog_point_75x_view_id);
quality_point5View = alertLayout.findViewById(R.id.quality_dialog_point_5x_view_id);
quality2XRadioBtn = alertLayout.findViewById(R.id.quality_dialog_2x_radio_btn_view_id);
quality1_5RadioBtn = alertLayout.findViewById(R.id.quality_dialog_1_5x_radio_btn_view_id);
quality1_Point25RadioBtn = alertLayout.findViewById(R.id.quality_dialog_1_point_25_x_radio_btn_view_id);
qualityNormalRadioBtn = alertLayout.findViewById(R.id.quality_dialog_normal_radio_btn_view_id);
quality_point75RadioBtn = alertLayout.findViewById(R.id.quality_dialog_normal_75x_radio_btn_view_id);
quality_point5RadioBtn = alertLayout.findViewById(R.id.quality_dialog_5x_radio_btn_view_id);
// This will also require to add extra buttons in the xml!
// getDialog().setTitle("If you want to change the title");
return view;
}
}

How do I insert layout into Android menu item row?

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);
}

Cant assign popup Window a background

My popup Window remains without background.
I want a white popup saying whom won the game.
Java:
LayoutInflater layoutInflater = (LayoutInflater)getBaseContext().getSystemService(LAYOUT_INFLATER_SERVICE);
PopupWindow pw = new PopupWindow(layoutInflater.inflate(R.layout.winner_popup,null, false), LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
pw.showAtLocation(findViewById(R.id.main_layout), Gravity.CENTER, 0, 0);
TextView winnerText = (TextView) pw.getContentView().findViewById(R.id.winnerTextView);
winnerText.setText("Player 1 is the winner"); //Update the TextView
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="#color/white">
<TextView
android:id="#+id/winnerTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/winnerTextViewString"
android:textAppearance="?android:attr/textAppearanceLarge" />
<Button
android:id="#+id/winnerButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/winnerButtonString"
android:layout_marginLeft="45dp"/>
</LinearLayout>
Is it possible or should I use the alert-window instead?
Br

1px line at left side of popup [duplicate]

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?

PopupWindow with Gallery child

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.

Categories

Resources