Change Title header of AlertDialog - android

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>

Related

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.

setVisibility(View.INVISIBLE) causes exception, why?

why my setVisibility(View.INVISIBLE) causes exception while
setVisibility for TextView works. I also tried set it for ImageView and it also does not work - I am getting exception too
public void alertdiag() {
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
LayoutInflater inflater = this.getLayoutInflater();
View dialogView = inflater.inflate(R.layout.alertdiag_layout, null);
dialogBuilder.setView(dialogView).show();
View Divider1 = (View) dialogView.findViewById(R.style.Divider1);
Divider1.setVisibility(View.INVISIBLE); //causes java.lang.IllegalStateException: Could not execute method of the activity"
TextView HELP0 = (TextView) dialogView.findViewById(R.id.HELP0);
HELP0.setVisibility(View.INVISIBLE); // this works
}
alertdiag_layout.xml:
<View style="#style/Divider1"
android:layout_below="#+id/HELP3"
android:background="#000000"
android:layout_alignRight="#+id/HELP_FIX_LINE"
android:layout_alignLeft="#+id/HELP3"
/>
<TextView
android:id="#+id/HELP0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_below="#+id/HELP_FIX_LINE"
android:textStyle="bold"
android:typeface="normal"
/>
styles.xml:
<style name="Divider1">
<item name="android:layout_width">match_parent</item>
<item name="android:layout_height">1dp</item>
</style>
You are trying to set the Visibility to a
Style (R.style.Divider1)
Add an Id to your View and change the R.style.Divider1 to R.id.yourNewId
<View style="#style/Divider1" android:id="#+id/yourNewId"
android:layout_below="#id/HELP3" android:background="#000000"
android:layout_alignRight="#id/HELP_FIX_LINE"
android:layout_alignLeft="#id/HELP3" />
Add id attribute to your view
<View style="#style/Divider1"
android:id="#+id/my_view"
android:layout_below="#id/HELP3"
android:background="#000000"
android:layout_alignRight="#id/HELP_FIX_LINE"
android:layout_alignLeft="#id/HELP3"
/>
NOTE: I switched #+id/HELP3 and #+id/HELP_FIX_LINE with #id/HELP3 and #id/HELP_FIX_LINE respectively because #+id/ is only used when you are giving a view an id. It is not used to refer other views.
Now, Change this line of code
View Divider1 = (View) dialogView.findViewById(R.style.Divider1);
with
View Divider1 = (View) dialogView.findViewById(R.id.my_view);
It will find the view with the given id, instead of style.
To make the view invisible you need to set
Divider1.setVisibility(View.INVISIBLE);
setVisibility must be used on the view, not on the style.

how to customize Dialog header in android studio?

I'm using a custom dialog that I've created in a separate xml file in my project, and I'm coloring the main window a blueish tint,
but the main header still remains the default white color.
Is there no way to change the font color, size, background for the header?
Is the only thing I can change in the header the text?
xml:
<?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"
android:paddingLeft="15dp"
android:paddingRight="15dp"
android:background="#3edfbc"
android:orientation="vertical">
<TextView
android:id="#+id/textaligmentManager_loader_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#ffffff"
android:text="Initlizing Wifi"
android:textAppearance="?android:attr/textAppearanceLarge" />
<com.github.ybq.android.spinkit.SpinKitView xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/barcodeScanning_spinkit"
style="#style/SpinKitView.Large.FoldingCube"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_below="#+id/textaligmentManager_loader_textview"
android:padding="20dp"
android:layout_centerHorizontal="true"
app:SpinKit_Color="#ffffff" />
</RelativeLayout>
dialog:
Dialog dialog = new Dialog(Scanning_Barcode_Activity.this);
dialog.setContentView(R.layout.aligment_manager_loader_layout);
dialog.setTitle("Loading");
dialog.setCancelable(false);
//set up text
loaderScreenMainText = (TextView) dialog.findViewById(R.id.textaligmentManager_loader_textview);
loaderScreenMainText.setText("Loading Wifi");
//progressBar = (ProgressBar) dialog.findViewById(R.id.barcodeScanning_spinkit);
//DoubleBounce doubleBounce = new DoubleBounce();
//progressBar.setIndeterminateDrawable(doubleBounce);
//now that the dialog is set up, it's time to show it
dialog.show();
You can use AlertDialog which is a subclass of Dialog class. Here you can define a custom layout containing everything such as title, body and buttons. No extra title section will appear. Here is a demo:
AlertDialog.Builder builder = new AlertDialog.Builder(Scanning_Barcode_Activity.this);
builder.setCancelable(false);
LayoutInflater inflater = LayoutInflater.from(Scanning_Barcode_Activity.this);
View dialogView = inflater.inflate(R.layout.aligment_manager_loader_layout, null);
TextView loaderScreenMainText = (TextView) dialogView.findViewById(R.id.textaligmentManager_loader_textview);
loaderScreenMainText.setText("Loading Wifi");
builder.setView(dialogView);
AlertDialog dialog = builder.create();
dialog.show();
Design your custom layout in such a way that the layout itself contains the header part. And then skip this code:
dialog.setTitle("Loading");
Instead add this statement:
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
You can also use AlertDialog. In that case requestWindowFeature() method is not required.
android.support.v7.app.AlertDialog.Builder builder = new android.support.v7.app.AlertDialog.Builder(Scanning_Barcode_Activity.this);
LayoutInflater inflater = LayoutInflater.from(Scanning_Barcode_Activity.this);
View customView = inflater.inflate(your_layout, null);
builder.setCancelable(false);
loaderScreenMainText = (TextView) customView.findViewById(R.id.textaligmentManager_loader_textview);
loaderScreenMainText.setText("Loading Wifi");
builder.setView(customView);
AlertDialog dialog = builder.create();
dialog.show();
There are plenty of stylish open source libraries that you can use for customizing header parts.
Here I'm sharing my newly developed open source library : PanterDialog
Hope it will help you.

Dialog.setTitle not showing a title

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.

To set background of Custom Layout with an ImageView to Transparent

I am building a Custom dialog box using the Android Developer docs link , for this i made a layout file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/rotatelayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#80000000"
>
<ImageView
android:id="#+id/dialogimage"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</LinearLayout>
and i am inflating this dialog,
AlertDialog dialog;
AlertDialog.Builder builder;
builder = new AlertDialog.Builder(TabsActivity.this);
LayoutInflater inflator = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
View view = inflator.inflate(R.layout.rotate_dialog_layout, (ViewGroup) findViewById(R.id.rotatelayout));
ImageView image = (ImageView) view.findViewById(R.id.dialogimage);
image.setOnClickListener(new RotateLockListener());
image.setBackgroundColor(Color.parseColor("#80000000"));
if(locked)
{
image.setImageResource(R.drawable.lock_icon);
}
else
{
image.setImageResource(R.drawable.unlock_icon);
}
builder.setView(view).setCancelable(true);
dialog = builder.create();
dialog.setView(view, 0, 0, 0, 0);
timerDelayRemoveDialog(time, dialog);
dialog.show();
but still it appears as
I have tried all the help provided at the stack over flow
Setting ImageView background to transparent,
Setting transparency by #80000000 and
Setting Dialog window transparency
But none of them worked, it still showing up.
Or you can use Dialog class for this,
Dialog dialog = new Dialog(this,
android.R.style.Theme_Translucent_NoTitleBar);
Window window = dialog.getWindow();
window.setFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON,
WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
window.setLayout(ViewGroup.LayoutParams.FILL_PARENT,
ViewGroup.LayoutParams.FILL_PARENT);
dialog.setCancelable(true);
dialog.setCanceledOnTouchOutside(true);
dialog.setContentView(R.layout.temp);
dialog.show();
Try using this as the style of your View :
<style name="Dialog_Fullscreen">
<item name="android:windowFullscreen">true</item>
<item name="android:windowNoTitle">true</item>
</style>

Categories

Resources