Advanced Android - Borderless Dialog



This example implements the way to make this dialog style through using DialogFragment

Example

This example demostrate about how to integrate Borderless Dialog.

Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project.

Step 2 − Add the following code to res/layout/activity_main.xml.

<?xml version = "1.0" encoding = "utf-8"?>
<RelativeLayout xmlns:android = "http://schemas.android.com/apk/res/android"
   xmlns:tools = "http://schemas.android.com/tools"
   android:layout_width = "match_parent"
   android:layout_height = "match_parent"
   android:paddingBottom = "@dimen/activity_vertical_margin"
   android:paddingLeft = "@dimen/activity_horizontal_margin"
   android:paddingRight = "@dimen/activity_horizontal_margin"
   android:paddingTop = "@dimen/activity_vertical_margin"
   tools:context = ".MainActivity">
   <Button
      android:id = "@+id/btn_dialog"
      android:text = "show Borderless Dialog"
      android:layout_centerInParent = "true"
      android:layout_width = "match_parent"
      android:layout_height = "wrap_content" />
</RelativeLayout>

Step 3 − Add the following code to src/MainActivity.java

package myapplication.example.com.myapplication;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {
   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      
      Button button = (Button) findViewById(R.id.btn_dialog);
      button.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View view) {
            BorderlessDialogFragment dFragment = new BorderlessDialogFragment();
            dFragment.setCancelable(false); //don't close when touch outside
            dFragment.show(getSupportFragmentManager(), "Dialog Fragment");
         }
      });
   }
}

Step 4 − Add the following code to src/BorderlessDialogFragment.java

package myapplication.example.com.myapplication;

import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.Toast;

public class BorderlessDialogFragment extends DialogFragment {
   @Override
   public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setStyle(DialogFragment.STYLE_NO_TITLE, 0);
   } 
   
   @Override
   public View onCreateView(LayoutInflater inflater, ViewGroup container, 
      Bundle savedInstanceState) {
      
      return inflater.inflate(R.layout.layout_dialog, container);
   } 
   
   @Override
   public void onViewCreated(View view, Bundle savedInstanceState) {
      super.onViewCreated(view, savedInstanceState);
      
      ImageView btnPlus = (ImageView)view.findViewById(R.id.btn_plus);
      ImageView btnOK = (ImageView)view.findViewById(R.id.btn_ok);
      ImageView btnClose = (ImageView)view.findViewById(R.id.btn_close);
      
      btnOK.setOnClickListener(onClickListener("Button OK clicked!"));
      btnPlus.setOnClickListener(onClickListener("Button Plus Clicked!"));
      btnClose.setOnClickListener(onCloseClickListener());
   } 
   
   private View.OnClickListener onCloseClickListener() {
      return new View.OnClickListener() {
         @Override
         public void onClick(View v) {
            BorderlessDialogFragment.this.dismiss();
         } 
      };
   } 
   
   private View.OnClickListener onClickListener(final String msg) {
      return new View.OnClickListener() {
         @Override
         public void onClick(View v) {
            Toast.makeText(getActivity(), msg, Toast.LENGTH_SHORT).show();
         } 
      };
   }
}

Step 5 − Add the following code to res/layout/layout_dialog.xml.

<?xml version = "1.0" encoding =" utf-8"?>
<FrameLayout xmlns:android = "http://schemas.android.com/apk/res/android"
   android:layout_width = "match_parent"
   android:layout_height = "wrap_content">
   <FrameLayout
      android:id = "@+id/fr_1"
      android:layout_width = "wrap_content"
      android:layout_height = "wrap_content">
      <ImageView
         android:layout_width = "270dp"
         android:layout_height = "400dp"
         android:background = "@drawable/abcd"
         android:contentDescription = "@string/app_name"
         android:scaleType = "centerCrop" />
      <ImageView
         android:id = "@+id/btn_close"
         android:layout_width = "wrap_content"
         android:layout_height = "wrap_content"
         android:layout_gravity = "end"
         android:contentDescription = "@string/app_name"
         android:src = "@drawable/abcde" />
   </FrameLayout>
   <FrameLayout
      android:layout_width = "270dp"
      android:layout_height = "60dp"
      android:layout_gravity = "bottom"
      android:background = "#f2f2f2">
      <ImageView
         android:id = "@+id/btn_ok"
         android:layout_width = "wrap_content"
         android:layout_height = "wrap_content"
         android:layout_gravity = "center_vertical|start"
         android:layout_marginLeft = "50dp"
         android:layout_marginStart = "50dp"
         android:contentDescription = "@string/app_name"
         android:src = "@drawable/abc" />
      <ImageView
         android:id = "@+id/btn_plus"
         android:layout_width = "wrap_content"
         android:layout_height = "wrap_content"
         android:layout_gravity = "center_vertical|end"
         android:layout_marginEnd = "50dp"
         android:layout_marginRight = "50dp"
         android:contentDescription = "@string/app_name"
         android:src = "@drawable/abc" />
   </FrameLayout>
</FrameLayout>

Step 6 − No need to change manifest.xml

Let's try to run your application. I assume you have connected your actual Android Mobile device with your computer. To run the app from android studio, open one of your project's activity files and click Run Eclipse Run Icon icon from the toolbar. Select your mobile device as an option and then check your mobile device which will display your default screen −

Borderless
Advertisements