Create Dialog Basic Single Multiple Alert Confirmation
1. Project Structure
2. Gradle
apply plugin: 'com.android.application'
android {
compileSdkVersion 28
defaultConfig {
applicationId "dialog.androidtutorials.fyprojects.basic"
minSdkVersion 16
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
implementation 'com.android.support:design:28.0.0'
}
3. Main Activity
package dialog.androidtutorials.fyprojects.basic.activity;
import android.content.DialogInterface;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;
import dialog.androidtutorials.fyprojects.basic.utils.Tools;
public class MainActivity extends AppCompatActivity {
private View parent_view;
private static final String[] RINGTONE = new String[]{
"None", "Callisto", "Ganymede", "Luna"
};
private boolean[] clicked_colors = new boolean[COLORS.length];
private static final String[] COLORS = new String[]{
"Red", "Green", "Blue", "Purple", "Olive"
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
parent_view = findViewById(android.R.id.content);
initToolbar();
}
private void initToolbar() {
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
toolbar.setNavigationIcon(R.drawable.ic_menu);
setSupportActionBar(toolbar);
getSupportActionBar().setTitle("Basic");
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
Tools.setSystemBarColor(this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_search_setting, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == android.R.id.home) {
finish();
} else {
Toast.makeText(getApplicationContext(), item.getTitle(), Toast.LENGTH_SHORT).show();
}
return super.onOptionsItemSelected(item);
}
public void clickAction(View view) {
int id = view.getId();
switch (id) {
case R.id.confirm_dialog:
showConfirmDialog();
break;
case R.id.alert_dialog:
showAlertDialog();
break;
case R.id.single_choice_dialog:
showSingleChoiceDialog();
break;
case R.id.multi_choice_dialog:
showMultiChoiceDialog();
break;
}
}
private void showConfirmDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Use Google's location services ?");
builder.setMessage(R.string.middle_lorem_ipsum);
builder.setPositiveButton(R.string.AGREE, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Snackbar.make(parent_view, "Agree clicked", Snackbar.LENGTH_SHORT).show();
}
});
builder.setNegativeButton(R.string.DISAGREE, null);
builder.show();
}
private void showAlertDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Discard draft ?");
builder.setPositiveButton(R.string.DISCARD, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Snackbar.make(parent_view, "Discard clicked", Snackbar.LENGTH_SHORT).show();
}
});
builder.setNegativeButton(R.string.CANCEL, null);
builder.show();
}
private String single_choice_selected;
private void showSingleChoiceDialog() {
single_choice_selected = RINGTONE[0];
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Phone Ringtone");
builder.setSingleChoiceItems(RINGTONE, 0, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
single_choice_selected = RINGTONE[i];
}
});
builder.setPositiveButton(R.string.OK, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Snackbar.make(parent_view, "selected : " + single_choice_selected, Snackbar.LENGTH_SHORT).show();
}
});
builder.setNegativeButton(R.string.CANCEL, null);
builder.show();
}
private void showMultiChoiceDialog() {
single_choice_selected = RINGTONE[0];
clicked_colors = new boolean[COLORS.length];
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Your preferred colors");
builder.setMultiChoiceItems(COLORS, clicked_colors, new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i, boolean b) {
clicked_colors[i] = b;
}
});
builder.setPositiveButton(R.string.OK, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Snackbar.make(parent_view, "Data submitted", Snackbar.LENGTH_SHORT).show();
}
});
builder.setNegativeButton(R.string.CANCEL, null);
builder.show();
}
}
4. Utils
Tools
package chips.androidtutorials.fyprojects.tags.utils;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.os.Build;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import chips.androidtutorials.fyprojects.tags.activity.R;
public class Tools {
public static void setSystemBarColor(Activity act) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
Window window = act.getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
window.setStatusBarColor(act.getResources().getColor(R.color.colorPrimaryDark));
}
}
}
5. Layout
activity_main.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:background="@android:color/white"
android:fitsSystemWindows="true"
android:orientation="vertical">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<include layout="@layout/toolbar" />
</android.support.design.widget.AppBarLayout>
<android.support.v4.widget.NestedScrollView
android:id="@+id/nested_scroll_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="none"
android:scrollingCache="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:descendantFocusability="blocksDescendants"
android:orientation="vertical">
<Button
android:id="@+id/confirm_dialog"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/selectableItemBackground"
android:gravity="left|center_vertical"
android:minHeight="?attr/actionBarSize"
android:onClick="clickAction"
android:paddingLeft="@dimen/spacing_large"
android:paddingRight="@dimen/spacing_large"
android:text="CONFIRMATION DIALOG"
android:textColor="@color/grey_60" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@color/grey_10" />
<Button
android:id="@+id/alert_dialog"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/selectableItemBackground"
android:gravity="left|center_vertical"
android:minHeight="?attr/actionBarSize"
android:onClick="clickAction"
android:paddingLeft="@dimen/spacing_large"
android:paddingRight="@dimen/spacing_large"
android:text="ALERT DIALOG"
android:textColor="@color/grey_60" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@color/grey_10" />
<Button
android:id="@+id/single_choice_dialog"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/selectableItemBackground"
android:gravity="left|center_vertical"
android:minHeight="?attr/actionBarSize"
android:onClick="clickAction"
android:paddingLeft="@dimen/spacing_large"
android:paddingRight="@dimen/spacing_large"
android:text="SINGLE CHOICE DIALOG"
android:textColor="@color/grey_60" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@color/grey_10" />
<Button
android:id="@+id/multi_choice_dialog"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/selectableItemBackground"
android:gravity="left|center_vertical"
android:minHeight="?attr/actionBarSize"
android:onClick="clickAction"
android:paddingLeft="@dimen/spacing_large"
android:paddingRight="@dimen/spacing_large"
android:text="MULTIPLE CHOICE DIALOG"
android:textColor="@color/grey_60" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@color/grey_10" />
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
</LinearLayout>
Toolbar.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:contentInsetStartWithNavigation="0dp"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>
6. Values
colors.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="colorPrimary">#1976D2</color>
<color name="colorPrimaryDark">#1565C0</color>
<color name="colorPrimaryLight">#1E88E5</color>
<color name="colorAccent">#FF4081</color>
<color name="colorAccentDark">#F50057</color>
<color name="colorAccentLight">#FF80AB</color>
<color name="light_green_600">#7CB342</color>
<color name="overlay_light_80">#CCFFFFFF</color>
<color name="grey_10">#e6e6e6</color>
<color name="grey_20">#cccccc</color>
<color name="grey_40">#999999</color>
<color name="grey_60">#666666</color>
<color name="grey_80">#37474F</color>
<color name="grey_90">#263238</color>
</resources>
dimens.xml
<resources>
<!-- Default screen margins, per the Android Design guidelines. -->
<dimen name="actionBarSize">56dp</dimen>
<dimen name="activity_horizontal_margin">16dp</dimen>
<dimen name="activity_vertical_margin">16dp</dimen>
<dimen name="nav_header_vertical_spacing">16dp</dimen>
<dimen name="nav_header_height">200dp</dimen>
<dimen name="fab_margin">16dp</dimen>
<dimen name="viewpager_margin_overlap">-60dp</dimen>
<dimen name="viewpager_margin_overlap_payment">-30dp</dimen>
<!--genaral spacing-->
<dimen name="spacing_xsmall">2dp</dimen>
<dimen name="spacing_small">3dp</dimen>
<dimen name="spacing_medium">5dp</dimen>
<dimen name="spacing_xmedium">7dp</dimen>
<dimen name="spacing_middle">10dp</dimen>
<dimen name="spacing_large">15dp</dimen>
<dimen name="spacing_smlarge">18dp</dimen>
<dimen name="spacing_mlarge">20dp</dimen>
<dimen name="spacing_mxlarge">25dp</dimen>
<dimen name="spacing_xlarge">35dp</dimen>
<dimen name="spacing_xmlarge">40dp</dimen>
<dimen name="spacing_xxlarge">50dp</dimen>
<dimen name="spacing_xxxlarge">55dp</dimen>
<dimen name="appbar_padding_top">8dp</dimen>
</resources>
strings.xml
<resources>
<string name="app_name">Dialog Basic</string>
<string name="middle_lorem_ipsum">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam efficitur ipsum in placerat molestie. Fusce quis mauris a enim sollicitudin</string>
<string name="AGREE">AGREE</string>
<string name="DISAGREE">DISAGREE</string>
<string name="DISCARD">DISCARD</string>
<string name="CANCEL">CANCEL</string>
<string name="OK">OK</string>
</resources>
styles.xml
<resources>
<style name="AppTheme" parent="BaseTheme" />
<style name="BaseTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="windowActionModeOverlay">true</item>
</style>
</resources>