-3

I am developing an Android application, where I need to show a popup containing some message on application opening (Image attached for reference). I have tried to achieve this using toast message and snack bar but could not do exact same.

Please suggest which component in Android to be used to achieve this functionality?

Here is the ScreenShot

Shashanth
  • 4,995
  • 7
  • 41
  • 51

2 Answers2

1

You can achieve the desired results using a dialog. I am sharing the code, you can modify it as you want but make sure you put it inside the onCreate method of the launcher activity.

This is the code for a simple dialog

AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Look at this dialog!")
   .setCancelable(false)
   .setPositiveButton("OK", new DialogInterface.OnClickListener() {
       public void onClick(DialogInterface dialog, int id) {
            //do things
       }
   });
AlertDialog alert = builder.create();
alert.show();

The one shown in the picture shared by you is a custom dialog where you have to make your own layout. Here is an example,

1. Create your own layout

<RelativeLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ffffffff">

<ImageView
    android:layout_width="match_parent"
    android:layout_height="120dp"
    android:id="@+id/a"
    android:gravity="center"
    android:background="#DA5F6A"
    android:src="@drawable/dialog_cross"
    android:scaleType="fitCenter" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="TEXT"
    android:id="@+id/text_dialog"
    android:layout_below="@+id/a"
    android:layout_marginTop="20dp"
    android:layout_marginLeft="4dp"
    android:layout_marginRight="4dp"
    android:layout_marginBottom="20dp"
    android:textSize="18sp"
    android:textColor="#ff000000"
    android:layout_centerHorizontal="true"
    android:gravity="center_horizontal" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="30dp"
    android:text="OK"
    android:id="@+id/btn_dialog"
    android:gravity="center_vertical|center_horizontal"
    android:layout_below="@+id/text_dialog"
    android:layout_marginBottom="20dp"
    android:background="@drawable/btn_flat_red_selector"
    android:layout_centerHorizontal="true"
    android:textColor="#ffffffff" />

</RelativeLayout>

2. Create a class for the custom dialog

public class ViewDialog {

public void showDialog(Activity activity, String msg){
    final Dialog dialog = new Dialog(activity);
    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
    dialog.setCancelable(false);
    dialog.setContentView(R.layout.dialog);

    TextView text = (TextView) dialog.findViewById(R.id.text_dialog);
    text.setText(msg);

    Button dialogButton = (Button) dialog.findViewById(R.id.btn_dialog);
    dialogButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            dialog.dismiss();
        }
    });

    dialog.show();

}
}

3. Make an object in your launcher activity of the custom dialog class and call the function showDialog

ViewDialog alert = new ViewDialog();
alert.showDialog(getActivity(), "Thank you for installing the Paytm 
App");

Or you could use an external library for getting the desired results

Pop.on(this).with().title(R.string.title).layout(R.layout.custom_pop).show();

where R.layout.custom_pop is the custom layout of your dialog.

Pritom Mazumdar
  • 325
  • 5
  • 20
1

Try this for same result:

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:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="right">

        <Button
            android:id="@+id/btnClose"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="X" />
    </LinearLayout>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:padding="10dp"
        android:text="Hello, this is your message"
        android:textSize="20sp" />

</LinearLayout>

code:

private void showDialog() {
    final Dialog dialog = new Dialog(mContext);
    dialog.setContentView(R.layout.dialog);

    Window window = dialog.getWindow();
    WindowManager.LayoutParams wlp = window.getAttributes();

    wlp.gravity = Gravity.BOTTOM;
    wlp.flags &= ~WindowManager.LayoutParams.FLAG_DIM_BEHIND;
    window.setAttributes(wlp);

    dialog.findViewById(R.id.btnClose).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            dialog.cancel();
        }
    });

    dialog.show();
}
Rumit Patel
  • 8,830
  • 18
  • 51
  • 70