I want to remove title section from dialog box in Android and I want to use my own created xml file as Dialog box.
Inflate your XML. Like this
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(context);
LayoutInflater inflater = context.getLayoutInflater();
final View dialogView = inflater.inflate(R.layout.yourlayout, null);
dialogBuilder.setView(dialogView);
AlertDialog b = dialogBuilder.create();
b.show();
You can inflate your own layout and set it to you Dialog.
Dialog dialog = new Dialog(ProfileSettingsActivity.this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.uploadphoto); // Your layout here
WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
lp.copyFrom(dialog.getWindow().getAttributes());
lp.width = WindowManager.LayoutParams.WRAP_CONTENT; //Custom width and height
lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
dialog.getWindow().setAttributes(lp);
dialog.setCancelable(true);
dialog.show();
Simply add style to dialog and remove title of the custom dialog.
Custom_dialog.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="wrap_content"
android:background="#color/colorPrimary"
android:orientation="vertical">
<TextView
android:id="#+id/txt_warning"`enter code here`
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="10dp"
android:text="Warning"
android:textAllCaps="true"
android:textColor="#android:color/white"
android:textSize="18dp"
android:textStyle="bold" />
<TextView
android:id="#+id/txt_dia"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="10dp"
android:text="Are you sure you want to Logout?"
android:textColor="#android:color/white"
android:textSize="18dp" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginTop="15dp"
android:background="#android:color/white" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/colorPrimary"
android:orientation="horizontal"
android:weightSum="1">
<Button
android:id="#+id/btn_no"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:layout_weight=".5"
android:background="#color/colorPrimary"
android:clickable="true"
android:gravity="center"
android:padding="10dp"
android:text="No"
android:textColor="#android:color/white"
android:textSize="18dp" />
<View
android:layout_width="1dp"
android:layout_height="match_parent"
android:background="#android:color/white" />
<Button
android:id="#+id/btn_yes"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".5"
android:background="#color/colorPrimary"
android:clickable="true"
android:gravity="center"
android:padding="10dp"
android:text="Yes"
android:textColor="#android:color/white"
android:textSize="18dp" />
</LinearLayout>
</LinearLayout>
Set style in Styles.xml:-
<style name="FullHeightDialog" parent="android:style/Theme.Dialog">
<item name="android:windowNoTitle">true</item>
</style>
On button Click listener you have to write this code:-
// custom dialog
final Dialog dialog = new Dialog(mContext, R.style.FullHeightDialog);
dialog.setContentView(R.layout.custom_dialog);
dialog.setCancelable(false);
WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
lp.copyFrom(dialog.getWindow().getAttributes());
lp.width = WindowManager.LayoutParams.MATCH_PARENT;
lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
dialog.getWindow().setAttributes(lp);
dialog.show();
Button btnYes = (Button) dialog.findViewById(R.id.btn_yes);
Button btnNo = (Button) dialog.findViewById(R.id.btn_no);
// if button is clicked, close the custom dialog
btnYes.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
documentImage = documentImagesList.get(getAdapterPosition());
//logout code here
}
});
btnNo.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
dialog.dismiss();
}
});
Here is code for same:
final AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(getContext());
LayoutInflater layoutInflater = getActivity().getLayoutInflater();
final ViewGroup viewGroup = null;
View dialogView = layoutInflater.inflate(R.layout.custom_email_dialog, viewGroup, false);
dialogBuilder.setView(dialogView);
final AlertDialog alertDialog = dialogBuilder.create();
alertDialog.show();
Here is "custom_email_dialog" is custom XML that we need to show on dialog.
and you can access dialog element as:
TextView headerTextView = (TextView) dialogView.findViewById(R.id.emailHeader);
Related
I am developing an Android application, where I need to show a popup containing some message on application opening (Image attached for reference).
I have tried to achieve this using toast message and snack bar but could not do exact same.
Please suggest which component in Android to be used to achieve this functionality?
You can achieve the desired results using a dialog. I am sharing the code, you can modify it as you want but make sure you put it inside the onCreate method of the launcher activity.
This is the code for a simple dialog
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Look at this dialog!")
.setCancelable(false)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
//do things
}
});
AlertDialog alert = builder.create();
alert.show();
The one shown in the picture shared by you is a custom dialog where you have to make your own layout. Here is an example,
1. Create your own layout
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ffffffff">
<ImageView
android:layout_width="match_parent"
android:layout_height="120dp"
android:id="#+id/a"
android:gravity="center"
android:background="#DA5F6A"
android:src="#drawable/dialog_cross"
android:scaleType="fitCenter" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TEXT"
android:id="#+id/text_dialog"
android:layout_below="#+id/a"
android:layout_marginTop="20dp"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:layout_marginBottom="20dp"
android:textSize="18sp"
android:textColor="#ff000000"
android:layout_centerHorizontal="true"
android:gravity="center_horizontal" />
<Button
android:layout_width="wrap_content"
android:layout_height="30dp"
android:text="OK"
android:id="#+id/btn_dialog"
android:gravity="center_vertical|center_horizontal"
android:layout_below="#+id/text_dialog"
android:layout_marginBottom="20dp"
android:background="#drawable/btn_flat_red_selector"
android:layout_centerHorizontal="true"
android:textColor="#ffffffff" />
</RelativeLayout>
2. Create a class for the custom dialog
public class ViewDialog {
public void showDialog(Activity activity, String msg){
final Dialog dialog = new Dialog(activity);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setCancelable(false);
dialog.setContentView(R.layout.dialog);
TextView text = (TextView) dialog.findViewById(R.id.text_dialog);
text.setText(msg);
Button dialogButton = (Button) dialog.findViewById(R.id.btn_dialog);
dialogButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
}
}
3. Make an object in your launcher activity of the custom dialog class and call the function showDialog
ViewDialog alert = new ViewDialog();
alert.showDialog(getActivity(), "Thank you for installing the Paytm
App");
Or you could use an external library for getting the desired results
Pop.on(this).with().title(R.string.title).layout(R.layout.custom_pop).show();
where R.layout.custom_pop is the custom layout of your dialog.
Try this for same result:
dialog.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">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="right">
<Button
android:id="#+id/btnClose"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="X" />
</LinearLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="10dp"
android:text="Hello, this is your message"
android:textSize="20sp" />
</LinearLayout>
code:
private void showDialog() {
final Dialog dialog = new Dialog(mContext);
dialog.setContentView(R.layout.dialog);
Window window = dialog.getWindow();
WindowManager.LayoutParams wlp = window.getAttributes();
wlp.gravity = Gravity.BOTTOM;
wlp.flags &= ~WindowManager.LayoutParams.FLAG_DIM_BEHIND;
window.setAttributes(wlp);
dialog.findViewById(R.id.btnClose).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
dialog.cancel();
}
});
dialog.show();
}
I am working on an Android Application , Here I have created a custom dialog box , The dialog box was appearing on complete height of the device , but I wanted to leave margins from top and bottom , so I set margin using below code
WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
lp.copyFrom(alertDialog.getWindow().getAttributes());
lp.width = WindowManager.LayoutParams.WRAP_CONTENT;
lp.height = Utility.convertDPtoPixel(400, screen);
alertDialog.show();
alertDialog.getWindow().setAttributes(lp);
Above code worked perfectly, but the dialog appears with black surroundings.
Here is the screen shot:
Now I want to remove these black surroundings from the dialog box.
Here is the code of dialog box:
public void showDialog() {
AlertDialog.Builder builder;
final AlertDialog alertDialog;
//final Dialog alertDialog;
Context mContext;
mContext = screen;
LayoutInflater inflater = (LayoutInflater) LoginActivity.screen.getSystemService(LoginActivity.screen.LAYOUT_INFLATER_SERVICE);
layout = inflater.inflate(R.layout.countrylist, null);
LinearLayout rellayout = (LinearLayout) layout.findViewById(R.id.rellayout);
RelativeLayout closeAcessCodeDialog = (RelativeLayout)layout.findViewById(R.id.closeAcessCodeDialog) ;
ListView listview = (ListView) layout.findViewById(R.id.listview);
listview.setAdapter(new CustomAdapter1(LoginActivity.screen, countryList));
builder = new AlertDialog.Builder(LoginActivity.screen);
builder.setView(layout);
alertDialog = builder.create();
alertDialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
lp.copyFrom(alertDialog.getWindow().getAttributes());
lp.width = WindowManager.LayoutParams.WRAP_CONTENT;
lp.height = Utility.convertDPtoPixel(400, screen);
alertDialog.show();
alertDialog.getWindow().setAttributes(lp);
listview.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int pos,
long id) {
countryName = idCountry.get(pos);
System.out.println("Country code is: " + countryName);
alertDialog.cancel();
text.setText(countryList.get(pos));
}
});
closeAcessCodeDialog.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
alertDialog.dismiss();
}
});
}
countrylist.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardBackgroundColor="#ffe264"
app:cardCornerRadius="10dp"
>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/rellayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_above="#+id/listview"
android:gravity="center_vertical"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Select your Country"
android:textColor="#000"
android:textSize="18sp"
android:textStyle="bold"
android:layout_centerInParent="true"
/>
<RelativeLayout
android:id="#+id/closeAcessCodeDialog"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_alignParentRight="true"
android:gravity="center">
<ImageButton
android:id="#+id/closeDialog"
android:layout_width="30dp"
android:layout_height="30dp"
android:background="#drawable/close_country_dialog" />
</RelativeLayout>
</RelativeLayout>
<ListView
android:id="#+id/listview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:cacheColorHint="#android:color/transparent"
android:divider="#e5e5e5"
android:background="#drawable/list_bacground"
android:dividerHeight="1px" />
</LinearLayout>
I visited many similar links over stackoverflow , but no one was helpfull .
Hope this will help you a lot
I think its default theme color which is Black so you have to check the import of your AlertDialog it should be import android.support.v7.app.AlertDialog; and one more thing you should use custom color for Background like below
alertDialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
Try this.
Dialog alertDialog = new Dialog(this);
alertDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
alertDialog.setContentView(R.layout.yourlayout);
alertDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
alertDialog.show();
I created a custom dialog but I got an unexpected result.
The final appearance of the dialog is:
As you can see, my custom dialog is shown with a kind of padding in both top and bottom of the dialog, and I don't code it to do this...
The code is the next:
#Override
protected Dialog onCreateDialog(int id){
LayoutInflater inflater = (LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View newAsig = inflater.inflate(R.layout.nuevaasignatura_act, null);
//Also, down here I tried to shape the dialog by setting a functional resource, but
//the stroke should be blue, not grey.
RelativeLayout re = (RelativeLayout)newAsig.findViewById(R.id.layoutCrearAsig1);
re.setBackgroundResource(R.drawable.borde);
Typeface tf = Typeface.createFromAsset(getAssets(), "Roboto-Light.ttf");
ImageView ico = (ImageView)newAsig.findViewById(R.id.ivicono);
ico.setBackgroundResource(R.drawable.colorprincipal);
LinearLayout ly = (LinearLayout)newAsig.findViewById(R.id.layoutCrearAsig2);
ly.setBackgroundColor(Color.rgb(214, 217, 224));
TextView txt = (TextView)newAsig.findViewById(R.id.textviewnuevaasig);
txt.setTypeface(tf);
TextView txt1 = (TextView)newAsig.findViewById(R.id.textViewCarpeta);
txt1.setTypeface(tf);
txt1.setBackgroundResource(R.drawable.colorprincipal);
Button btn = (Button)newAsig.findViewById(R.id.buttonCrearAsig);
btn.setTypeface(tf);
switch (id){
case 0:
return new AlertDialog.Builder(this)
.setView(newAsig).create();
}return null;
And the XML file...
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/layoutCrearAsig1">
<ImageView
android:id="#+id/ivicono"
android:layout_width="50dp"
android:layout_height="50dp"
android:paddingLeft="4dp"
android:background="#color/DarkGray"
android:contentDescription="Icono ClassMarks"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:src="#drawable/icono_cm" />
<TextView
android:id="#+id/textViewCarpeta"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_toRightOf="#id/ivicono"
android:background="#color/DarkGray"
android:clickable="true"
android:gravity="center"
android:text="Carpeta"
android:textAppearance="?android:attr/textAppearanceLarge" />
<LinearLayout
android:id="#+id/layoutCrearAsig2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/ivicono"
android:orientation="vertical">
<TextView
android:id="#+id/textviewnuevaasig"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="20dp"
android:text="Crear nueva asignatura"
android:textSize="20sp" />
<EditText
android:id="#+id/edittextAsig"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:layout_gravity="center_horizontal"
android:layout_marginTop="20dp" >
</EditText>
<Button
android:id="#+id/buttonCrearAsig"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="20dp"
android:layout_marginBottom="20dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:textSize="20sp"
android:text="Crear"/>
</LinearLayout>
</RelativeLayout>
I hope you can help me. Thanks!
The idea is AlertDialog always has 5dp top and bottom padding.
Try something like this:
AlertDialog dialog = new AlertDialog.Builder(this).create();
dialog.setView(newAsig, 0, 0, 0, 0);
Try this validation.
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder;
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) {
builder = new AlertDialog.Builder(getActivity());
builder.setInverseBackgroundForced(true);
} else {
builder = new AlertDialog.Builder(getActivity(), AlertDialog.THEME_DEVICE_DEFAULT_LIGHT);
}
}
I want to create a different theme for all of alertDialog instances. I need my own title view instead of the usual black title background. All text should have a blue color, and set the edge of the alertDialog to a round shape.
Is it possible to create our own theme for alert dialogs, using any style or creating a class which extends AlertDialog.Builder? I need a common theme or style for my all instances of alertDialog. I am using alertDialog in many places - one for singleChoice items, one with ArrayAdapter.
My alertDialog with array adapter:
String[] items = {"Edit profile","Change user","Change password","Logout"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(Loged.this,
R.layout.my_spinner_layout, items);
settingMenu.setAdapter(adapter, listener);
My alertDialog with single choice items:
alertDelete = new AlertDialog.Builder(getParent());
alertDelete.setTitle("Delete");
alertDelete.setCancelable(true);
CharSequence[] choice = {"this user","All user"};
alertDelete.setSingleChoiceItems(choice, 0,
For my all alertDialog, I need a common theme, like:
please check this link. here am creating my own alertDialog class
and its a simple method if you reuse the alertDialog in many situation in your application
how Create setAdapter() for a AlertDialog
I solved my problem by creating a class which extends Dialog, and I created my own functions. for example setMessage,setTitle,setPositiveButton,setNegativeButton etc.
But am confusing on how we can replace the setAdapter() and setSingleChoice().
Here one example For Dialog:
button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// custom dialog
final Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.custom);
dialog.setTitle("Give a Title...");
// set the custom dialog components - text, image and button
TextView text = (TextView) dialog.findViewById(R.id.text);
text.setText("Put here any custom text!");
ImageView image = (ImageView) dialog.findViewById(R.id.image);
image.setImageResource(R.drawable.ic_launcher);
Button dialogButton = (Button) dialog.findViewById(R.id.ButtonOK);
// if button is clickedthen dialog will closed
dialogButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
}
});
Custom AlertDialog
AlertDialog.Builder builder;
AlertDialog alertDialog;
Context mContext;
LayoutInflater inflater = (LayoutInflater)mContext.getSystemService(LAYOUT_INFLATER_SERVICE);
View urlayoutfile = inflater.inflate(R.layout.custom_dialog_xmlfile,(ViewGroup)findViewById(R.id.Yourlayout_id));
builder = new AlertDialog.Builder(this);
builder.setView(urlayoutfile);
alertDialog = builder.create();
alertDialog.show();
To use a specific style, use
ctw = new ContextThemeWrapper(this, R.style.MyStyle);
new AlertDialog.Builder(ctw)
.setTitle(...)
You can use layout as Different themes, styles, Backgrounds
customdialog.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/relativeLayout1"
style="#android:style/Theme.Black.NoTitleBar.Fullscreen"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:id="#+id/layoutsample"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:orientation="vertical" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/dialoghdrbg"
android:orientation="horizontal" >
<ImageView
android:id="#+id/dialogheaderimage1"
android:layout_width="50dp"
android:layout_height="48dp"
android:scaleType="fitXY"
/>
<TextView
android:id="#+id/dialogheadertext1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|right"
android:layout_marginLeft="1dp"
android:gravity="center_vertical|right"
android:textColor="#000000"
android:textSize="25dp"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/dialogcontentbg"
android:orientation="vertical" android:gravity="center">
<TextView
android:id="#+id/dialogmessgetext1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:textColor="#ffffff"
android:textSize="23dp"
android:text=""
android:gravity="center"
/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center|center_horizontal"
android:gravity="center|center_horizontal"
android:layout_marginTop="20dip"
android:orientation="horizontal">
<Button
android:id="#+id/dialogokbutton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="15dp"
android:background="#drawable/buttonanimation"
android:text="Retry"
android:textSize="20dp"
android:textStyle="bold" />
<Button
android:id="#+id/dialogcancelbutton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:background="#drawable/buttonanimation"
android:text="Report"
android:textSize="20dp"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
</RelativeLayout>
customDialog.java
final Dialog favDialog = new Dialog(Myclass.this,
android.R.style.Theme_Translucent_NoTitleBar_Fullscreen);
favDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
favDialog.getWindow().setFlags(
WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
favDialog.setContentView(R.layout.reloadurlpopuplayout);
favDialog.setCancelable(false);
ImageView dialogImage = (ImageView) favDialog
.findViewById(R.id.dialogheaderimage1);
dialogImage.setBackgroundResource(R.drawable.questionmark);
TextView dialogMesage = (TextView ) favDialog
.findViewById(R.id.dialogmessgetext1);
TextView dialogHeader = (TextView) favDialog
.findViewById(R.id.dialogheadertext1);
String descText = getString(R.string.RetryReportMessage);
dialogMesage.setBackgroundColor(0x00000000);
dialogMesage.setText(descText);
dialogHeader.setText(R.string.BrockenLinkHeader);
try {
favDialog.show();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
I have a task to change the AlertDialog width and height by xml,I want make that become style,so I can use it easy.And that ,I need to change button of AlertDialog style also.Can you tell me a way to achieve the target。Thank you Very grateful。
PS,I'd better achieve the target by change xml。
There are two methods 1) programatically 2) By using xml layout
1)=======>
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setView(layout);
builder.setTitle("Title");
alertDialog = builder.create();
alertDialog.show();
alertDialog.getWindow().setLayout(600, 400); //Controlling width and height.
( or )
alertDialog.show();
WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
lp.copyFrom(alertDialog.getWindow().getAttributes());
lp.width = 150;
lp.height = 500;
lp.x=-170;
lp.y=100;
alertDialog.getWindow().setAttributes(lp);
If you want to show the layout to be displayed like Alert dialog see this
2)========>
choose.xml
<TextView
android:id="#+id/img"
android:layout_width="wrap_content"
android:text="#string/choose"
android:textSize="25dp"
android:textColor="#fff"
android:layout_height="50dp"/>
<TableLayout android:id="#+id/table"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#fff"
android:orientation="vertical">
<TableRow
android:id="#+id/tr1"
android:orientation="horizontal"
android:layout_margin="10dp">
<ImageView
android:contentDescription="#string/phone"
android:src="#drawable/phone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="#+id/phnText"
android:layout_width="wrap_content"
android:text="#string/phone"
android:gravity="left|center_vertical"
android:layout_marginLeft="10dp"
android:textSize="25dp"
android:textColor="#000"
android:layout_height="50dp"/>
</TableRow>
<View
android:layout_width="fill_parent"
android:layout_height="1dip"
android:background="#FF000000" />
<TableRow
android:id="#+id/tr2"
android:orientation="horizontal"
android:layout_margin="10dp">
<ImageView
android:contentDescription="#string/sms"
android:src="#drawable/sms"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="#+id/smsText"
android:layout_width="wrap_content"
android:text="#string/sms"
android:gravity="left|center_vertical"
android:layout_marginLeft="10dp"
android:textSize="25dp"
android:textColor="#000"
android:layout_height="50dp"/>
</TableRow>
</TableLayout>
</LinearLayout>
display this as popup as like below
private void showPopUp()
{
final AlertDialog.Builder helpBuilder = new AlertDialog.Builder(this);
helpBuilder.setTitle("");
LayoutInflater inflater = getLayoutInflater();
final View checkboxLayout = inflater.inflate(R.layout.choose, null);
helpBuilder.setView(checkboxLayout);
final AlertDialog helpDialog = helpBuilder.create();
helpDialog.show();
TableRow tablerowPhone = (TableRow)checkboxLayout.findViewById(R.id.tr1);
TableRow tablerowSms = (TableRow)checkboxLayout.findViewById(R.id.tr2);
tablerowPhone.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
helpDialog.dismiss();
Uri callUri = Uri.parse("tel:" + listview_array[4]);
Intent intent = new Intent(Intent.ACTION_CALL, callUri);
startActivity(intent);
}
});
tablerowSms.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
helpDialog.dismiss();
Uri smsUri = Uri.parse("sms:" + listview_array[4]);
Intent intent = new Intent(Intent.ACTION_VIEW, smsUri);
startActivity(intent);
}
});
}
call this showPopUp() method where you want. so that you can set height and width to the layout in xml file