Set AlertBox Title Bar Background Color - android

How can I change the background color for an alertbox's title bar?
AlertDialog.Builder alert=new AlertDialog.Builder(getParent());
alert.setTitle("sample");
alert.show();

The easiest way is to subclass a dialog by creating a class which extends dialog and implements the constructor which take style as a parameter. Then make your own custom layout to it.
The Code to show the dialog:
private void showDialog()
{
Custom_Dialog dialog = new Custom_Dialog(this, R.style.myCoolDialog);
dialog.setContentView(R.layout.custom_dialog);
dialog.setTitle("Custom Dialog");
TextView text = (TextView) dialog.findViewById(R.id.text);
text.setText("Hello, this is a custom dialog!");
ImageView image = (ImageView) dialog.findViewById(R.id.image);
image.setImageResource(R.drawable.icon);
dialog.show();
}
The code for the subclass:
package com.stackoverflow;
import android.app.Dialog;
import android.content.Context;
public class Custom_Dialog extends Dialog {
protected Custom_Dialog(Context context, int theme) {
super(context, theme);
// TODO Auto-generated constructor stub
}
}
the style: myCoolDialog.xml
<resources>
<style name="myCoolDialog" parent="android:Theme.Dialog">
<item name="android:windowBackground">#drawable/blue</item>
<item name="android:colorForeground">#f0f0</item>
</style>
</resources>
and last the layout:custom_dialog.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/layout_root"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp"
>
<ImageView android:id="#+id/image"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_marginRight="10dp"
/>
<TextView android:id="#+id/text"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:textColor="#FFF"
/>
</LinearLayout>

You can just set custom title like this
LayoutInflater inflater = this.getLayoutInflater();
View titleView = inflater.inflate(R.layout.custom_title, null);
new AlertDialog.Builder(SubCategoryActivity.this)
.setCustomTitle(titleView);
and in custom_title layout you can create custom title like this
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:id="#+id/llsubhead"
android:background="#color/colorPrimary">
<TextView
android:id="#+id/exemptionSubHeading4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:layout_weight="1"
android:text="Exemption Sub Head"
android:textAppearance="#style/Base.TextAppearance.AppCompat.Medium"
android:textColor="#color/white" />
</LinearLayout>
</LinearLayout>

From the answer #CornflakesDK and #ice spirit, I thought you can use the current AlertDialog.Builder implementation to do the custom dialog and make it easy to maintain.
CustomDialogBuilder.java
public class CustomDialogBuilder extends AlertDialog.Builder {
private View view;
public CustomDialogBuilder(Context context) {
super(context);
view = LayoutInflater.from(getContext()).inflate(R.layout.custom_dialog_title, null);
setCustomTitle(view);
}
#Override
public Builder setTitle(int titleId) {
TextView titleTextView = view.findViewById(R.id.exemptionSubHeading4);
titleTextView.setText(getContext().getString(titleId));
return this;
}
#Override
public Builder setTitle(CharSequence title) {
TextView titleTextView = view.findViewById(R.id.exemptionSubHeading4);
titleTextView.setText(title);
return this;
}
}
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="match_parent"
android:orientation="vertical">
<LinearLayout
android:id="#+id/llsubhead"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:background="#color/black"
android:orientation="vertical">
<TextView
android:id="#+id/exemptionSubHeading4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_margin="15dp"
android:layout_gravity="center"
android:text="Exemption Sub Head"
android:textAppearance="#style/Base.TextAppearance.AppCompat.Medium"
android:textColor="#color/white" />
</LinearLayout>
</LinearLayout>
Inside your activity code,
new CustomDialogBuilder(MyActivity.this)
.setTitle(R.string.actions)
.setItems(R.array.items_actions, (dialog, which) -> {
// handle items
}).create().show();
Then, you can have styling inside the DialogBuilder and also utilize the functions of the AlertDialog.Builder.

Related

Android popup dialog screen size doesn't change in android Oreo

I'm working on android project. I want to show a popup dialog inside my activity. I have implemented it (All the codes attached below). But my question is, when I run this project in android the popup shows like this. I need to increase the width of this popup dialog. I tried a few solutions, but didn't work for me. Please help me to solve this matter. Thanks in advance.
Here is my code
xml file--
<?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:id="#+id/lv_main">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:id="#+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="22sp"
android:text="MSGP3006"
android:textColor="#color/Black"
android:textStyle="bold"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="#+id/type"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="QTY:2 | Weight : 500g | Discount::25%"/>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="2"
android:layout_marginBottom="#dimen/dimen_5dp"
android:layout_gravity="bottom">
<TextView
android:id="#+id/amount"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Rs.360.00"
android:gravity="end"
android:textColor="#color/Sky_Dark_Blue"
android:textSize="22sp"/>
</LinearLayout>
</LinearLayout>
java file--
public class AddInvoiceDialog extends Dialog {
public Activity activity;
public Dialog dialog;
public Button btn_add;
public AddInvoiceDialog(Activity a) {
super(a);
// TODO Auto-generated constructor stub
this.activity = a;
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.inv_add_new_invoice_popup);
btn_add = (Button) findViewById(R.id.btn_add);
btn_add.setOnClickListener(new View.OnClickListener() {
public void onClick(View v)
{
activity.finish();
}
});
}
}
Add below line after setContentView(R.layout.inv_add_new_invoice_popup);
getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.MATCH_PARENT);
You will get popup window matching device width.
You also need to add android:orientation="vertical" in your parent LinearLayout, it contains multiple child.
You can set custom view in Alert Dialog as mentioned below :
AlertDialog alertDialog = new AlertDialog.Builder(context).create();
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View dialogView = inflater.inflate(R.layout.inv_add_new_invoice_popup, null);
alertDialog.setView(dialogView);
alertDialog.setCancelable(false);
alertDialog.show();

Scroll View not working in Alert Dialog

Somehow the scroll view is not working . The message alert dialog box is showing is really big and so I need to implement vertical scrollbar. I tried to get data from previous asked question but it isn't solving my issue please help.
I need to show the alert dialog on button click event.
benefits.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
AlertDialog ad = new AlertDialog.Builder(Panchgavya.this).create();
ad.setCancelable(false); // This blocks the 'BACK' button
ad.setMessage(getString(R.string.benefits));
ad.setTitle("Benefits");
ad.setButton("OK", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
ad.show();
TextView textView = (TextView) ad.findViewById(android.R.id.message);
textView.setScroller(new Scroller(Panchgavya.this));
textView.setVerticalScrollBarEnabled(true);
textView.setMovementMethod(new ScrollingMovementMethod());
}
});
My XML File Code:
<?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"
tools:context=".Design.Panchgavya"
android:orientation="vertical">
<android.support.v7.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:background="#color/Green"
android:minHeight="?attr/actionBarSize"
android:theme="#style/ThemeOverlay.AppCompat.ActionBar">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Panchgavya"
android:textColor="#color/white"/>
</android.support.v7.widget.Toolbar>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="6"
android:orientation="vertical">
<TextView
android:layout_marginTop="10dp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:ellipsize="end"
android:text="#string/panchgavya"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="3"
android:orientation="vertical">
<TextView
android:id="#+id/panchgavya_tv_cow_dung"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Cow Dung"
android:textAppearance="#style/Base.TextAppearance.AppCompat.Medium"
android:textStyle="bold"
android:textColor="#color/black"/>
<TextView
android:id="#+id/panchgavya_tv_cow_urine"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:text="Cow Urine"
android:textStyle="bold"
android:textAppearance="#style/Base.TextAppearance.AppCompat.Medium"
android:textColor="#color/black"/>
<TextView
android:id="#+id/panchgavya_tv_cow_milk"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:text="Cow Milk"
android:textStyle="bold"
android:textAppearance="#style/Base.TextAppearance.AppCompat.Medium"
android:textColor="#color/black"/>
<TextView
android:id="#+id/panchgavya_tv_ghee"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:textAppearance="#style/Base.TextAppearance.AppCompat.Medium"
android:text="Ghee"
android:textStyle="bold"
android:textColor="#color/black"/>
<TextView
android:id="#+id/panchgavya_tv_dahi"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:textAppearance="#style/Base.TextAppearance.AppCompat.Medium"
android:text="Dahi"
android:textStyle="bold"
android:textColor="#color/black"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="vertical"
android:scrollbars="vertical"
android:scrollbarAlwaysDrawVerticalTrack="true">
<Button
android:id="#+id/panchgavya_btn_benefits"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Benefits"
android:background="#color/Green"
android:textColor="#color/white"
/>
</LinearLayout>
</LinearLayout>
You can create custom layout for your dialog and set any property easily.
For your requirement, Crate a layout for your required Dialog. Put android:scrollbars = "vertical" in your textView inside your layout. And textview.setMovementMethod(new ScrollingMovementMethod());.
You can set custom layout on your by following method.
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();
}
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<Textview
android:id="#+id/txtDescription"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="#dimen/margin_16dp"
android:paddingRight="#dimen/margin_16dp"
android:paddingBottom="#dimen/margin_16dp"
android:paddingTop="#dimen/margin_10dp"
android:text="#string/dummy_text_"
android:textSize="#dimen/font_14dp"
android:textColor="#color/colorPrimary"
android:layout_below="#+id/imgClose"/>
<ImageView
android:id="#+id/imgClose"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_close"
android:layout_alignParentRight="true"
android:layout_marginRight="#dimen/margin_10dp"
android:layout_marginTop="#dimen/margin_10dp" />
</RelativeLayout>
</ScrollView>
// in your java file put below code
private void showPopup() {
final AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
LayoutInflater inflater = this.getLayoutInflater();
final View dialogView = inflater.inflate(R.layout.dialog_terms_services,
null);
dialogBuilder.setView(dialogView);
TextView txtDescription = dialogView.findViewById(R.id.txtDescription);
ImageView imgClose = dialogView.findViewById(R.id.imgClose);
txtDescription.setText(message);
final AlertDialog b = dialogBuilder.create();
b.show();
imgClose.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
b.dismiss();
}
});
}
// after than call that showPopup() methos on button click.Hope it works for you
class extending DialogFragment can have UI elements as per your requirement.
public class OrderDetailFragment extends DialogFragment{
#Override
public void onStart() {
super.onStart();
Dialog d = getDialog();
if (d!=null){
int width = ViewGroup.LayoutParams.MATCH_PARENT;
int height = ViewGroup.LayoutParams.WRAP_CONTENT;
d.getWindow().setLayout(width, height);
}
}
public static OrderDetailFragment getInstance(GeneralListDataPojo dataList){
OrderDetailFragment orderDetailFragment=new OrderDetailFragment();
Bundle bundle = new Bundle();
bundle.putParcelable(DATA, dataList);
orderDetailFragment.setArguments(bundle);
return orderDetailFragment;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View root_view = inflater.inflate(R.layout.fragment_order_detail, container, false);
GeneralListDataPojo mDataList = getArguments().getParcelable(DATA);//My class which holds data implemented Parcelable
//population of data from mDataList
return root_view;
}
void closeDialog(){
this.dismiss();
}
}
fragment_order_detail is having my requirement specific element's (ScrollView/LinearLayouts/Buttons etc)
GeneralListDataPojo is the class which holds data implements Parcelable for transferring data between components
Now you can invoke this DialogFragment from your Fragment like this. (Im invoking from a Fragment. Change the FragmentManager retrieval accordingly if you are using from Activity)
OrderDetailFragment orderDetailFragment=OrderDetailFragment.getInstance((GeneralListDataPojo) responseObj);
FragmentManager fragmentManager=getFragmentManager();
orderDetailFragment.show(fragmentManager,"OrderDetailFragment");
Create The Custom Dialog layout Using Scroll View in your own custom Layout
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
final Dialog dialog= new Dialog(getApplicationContext());
dialog.setContentView(R.layout.activity_dialog);
Button click= dialog.findViewById(R.id.click);
click.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
}
});

How to change title color of dialog from white to any other color

In my code dialog2.setTitle("FeedBack"); is shown in a white color. How do I change title change color? Because my layout background is white so I can't see it. How do I change dialog title color?
public Dialog dialog2;
ImageView b = (ImageView) findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
dialog2 = new Dialog(context);
View vLoad = LayoutInflater.from(fifthscreen.this).inflate(R.layout.timer, null);
dialog2.setContentView(vLoad);
dialog2.setTitle("FeedBack");
dialog2.setCancelable(false);
dialog2.show();
}
});
}
////////////timer.xml///////////
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ffffff"
android:id="#+id/layouttimer">
<TextView
android:id="#+id/text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:textSize="15dp"
android:textColor="#000000" ></TextView>
<Button
android:id="#+id/FeedbackYummiSlice"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text=" YumiiSlice" >
</Button>
<Button
android:id="#+id/FeedbackThisdish"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text=" Feedback This dish " >
</Button>
<Button
android:id="#+id/nothanks"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text=" No,Thanks " >
</Button>
</LinearLayout>
The method setCustomTitle(View customTitleView) allows you to do that. You need to create a layout to set in this, and in that layout simply set to the TextView the style you want.
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View customTitleView = inflater.inflate(R.layout.custom_title_view, null);
AlertDialog.Builder builder = new AlertDialog.Builder(context)
.setCustomTitle(customTitleView);
Please refer This Answer for more information.

How to put close button at top corner in alert dialog box for android?

How to put close button at top corner in alert dialog box for android?
put close button at right side top corner in alert dialog.
i have used below code for scroll and dialog box
<ScrollView
android:id="#+id/ScrollView01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:scrollbars="vertical"
android:scrollbarAlwaysDrawVerticalTrack="true" >
<ImageVieandroid:id="#+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="200dp"
android:src="#drawable/ic_launcher" />
</RelativeLayout>
</ScrollView>
in java coding i have used below coding
i want to put a image to close the dialog box
please help
public void onClick(View v)
{
// TODO Auto-generated method stub
switch (v.getId())
{
case R.id.How_to_use:
final Dialog dialog = new Dialog(this);
dialog.setContentView(R.layout.description);
dialog.setTitle("How to use");
TextView text = (TextView) dialog.findViewById(R.id.description);
text.setText(R.string.Descr_How_to_use);
ImageView image = (ImageView) dialog.findViewById(R.id.image);
image.setImageResource(R.drawable.ic_launcher);
image.setOnClickListener(new OnClickListener()
{
// #Override
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
break;
default:
break;
}
I know, I am late to answer this 2 yrs old question, but this one is for those who don't know a correct approach yet....
Use a Custom Dialog as (everyone has suggested).
Use a RelativeLayout as the main layout of custom_dialog.xml as you will have to float that cancel button on top corner (right/left) of the main window.
custom_dialog.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="wrap_content">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="400dp"
android:layout_alignParentTop="true"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="15dp"
android:background="#F4F4F4">
<!--Main Body of your custom dialog-->
</RelativeLayout>
<LinearLayout
android:id="#+id/llTop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:orientation="vertical">
<ImageButton
android:id="#+id/btnCancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/btnBookK"
android:background="#null"
android:src="#drawable/ic_close" />
</LinearLayout>
</RelativeLayout>
In your custom dialog code use following line to make it transparent:
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
customDialog.xml
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<LinearLayout
android:id="#+id/llTop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:paddingBottom="10dp"
>
<Button
android:id="#+id/btnClose"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#000000"
android:text="Close"
/>
</LinearLayout>
<ImageView
android:src="#drawable/ic_launcher"
android:layout_width="130dp"
android:layout_height="130dp"
android:text="Close"
android:layout_centerInParent="true"
android:layout_below="#+id/llTop"
/>
</RelativeLayout>
I have created 1 method whenever you want to display dialog just call this method.
private Dialog dialog; // class variable
private void showDialog
{
dialog = new Dialog(Activity.this); // always give context of activity.
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.customDialog);
Button dialogButton = (Button) dialog.findViewById(R.id.btnClose);
dialogButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v)
{
dialog.dismiss();
}
});
dialog.show();
}
Yo have to create custom dialog
import android.app.Dialog;
import android.content.Context;
public class CustomDialog extends Dialog
{
public CustomDialog(Context context)
{
super(context);
setContentView(R.layout.dialog);
}
}
create dialog.xml file. design according to your requirment.

how to remove rectangle frame of the custom dialog

i custom a dialog :
public class CustomizeDialog extends Dialog implements OnClickListener {
Button close;
TextView tv;
public CustomizeDialog(Context context,String Stringcontent) {
super(context);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.custom_diolog_main);
tv=(TextView) findViewById(R.id.content);
tv.setText(Stringcontent);
close = (Button) findViewById(R.id.close);
close.setOnClickListener(this);
}
#Override
public void onClick(View v) {
if (v == close)
dismiss();
}
}
the xml is
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="100dip"
android:orientation="vertical"
android:background="#drawable/custom_diolog_bg"
android:layout_width="250dip">
<TextView android:layout_height="wrap_content"
android:textColor="#000"
android:textStyle="bold"
android:textSize="18sp"
android:id="#+id/content"
android:layout_marginLeft="15dip"
android:layout_marginTop="5dip"
android:layout_alignParentTop="true"
android:layout_width="250dip"
android:text=" Custom Dialog "/>
<Button android:layout_width="70dip"
android:layout_marginLeft="80dip"
android:background="#drawable/custom_dialog_button_bg"
android:layout_alignParentBottom="true"
android:layout_height="40dip" android:text="关闭"
android:id="#+id/close"></Button>
</RelativeLayout>
my dialog is vrry well,but custom_diolog_bg is a rounded rectangle image,and when i show my dialog ,it show a system Frame behide my custom,so i used this.getwindow.setBackgroundDrawable(null),then the system Frame seems have remove but only the Four Corners not remove,we also see dark Four Corners,because i used the rounded rectangle image.so my question how to remove all the Frame so that my dialog seem Very well
the pic is http://i.stack.imgur.com/EG7oz.jpg ,so you can see there is dark frame in the last,how to remove it? thank you
Solution that worked for me
<style name="DialogTheme" parent="#android:style/Theme.Dialog">
<item name="android:windowBackground">#android:color/transparent</item>
</style>
Dialog dialog = new Dialog(this, R.style.DialogTheme);
Use following lines before calling setContentView() :-
getWindow().requestFeature(Window.FEATURE_NO_TITLE);
getWindow().setBackgroundDrawable(
new ColorDrawable(android.graphics.Color.TRANSPARENT));
will work perfectly.
Dialog mydialog = new dialog (this,android.R.style.Theme_Translucent_NoTitleBar);
instead of calling
super(context);
call
super(context, android.R.style.Theme_Translucent_NoTitleBar_Fullscreen);
Update : Use this xml layout instead
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="100dip"
android:orientation="vertical"
android:background="#drawable/custom_diolog_bg"
android:layout_width="250dip">
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:orientation="vertical"
android:layout_centerInParent="true">
<TextView
android:layout_height="wrap_content"
android:textColor="#000"
android:textStyle="bold"
android:textSize="18sp"
android:id="#+id/content"
android:layout_marginLeft="15dip"
android:layout_marginTop="5dip"
android:layout_alignParentTop="true"
android:layout_width="250dip"
android:text=" Custom Dialog " />
<Button
android:layout_width="70dip"
android:layout_marginLeft="80dip"
android:background="#drawable/custom_dialog_button_bg"
android:layout_alignParentBottom="true"
android:layout_height="40dip"
android:text="关闭"
android:id="#+id/close"></Button>
</LinearLayout>
</RelativeLayout>
Try this it worked for me like a charm.
ContextThemeWrapper wrapper = new ContextThemeWrapper(this, android.R.style.Theme_Holo);
final LayoutInflater inflater = (LayoutInflater) wrapper.getSystemService(LAYOUT_INFLATER_SERVICE);
AlertDialog.Builder builder = new AlertDialog.Builder(wrapper);

Categories

Resources