To use Roboto font, download it from here ==> http://material-design.storage.googleapis.com/downloads/RobotoTTF.zip
Last active
July 31, 2020 01:58
-
-
Save cesco89/65dd590c3203b2d9abfb to your computer and use it in GitHub Desktop.
A custom AlertDialog that follows Material Design guidelines
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<resources> | |
<color name="text_title">#CC000000</color> | |
<color name="text_secondary">#8C000000</color> | |
<color name="light_blue">#03a9f4</color> | |
</resources> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<inset xmlns:android="http://schemas.android.com/apk/res/android" | |
android:drawable="@android:color/white" | |
android:insetBottom="32dp" | |
android:insetLeft="32dp" | |
android:insetRight="32dp" | |
android:insetTop="32dp" > | |
</inset> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:background="@drawable/dialog_bg" | |
android:orientation="vertical" | |
android:windowMinWidthMajor="@android:dimen/dialog_min_width_major" > | |
<TextView | |
android:id="@android:id/text1" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:paddingBottom="16dp" | |
android:paddingLeft="24dp" | |
android:paddingRight="24dp" | |
android:paddingTop="24dp" | |
android:textColor="@color/text_title" | |
android:textSize="20sp" | |
android:textStyle="bold" /> | |
<ScrollView | |
android:id="@+id/scrolltext" | |
android:layout_width="match_parent" | |
android:layout_height="0dp" | |
android:layout_marginBottom="24dp" | |
android:layout_weight="1" | |
android:paddingLeft="24dp" | |
android:paddingRight="24dp" | |
android:scrollbarStyle="outsideOverlay" > | |
<LinearLayout | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:orientation="vertical" > | |
<TextView | |
android:id="@android:id/text2" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:textColor="@color/text_secondary" | |
android:textSize="14sp" /> | |
</LinearLayout> | |
</ScrollView> | |
<FrameLayout | |
android:id="@+id/content" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:layout_marginBottom="24dp" | |
android:paddingLeft="24dp" | |
android:paddingRight="24dp" > | |
</FrameLayout> | |
<LinearLayout | |
android:layout_width="match_parent" | |
android:layout_height="36dp" | |
android:gravity="center|right" | |
android:orientation="horizontal" | |
android:paddingBottom="16dp" | |
android:paddingLeft="16dp" | |
android:paddingRight="16dp" > | |
<Button | |
android:id="@android:id/button1" | |
style="?android:attr/borderlessButtonStyle" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:background="@android:color/transparent" | |
android:gravity="center" | |
android:minHeight="36dp" | |
android:minWidth="88dp" | |
android:text="decline" | |
android:textAllCaps="true" | |
android:textColor="@color/text_title" | |
android:textSize="14sp" | |
android:textStyle="bold" /> | |
<Button | |
android:id="@android:id/button2" | |
style="?android:attr/borderlessButtonStyle" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:background="@android:color/transparent" | |
android:gravity="center" | |
android:minHeight="36dp" | |
android:minWidth="88dp" | |
android:text="accept" | |
android:textAllCaps="true" | |
android:textColor="@color/light_blue" | |
android:textSize="14sp" | |
android:textStyle="bold" /> | |
</LinearLayout> | |
</LinearLayout> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.app.AlertDialog; | |
import android.content.Context; | |
import android.graphics.Typeface; | |
import android.os.Bundle; | |
import android.view.LayoutInflater; | |
import android.view.View; | |
import android.view.WindowManager; | |
import android.widget.Button; | |
import android.widget.FrameLayout; | |
import android.widget.ScrollView; | |
import android.widget.TextView; | |
public class MaterialDialog extends AlertDialog { | |
private Context mContext; | |
private TextView mTitle; | |
private TextView mContent; | |
private Button mPositive; | |
private Button mNegative; | |
private FrameLayout mCustomContainer; | |
private ScrollView mScrollText; | |
private String title; | |
private String contentText; | |
private View customView; | |
private Integer customResId; | |
private Button.OnClickListener mPositiveClickListener; | |
private Button.OnClickListener mNegativeClickListener; | |
private String positiveText; | |
private String negativeText; | |
private boolean canDismiss = true; | |
public MaterialDialog(Context context) { | |
super(context); | |
this.mContext = context; | |
} | |
@Override | |
public void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
this.setContentView(R.layout.dialog_layout_base); | |
mTitle = (TextView) findViewById(android.R.id.text1); | |
mContent = (TextView) findViewById(android.R.id.text2); | |
mCustomContainer = (FrameLayout) findViewById(R.id.content); | |
mPositive = (Button) findViewById(android.R.id.button2); | |
mNegative = (Button) findViewById(android.R.id.button1); | |
mScrollText = (ScrollView) findViewById(R.id.scrolltext); | |
mTitle.setTypeface(Typeface.createFromAsset(mContext.getAssets(), "fonts/Roboto-Medium.ttf")); | |
mContent.setTypeface(Typeface.createFromAsset(mContext.getAssets(), "fonts/Roboto-Regular.ttf")); | |
} | |
@Override | |
public void onStart() { | |
super.onStart(); | |
if(title != null) { | |
mTitle.setText(title); | |
}else{ | |
mTitle.setVisibility(View.GONE); | |
} | |
if(contentText != null) { | |
mContent.setText(contentText); | |
}else { | |
mScrollText.setVisibility(View.GONE); | |
} | |
if(customView != null && customResId == null) { | |
mCustomContainer.addView(customView); | |
}else if(customView == null && customResId != null) { | |
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); | |
customView = inflater.inflate(customResId, null, false); | |
mCustomContainer.addView(customView); | |
}else if(customView == null && customResId == null){ | |
mContent.setVisibility(View.GONE); | |
} | |
if(positiveText != null && mPositiveClickListener != null) { | |
mPositive.setText(positiveText); | |
mPositive.setOnClickListener(mPositiveClickListener); | |
}else{ | |
mPositive.setVisibility(View.GONE); | |
} | |
if(negativeText != null && mNegativeClickListener != null) { | |
mNegative.setText(negativeText); | |
mNegative.setOnClickListener(mNegativeClickListener); | |
}else { | |
mNegative.setVisibility(View.GONE); | |
} | |
this.setCanceledOnTouchOutside(canDismiss); | |
this.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE|WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM); | |
} | |
public MaterialDialog setTitle(String t) { | |
this.title = t; | |
return this; | |
} | |
public MaterialDialog setMessage(String m) { | |
this.contentText = m; | |
return this; | |
} | |
public MaterialDialog setupPositiveButton(String text, Button.OnClickListener listener) { | |
this.positiveText = text; | |
this.mPositiveClickListener = listener; | |
return this; | |
} | |
public MaterialDialog setupNegativeButton(String text, Button.OnClickListener listener) { | |
this.negativeText = text; | |
this.mNegativeClickListener = listener; | |
return this; | |
} | |
public MaterialDialog setCustomView(View v) { | |
this.customView = v; | |
return this; | |
} | |
public MaterialDialog setCustomViewResource(int ResId) { | |
this.customResId = ResId; | |
return this; | |
} | |
public MaterialDialog dismissOnTouchOutside(boolean dismiss) { | |
this.canDismiss = dismiss; | |
return this; | |
} | |
public View getCustomView() { | |
return this.customView; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
final MaterialDialog dialog = new MaterialDialog(MainActivity.this); | |
dialog.setTitle("Test") | |
//Use this if you want to set a text message | |
.setMessage("Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test") | |
//Use this for a custom layout resource | |
//.setCustomViewResource(R.layout.dialog_test_layout); | |
//Or pass the View | |
//.setCustomView(yourView); | |
//Set cancelable on touch outside (default true) | |
//.dismissOnTouchOutside(false) | |
.setupPositiveButton("Accept", new OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
// TODO Auto-generated method stub | |
dialog.dismiss(); | |
} | |
}); | |
.setupNegativeButton("Decline", new OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
// TODO Auto-generated method stub | |
dialog.dismiss(); | |
} | |
}); | |
dialog.show(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment