This is my dialog,
public class TestDialog extends DialogFragment {
#Override
public View onCreateView(#NonNull LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.test_dialog, container, false);
}
}
When I am using RelativeLayout for my design like below,
<?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"
android:orientation="vertical">
<TextView
android:id="#+id/textTitle"
android:layout_width="match_parent"
android:layout_height="#dimen/dp48"
android:layout_alignParentTop="true"
android:fontFamily="#font/trebuchet"
android:gravity="center"
android:text="Test Dialog"
android:textColor="?attr/colorAccent"
android:textSize="18sp" />
<ImageView
android:id="#+id/icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textTitle"
android:src="#drawable/ic_baseline_public_24px"
android:tint="#color/black" />
<TextView
android:id="#+id/text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/textTitle"
android:layout_marginStart="5dp"
android:layout_toEndOf="#+id/icon"
android:fontFamily="#font/trebuchet"
android:text="This is very long text, This is very long text, This is very long text" />
<com.google.android.material.button.MaterialButton
android:id="#+id/imageView_close"
style="#style/Widget.MaterialComponents.Button.OutlinedButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/text"
android:layout_centerHorizontal="true"
android:contentDescription="#string/close"
android:src="#drawable/ic_baseline_close_24dx"
android:text="#string/cancel" />
</RelativeLayout>
I am getting this output.
But When I am using ConstraintLayout for my design like below,
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/textTitle"
android:layout_width="0dp"
android:layout_height="#dimen/dp48"
android:fontFamily="#font/trebuchet"
android:gravity="center"
android:text="Test Dialog"
android:textColor="?attr/colorAccent"
android:textSize="18sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="#+id/icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textTitle"
android:src="#drawable/ic_baseline_public_24px"
android:tint="#color/black"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textTitle" />
<TextView
android:id="#+id/text"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="5dp"
android:fontFamily="#font/trebuchet"
android:text="This is very long text, This is very long text, This is very long text"
app:layout_constraintStart_toEndOf="#+id/icon"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textTitle" />
<com.google.android.material.button.MaterialButton
android:id="#+id/imageView_close"
style="#style/Widget.MaterialComponents.Button.OutlinedButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="#string/close"
android:src="#drawable/ic_baseline_close_24dx"
android:text="#string/cancel"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/text" />
</androidx.constraintlayout.widget.ConstraintLayout>
I am getting following output.
I don't know why this strange behavior. Only in dialog I am getting this behavior.
If anything I'm missing in constraint layout, please let me know friends, I'll correct myself.
all you have to do is set Theme to your dialog.
#Override
public int getTheme() {
return R.style.dialog;
}
style
<style name="dialog" parent="android:Theme.Dialog">
<item name="android:windowBackground">#drawable/radius</item>
<item name="android:windowMinWidthMajor">80%</item>
<item name="android:windowMinWidthMinor">85%</item>
<item name="android:windowNoTitle">true</item>
</style>
radius.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#color/white" />
<corners android:radius="10dp" />
<padding
android:bottom="8dp"
android:left="8dp"
android:right="8dp"
android:top="8dp" />
</shape>
I think the problem is android:layout_width="match_parent" in your ConstraintLayout tag. Since a Dialog has no fixed width, using match_parent won't work. Maybe you could set a fixed width, or a minimum width.
First, it's not the ConstraintLayout but how you are implementing the Dialog itself. It's a suggestion and according to Dialog Documentation:
The Dialog class is the base class for dialogs, but you should avoid
instantiating Dialog directly. Instead, use one of the following
subclasses:
AlertDialog
A dialog that can show a title, up to three buttons, a
list of selectable items, or a custom layout.
DatePickerDialog or TimePickerDialog
A dialog with a pre-defined UI that allows the user
to select a date or time.
So basically you'll be using setView api:
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(requireActivity());
// Setting my layout here
builder.setView(R.layout.my_layout);
builder.setPositiveButton(/* My Methods */)
.setNegativeButton(/* My Methods */);
return builder.create();
}
Read about it in Creating a Custom Layout documentation. If you want more customization then create a custom theme within values > styles.xml which can be used throughout the app or just by setting it along with the builder, like:
new AlertDialog.Builder(requireActivity(), R.style.My_Alert_Dialog_Theme);
Related
My motive is to have a base-card with an overlapping cardview icon view. I tried to achieve the same using Constraint Layout as a root and creating a DialogFragment out of it. However, the preview on IDE vs Phone is quite different.
I want to achieve:
DialogFragment with overlapping Icon
And The Background of ConstraintLayout should be transparent.
Here is comparison
user_profile_dialog.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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">
<LinearLayout
android:id="#+id/coins_count"
android:layout_marginTop="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
app:layout_constraintLeft_toLeftOf="#+id/cardView"
app:layout_constraintStart_toStartOf="#id/textView2"
app:layout_constraintEnd_toEndOf="#id/textView2"
app:layout_constraintTop_toBottomOf="#id/textView2">
<TextView
style="#style/cardBodyText"
android:id="#+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="200" />
<TextView
style="#style/cardBodyText"
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="300"/>
</LinearLayout>
<TextView
style="#style/viewParent.headerText"
android:id="#+id/textView2"
android:layout_marginTop="16dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Aman Sharma"
app:layout_constraintRight_toRightOf="#id/cardView"
app:layout_constraintLeft_toLeftOf="#id/cardView"
app:layout_constraintTop_toBottomOf="#+id/materialCardView2" />
<androidx.cardview.widget.CardView
android:translationY="-50dp"
android:translationZ="-3dp"
android:id="#+id/cardView"
android:layout_width="match_parent"
android:layout_height="400dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
<com.google.android.material.card.MaterialCardView
android:id="#+id/materialCardView2"
android:layout_width="100dp"
android:layout_height="100dp"
android:clipChildren="true"
app:cardCornerRadius="50dp"
app:layout_constraintBottom_toTopOf="#id/cardView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent">
<ImageView
android:id="#+id/imageView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="fitCenter"
android:src="#drawable/ic_user" />
</com.google.android.material.card.MaterialCardView>
<com.google.android.material.button.MaterialButton
style="#style/myMaterialButton"
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add as Friend"
app:layout_constraintStart_toStartOf="#id/flexbox"
app:layout_constraintEnd_toEndOf="#id/flexbox"
app:layout_constraintTop_toBottomOf="#id/flexbox"
app:layout_constraintBottom_toBottomOf="#id/cardView"/>
<com.google.android.flexbox.FlexboxLayout
android:id="#+id/flexbox"
android:layout_marginTop="20dp"
android:minHeight="60dp"
app:alignItems="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintStart_toStartOf="#id/coins_count"
app:layout_constraintEnd_toEndOf="#id/coins_count"
app:layout_constraintTop_toBottomOf="#id/coins_count">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_user"/>
</com.google.android.flexbox.FlexboxLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
Here is my DialogFragmentClass
UserProfileDialogjava
public class UserProfileDialog extends DialogFragment {
public UserProfileDialog() {
}
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
DialogUserProfileBinding binding=DialogUserProfileBinding.inflate(inflater,container,false);
return binding.getRoot();
}
#Override
public void onResume() {
super.onResume();
ViewGroup.LayoutParams params=getDialog().getWindow().getAttributes();
params.width= ViewGroup.LayoutParams.MATCH_PARENT;
params.height=ViewGroup.LayoutParams.WRAP_CONTENT;
getDialog().getWindow().setAttributes((WindowManager.LayoutParams) params);
}
}
Okay, here is what I did. First of all, I deleted your lines from onResume inside DialogFragment class. Then, inside your XML I added my picture for the user.
Inside the activity where you call for UserProfileDialog I used this:
FragmentManager fm = getSupportFragmentManager();
UserProfileDialog upd = new UserProfileDialog();
upd.setStyle(DialogFragment.STYLE_NO_TITLE, R.style.NoActionBarDialog);
upd.show(fm, "upd");
As you can see, I used setStyle() to set a custom style for your Dialog which is defined in themes.xml like this:
<!-- Dialog Theme -->
<style name="NoActionBarDialog" parent="Theme.MaterialComponents.Light.Dialog.Bridge">
<item name="windowActionBar">false</item>
<item name="android:windowMinWidthMajor">97%</item>
<item name="android:windowMinWidthMinor">97%</item>
<item name="windowNoTitle">true</item>
<item name="android:colorBackground">#android:color/transparent</item>
<item name="android:windowBackground">#android:color/transparent</item>
<item name="android:windowActionBarOverlay">false</item>
<item name="android:windowCloseOnTouchOutside">false</item>
</style>
And then, you get this:
You can now play more with some other attributes to make it as you want it to be. Sorry for spacing and colors inside the view but this is my temp_project just for things like this and themes are chaos. So, it should be working fine when you implement this in your code.
I've been trying to make a DialogFragment have a close button in the top left, as seen in the photo. What the final result should look like (Sorry about the text, it's in romanian) Could anyone tell me how I could go about doing that?
Here's the layout for the fragment:
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="5dp"
android:layout_marginTop="10dp"
android:background="#FFFFFF"
android:gravity="center"
android:orientation="vertical"
android:paddingBottom="-50dp" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Sample Text"/>
<!---add your views here-->
</LinearLayout>
<ImageView
android:id="#+id/imageView_close"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
android:clickable="true"
android:src="#drawable/ic_close_dialog" />
The way the fragment looks now: A small box where the image and text are overlayed
You can create a custom layout and use
dialog.setContentView(R.layout.custom_view);
Refer custom dialog with close button
If it still comes like it does in the picture, a possibility is that the container view for this constraint layout has wrap_content as its width and height.
Create a custom dialog class that extends dialog and then use an xml file to define your layout like you would for an activity.
public class CustomDialogClass extends Dialog {
public CustomDialogClass(Activity a) {
super(a);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.custom_dialog);
}
}
And have your custom_dialog.xml layout file define the layout
on your button click use :
if (mProgressDialog != null) {
if (mProgressDialog.isShowing()) {
mProgressDialog.dismiss();
mProgressDialog = null;
}
}
ProgressDialog mProgressDialog;
dialog_xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:background="#color/transparent">
<FrameLayout
android:id="#+id/contentView"
android:layout_width="300dp"
android:layout_height="300dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<!-- you will add content here -->
</FrameLayout>
<ImageView
android:id="#+id/ivCloseButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:contentDescription="#string/content_description"
app:layout_constraintBottom_toTopOf="#+id/contentView"
app:layout_constraintEnd_toEndOf="#+id/contentView"
app:layout_constraintStart_toEndOf="#+id/contentView"
app:layout_constraintTop_toTopOf="#+id/contentView"
app:srcCompat="#android:drawable/ic_menu_close_clear_cancel" />
</android.support.constraint.ConstraintLayout>
style:
<style name="TransparentDialog" parent="#style/Base.Theme.AppCompat.Light.Dialog">
<item name="colorPrimaryDark">#android:color/black</item> <!-- you can change here -->
<item name="android:windowBackground">#android:color/transparent</item> <!-- this is important -->
<item name="android:windowIsFloating">false</item> <!-- this is important -->
<item name="android:windowNoTitle">true</item> <!-- this is important -->
</style>
I create my Dialog like this:
The layout dialog_app_rating:
<?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="wrap_content"
android:focusable="true"
android:focusableInTouchMode="true"
android:orientation="vertical"
android:paddingEnd="#dimen/spacing_large"
android:paddingStart="#dimen/spacing_large"
tools:ignore="MissingPrefix">
<TextView
fontPath="#string/font_roboto_bold"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/spacing_normal"
android:text="#string/rating_remind_title"
android:textColor="#color/text_blue"
android:textSize="#dimen/font_large"
tools:text="Rate my app" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/spacing_small"
android:text="#string/rating_remind_msg"
android:textSize="#dimen/font_normal"
tools:text="Rate if it is useful" />
<com.iarcuschin.simpleratingbar.SimpleRatingBar
android:id="#+id/app_rating_rb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="#dimen/spacing_normal"
app:srb_borderColor="#color/grey"
app:srb_fillColor="#color/pink"
app:srb_numberOfStars="5"
app:srb_rating="5"
app:srb_starBorderWidth="1"
app:srb_starSize="#dimen/spacing_larger"
app:srb_starsSeparation="#dimen/spacing_small"
app:srb_stepSize="1" />
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/spacing_normal">
<android.support.v7.widget.AppCompatButton
android:id="#+id/not_show_again_btn"
style="?attr/buttonBarNeutralButtonStyle"
android:layout_width="wrap_content"
android:layout_height="#dimen/text_button_height"
android:text="#string/rating_remind_dont_show_again"
android:textColor="#color/text_blue" />
<android.support.v7.widget.AppCompatButton
android:id="#+id/send_btn"
style="?attr/buttonBarNeutralButtonStyle"
android:layout_width="wrap_content"
android:layout_height="#dimen/text_button_height"
android:layout_gravity="end"
android:text="#string/send"
android:textColor="#color/text_blue" />
</FrameLayout>
</LinearLayout>
The code for RatingDialog:
class RatingDialog extends Dialog {
#BindView(R.id.app_rating_rb) SimpleRatingBar mAppRatingBar;
#BindView(R.id.not_show_again_btn) View mNotAgainBtn;
#BindView(R.id.send_btn) View mSendBtn;
RatingDialog(#NonNull Context context) {
super(context, R.style.DialogTheme);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.dialog_app_rating);
ButterKnife.bind(this, this);
mNotAgainBtn.setOnClickListener(this);
mSendBtn.setOnClickListener(this);
}
#Override
protected void onStart() {
super.onStart();
getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
}
// Other implementation...
}
My DialogTheme in styles.xml:
<style name="DialogTheme" parent="Theme.AppCompat.Light.Dialog">
<item name="android:windowNoTitle">true</item>
<item name="android:windowSoftInputMode">stateUnchanged|adjustPan</item>
<item name="colorAccent">#color/pink</item>
</style>
It simply has some text, a rating bar, and 2 buttons. The buttons have Ripple effect on touch by default.
The problem is the Ripple effect not smooth when using in Dialog. It dims the background, shows a ripple and delays a bit then finish like the frame is being skipped. I've tried the effect on other dialogs as well but the problem is the same. Is that Android set low-priority for animation for Dialog only or something wrong with my config, can I fix it?
Thanks in advance
I have an arraylist named incorrectAnswers, I want to change the text color of those strings inside that arraylist.
Here's a snippet of my code:
ArrayList<String> incorrectAnswers = new ArrayList<>();
if(mItemArray.get(0).second.startsWith("In")){
numberOfCorrect++;
numberOfTries++;
} else{
numberOfErrors++;
incorrectAnswers.add(mItemArray.get(0).second);
}
if(mItemArray.get(1).second.startsWith("If")){
numberOfCorrect++;
numberOfTries++;
}
else{
numberOfErrors++;
incorrectAnswers.add(mItemArray.get(1).second);
}
This is the layout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/item_layout"
android:layout_width="match_parent"
android:layout_height="70dp"
android:orientation="horizontal"
android:gravity="center_vertical"
android:padding="10dp"
android:backgroundTint="#android:color/holo_green_light"
android:layout_marginRight="5dp"
android:layout_marginEnd="5dp"
android:layout_marginBottom="5dp"
android:background="#drawable/border_spinner">
<ImageView
android:id="#+id/image"
android:layout_width="1dp"
android:layout_height="1dp"
android:src="#drawable/ic_abrasion"
android:visibility="invisible"/>
<TextView
android:id="#+id/list_text"
android:layout_width="wrap_content"
android:textSize="16sp"
android:textColor="#color/textColor"
android:layout_height="wrap_content"/>
I want to change programmatically the text color of those items I've added inside incorrectAnswers. How can I achieve that?
First of all create a xml in drawables folder like this to setup the color states:
Lets call it text_color.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="#android:color/holo_red_dark" android:state_selected="true" />
<item android:color="#android:color/holo_green_light" />
</selector>
Then, use this xml as your text color in your layout:
<TextView
android:id="#+id/list_text"
android:layout_width="wrap_content"
android:textSize="16sp"
android:textColor="#drawable/text_color"
android:layout_height="wrap_content"/>
Finally, you have to set only the textviews that will have the incorrect answers as checked like this:
TextView textList = (TextView) findViewById(R.id.list_text);
textList.setSelected(true);
I have implemented the following Dialog in Android but have certain problems that I cant figure out why it is happening.
I cant figure out why there is a white space at the top and the bottom corners. And how to remove it!
As i have used Dialog and not AlertDialog, this message disappears when I touch elsewhere on the screen. I want to prevent this from happening and want the message box to be closed only when the user selects any one of the two options.
How much ever I try, I am not able to get the Cancel and Erase button of the same width.
Here are the XMLs
custom_alert.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:background="#drawable/custom_alert_layout"
android:id="#+id/alert_layout"
android:paddingBottom="15dp"
android:paddingTop="10dp"
android:layout_height="wrap_content">
<ImageView
android:layout_width="40dp"
android:layout_height="40dp"
android:id="#+id/alert_icon_imageview"
android:src="#drawable/alerticon"
android:layout_alignParentTop="true"
android:layout_alignLeft="#+id/alert_msg"
android:layout_alignStart="#+id/alert_msg" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/alert_title"
android:text="TITLE"
android:textSize="18sp"
android:textStyle="bold"
android:textColor="#0a4d4a"
android:layout_alignParentTop="true"
android:layout_toRightOf="#+id/alert_icon_imageview"
android:layout_toEndOf="#+id/alert_icon_imageview"
android:layout_marginLeft="20dp" />
<ImageView
android:layout_width="match_parent"
android:layout_height="2dp"
android:layout_marginTop="10dp"
android:layout_marginRight="4dp"
android:layout_marginLeft="2dp"
android:id="#+id/alert_divider_imageview"
android:layout_below="#+id/alert_title"
android:src="#drawable/alertdivider"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/alert_msg"
android:text="MESSAGE"
android:layout_marginTop="10dp"
android:layout_marginLeft="10dp"
android:textSize="16sp"
android:layout_below="#+id/alert_divider_imageview"
android:textColor="#ff373334"/>
<ImageView
android:layout_width="match_parent"
android:layout_height="2dp"
android:layout_marginRight="4dp"
android:layout_marginLeft="2dp"
android:layout_marginTop="10dp"
android:id="#+id/alert_divider_imageview2"
android:layout_below="#+id/alert_msg"
android:src="#drawable/alertdivider"/>
<Button
android:layout_width="150dp"
android:id="#+id/alert_cancel"
android:layout_marginLeft="10dp"
android:layout_marginRight="5dp"
android:layout_marginTop="15dp"
android:textColor="#ffffff"
android:textStyle="bold"
android:text="CANCEL"
android:background="#drawable/custom_alert_cancel"
android:layout_height="wrap_content"
android:layout_below="#+id/alert_divider_imageview2"
/>
<Button
android:layout_width="150dp"
android:id="#+id/alert_ok"
android:text="ERASE"
android:textStyle="bold"
android:layout_marginRight="10dp"
android:layout_marginLeft="5dp"
android:layout_marginTop="15dp"
android:textColor="#ffffff"
android:background="#drawable/custom_alert_ok"
android:layout_toRightOf="#+id/alert_cancel"
android:layout_height="wrap_content"
android:layout_below="#+id/alert_divider_imageview2" />
</RelativeLayout>
custom_alert_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="false" >
<shape android:shape="rectangle">
<corners android:radius="10dp"/>
<solid android:color="#139977" />
<stroke android:width="2dp" android:color="#0a4d4a" />
</shape>
</item>
</selector>
Can you post your Activity's code ?
Try this in your Activity :
final Dialog dialog = new Dialog(getActivity());
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.alert_dialog);
Button okBtn = (Button) dialog.findViewById(R.id.ok_btn);
Button cancelBtn = (Button) dialog.findViewById(R.id.cancel_btn);
okBtn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
doErase();
}
});
cancelBtn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
dialog.setCancelable(false);
dialog.show();
to solve 3rd problem, remove buttons from xml and use inbuild button of dialog. methods like setPositiveButton() and setNegativeButton() of dialog class.
it will give you same size button.
to solve the 1st problem you can use in xml like
<item name="android:background">#android:color/transparent</item>
or in java
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
Just add a line to your dialog box which remove your actionbar and title bar of the dialog:
Dialog dialog = new Dialog(MainActivity.this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); //add this line
dialog.setContentView(R.layout.activity_main);
for solve your first problem add this code before setContentView
Window window = actDialog.getWindow();
WindowManager.LayoutParams wlp = window.getAttributes();
window.setAttributes(wlp);
window.setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
actDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
your second problem solution
actDialog.setCanceledOnTouchOutside(false);
your third problem solution here
place your two Button in LinearLayout and give layout_weight same
1.To remove white space at top:
add these two lines to your dialog ,
dialog.getWindow();
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.alert_dialog);
remove white space at bottom,in your code just remove below line
<corners android:radius="10dp"/>
from your custom_alert_layout.xml file.
2.To solve second problem:add below two lines to your code,
dialog.setCancelable(false);
dialog.setCanceledOnTouchOutside(false);
3.To get same width for buttons use LinearLayout as parent for both the buttons,and give equal weights for buttons.
For your 3rd problem
here is code
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<Button
android:layout_width="match_parent"
android:id="#+id/alert_cancel"
android:layout_marginLeft="10dp"
android:layout_weight="1"
android:layout_marginRight="5dp"
android:layout_marginTop="15dp"
android:textColor="#ffffff"
android:textStyle="bold"
android:text="CANCEL"
android:background="#drawable/custom_alert_cancel"
android:layout_height="wrap_content"
/>
<Button
android:layout_width="match_parent"
android:id="#+id/alert_ok"
android:text="ERASE"
android:textStyle="bold"
android:layout_weight="1"
android:layout_marginRight="10dp"
android:layout_marginLeft="5dp"
android:layout_marginTop="15dp"
android:textColor="#ffffff"
android:background="#drawable/custom_alert_ok"
android:layout_height="wrap_content"
/>
</LinearLayout>