Custom Layout for Dialog across different versions? - android

I have a Dialog with a custom layout (very simple). I set it up using the code below:
public static Dialog createGPSDialog(final Activity activity, boolean isLocationEnabled) {
final Dialog dialog = new Dialog(activity, R.style.Theme_Sherlock_Light_Dialog);
LinearLayout contentView = (LinearLayout) activity.getLayoutInflater().inflate(R.layout.dialog_twobutton, null);
dialog.setContentView(contentView);
return dialog;
}
There's some code I omitted, but it isn't relevant. Anyway, this is how it looks in gingerbread:
and this is how it looks on jellybean (probably ics as well):
The title area for the dialog is kept on JB, even after setting ContentView. Is there a workaround for this?

If you want to be completely custom and get rid of the title bar, try adding this line to your code:
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);

The key is defining a theme for your dialog. The constructor for Dialog takes a theme resources id. Just use one of android's android.R.theme.xxx.
When using the AlertDialog.Builder you can define a new theme using the contextthemewrapper.
http://developer.android.com/reference/android/view/ContextThemeWrapper.html

Related

Place a button inside DialogFragment's title

I show a DialogFragment from another DialogFragment. I need to set title and a button just next to it. I don't want to reimplement the theme and create a custom view inside DialogFragment's content view instead of dialog title (because it's error-prone and time wasting). Is it even possible? I tried many API functions, AlertBuilder, ActionBar, this and that, still didn't found anything that fits my needs.
Try something like this:
// Title
final int titleId = getResources().getIdentifier("alertTitle","id","android");
TextView title = (TextView) popup.findViewById(titleId);
title.setText("My new title");
// Title's parent layout
ViewGroup viewGroup = (ViewGroup) title.getRootView();
// Button
Button button = new Button(this);
button.setText("A Button");
viewGroup.addView(button);
Note: You may need to adjust the button and title objects' LayoutParams.
An update. This can be achieved by calling setCustomTitle on AlertDialog.Builder class. Available since api level 1, and do not require a bunch of code.
Example:
AlertDialog.Builder(getActivity()).setCustomTitle(getActivity().getLayoutInflater().inflate(R.layout.my_custom_view, viewGroup)).create()

Is there a way to extend AlertDialog and then totally take control of its appearance?

I'm looking at the documentation here:
http://developer.android.com/guide/topics/ui/dialogs.html#PassingEvents
And all their examples seem to overlook the developer's potential desire to totally alter the appearance and/or positioning of the OK | Cancel buttons.
I feel like the solution with the DialogFragment comes close, but still there's no obvious way for me to define what View in my activity should BE the OK button, so that I can easily attach the callback to it.
What am I missing? Or is it really not possible to manipulate the appearance and position of the buttons in anything that extends AlertDialog or DialogFragment?
You can use Dialog and add your own custom layout to it and control the appearance of every view in it. Here is an example how you can do this :
final Dialog alert = new Dialog(FingerPaintActivity.this, android.R.style.Theme_Light_Panel);
alert.requestWindowFeature(Window.FEATURE_NO_TITLE); // no title
alert.getWindow().getAttributes().windowAnimations = R.style.PauseDialogAnimation; // this is used for custom animation when dialog is showing and hiding
alert.setContentView(getLayoutInflater().inflate(R.layout.stamps, null)); // here is your custom layout
alert.getWindow().setLayout(width-50, (height-100)); // set height / width
and you can use set listeners to your views (for example a button) like this :
Button myOkBtn = (Button) alert.findViewById(R.id.myOkBtn);
myOkBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});

Changing contents of an EditText widget in AlertDialog without onPrepareDialog()

I'm using an AlertDialog with a custom view that contains an EditText widget. I want to change the contents of the EditText widget before the AlertDialog is shown. I know that this is usually done by overriding onPrepareDialog() and setting the text there. However, as far as I can see, this will not work in my specific case because I use AlertDialog.show() instead of Activity.showDialog().
So how should I do this with a dialog that is shown using AlertDialog.show()? One solution is to set the text after the dialog has been brought to the front, i.e.:
AlertDialog alertDialog = builder.create();
alertDialog.show();
EditText editText = (EditText) alertDialog.findViewById(R.id.text);
editText.setText("Foo bar");
However, I don't think that this is a nice solution because the dialog is first shown and then the text is set. I'd like to set the text before the dialog is actually shown.
Is there any way to achieve this? I cannot do it before alertDialog.show() because findViewById() returns null when called before alertDialog.show().
Thanks for any help!
AlertDialog alertDialog = builder.create();
alertDialog.show();
Since you have access to the AlertDialog.Builder object, simply change the layout before calling builder.create().
Addition
I have an EditText widget in my XML file which I inflate using builder.setView(inflater.inflate(R.layout.mydialog, null)). How do I change the text of this EditText widget without calling findViewById()?
Break that line into a series of commands. Specifically: inflate the XML, alter the layout, and pass it to setView().
View view = inflater.inflate(R.layout.mydialog, null);
EditText editText = (EditText) view.findViewById(R.id.text);
editText.setText("Foo bar");
builder.setView(view);

Custom title vs. Dialog in Android

I am trying to set a custom title on my activity (using requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);), with a custom title height. I have my custom style and theme all set up. Everything works fine as long as my theme inherits from android:Theme. It gets the custom background, custom titlebar height, all as expected. However, if my custom theme inherits from android:Theme.Dialog then it refuses to honor my custom titlebar height. It is just stuck at about 25px or so. (Which is weird, since before putting in any custom title stuff, the default titlebar was about 50px or so).
Is there any way to set a custom title on a Dialog?
Why don't u create custom dialog instead.
Hope this can help
Dialog dialog = new Dialog(this);
dialog.setContentView(R.layout.maindialog);//this will get the custom dialgo layout
dialog.setTitle("Here is your custom title");
dialog.setCancelable(true);
Button button = (Button) dialog.findViewById(R.id.Button01);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
finish();
}
});
//custom dialog has been created. Time to show
dialog.show()

Custom Alert dialog not centered vertically on Android

I made a custom alert dialog box to be displayed at the end of my game so that the player can enter its name to save it. The problem is when I call show() on the dialog appears but it's not vertically centered! It's a bit lower than it should and no matter what properties I set in the xml or when using setGravity().
I think this is the same problem as the one mentioned here, but no one gave a proper answer.
Thanks for your help.
For more details, here is my code:
AlertDialog.Builder builder;
LayoutInflater inflater = (LayoutInflater)this.getSystemService(LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.newrecord,(ViewGroup)findViewById(R.layout.shoot));
builder = new AlertDialog.Builder(this);
builder.setView(layout);
newRecDialog = builder.create();
And here is the code of the first element of the XML layout of newrecord.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:gravity="center"
android:padding="10dp"
android:baselineAligned="true">
Here is the output screenshot:
(source: free.fr)
The bug is described here. The AlertDialog is reserving space for the title/icon panel even where there is neither a title nor an icon.
The fix is, I think, quite simple: it should set the top panel layout to GONE, just as it does for the button panel in the event of there being no buttons. Until that's done, the only workaround is to implement your own Dialog subclass.
If you implement your own dialog the line requestWindowFeature(Window.FEATURE_NO_TITLE) hides the title panel and the dialog is centered on the screen. Maybe it works with a AlertDialog too.
You can use an Activity instead of a custom alert for this. You have to set the theme of activity as dialog in the android manifest file:
android:theme="#android:style/Theme.Dialog"
And you can adjust the activity xml layout as per your need.
android:theme="#android:style/Theme.Dialog"
If you are dealing with any height and width attributes, you must make sure not to alter the height, since it will alter the position, here is a sample.
myProgressDialog.show();
float widthPecent = 0.60f;
//order matters
DisplayMetrics displayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
int displayWidth = displayMetrics.widthPixels;
//dont do any adjustments to the height. ************************** <<<<<<<<<<
WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams();
layoutParams.copyFrom(myProgressDialog.getWindow().getAttributes());
int dialogWindowWidth = (int) (displayWidth * widthPecent);
layoutParams.width = dialogWindowWidth;
//dont do any changes to the height. ************************** <<<<<<<<<<
myProgressDialog.getWindow().setAttributes(layoutParams);
layoutParams.height = dialogWindowHeight; //comment or remove this line.
set the attribute on your AlertDialog as android:gravity="center" or programmatically as setGravity(Gravity.CENTER). This Gravity is for your layout only not for the display of your mobile. if you use Custom Title its did not look like center vertical.
Not really an answer, but I was having a similar issue. It appears that it is centered, but it is assuming the AlerterDialog has a title set. In my case, I just set a title.
Can you try using AlertDialog.Builder.setCustomTitle(View); instead of setView? We use it because alert dialog looks a bit better than dialog with empty title.
you should use the following line in the xml
firstly remove that padding line from your xml and after that
android:layout_gravity="center"
after this your dialog will appear in center and if you are using the margin from left etc than remove that and also if you are using that
android:layout_width="fill_parent"
than change it with the
android:layout_width="wrap_content"
after that your dialog will be appear in the center.
Try:
newRecDialog.getWindow().getAttributes().gravity = Gravity.CENTER;
You need to disable the window title.
First create a custom style for your dialog (Make sure to use the parent theme that fits your needs):
<style name="CustomDialog" parent="#android:style/Theme.Dialog">
<item name="android:windowNoTitle">true</item>
</style>
Then apply your style to the dialog builder:
AlertDialog.Builder builder;
LayoutInflater inflater = (LayoutInflater)this.getSystemService(LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.newrecord,(ViewGroup)findViewById(R.layout.shoot));
builder = new AlertDialog.Builder( new ContextThemeWrapper(this, R.style.CustomDialog));
builder.setView(layout);
newRecDialog = builder.create();

Categories

Resources