This question already has answers here:
Dialog with transparent background in Android
(25 answers)
Closed 6 years ago.
I have a DialogFragment class with following code:
public class LeavingAppDialog extends DialogFragment {
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = getActivity().getLayoutInflater();
View v = inflater.inflate(R.layout.leaving_app_dialog, null);
builder.setView(v);
Typeface segoeuib = Typeface.createFromAsset(getActivity().getApplicationContext().getAssets(), "fonts/segoeuib.ttf");
TextView text = (TextView) v.findViewById(R.id.msg_dialog);
text.setTypeface(segoeuib);
Dialog dialog = builder.create();
return dialog;
}
}
This dialog uses a custom layout, here is its code:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#00000000">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true">
<ImageView
android:layout_width="300dp"
android:layout_height="300dp"
android:id="#+id/imageView"
android:src="#drawable/box_1_fit"
android:scaleType="centerInside"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:background="#00000000" />
<FrameLayout
android:layout_width="275dp"
android:layout_height="150dp"
android:id="#+id/frameLayout"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="LEAVING THIS APP?"
android:id="#+id/msg_dialog"
android:textSize="35dp"
android:layout_gravity="center_horizontal|top"
android:gravity="center_horizontal|top"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp" />
</FrameLayout>
<Button
android:layout_width="100dp"
android:layout_height="37dp"
android:id="#+id/btn_cancel"
android:background="#drawable/cancel_button_fit"
android:singleLine="false"
android:layout_alignRight="#+id/frameLayout"
android:layout_alignEnd="#+id/frameLayout"
android:layout_marginTop="75dp"
android:layout_below="#+id/frameLayout"
android:layout_marginRight="25dp"
android:onClick="ExitApp"/>
<Button
android:layout_width="100dp"
android:layout_height="37dp"
android:id="#+id/btn_accept"
android:background="#drawable/accept_button_fit"
android:layout_below="#+id/frameLayout"
android:layout_alignLeft="#+id/frameLayout"
android:layout_alignStart="#+id/frameLayout"
android:layout_marginTop="75dp"
android:layout_marginLeft="25dp"
android:onClick="ExitApp"/>
</RelativeLayout>
</RelativeLayout>
The problem is on the screenshot. I need to get rid of these white borders. I've tried to set background color to transparent through layout editor and through code. I've tried to set transparent backgound color to all the layouts and to the image itself, but it all had no effect. My source image is a png file and it really has transparent background, so the problem is not in the source image.
Set Background Transparent:
Like this: mDialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
Try this Custom Alert Dialog Class:
class CustomAlertDialog extends AlertDialog {
CustomAlertDialog(Context context) {
super(context);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
setContentView(R.layout.view_custom_alert_dialog);
RMTextView txtTitle = (RMTextView) findViewById(R.id.txt_alert_title);
txtTitle.setText("Title");
}
}
try this way.
mDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
// mDialog.getWindow().setBackgroundDrawableResource("#0069ad");
mDialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
Related
This question already has answers here:
Dialog with transparent background in Android
(25 answers)
Closed 6 years ago.
Need to make a layout like this.
Below is an alert box which has a transparent background containing spinner and a button below.
i ve tried this as far.
this is the layout file.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:background="#fff"
android:paddingBottom="50dp">
<Spinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/selectCity_xspn"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_marginEnd="6dp"
android:layout_marginTop="75dp" />
<Spinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/selectCity_xspn"
android:layout_alignParentStart="true"
android:layout_marginTop="70dp"
android:id="#+id/selectCtgry_xspn" />
<Button
android:text="Button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/button2"
android:layout_marginTop="300dp"
android:layout_alignParentEnd="true" />
This is the java class where i ve used the transparent option at many places but it doesnt work.
accessing the function at the bottom in the following button.
customdialog_btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
onCreateDialog2().show();
}
});
public Dialog onCreateDialog2() {
// Use the Builder class for convenient dialog construction
AlertDialog.Builder builder = new AlertDialog.Builder(Registeration.this);
View dialog_view = getLayoutInflater().inflate(R.layout.custom_dialog, null);
//dialog_view.setBackgroundResource(android.R.color.transparent);
builder.setView(dialog_view);
selectCity_spn=(Spinner)dialog_view.findViewById(R.id.selectCity_xspn);
selectCtgry_spn=(Spinner)dialog_view.findViewById(R.id.selectCtgry_xspn);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this.getBaseContext(), R.array.selectCity_spn, android.R.layout.simple_spinner_dropdown_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
assert selectCity_spn != null;
selectCity_spn.setAdapter(adapter);
ArrayAdapter<CharSequence> adapter2 = ArrayAdapter.createFromResource(this.getBaseContext(), R.array.selectCtgry_spn, android.R.layout.simple_spinner_dropdown_item);
adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
assert selectCtgry_spn != null;
selectCtgry_spn.setAdapter(adapter2);
View v = getWindow().getDecorView();
v.setBackgroundResource(android.R.color.transparent);
return builder.create();
}
This will do
Dialog dialog = builder.create();
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
return dialog;
Try this:-
Dialog dialog1 = new Dialog(Main3Activity.this);
dialog1.setCancelable(true);
dialog1.setContentView(view1);
dialog1.show();
you can set background transparent of any layout or any view or any component by adding this code in XML
android:background="#android:color/transparent"
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);
}
}
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.
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);
I'm using a custom layout for an alert dialog, in which I set a custom background image.
When displayed, it seems that the dialog box is higher than my image background, while I set the layout dimensions to the image dimensions.
How can I set the dialog box dimensions to the dimensions of my background image? How can I remove this white border?
Thanks
dialog layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/layout_root"
android:orientation="vertical"
android:background="#drawable/whip_sel_dialog_bg"
android:layout_width="279dp"
android:layout_height="130dp" >
<TextView android:id="#+id/dialog_title"
android:textColor="#FFF"
android:textSize="16sp"
android:textStyle="bold"
android:text="#string/dialog_title"
android:layout_width="fill_parent" android:gravity="center" android:layout_height="30dp" android:layout_marginTop="5dp"/>
<TextView android:id="#+id/dialog_text"
android:textColor="#FFF"
android:layout_height="35dp" android:layout_width="fill_parent" android:gravity="center"/>
<LinearLayout android:id="#+id/confirmation"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent" android:gravity="center">
<Button android:id="#+id/button_cancel"
android:layout_width="128dp"
android:layout_height="43dp"
android:background="#drawable/btn_ok"
android:textColor="#FFF"
android:text="#string/cancel"
android:layout_marginRight="10dp"/>
<Button android:id="#+id/button_ok"
android:layout_width="128dp"
android:layout_height="43dp"
android:background="#drawable/btn_ok"
android:textColor="#FFF"
android:text="#string/ok" />
</LinearLayout>
</LinearLayout>
dialog class
public class WSelDialog extends Dialog implements OnClickListener {
private Button okButton, cancelButton;
public WSelDialog() {
super(MainActivity.this);
setContentView(R.layout.whipem_dialog);
okButton = (Button) findViewById(R.id.button_ok);
okButton.setText(getString(R.string.whip_sel_ok));
okButton.setOnClickListener(this);
cancelButton = (Button) findViewById(R.id.button_cancel);
cancelButton.setText(getString(R.string.whip_sel_cancel));
cancelButton.setOnClickListener(this);
TextView text = (TextView) findViewById(R.id.dialog_text);
text.setText(getString(R.string.whip_sel));
text.setOnClickListener(this);
}
#Override
public void onClick(View v) {
((GlobalVars)getApplicationContext()).playSound(100);
if (v == cancelButton)
dismiss();
else {
Intent i = new Intent(MainActivity.this, WhipSelection.class);
startActivity(i);
}
}
};
It looks like the dialog reserves this space for its title. See Android: How to create a Dialog without a title.