android alertdialog theme inconsistency error - android

I have an alert dialog which popups when you click a button in my app. The code is pretty simple and can be seen below:
final String[] options = {"Bar Graph", "Pie Chart", "Text Summary"};
AlertDialog.Builder builder = new AlertDialog.Builder(myActivity.this);
builder.setTitle("Choose a summary");
builder.setIcon(R.drawable.summaryicon);
builder.setItems(options, ... );
See below an image of what it looks like. This is good.
However, strangely sometimes when I build my app on the emulator the theme of the alert dialog changes and looks like this instead:
I cannot imagine what would be causing this random change in a seemingly unpredictable way.
I have tried using this line to set the theme manually, but it seems to have no effect.
ContextThemeWrapper themedContext = new ContextThemeWrapper( AnalysisActivity.this, android.R.style.ThemeOverlay_Material_Dialog_Alert );
AlertDialog.Builder builder = new AlertDialog.Builder(themedContext);
...
I am confused and looking for a way to set the theme manually or ensure it doesn't change from the default.
I can't entirely tell if the rest of my app also experiences the same theme change because most has all been overridden by custom code, but I don't believe it is changing. Any ideas would be welcome.
Note: this alternate theme looks like an older theme so perhaps there is some version issue?

try this code...
step-1: create layout file of your alert dialog(this layout is your designed alertDialog)
step-2: and use this code
public void displayAlertDialog() {
LayoutInflater factory = LayoutInflater.from(this);
final View alertDialogView = factory.inflate(R.layout.alert_dialog, null);
final AlertDialog alertDialog = new AlertDialog.Builder(this).create();
alertDialog.setView(alertDialogView);
alertDialogView.findViewById(R.id.bar_graph).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//your business logic
alertDialog.dismiss();
}
});
alertDialogView.findViewById(R.id.pie_chart).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//your business logic
alertDialog.dismiss();
}
});
alertDialogView.findViewById(R.id.text_summary).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//your business logic
alertDialog.dismiss();
}
});
alertDialog.show();
}

Along with Dinesh Sarma's answer the code I have now implemented to create the alertdialog activity can be seen here.
It creates a layout that looks like this:
Use this layout code in a file called alert_dialog.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/background_light"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/background_light"
android:orientation="horizontal"
android:weightSum="12">
<ImageView
android:id="#+id/imageView"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginStart="10dp"
android:layout_marginLeft="10dp"
android:layout_weight="7"
android:contentDescription="Icon"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:scaleType="fitCenter"
app:srcCompat="#drawable/summaryicon" />
<TextView
android:id="#+id/textView13"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="5"
android:gravity="center_vertical"
android:text="Choose a Summary"
android:textSize="18sp"
android:textStyle="normal" />
</LinearLayout>
<Button
android:id="#+id/bar_graph"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0"
android:background="#android:color/background_light"
android:gravity="center_vertical"
android:paddingStart="20dp"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:text="Pie Chart"
android:textAllCaps="false"
android:typeface="sans" />
<Button
android:id="#+id/pie_chart"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0"
android:background="#android:color/background_light"
android:gravity="center_vertical"
android:paddingStart="20dp"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:text="Bar Graph"
android:textAllCaps="false"
android:typeface="sans" />
<Button
android:id="#+id/text_summary"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0"
android:background="#android:color/background_light"
android:gravity="center_vertical"
android:paddingStart="20dp"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:text="Text Summary"
android:textAllCaps="false"
android:typeface="sans" />
</LinearLayout>

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

What should I use instead of custom dialog in android api less 20

I wrote an application which have to call dialog to get name. I need the custom view in dialog so I used .setContentView(). But in android with api less 20 I have problems with displaying it.
This is calling dialog:
public void setName(View v) {
nameDialog = new Dialog(this);
//using our dialog
nameDialog.setContentView(R.layout.new_name);
EditText text = (EditText) nameDialog.findViewById(R.id.form2_dialog_name);
//if we have previous name we show it
if (haveName) {
text.setText(((Button) findViewById(R.id.form2_name_button)).getText());
}
//request focus and call keyboard for input name
text.requestFocus();
text.selectAll();
nameDialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
nameDialog.show();
}
the xml file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#android:color/holo_blue_dark"
tools:context=".CreateNoteActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="#string/form2_dialog_name_of_event"
android:id="#+id/textView11" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:text=""
android:ems="10"
android:id="#+id/form2_dialog_name"
android:maxLines="1" />
<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end">
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/close_button"
android:id="#+id/button8"
android:onClick="onCancelDialog"/>
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/okay_button"
android:id="#+id/okay_form2_dialog_button"
android:onClick="onFinishDialog" />
</LinearLayout>
</LinearLayout>
How it looks when api upper then 20:
http://i.imgur.com/pJ1oZky.png
Less then 20:
http://i.imgur.com/TJGEwXh.png
OK the answer is quite simple: Use the android.support.v7.app.AlertDialog
Your setName will have to look like this then:
AlertDialog.Builder builder = new AlertDialog.Builder(this);
LayoutInflater inflater = this.getLayoutInflater();
View dialogView = inflater.inflate(R.layout.new_name, null);
builder.setView(dialogView);
EditText text = (EditText) dialogView.findViewById(R.id.form2_dialog_name);
//if we have previous name we show it
if (haveName) {
text.setText(((Button) findViewById(R.id.form2_name_button)).getText());
}
//request focus and call keyboard for input name
text.requestFocus();
text.selectAll();
AlertDialog dialog = builder.create();
dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
dialog.show();
I guess you will have to play a little with your layout file because the buttons are quite a bit too far away but I think this will be just a little problem ;)

Crouton CustomView with infinit duration

I have been pondering with crouton, and after creating a customView crouton popup I have noticed 2 things;
- the layout is slightly modified upon creation and would be corrected if the view if updated
- there is no way of adding a style to make custom crouton, or no way I know of ...
Would someone please provide a way to do each of these neatly
Crouton creat code:
View crouton_view = getLayoutInflater().inflate(R.layout.crouton_layout, null);
Crouton Date_crounton = Crouton.make(this, crouton_view);
crouton_view.setBackgroundColor(Color.rgb(00, 153, 204));
Date_crounton.show();
Layout code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/crouton_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<EditText
android:layout_width="match_parent"
android:layout_height="50dp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<ImageView
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#b4b4b4" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="#+id/crouton_cancel"
style="?android:attr/actionButtonStyle"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_weight="1"
android:gravity="center"
android:text="Cancel"
android:textSize="14sp" />
<ImageView
android:layout_width="1dp"
android:layout_height="30dp"
android:layout_gravity="center_vertical"
android:background="#b4b4b4" />
<Button
android:id="#+id/crouton_ok"
style="?android:attr/actionButtonStyle"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_weight="1"
android:gravity="center"
android:text="OK"
android:textSize="14sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<ImageView
android:layout_width="match_parent"
android:layout_height="5dp"
android:background="#b4b4b4" />
</LinearLayout>
Try to use this following code
View customView = getActivity().getLayoutInflater().inflate(R.layout.crouton_addexam, null);
final Configuration CONFIGURATION_INFINITE = new Configuration.Builder().setDuration(Configuration.DURATION_INFINITE).build();
final Crouton crouton = Crouton.make(getActivity(),customView,R.id.btnTest,CONFIGURATION_INFINITE);
crouton.setConfiguration(new Configuration.Builder().setDuration(Configuration.DURATION_INFINITE).build());
test.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
crouton.hide();
}
});
I use this code for my Crouton:
you can use this:
final Configuration CONFIGURATION_INFINITE = new Configuration.Builder()
.setDuration(Configuration.DURATION_INFINITE)
.build();
final Crouton infiniteCrouton;
infiniteCrouton = Crouton.makeText(this, "TEXT", Style.INFO);
infiniteCrouton.setConfiguration(CONFIGURATION_INFINITE);
infiniteCrouton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Crouton.hide(infiniteCrouton);
}
});
infiniteCrouton.show();
Yes, there is no way to add a Style to a Crouton with a custom view. But you can add a Configuration.
If you want to update the view you'll need to keep a reference outside of the Crouton and update it on your own.
Also I'm not quite sure if using a Crouton as some kind of modal dialog is a good thing from the UX point of view.

Custom theme of all dialogs in an app

Is there any similar theme generator as HoloColors or Action Bar Style Generator which would help us change theme of Dialogs and AlertDialogs in our apps?
My app uses Holo theme and I managed to change style of views such as EditTexts and Buttons but they remain unchanged when they appear in a Dialog. I would also like to change color of the blue line under a title.
I've found a few question and answers related to this topic but they practically say it's not even possible. I cannot believe it isn't.
At least for the AlertDialog it is possible. here i posted my solution of a function which is called at some point (for example button click) in your code and creates an alert dialog own layout over your screen.. the layout is a normal layout.xml which you can define the way you want. works perfect for me!
private void showAddUserGeneratedStationDialog() {
AlertDialog.Builder alertDialog = new AlertDialog.Builder(
AmplifyActivity.this);
LayoutInflater inflater = getLayoutInflater();
//inflate your layout.xml
alertDialog
.setView(inflater.inflate(R.layout.dialog_add_station, null));
//show dialog over activity screen
final AlertDialog ad = alertDialog.show();
//put actions to your ui-elements
Button cancelBtn = (Button) ad
.findViewById(R.id.list_user_generated_cancelBU);
cancelBtn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
ad.cancel();
}
});
Button doneBtn = (Button) ad
.findViewById(R.id.list_user_generated_doneBU);
doneBtn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//get sth from your layout f.e. some textInput
String stationUrl = ((TextView) ad
.findViewById(R.id.list_user_generated_stationURLInput))
.getText().toString();
// did some other things
ad.dismiss();
}
});
}
and my layout.xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/list_user_generated_addStationDialog"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#99000000"
android:clickable="true"
android:gravity="center"
android:orientation="vertical" >
<LinearLayout
android:layout_width="300dp"
android:layout_height="200dp"
android:padding="10dp"
android:background="#000"
android:orientation="vertical" >
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="#+id/list_user_generated_cancelBU"
style="#style/ButtonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center|right"
android:text="#string/cancel" />
<TextView
style="#style/AmplifyHeadlineElement"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="#string/list_userGenerated_addStation" />
<Button
android:id="#+id/list_user_generated_doneBU"
style="#style/ButtonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center|right"
android:text="#string/done" />
</LinearLayout>
<EditText
android:id="#+id/list_user_generated_stationURLInput"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"
android:layout_marginTop="10dp"
android:background="#FFF"
android:ems="10"
android:hint="#string/list_userGenerated_addUrlExample"
android:inputType="textNoSuggestions" >
</EditText>
<!-- MORE BUT I DELETED IT FOR BETTER OVERVIEW -->
</LinearLayout>
</LinearLayout>

Properly closing custom dialog

I'd like to easily display custom-style popups across the whole application.
For that in my Utils class I created this static method:
public static Dialog showStyledPopup(Context context, String sHeader, String sMessage,
String sBtn0Text, View.OnClickListener onClick0,
String sBtn1Text, View.OnClickListener onClick1,
String sBtn2Text, View.OnClickListener onClick2)
{
Dialog dialog = new Dialog(context, R.style.dialog_style_no_bg);
dialog.setContentView(R.layout.popup);
dialog.setCancelable(false);
TextView txtHeader = (TextView)dialog.findViewById(R.id.txt_popup_header);
if (null == sHeader)
{
txtHeader.setVisibility(View.GONE);
}
else
{
txtHeader.setText(sHeader);
txtHeader.setTypeface(App.fontHeader);
}
TextView txtMessage = (TextView)dialog.findViewById(R.id.txt_popup_message);
txtMessage.setText(sMessage);
txtMessage.setTypeface(App.fontCommon);
Button btn0 = (Button)dialog.findViewById(R.id.btn_popup_0);
btn0.setVisibility(View.VISIBLE);
btn0.setText(sBtn0Text);
btn0.setTypeface(App.fontCommon);
btn0.setOnClickListener(onClick0);
if (null != onClick1)
{
Button btn1 = (Button)dialog.findViewById(R.id.btn_popup_1);
btn1.setVisibility(View.VISIBLE);
btn1.setText(sBtn1Text);
btn1.setTypeface(App.fontCommon);
btn1.setOnClickListener(onClick1);
}
if (null != onClick2)
{
Button btn2 = (Button)dialog.findViewById(R.id.btn_popup_2);
btn2.setVisibility(View.VISIBLE);
btn2.setText(sBtn2Text);
btn2.setTypeface(App.fontCommon);
btn2.setOnClickListener(onClick2);
}
dialog.show();
return dialog;
}
popup.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="250dp"
android:layout_height="170dp"
android:background="#drawable/dialog_250_170"
android:gravity="center_horizontal"
android:orientation="vertical" >
<TextView
android:id="#+id/txt_popup_header"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="15dp"
android:textColor="#android:color/black"
android:textSize="20dp" />
<LinearLayout
android:id="#+id/group_popup_btns"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
android:layout_marginBottom="10dp"
android:orientation="horizontal"
android:baselineAligned="false" >
<Button
android:id="#+id/btn_popup_0"
android:layout_width="60dp"
android:layout_height="40dp"
android:background="#drawable/btn_60_40"
android:textColor="#android:color/black"
android:textSize="20dp" />
<Button
android:id="#+id/btn_popup_1"
android:layout_width="60dp"
android:layout_height="40dp"
android:layout_marginLeft="5dp"
android:background="#drawable/btn_60_40"
android:textColor="#android:color/black"
android:textSize="20dp"
android:visibility="gone" />
<Button
android:id="#+id/btn_popup_2"
android:layout_width="60dp"
android:layout_height="40dp"
android:layout_marginLeft="5dp"
android:background="#drawable/btn_60_40"
android:textColor="#android:color/black"
android:textSize="20dp"
android:visibility="gone" />
</LinearLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_above="#id/group_popup_btns"
android:layout_below="#id/txt_popup_header"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp" >
<ScrollView
android:id="#+id/scroll_popup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="#+id/txt_popup_message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:textColor="#android:color/black"
android:textSize="18dp" />
</LinearLayout>
</ScrollView>
</RelativeLayout>
</RelativeLayout>
in styles.xml:
<style name="dialog_style_no_bg" parent="#android:style/Theme.Dialog">
<item name="android:windowNoTitle">true</item>
<item name="android:windowBackground">#android:color/transparent</item>
</style>
The caller is supposed to pass the View.OnClickListeners.
To be able to close the dialog, the caller must have Dialog dialog field which is then used in the OnClickListener to dismiss the dialog, e.g.:
this.dialog = Utils.showStyledPopup(this, null, getString(R.string.STR_ENABLE_SOUND_AND_MUSIC_Q),
getString(R.string.STR_YES), new View.OnClickListener()
{
#Override
public void onClick(View v)
{
doToggleSound();
doToggleMusic();
dialog.dismiss();
dialog = null;
}
}, getString(R.string.STR_NO), new View.OnClickListener()
{
#Override
public void onClick(View v)
{
dialog.dismiss();
dialog = null;
}
});
The problem is that for some reason in rare cases the dialog field gets reset while the popup is still shown. I can't reproduce this, but got a few crash reports because of this in the field.
Has someone tried to do the same?
The activity has it's orientation fixes to landscape, so onCreate should never get called in-between to reset dialog variable.
I don't like the idea of storing the reference to the dialog, but there is just no way to use a final variable in this case, because it gets a value after it should be passed to the OnClickListener.
My question is similar to Android close custom dialog but the problem there is also not solved, at least as I want it (as 1 method in Utils).
I've seen onCreate() being called on DellStreak device at weird times, so this might be just some device-specific issue (PMP5080BRU)
The solution would be to declare Dialog dialog as class field, declare it at class level.
EDIT
Try this solution too. Have the dialog as class field in the Utils class. Do not return the dialog object from the method, instead use it to initialize the dialog variable. The use a getter method (or try accessing directly though not recommended) of dialog variable. This is kinda dirty solution, but it might work for you too.

Categories

Resources