how to remove rectangle frame of the custom dialog - android

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);

Related

Android DialogFragment with custom layout remove white border [duplicate]

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);

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.

Custom theme of all dialogs in an app

Is there any similar theme generator as HoloColors or Action Bar Style Generator which would help us change theme of Dialogs and AlertDialogs in our apps?
My app uses Holo theme and I managed to change style of views such as EditTexts and Buttons but they remain unchanged when they appear in a Dialog. I would also like to change color of the blue line under a title.
I've found a few question and answers related to this topic but they practically say it's not even possible. I cannot believe it isn't.
At least for the AlertDialog it is possible. here i posted my solution of a function which is called at some point (for example button click) in your code and creates an alert dialog own layout over your screen.. the layout is a normal layout.xml which you can define the way you want. works perfect for me!
private void showAddUserGeneratedStationDialog() {
AlertDialog.Builder alertDialog = new AlertDialog.Builder(
AmplifyActivity.this);
LayoutInflater inflater = getLayoutInflater();
//inflate your layout.xml
alertDialog
.setView(inflater.inflate(R.layout.dialog_add_station, null));
//show dialog over activity screen
final AlertDialog ad = alertDialog.show();
//put actions to your ui-elements
Button cancelBtn = (Button) ad
.findViewById(R.id.list_user_generated_cancelBU);
cancelBtn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
ad.cancel();
}
});
Button doneBtn = (Button) ad
.findViewById(R.id.list_user_generated_doneBU);
doneBtn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//get sth from your layout f.e. some textInput
String stationUrl = ((TextView) ad
.findViewById(R.id.list_user_generated_stationURLInput))
.getText().toString();
// did some other things
ad.dismiss();
}
});
}
and my layout.xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/list_user_generated_addStationDialog"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#99000000"
android:clickable="true"
android:gravity="center"
android:orientation="vertical" >
<LinearLayout
android:layout_width="300dp"
android:layout_height="200dp"
android:padding="10dp"
android:background="#000"
android:orientation="vertical" >
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="#+id/list_user_generated_cancelBU"
style="#style/ButtonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center|right"
android:text="#string/cancel" />
<TextView
style="#style/AmplifyHeadlineElement"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="#string/list_userGenerated_addStation" />
<Button
android:id="#+id/list_user_generated_doneBU"
style="#style/ButtonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center|right"
android:text="#string/done" />
</LinearLayout>
<EditText
android:id="#+id/list_user_generated_stationURLInput"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"
android:layout_marginTop="10dp"
android:background="#FFF"
android:ems="10"
android:hint="#string/list_userGenerated_addUrlExample"
android:inputType="textNoSuggestions" >
</EditText>
<!-- MORE BUT I DELETED IT FOR BETTER OVERVIEW -->
</LinearLayout>
</LinearLayout>

Set AlertBox Title Bar Background Color

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.

Categories

Resources