Dialog.setTitle not showing a title - android

I am trying to add a custom title to my Dialog, however whenever I run my application it doesn't show a title.
My code for creating the dialog is
final Dialog passwordDialog = new Dialog(this);
passwordDialog.setContentView(R.layout.admin_password_dialog);
passwordDialog.setTitle("Enter An Administrative Password");
passwordDialog.show();
And my layout file is
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="#+id/btn_confirmPassword"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentStart="true"
android:layout_below="#+id/edit_adminPassword"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:text="#string/confirmPassword"/>
<EditText
android:id="#+id/edit_adminPassword"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:ems="10"
android:inputType="textPassword"/>
And here is what I am getting
Is there something I am missing?

you should define your style like this:
<style name="Dialog" parent="Theme.AppCompat.Dialog">
<item name="android:windowNoTitle">false</item>
<item name="android:windowIsFloating">true</item>
</style>
and then pass this style to the constructor of the Dialog
final Dialog passwordDialog = new Dialog(this,R.style.Dialog);

Like the other answer, but more concise
final AlertDialog diag = new AlertDialog.Builder(this)
.setTitle("Enter An Administrative Password")
.setView(R.layout.admin_password_dialog)
.create();
diag.show();
Button diagButton = (Button) diag.findViewById(R.id.btn_confirmPassword);
diagButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// handle button click
EditText input = (EditText) diag.findViewById(R.id.edit_adminPassword);
String s = input.getText().toString();
}
});

You can try this method as well and get different view styles based upon theme used.
<style name="FilterDialogTheme" parent="#android:style/Theme.Holo.Light.Dialog">
<item name="android:windowNoTitle">false</item>
</style>
In Dialog constructor
public FilterDialog(Context context) {
super(context, R.style.FilterDialogTheme);
}
Use #style/Theme.Appcompat.Light.Dialog for your project.

You should use an AlertDialog.Builder instead of just creating a Dialog:
// 1. Instantiate an AlertDialog.Builder with its constructor
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
// 2. Chain together various setter methods to set the dialog characteristics
builder.setView(R.layout.admin_password_dialog);
builder.setTitle("Enter An Administrative Password");
// 3. Get the AlertDialog from create()
AlertDialog dialog = builder.create();
dialog.show();
See here for the Android Developers Guide on Dialogs.

Related

Change Title header of AlertDialog

I am trying to change the title header of Alertdialog box but the output is not what I exactly wanted. I am creating the following style in styles.xml:
<style name="question_dialog"
parent="#android:style/Theme.Holo.Dialog">
<item name="android:windowTitleStyle">#style/question_dialog_title</item>
</style>
<style name="question_dialog_title" parent="android:Widget.TextView">
<item name="android:background">#5cc5cc</item>
<item name="android:textSize">21sp</item>
<item name="android:textColor">#ffffff</item>
</style>
The java code is as follows :
new AlertDialog.Builder(this,
R.style.question_dialog).setTitle("Assam Quiz" ).
setMessage("Hello world Hello world").
setPositiveButton("OK", (dialog, which) - >
{dialog.dismisd();
}).show();
}
The AlertDialog image is attached.
out of the style - I think your dialog has some header layout set to it that prevents the title to be at the top, but if that is the case or not - you can easily set a custom title for the dialog header giving it the header layout only, and so you will have the full control for the dialog header:
// creating the Dialog
AlertDialog.Builder builder = new AlertDialog.Builder(context);
// getting dialog context
Context mContext = builder.getContext();
// building the inflater
LayoutInflater mLayoutInflater = LayoutInflater.from(mContext);
// inflate the dialog header layout
View mView = mLayoutInflater.inflate(R.layout.simple_dialog_header, null);
// get the TextView for the header (contained in the header layout)
TextView mTextView = (TextView) mView.findViewById(R.id.title_text);
// set the text for that TextView
mTextView.setText(message);
// set the custom header view for the dialog
builder.setCustomTitle(mView);
/*
here you can set positive , negative ,neutral buttons
or set the dialog message or any attribute you want
*/
// finally, show the dialog
builder.show();
and for the header layout (R.layout.simple_dialog_header):
<?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="wrap_content"
android:background="#color/primary"
android:orientation="vertical"
android:paddingBottom="15dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingTop="15dp">
<TextView
android:id="#+id/title_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:maxLines="1"
android:textAlignment="center"
android:textColor="#color/primary"
android:textSize="16sp"
android:textStyle="bold" />
</LinearLayout>

How to change the text gravity on alertdialog

I've been searching for a while and I haven't found the solution to this problem of mine yet, I have a alertdialog with a few items (Here it is)
But as you can see, the text is aligned to the left. Is there a way to align the items text to the center?
Here's a bit of my code
chosenItem.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// setup the alert builder
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle("Choose a category");
// add a list
String[] items = {"Hey", "stack", "overflow", "please", "help", "Medical", "Ammo"};
// String[] items = {"Weapons", "Tools", "Items", "Clothing", "Components", "Medical", "Ammo"};
builder.setItems(items, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
switch (which) {
case 0:
reciclador(3, rSlotFrame, rSlotText, rSlotProbabilidade, 30, 50, 1, 0, 100, 100, 100, 0, "cloth_icon", "metalfrags_icon", "sewingkit_icon", "");
break;
case 1:
case 2:
case 3:
case 4:
case 5:
case 6:
}
}
});
AlertDialog dialog = builder.create();
dialog.show();
dialog.getWindow().setLayout(800, 700);
}
});
}
Thanks in advance.
you can use custom dialog for that like this create a custom layout like this
<?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">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="title"
android:textColor="#000" />
<TextView
android:id="#+id/tvTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="Lorem Ipsum is simply dummy text " />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="description" />
</LinearLayout>
now create custom dailog llike below code
final Dialog dialog = new Dialog(LoginActivity.this);
dialog.setContentView(R.layout.custom_dialog_layout);
Textview tvTitle;
tvTitle = (Textview) dialog.findViewById(R.id.tvTitle);
Window window = dialog.getWindow();
window.setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT);
window.setGravity(Gravity.BOTTOM);
tvTitle.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// perform your action here
}
});
dialog.show();
You can use custom view in AlertDialog.
AlertDialog.Builder alertDialog = new AlertDialog.Builder(YourActivity.this);
View alertView = getLayoutInflater().inflate(R.layout.fragment_dialog, null, false);
alertDialog.setView(alertView);
Add your custom requirement of your text in your layout file.
From documenation:
If you want a custom layout in a dialog, create a layout and add it to an AlertDialog by calling setView() on your AlertDialog.Builder object.
you need to apply theme for the dialog on the initial state
AlertDialog dialog = new AlertDialog(context, R.theme.DialogTheme);
and declare theme in your theme.xml or styles.xml
<style name="DialogTheme" parent="Animation.AppCompat.Dialog">
<item name="android:itemTextAppearance">#style/DialogTextAppear</item>
</style>
<style name="DialogTextAppear" parent="TextAppearance.AppCompat">
<item name="android:gravity">center</item>
</style>
You can find AlertDialog Title view by this code and set gravity manual but first you must create a AlertDialogBuilder and one Alert dialog like in code
MaterialAlertDialogBuilder dialogBuilder = new MaterialAlertDialogBuilder(this);
dialogBuilder
.
.
.
AlertDialog alertDialog = dialogBuilder.create();
alertDialog.show();
LinearLayout titleTemplate = alertDialog.findViewById(R.id.title_template);
titleTemplate.setGravity(Gravity.RIGHT);
You can find all view in alert_dialog.xml and find in code and use it.

Custom Dialog filling the screen height

I am trying to create a custom Dialog, and is working just fine, but the Dialog is filling the whole screen height. I've done some unsuccessful research on the internet but I don't have a lot of time for this task, if anyone knows why is this happening I appreciate it hard.
here is the onCreateDialog():
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder= new AlertDialog.Builder(getContext());
LayoutInflater inflater= (LayoutInflater) builder.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.dialog__warning, mContainer, false);
txtWarning = (TextView)view.findViewById(R.id.txtWarning);
if(messageWarning.length()>0)
{
txtWarning.setText(messageWarning);
}
btnOkDialog = (Button) view.findViewById(R.id.btnOkDialog);
btnOkDialog.setOnClickListener(MyListener);
btnCancelDialog = (Button) view.findViewById(R.id.btnCancelDialog);
btnCancelDialog.setOnClickListener(MyListener);
builder.setView(view);
return builder.create();
}
and the xml of dialog_warning:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="300dp"
android:layout_height="300dp"
android:background="#b1b0b0"
tools:context="ipat.johanbayona.gca.ipat.NewEvidence.Dialog_Warning">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:textSize="25sp"
android:id="#+id/txtWarning"
android:text="Mensaje error" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="OK"
android:id="#+id/btnOkDialog"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_marginBottom="10dp"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Cancel"
android:id="#+id/btnCancelDialog"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginBottom="10dp"
/>
</RelativeLayout>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="300dp"
android:layout_height="300dp"
android:background="#b1b0b0"
tools:context="ipat.johanbayona.gca.ipat.NewEvidence.Dialog_Warning">
Change "layout_height" to wrap_content. You fixed height and width, that may be the problem depending on your screen size.
try creating your own theme for the dialog, something like this
<style name="AppDialog">
<item name="android:windowFrame">#null</item>
<item name="android:windowIsFloating">true</item>
<item name="android:windowContentOverlay">#null</item>
<item name="android:windowAnimationStyle">#android:style/Animation.Dialog</item>
<item name="android:windowBackground">#android:color/transparent</item>
<item name="android:colorBackgroundCacheHint">#null</item>
</style>
and set this theme passing to the dialog's super constructor with R.style.AppDialog
upd.
it's not the builder's constructor. you should create your own class that extends the AlertDialog, override it's onCreate and the super constructor and your class should be something like this
public class YourDialog extends Dialog {
public YourDialog(#NonNull Context context) {
super(context, R.style.AppDialog);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dialog__warning);
txtWarning = (TextView) findViewById(R.id.txtWarning);
if(messageWarning.length()>0)
{
txtWarning.setText(messageWarning);
}
btnOkDialog = (Button) findViewById(R.id.btnOkDialog);
btnOkDialog.setOnClickListener(MyListener);
btnCancelDialog = (Button) findViewById(R.id.btnCancelDialog);
btnCancelDialog.setOnClickListener(MyListener);
}
}
overriding the main constructor, and passing the R.style.AppDialog should do the trick. all you need is to call where you want to show the dialog new YourDialog(context).show();
note that you don't need to call manually the layout inflater to pass your own contentview, just calling the setContentView with resId will automatically inflate the view beneath.

Android Dialog without transparent margins (fullscreen)

I have an alert dialog created using the alertdialog builder. I want to remove the space in the left and right of the dialog... basically extend it from side to side. I know I could you an activity instead of a dialog, but I want to keep the button style and implementing that button style in an activity requires making a layout for different SDKs, which is not convenient in the long run.
Why I need it full width?
Because I need to display AdMob ads and if they are not full width the ads will not load.
Any help is appreciated as I have tried all kinds of theme properties...
Thanks,
Adrian
PS: Here is my current code for creating the dialog:
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(new ContextThemeWrapper(this, R.style.DialogTheme));
alertDialogBuilder.setInverseBackgroundForced(true);
LayoutInflater inflater = this.getLayoutInflater();
View view = inflater.inflate(R.layout.dial_dialog, null);
AdView adView = (AdView) view.findViewById(R.id.adView);
if (!application.getLicense().isValid()) {
adView.loadAd(new AdRequestWrapper(this));
}
alertDialogBuilder.setView(view);
alertDialogBuilder.setTitle(R.string.dial_dialog_title).setCancelable(false);
alertDialogBuilder.setPositiveButton(R.string.dial_dialog_message_positive_text, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.dismiss();
}
});
alertDialogBuilder.setNeutralButton(R.string.dial_dialog_message_neutral_text, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.dismiss();
}
});
alertDialogBuilder.setNegativeButton(R.string.dial_dialog_message_negative_text, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.dismiss();
}
});
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
alertDialog.getWindow().setLayout(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
PS2: Here is an image with what I need... http://www.tiikoni.com/tis/view/?id=f3aed38
I need to loose all the space with red. I don't have any problems with the space marked in yellow. (it can be kept or it can be removed)
I know a little bit late to come back with an answer, but following the recipes from here I managed to solved almost everything. Basically I have the following Dialog theme:
<style name="DialogTheme" parent="android:Theme.Holo.Light.Dialog">
<item name="android:windowBackground">#drawable/screen_background_selector_light</item>
<item name="android:windowIsFloating">true</item>
<item name="android:windowMinWidthMajor">100%</item>
<item name="android:windowMinWidthMinor">100%</item>
</style>
Note the reference to screen_background_selector_light which means I also had to add screen_background_selector_light.xml and (its dependency background_holo_light.xml).
Other that that I only had to specify the activity will use this new theme:
<activity android:name=".zom.xyz.app.activity.MyActivity_" android:label="#string/activity_title" android:theme="#style/DialogTheme"/>
And of course create it as any other activity.
As for the other need I had... easy buttons I did it this way. Here is the layout of the activity
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:focusable="true"
android:focusableInTouchMode="true"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin">
INSERT_DIALOG_CONTENT_HERE
</RelativeLayout>
<LinearLayout
style="?android:attr/buttonBarStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="#+id/cancelButton"
style="?android:attr/buttonBarButtonStyle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="fill_vertical"
android:layout_weight="1"
android:text="#string/activity_message_negative_text"/>
<Button
android:id="#+id/skipButton"
style="?android:attr/buttonBarButtonStyle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="fill_vertical"
android:layout_weight="1"
android:text="#string/activity_message_neutral_text"/>
<Button
android:id="#+id/correctButton"
style="?android:attr/buttonBarButtonStyle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="fill_vertical"
android:layout_weight="1"
android:text="#string/activity_message_positive_text"/>
</LinearLayout>
Using this layout and theme will give you the exact feel of a dialog while giving it full width.
1 )Create your own dialog extended from Dialog and in constructor set style, something like this:
public MyDialog(Context c, Event e) {
super(c, android.R.style.Theme_Light);
}
or
2) when creating instanse, set style:
Dialog dialog = new Dialog(this, R.style.MyDialogTheme);
example here

Need help by creating a dialog with 2 NumberPickers

I'm trying to create a dialog with 2 NumberPickers. I would like them to be default Holo theme styled. I cant find any good examples. So far i got:
LayoutInflater inflater = (LayoutInflater)
getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View npView = inflater.inflate(R.layout.number_picker_dialog, null);
NumberPicker minPicker = (NumberPicker) npView.findViewById(R.id.min_picker);
minPicker.setMaxValue(100);
minPicker.setMinValue(0);
NumberPicker maxPicker = (NumberPicker) npView.findViewById(R.id.max_picker);
maxPicker.setMaxValue(100);
maxPicker.setMinValue(0);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Text Size:");
builder.setView(npView);
builder.setPositiveButton("Okay",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
}
});
builder.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
}
});
dialog = builder.create();
dialog.show();
number_picker_dialog xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:holo="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:gravity="center">
<NumberPicker
android:id="#+id/min_picker"
android:width="100dip"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:/>
<NumberPicker
android:id="#+id/max_picker"
android:width="100dip"
android:layout_height="wrap_content"
android:layout_width="wrap_content"/>
</LinearLayout>
But the colorpicker text are white (Like the background), and i cant set the textColor of the NumberPicker.
How can i set the textColor or does anyone knows a good NumberPicker example?
I found the problem why my NumberPicker wasn't Holo.Light styled:
instead of calling:
LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View npView = inflater.inflate(R.layout.number_picker_dialog, null);
i solved it by calling:
View npView = getLayoutInflater().inflate(R.layout.number_picker_dialog, null);
My approach was to create a Custom Dialog class as show below.
public class CustomDialog extends Dialog {
public CustomDialog(Context context) {
super(context, R.style.customDialog); //use your style id from styles.xml
}
public void setNumberDialog() {
setContentView(R.layout.number_picker_dialog);
//add required listeners
show();
}
}
Invoke the dialog from calling acitivty.
new CustomDialog(context).setNumberDialog();
And the style parameters are defined in styles.xml
<style name="customDialog" parent="android:Theme.Dialog">
<item name="android:windowAnimationStyle">#android:style/Animation.Dialog</item>
<item name="android:windowSoftInputMode">stateUnspecified|adjustPan</item>
<item name="android:background">#android:color/transparent</item>
<item name="android:textColor">#color/textColorWhite</item>
</style>
Try :
<NumberPicker
style="#android:style/TextAppearance">
</NumberPicker>

Categories

Resources