This is Sample Project Which you can improve UI design & Coding skills .
1) First you need four drawable resource files.
example:
- button_yes_background
- button_no_background
- dialog_background
- title_warning_background
2) And then need one layout file
example:
- layout_warning_dialog
3) Now Coding to Activity Class.
example: I use HomeActivity.class
4) I use some styles and Strings
1) button_yes_background
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="@color/coloryellow"/>
<corners android:radius="20dp"/>
</shape>
2) button_no_background
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="@color/colorNeutral"/>
<corners android:radius="20dp"/>
</shape>
3) dialog_background
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="@color/white"/>
<corners android:radius="10dp"/>
</shape>
4) Title_warning_background
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="@color/coloryellow"/>
<corners android:topLeftRadius="10dp"
android:topRightRadius="10dp"/>
</shape>
2)
1) layout_warning_dialog
<?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:id="@+id/layoutDialogContainer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:padding="20dp"
>
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/layoutDialog"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/dialog_background"
app:layout_constraintTop_toTopOf="parent"
>
<TextView
android:id="@+id/textTitile"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/title_warning_background"
android:padding="10dp"
android:textColor="@color/white"
android:textSize="18sp"
android:textStyle="bold"
app:layout_constraintTop_toTopOf="parent"
/>
<ImageView
android:id="@+id/imageIcon"
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_marginEnd="10dp"
android:layout_marginRight="10dp"
android:contentDescription="@string/app_name"
app:tint="@color/white"
app:layout_constraintBottom_toBottomOf="@id/textTitile"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="@id/textTitile"
/>
<TextView
android:id="@+id/textMessage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginTop="18dp"
android:layout_marginEnd="20dp"
android:layout_marginBottom="40dp"
android:textColor="@color/blacks"
android:textSize="16sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toBottomOf="@id/textTitile"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
<Button
android:id="@+id/buttonNo"
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_marginStart="40dp"
android:layout_marginEnd="10dp"
android:background="@drawable/button_no_background"
android:textColor="@color/white"
android:textSize="14sp"
app:layout_constraintBottom_toBottomOf="@id/layoutDialog"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toStartOf="@id/buttonYes"
app:layout_constraintTop_toBottomOf="@id/layoutDialog"
android:layout_marginLeft="40dp"
android:layout_marginRight="10dp" />
<Button
android:id="@+id/buttonYes"
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_marginStart="10dp"
android:layout_marginEnd="40dp"
android:background="@drawable/button_yes_background"
android:textColor="@color/white"
android:textSize="14sp"
app:layout_constraintBottom_toBottomOf="@id/layoutDialog"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@id/buttonNo"
app:layout_constraintTop_toBottomOf="@id/layoutDialog"
android:layout_marginLeft="40dp"
android:layout_marginRight="10dp" />
</androidx.constraintlayout.widget.ConstraintLayout>
3) I use Navigation Drawer clicked navigation logout in HomeActivity.(you can use button on click lisner)
First I create Method called showWarningDialog();
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
switch (menuItem.getItemId()){
case R.id.nav_home:
Intent intent = new Intent(HomeActivity.this,
HomeActivity.class);
startActivity(intent);
break;
case R.id.nav_profile:
Intent intent1 = new Intent(HomeActivity.this,
ProfileActivity.class);
startActivity(intent1);
break;
case R.id.nav_bus:
Toast.makeText(HomeActivity.this,"You clicked Bus",
Toast.LENGTH_SHORT).show();
break;
case R.id.nav_logout:
showWarningDialog();
private void showWarningDialog(){
AlertDialog.Builder builder = new AlertDialog.Builder
(HomeActivity.this,R.style.AlertDialogTheme);
View view = LayoutInflater.from(HomeActivity.this).inflate(
R.layout.layout_warning_dialog,
(ConstraintLayout) findViewById(R.id.layoutDialogContainer)
);
builder.setView(view);
((TextView) view.findViewById(R.id.textTitile)).
setText(getResources().getString(R.string.warning_title));
((TextView) view.findViewById(R.id.textMessage)).
setText(getResources().getString(R.string.dummy_text));
((Button) view.findViewById(R.id.buttonYes)).
setText(getResources().getString(R.string.yes));
((Button) view.findViewById(R.id.buttonNo)).
setText(getResources().getString(R.string.no));
((ImageView) view.findViewById(R.id.imageIcon)).
setImageResource(R.drawable.ic_warning);
final AlertDialog alertDialog = builder.create();
view.findViewById(R.id.buttonYes).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
alertDialog.dismiss();
FirebaseAuth.getInstance().signOut();
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.clear();
editor.commit();
Toast.makeText(HomeActivity.
this,"Logeed Out!",Toast.LENGTH_SHORT).show();
startActivity(new Intent(HomeActivity.
this,LoginActivity.class));
finish();
}
});
view.findViewById(R.id.buttonNo).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
alertDialog.dismiss();
}
});
if (alertDialog.getWindow() != null){
alertDialog.getWindow().setBackgroundDrawable(new ColorDrawable(0));
}
alertDialog.show();
}
4) Styles & Strings
<style name="AlertDialogTheme" parent="Theme.AppCompat.Light.Dialog.Alert">
<item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item>
</style>
0 Comments