Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Giving Android Dialog a custom layout.

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 401
    Comment on it

    We see situations while developing an app that we need to put certain things inside the Android Dialog box. Here is how we can achieve the same:

    First, create the layout that you want to give to the Dialog. Below is a sample layout:

    custom_dialog.xml

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@android:color/white"
        android:gravity="center_horizontal"
        android:orientation="vertical" >
    
        <RelativeLayout
            android:layout_width="fill_parent"
            android:layout_height="55dp"
            android:background="@drawable/header" 
            android:padding="1dp">
    
            <ImageView
                android:id="@+id/close_dialog"
                android:layout_width="52dp"
                android:layout_height="52dp"
                android:layout_alignParentLeft="true"
                android:layout_centerVertical="true"
                android:layout_marginLeft="10dp"
                android:onClick="onClick"
                android:src="@drawable/backbtn" />
        </RelativeLayout>
    
        <LinearLayout
            android:id="@+id/buttons"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="25dp"
            android:gravity="center"
            android:orientation="horizontal" >
    
            <ImageView
                android:id="@+id/retake"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/fail_retake" />
    
            <ImageView
                android:id="@+id/addManually"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="30dp"
                android:src="@drawable/add_manually" />
        </LinearLayout>
    </LinearLayout>
    

    And below is the method that you can call to show the Dialog with custom layout we made above:

       void showCustomDialog()
        {
            final Dialog dialog = new Dialog(this, android.R.style.Theme_Black_NoTitleBar_Fullscreen);
            dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
            dialog.setCancelable(false);
            dialog.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
            dialog.setContentView(R.layout.custom_dialog);
    
     ImageView use = (ImageView) dialog.findViewById(R.id.addManually);
        ImageView retake = (ImageView) dialog.findViewById(R.id.retake);
        ImageView closeImage = (ImageView) dialog.findViewById(R.id.close_dialog);
        use.setOnClickListener(new OnClickListener()
        {
            @Override
            public void onClick(View v) 
            {
                Intent intent = new Intent(CaptureActivity.this, AddManually.class);
                this.startActivity(intent);
                dialog.dismiss();
                this.finish();
            }
        });
        retake.setOnClickListener(new OnClickListener()
        {
            @Override
            public void onClick(View v) 
            {
                dialog.dismiss();         
            }
        });
        closeImage.setOnClickListener(new OnClickListener()
        {
            @Override
            public void onClick(View v) 
            {
                dialog.dismiss();         
            }
        }); 
        dialog.show();
    }
    

    We can use many buttons or images or any views inside the above layout according to our needs and perform various actions for further navigation using them.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: