Remove alert dialog border With Custom Theme - android

In my application i am using alert dialog with rounded rectangle theme.But it have alertdialog rectangle and my theme.My problem is how to replace alert dialog border like dialog.I want to show this set item with own theme only.
I want output this manner instead of the above theme:
Main Activity:
AlertDialog.Builder alertSeverity = new AlertDialog.Builder(
getActivity(), R.style.Theme_CustomDialog);
alertSeverity.setTitle("Severity Status");
CharSequence[] severityStatus = { "Low-Severity",
"Middle-Severity", "High-Severity" };
alertSeverity.setItems(severityStatus,
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
}
});
My Theme:
<style name="Theme.CustomDialog" parent="android:style/Theme.Dialog">
<item name="android:windowBackground">#drawable/shapedialogtheme</item>
<item name="android:windowFrame">#null</item>
</style>
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<solid android:color="#565656" />
<stroke
android:width="5dp"
android:color="#ffff8080" />
<corners android:radius="30dp" />
<padding
android:bottom="10dp"
android:left="10dp"
android:right="10dp"
android:top="10dp" />
<size
android:width="150dp"
android:height="150dp"/>
</shape>

Use Dialog instead of AlertDialog.
Create your custom layout which you want to show in dialog and setContent in dialog.
Apply this theme android.R.style.Theme_Translucent_NoTitleBar in dialog it will hide border.
Here is sample code.
Dialog dialog = new Dialog(activity.this, android.R.style.Theme_Translucent_NoTitleBar);
// your layout file
dialog.setContentView(R.layout.dialog);
// for hide title
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
//for set title
dialog.setTitle("Custom Dialog");
dialog.show();
Updated:
just tried this in AlertDialog.
AlertDialog.Builder alertSeverity = new AlertDialog.Builder(
getActivity(), android.R.style.Theme_Translucent_NoTitleBar);

try the next solution:
extend from dialog, and set the exact view to use by using setContentView .
the alertDialog is used for some functionalities. it's not that it can do anything you want.
maybe instead of extending you could take the dialog and then use the setContentView.

Use Dialog instead of AlertDialog
Dialog callAlert = new Dialog(LoginActivity.this,R.style.CutomDialog);
callAlert.setContentView(R.layout.call);
Style.xml
<style name="CutomDialog" parent="android:style/Theme.Dialog">
<item name="android:windowBackground">#android:color/transparent</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowAnimationStyle">#style/Animations.DialogAnimation</item>
</style>
call.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_margin="20dp"
android:background="#drawable/call_bg"></RelativeLayout>
call_bg.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<stroke android:width="3dp" android:color="#A20B3F" />
<corners android:bottomRightRadius="4dp" android:bottomLeftRadius="4dp"
android:topLeftRadius="4dp" android:topRightRadius="4dp"/>
Main thing is that you have to make layout backgrpund transparent otherwise you will not able to get output as you want.

You need to design a custom dialog for this purpose :
**dialog.xml**
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/txt_view_SaveAs"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/save_as"
android:layout_margin="10dp"
android:textSize="25dp" />
<EditText
android:id="#+id/edit_txt_SaveAs"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minWidth="300dp"
android:maxLines="1"
android:textSize="20dp"
android:maxLength="50"
android:layout_margin="10dp"
android:text="#string/save_as" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_margin="10dp"
android:weightSum="1.0" >
<Button
android:id="#+id/btn_SaveAs"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_weight="0.5"
android:minWidth="100dp"
android:textSize="20dp"
android:text="#string/save"
android:layout_margin="3dp" />
<Button
android:id="#+id/btn_Cancel"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_weight="0.5"
android:minWidth="100dp"
android:textSize="20dp"
android:text="#string/cancel"
android:layout_margin="3dp" />
</LinearLayout>
You can then make different class for the particular dialog like this:
public class SaveDialog extends Dialog implements android.view.View.OnClickListener {
private Context context;
private TextView txt_view_SaveAs;
private EditText edit_txt_SaveAs;
private Button btn_SaveAs;
private Button btn_Cancel;
public SaveDialog(Context context) {
super(context);
this.context = context;
// TODO Auto-generated constructor stub
}
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dialog);
setCancelable(true); // Setting the Dialog to be Cancellable on Back Key Press
txt_view_SaveAs = (TextView) findViewById(R.id.txt_view_SaveAs);
edit_txt_SaveAs = (EditText) findViewById(R.id.edit_txt_SaveAs);
btn_SaveAs = (Button) findViewById(R.id.btn_SaveAs);
btn_SaveAs.setOnClickListener(this);
btn_Cancel = (Button) findViewById(R.id.btn_Cancel);
btn_Cancel.setOnClickListener(this);
}
#Override
public void onClick(View v) {
// Write code for all the buttons on click methods
}
}
Then you can call the custom dialog in your main class by using the following code :
SaveDialog save_dialog = new SaveDialog(saving_activity);
save_dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
save_dialog.show();

You can not remove border from alert dialog.
use this
public class ActivityIndicator extends Dialog implements android.view.View.OnClickListener{
protected static final String TAG = InfoIndicator.class.getName();
ImageView close;
WebView info;
public ActivityIndicator (Context context,String information)
{
super(context, android.R.style.Theme_Translucent_NoTitleBar);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.info);
setCancelable(true);
}
}
and try this below function for show, hide and clear dialog
private static ActivityIndicator activityIndicator;
public static void hideActivityViewer() {
if (activityIndicator != null) {
activityIndicator.dismiss();
}
activityIndicator = null;
}
public static void showActivityViewer(Context context) {
if (activityIndicator == null)
{
activityIndicator = new ActivityIndicator(context);
}
activityIndicator.show();
}
public static void clearDialogs()
{
activityIndicator = null;
}

you can use popwindow for more style by yourself

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.

How to make an AppCompat Activity as a Dialog?

I need to use my AppCompat Activity as a Dialog.For this I tried so my solution that answered in StackOverflow. But nothing worked.Please answer me. I am getting activity as dialog. But it shows very narrow both in height & width.
I used the following Theme:
<style name="AppDialogTheme" parent="Theme.AppCompat.Light.Dialog">
<item name="android:windowFrame">#null</item>
<item name="android:windowIsFloating">true</item>
<item name="android:windowIsTranslucent">true</item>
<item name="windowNoTitle">true</item>
<item name="android:background">#android:color/transparent</item>
</style>
You can use DialogFragment and customize the layout accordingly.
public class CustomDialogFrag extends DialogFragment{
static FragmentManager fragmentManager;
public static CustomDialogFrag showDialog(FragmentManager fm){
CustomDialogFrag customDialogFrag=new CustomDialogFrag();
fragmentManager=fm;
return customDialogFrag;
}
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(getActivity());
View view = getActivity().getLayoutInflater().inflate(R.layout.dialogfrag_layout, null);
alertDialogBuilder.setView(view);
setupUI(view);
alertDialogBuilder.setTitle("Notification Message");
alertDialogBuilder.setIcon(R.drawable.notificationicon);
alertDialogBuilder.setPositiveButton("Close", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
return alertDialogBuilder.create();
}
void setupUI(View view){
TextView textViewOne=(TextView)view.findViewById(R.id.txtEventAlias);
TextView textViewTwo=(TextView)view.findViewById(R.id.txtTime);
TextView textViewThree=(TextView)view.findViewById(R.id.txtLogMessage);
textViewOne.setText("Text 1");
textViewTwo.setText("Text 2");
textViewThree.setText("Text 3");
}
}
And the dialogfrag_layout.xml will be
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="#dimen/margin_10"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/txtEventAlias"
android:text="Sample"
android:textColor="#android:color/darker_gray"
android:textSize="#dimen/textSizeMedium"
android:padding="#dimen/margin_10"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/txtTime"
android:text="Sample"
android:textColor="#android:color/darker_gray"
android:textSize="#dimen/textSizeMedium"
android:padding="#dimen/margin_10"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/txtLogMessage"
android:text="Sample"
android:textColor="#android:color/darker_gray"
android:textSize="#dimen/textSizeMedium"
android:padding="#dimen/margin_10"
/>
</LinearLayout>
For invoking this Dialog from a Fragment:
DialogFragment dialogFragment=CustomDialogFrag.showDialog(getFragmentManager());
dialogFragment.show(getActivity().getFragmentManager(), "tag");
In your activity's onCreate put the following lines:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_your);
// Make the window's width full sized
WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams();
Window window = getWindow();
layoutParams.copyFrom(window.getAttributes());
layoutParams.width = WindowManager.LayoutParams.MATCH_PARENT;
layoutParams.height = WindowManager.LayoutParams.MATCH_PARENT;
window.setAttributes(layoutParams);
}
Tested and works. You can set to both width and height to WRAP_CONTENT if needed.

Android Custom Alert Dialog Design

I´m trying to implement a basic custom Alert Dialog.
It should look like this
With the following XML Code:
<?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/tv_custom_dialog_event"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:background="#drawable/actionbar_background"
android:gravity="top"
android:orientation="horizontal" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:paddingLeft="100dp"
android:text="Delete Event ?"
android:textColor="#color/white"
android:textSize="20sp" />
<View
android:layout_width="fill_parent"
android:layout_height="1dp"
android:background="#DFDFDF" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<ImageButton
android:id="#+id/btn_custom_dialog_events_cancel"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:src="#drawable/ic_action_cancel" />
<ImageButton
android:id="#+id/btn_custom_dialog_events_true"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:src="#drawable/ic_action_accept" />
</LinearLayout>
</LinearLayout>
However when I run it on my emulator it looks like this:
Does anybody know why this happens and how I can fix that?
Assuming you have a class that extends Dialog you can do the following:
First define a style in styles.xml with something like this:
<style name="CustomDialogThemeTrasparent" parent="#android:style/Theme.Dialog">
<item name="android:layout_width">match_parent</item>
<item name="android:layout_height">match_parent</item>
<item name="android:windowBackground">#color/transparent</item>
<item name="android:windowIsFloating">true</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowContentOverlay">#null</item>
</style>
Then in the constructor of your custom dialog class you set this theme:
public class MyCustomDialog extends Dialog {
public MyCustomDialog(final Context context)
{
super(context, R.style.CustomDialogThemeTrasparent);
}
To set your custom layout as view you can make a function called setTheme() and then called in the show() of your dialog, even with parameters and a nice done layout you can make a more generic class that you can use to show your custom dialogs all over your app, something like this:
//Function to set the layout when the dialog is instantiated, here we
//set the layout and if you want you can set parameter to
//show/hide controls/views and can show different types of dialogs with the same class but with a unified style
private void setTheme()
{
LayoutInflater layoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mView = layoutInflater.inflate(R.layout.custom_dialog, null);
this.setContentView(mView);
}
then you can make function(s) to show your dialog(s), like:
public void showDeletionDialog(String pMessage)
{
this.setTheme();
this.show();
}
hope it help you
Try this approach. You can inflate your layout:
public AlertDialog displayLayoutDialog(int layout,final Context context, int theme){
AlertDialog.Builder builder = new AlertDialog.Builder(context, theme);
LayoutInflater inflater = LayoutInflater.from(context);
View view = inflater.inflate(layout, null);
builder.setView(view);
builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
AlertDialog dialog= builder.create();
dialog.show();
Button tb = dialog.getButton(DialogInterface.BUTTON_POSITIVE);
tb.setOnClickListener(new CustomListener(dialog, context));
return dialog;
}
And your Listener where all the validation and anything else that you need happens:
public class CustomListener implements View.OnClickListener {
private final Dialog dialog;
private Context context;
public CustomListener(Dialog dialog, Context context) {
this.dialog = dialog;
this.context = context;
}
#Override
public void onClick(View v) {
Toast.makeText(context, "Custom Layout", Toast.LENGTH_LONG).show();
}
}
Hope it helps!!!

Android Dialog - Rounded Corners and Transparency

I'm trying to make a custom android dialog with rounded corners. My current attempts have given me this result.
As you can see, the corners are rounded, but it leaves the white corner still intact.
Below is the xml that I put in the drawable folder to create the blue dialog with the red border with the rounded corners.
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape
android:shape="rectangle">
<solid android:color="#color/transparent_black" />
<corners android:radius="#dimen/border_radius"/>
</shape>
</item>
<item
android:left="#dimen/border_width"
android:right="#dimen/border_width"
android:top="#dimen/border_width"
android:bottom="#dimen/border_width" >
<shape android:shape="rectangle">
<solid android:color="#color/blue" />
<corners android:radius="#dimen/border_radius"/>
</shape>
</item>
</layer-list>
Below is the layout of the dialog.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
style="#style/fill"
android:orientation="vertical"
android:layout_margin="#dimen/spacing_normal"
android:padding="#dimen/spacing_normal"
android:background="#drawable/border_error_dialog" >
<RelativeLayout
style="#style/block"
android:layout_gravity="center" >
<ImageView
android:id="#+id/imageView1"
style="#style/wrap"
android:layout_alignParentLeft="true"
android:layout_centerHorizontal="true"
android:contentDescription="#string/content_description_filler"
android:src="#drawable/ic_launcher" />
<TextView
android:id="#+id/textView1"
style="#style/error_text"
android:layout_centerVertical="true"
android:layout_toRightOf="#+id/imageView1"
android:text="#string/error_login" />
</RelativeLayout>
<Button
android:id="#+id/button1"
style="#style/wrap"
android:layout_gravity="center"
android:text="Button" />
</LinearLayout>
And below is the Activity in which I create the dialog.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button b1 = (Button) findViewById(R.id.button1);
b1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(MainActivity.this);
View child = getLayoutInflater().inflate(R.layout.dialog_custom_tom, null);
alertDialogBuilder.setView(child);
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
}
});
}
The only solution I have found is here. Use Dialog instead of AlertDialog and set transparent background:
dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
Therefore you can't use the builder. But you can use new Dialog() also in onCreateDialog callback of DialogFragment if you follow to best guidelines.
This works also for Gingerbread.
Besides the layered drawable can be simplified to one shape with xml element <stroke> for the border.
I had similar issue when made dialog extending DialogFragment and to fix this used:
dialog.setStyle(DialogFragment.STYLE_NO_FRAME, 0);
Like this:
public class ConfirmBDialog extends DialogFragment {
public static ConfirmBDialog newInstance() {
ConfirmBDialog dialog = new ConfirmBDialog();
Bundle bundle = new Bundle();
dialog.setArguments(bundle);
return dialog;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// This removes black background below corners.
setStyle(DialogFragment.STYLE_NO_FRAME, 0);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.confirm_dialog, container, true);
getDialog().setCanceledOnTouchOutside(true);
return view;
}
Hope this helps.
Just try
myDialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
The below code solved the issue
MyDialog mydialog = new MyDialog(this, "for testing",
new myOnClickListener() {
#Override
public void onPositiveButtonClick() {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(),
"I am positive button in the dialog",
Toast.LENGTH_LONG).show();
}
#Override
public void onNegativeButtonClick() {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(),
"I am negative button in the dialog",
Toast.LENGTH_LONG).show();
}
});
// this will remove rectangle frame around the Dialog
mydialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
mydialog.show();
Thanks,
Nagendra
In you java file keep below code and change your layout name
View mView =LayoutInflater.from(mContext).inflate(R.layout.layout_pob,null);
alertDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
just try using this, this worked for me
dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
Use 9-patch PNG with transparency in those corners.
public void initDialog() {
exitDialog = new Dialog(this);
exitDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
View view = View.inflate(this, R.layout.dialoglayout, null);
exitDialog.setContentView(view);
AdSize adSize = new AdSize(300, 250);
dialogAdview = new AdView(this);
dialogAdview.setAdUnitId(getResources().getString(R.string.banner_id));
dialogAdview.setAdSize(adSize);
RelativeLayout adLayout = (RelativeLayout) view.findViewById(R.id.adLayout);
adLayout.addView(dialogAdview);
AdRequest adRequest = new AdRequest.Builder()
.build();
dialogAdview.loadAd(adRequest);
dialogAdview.setAdListener(new AdListener() {
#Override
public void onAdLoaded() {
Log.d("Tag", "adLoaded");
super.onAdLoaded();
}
});
view.findViewById(R.id.yes_btn).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
exit = true;
onBackPressed();
}
});
view.findViewById(R.id.no_btn).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
exit = false;
exitDialog.dismiss();
}
});
}
dialoglayout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/custom_dialog_round"
android:orientation="vertical">
<TextView
android:id="#+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="10dp"
android:text="Do you want to exit?"
android:textColor="#000"
android:textSize="18dp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/text"
android:orientation="horizontal">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:gravity="center"
android:orientation="horizontal">
<TextView
android:id="#+id/yes_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/background_draw"
android:padding="8dp"
android:text="Yes"
android:textAlignment="center"
android:textColor="#9fa8da"
android:textSize="20dp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:gravity="center"
android:orientation="horizontal">
<TextView
android:id="#+id/no_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:background="#drawable/background_draw"
android:padding="8dp"
android:text="No"
android:textAlignment="center"
android:textColor="#d50000"
android:textSize="20dp" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
`
custom_dialog_round.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid
android:color="#fff"/>
<corners
android:radius="10dp" />
<padding
android:left="10dp"
android:top="10dp"
android:right="10dp"
android:bottom="10dp" />
</shape>
reference http://techamongus.blogspot.com/2018/02/android-create-round-corner-dialog.html
UPDATE
I understood that activity's background makes sense. So use #robert's answer with these changes.
in DialogFragment layout set width and height or add minimum sizes:
<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="wrap_content" // Or match_parent, 300dp.
android:layout_height="wrap_content"
android:layout_marginLeft="50dp"
android:layout_marginRight="50dp"
android:background="#drawable/white_round_corner_background"
android:gravity="center"
android:minWidth="300dp"
android:minHeight="200dp"
android:orientation="vertical"
android:padding="15dp"
>
...
Remove <item name="android:background">#color/...</item> from styles of needed activities and set these backgrounds in activity's layouts.
In DialogFragment write:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// This removes black background below corners.
setStyle(DialogFragment.STYLE_NO_FRAME, 0);
}
Old variant
According to robert answer, you should apply setStyle(STYLE_NO_FRAME, 0), but there appear new problems. If you have a narrow DialogFragment like in Custom dialog too small, then you should follow this guide.
Add to styles.xml these 3 lines for dialog size:
<style name="ErrorDialogTheme" parent="#android:style/Theme.Dialog">
<item name="android:minWidth" type="dimen">300dp</item>
<!-- This option makes dialog fullscreen and adds black background, so I commented it -->
<!-- <item name="android:minHeight" type="dimen">200dp</item> -->
<!-- This option doesn't work, so I commented it -->
<!-- <item name="android:layout_width">match_parent</item> -->
</style>
In layout of your DialogFragment add style:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
...
android:minWidth="300dp" // Optional, remove this line.
android:minHeight="200dp" // Optional, remove this line.
style="#style/ErrorDialogTheme"
android:theme="#style/ErrorDialogTheme"
>
In code of your DialogFragment write:
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
// This removes black background. If not 0 as a parameter, black background will appear.
setStyle(STYLE_NO_FRAME, 0)
}
// If you want a fullscreen dialog, use this, but it doesn't remove a black background.
override fun onStart() {
super.onStart()
dialog.window?.setLayout(WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.MATCH_PARENT)
}
Look at AndroidManifest.xml and find all activities that can show these dialogs, check android:theme="..." themes and go to styles.xml. Now take a look at <item name="android:background">#color/...</item> items of these themes. There should be a transparent color or these items might not exist. If you have these background items, whole activities will have those backgrounds and dialogs too! So, if you have a camera activity with DialogFragment above it, you will see this.
Remove background items of needed styles. Also maybe background is set in code, check it.
In Dialog with transparent background in Android and many pages it is written to add one of these:
dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(0));
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
probably in onViewCreated() or onCreateDialog(), but it didn't help me, because the background of the Activity was set in styles.
Tested on Samsung Galaxy S4 running Android 5.0.1.
Use CardView and make
app:cardCornerRadius="dp"
According shape xml.
I will post my solution here because it may be helpful. The solution that worked for me was to set the drawable resource in the layout xml and also in the activity that starts the dialog, without switching from AlertDialog to Dialog.
This would mean that in the layout where we create our design for the dialog alert_dialog_design.xml we will have the property android:background filled with our own defined background alert_dialog_shape.xml:
android:background="#drawable/alert_dialog_shape"
But also inside the activity that starts the dialog:
alert.getWindow().setBackgroundDrawableResource(R.drawable.alert_dialog_shape);
This way the parent (the alert itself) of your custom layout will have the shape you desire. Using this method I achieved the following:
https://i.stack.imgur.com/drCw3.png

AlertDialog background color

I'm using an AlertDialog with custom layout. The color of TextView in the layout is black, so when opening the dialog on Android 4.0 with Holo.Light, the text is visible. However if you open the dialog in Android 2.2 the text is not visible because of the gray background. Is there a way to change the background color?
However if you open the dialog in Android 2.2 the text is not visible because of the gray background. Is there a way to change the background color?
Yes it is possible, I used it on my app using DialogBuilder.
Just put inverseBackgroundForced to true
builder.setInverseBackgroundForced(true);
AlertDialog dialog = builder.create();
dialog.show();
on your dialog builder. It will force the background to white color (instead of dark grey) on android version before Froyo.
Just define the background of the root view in the layout.xml file for your dialog to a color that you want.
Like this:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#color/dialog_background" >
...
Thank you very much to StinePike and Artjom B.
The idea of StinePike is very good.
I put a TextView in AlertDialog having a customized background.
I show how to use solid and gradient background to customize objects.
Please let me to present you the context in which I applied StinePike's Idea.
// location: MainActivity.java
AlertDialog mAlertDialog_With_Radio_Buttons;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ini();
}
public void onAlert_With_Radio_Buttons_Close_Click(View view) {
mAlertDialog_With_Radio_Buttons.dismiss();
} // onAlert_With_Radio_Buttons_Close_Click
public void alert_with_radio_buttons(){
AlertDialog.Builder
mAlertDialog_Builder = new AlertDialog.Builder(this);
mAlertDialog_Builder
.setView(getLayoutInflater()
.inflate(R.layout.alert_with_radio_buttons, null));
mAlertDialog_Builder
.setTitle("Select The Directory");
mAlertDialog_With_Radio_Buttons = mAlertDialog_Builder.create();
mAlertDialog_With_Radio_Buttons.show();
} // public void alert_with_radio_buttons(){
// location: alert_with_radio_buttons.xml in layout
<LinearLayout
android:id="#+id/alert_with_radio_buttons_tv_ll"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#color/turquoise1"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true">
<TextView
android:id="#+id/mAlert_With_Radio_Buttons_TextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textMultiLine"
android:background="#color/turquoise2"
android:textSize="#dimen/main_wiz_size"
android:text = "#string/alert_with_rb_tv_text" />
</LinearLayout>
// Location: colors in values
<color name="turquoise1">#FF00ABAB</color>
<color name="turquoise2">#FF00BCBC</color>
// Location: strings in values
<string name="alert_with_rb_tv_text">Directory Names</string>
// Close Definition
// location: alert_with_radio_buttons.xml in layout
<Button
android:id="#+id/alert_with_radio_buttons_close_btn"
android:text="#string/alert_with_radio_buttons_close"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/btn_decor"
android:onClick="onAlert_With_Radio_Buttons_Close_Click" />
// location: btn_decor.xml in drawable
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:dither="true">
<gradient
android:startColor="#700000ff"
android:endColor="#70009B80"
android:angle="-90"/>
</shape>
location: strings.xml in values
<string name="alert_with_radio_buttons_close">Close</string>
"Is there a way to change the background color?"
Yes there are several ways for different contexts.
Please let me to "provide details and share my research" to you.
My code shows how to get customized TextView Background for items of ListView incorporated in Alert Dialog.
Let's start with the model for the item of ListView
// location: customized_tv_for_list_view.xml from layout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/layer_border">
<TextView
android:id="#+id/text_view_for_lv"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity ="center"
android:padding ="5dip"
android:background="#color/turquoise2"
android:textSize="#dimen/lv_text_size"
android:textColor="#color/blue0"/>
</LinearLayout>
// location: main_activity.xml in layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/decor"
android:orientation="vertical" >
<LinearLayout
android:id="#+id/main_activity_files_btn_ll"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/layer_border" >
<Button
android:text="Files"
android:id="#+id/files_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/btn_decor"
android:onClick="onMainActivity_Files_Click" />
</LinearLayout>
</LinearLayout>
// location: colors.xml in values
<color name="blue0">#0000FF</color>
<color name="turquoise2">#FF00BCBC</color>
// location: dimens.xml in values
<dimen name="lv_text_size">24dp</dimen>
// location: layer_border.xml in drawable
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<stroke android:width="1dp" android:color="#9999FF" />
<solid android:color="#CCCCFF" />
<padding android:left ="4dp" android:top="4dp"
android:right="4dp" android:bottom="4dp" />
<corners android:radius="4dp" />
</shape>
// location: decor.xml in drawable
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:dither="true">
<gradient
android:startColor="#aa0000ff"
android:endColor="#aa009B80"
android:angle="-90"/>
</shape>
// location: MainActivity.java
ListView mListView;
AlertDialog mAlertDialog;
ArrayAdapter<String> mArrayAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mListView = new ListView(this);
ArrayList<String>
mArrayList_Days = new ArrayList<>();
for(int i = 0; i< 32; i++)
mArrayList_Days.add("Day " + String.valueOf(i));
mArrayAdapter = new ArrayAdapter<>(
this, R.layout.customized_tv_for_list_view,
R.id.text_view_for_lv, mArrayList_Days);
mListView.setAdapter(mArrayAdapter);
mListView
.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
String sel_item = (String) mListView
.getItemAtPosition(position);
Toast.makeText(MainActivity.this, sel_item, Toast.LENGTH_SHORT).show();
mAlertDialog.cancel();
} // onItemClick
}); // .setOnItemClickListener
build_files_alert_dialog();
}
public void build_files_alert_dialog() {
AlertDialog.Builder
mAlertBuilder = new AlertDialog.Builder(MainActivity.this);
mAlertBuilder.setTitle("Days");
mAlertBuilder.setView(mListView);
mAlertDialog = mAlertBuilder.create();
WindowManager.LayoutParams mLayoutParams = new WindowManager.LayoutParams();
mLayoutParams.copyFrom(mAlertDialog.getWindow().getAttributes());
}
public void onMainActivity_Files_Click(View view) {
mAlertDialog.show();
} // onMainActivity_Files_Click
AlertDialog.Builder.setView(getLayoutInflater().inflate(R.layout.your_layout, null));
by using this function you can inflate a layout to your dialogue. now do whatever you want in the layout xml. for example see the following code.
AlertDialog.Builder about = new AlertDialog.Builder(this);
about.setTitle(getString(name));
about.setIcon(R.drawable.icon);
about.setView(getLayoutInflater().inflate(R.layout.your_layout, null));

Categories

Resources