Tuesday, 20 January 2015

Custom Dialog Example

customized dialog in android.

ScreenShots:




strings.xml


<?xml version="1.0" encoding="utf-8"?>

<resources>

    <string name="app_name">CustomDialog</string>
    <string name="custom_button_finish">Finish</string>
    <string name="custom_button_ok">OK</string>
    <string name="custom_button_cancel">Cancel</string>
    <string name="custom_edittext_hint"> </string>
       
</resources>

activity1_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
   >

    <Button
        android:id="@+id/custom_button_finish"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="63dp"
        android:text="@string/custom_button_finish" />

</RelativeLayout>

activity2.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/RelativeLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <Button
        android:id="@+id/custom_button_cancel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/custom_button_ok"
        android:layout_alignBottom="@+id/custom_button_ok"
        android:layout_alignParentRight="true"
        android:layout_marginRight="58dp"
        android:text="@string/custom_button_cancel" />
    
 <EditText
        android:id="@+id/custom_edittext"
        android:layout_width="300dp"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="20dp"
        android:hint="@string/custom_edittext_hint" >

        <requestFocus />

    </EditText>

    <Button
        android:id="@+id/custom_button_ok"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/custom_edittext"
        android:layout_below="@+id/custom_edittext"
        android:layout_marginLeft="28dp"
        android:layout_marginTop="18dp"
        android:text="@string/custom_button_ok" />

</RelativeLayout>

MainActivity1.java
package com.swamys.customdialogexample;

import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity1 extends Activity implements OnClickListener{

    EditText edittext;
       Button button_finish;
       Dialog dialog;
             
       @Override
       protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.activity1_main);
             
              button_finish=(Button) findViewById(R.id.custom_button_finish);
             
              button_finish.setOnClickListener(this);
             
     }

    @Override
       public void onClick(View v) {

       dialog=new Dialog(MainActivity1.this);
       dialog.setTitle("Edit Button Name");
       dialog.setContentView(R.layout.activity2);
      
       String button_name=button_finish.getText().toString();
      
       edittext=(EditText) dialog.findViewById(R.id.custom_edittext);
      
       edittext.setText(button_name);
      
       Button dialog_ok=(Button) dialog.findViewById(R.id.custom_button_ok);
       Button dialog_cancel=(Button) dialog.findViewById(R.id.custom_button_cancel);
      
      
       dialog_ok.setOnClickListener(new OnClickListener() {
                    
                     @Override
                     public void onClick(View v) {
                          
                           button_finish.setText(edittext.getText().toString());
                           dialog.dismiss();
                          
                     }
              });
      
      dialog_cancel.setOnClickListener(new OnClickListener() {
             
              @Override
              public void onClick(View v) {
                    
                     dialog.dismiss();
              }
       });
      
      dialog.show();
      
    }

 }

No comments:

Post a Comment