Android custom style dialog

Recommended for you: Get network issues from WhatsUp Gold. Not end users.
First look at the effect of map, that is the effect you want:



The XML file:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:background="@drawable/pop_bg"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/title"
        android:layout_width="fill_parent"
        android:layout_height="40.0dip"
        android:layout_marginTop="15dp"
        android:gravity="center"
        android:text="Set number"
        android:textColor="#fffffb"
        android:textSize="20sp"
        android:visibility="visible" />

  <!--   <LinearLayout
        android:id="@+id/contentView"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" > -->

        <EditText
            android:id="@+id/number"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:layout_marginTop="5dp"
            android:gravity="left|center"
            android:hint="Number" />
    <!-- </LinearLayout> -->


    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="60.0dip"
        android:layout_gravity="bottom"
        android:layout_marginTop="5dp"
        android:gravity="center"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/positiveButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/btn_ok_selector"
            android:gravity="center" />

        <Button
            android:id="@+id/negativeButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="20.0dip"
            android:background="@drawable/btn_cancel_selector"
            android:gravity="center" />
    </LinearLayout>

</LinearLayout>

Java code

import android.app.Dialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
/**
 * @Function: custom dialog
 * @Date: 2013-10-28
 * @Time: 12:37:43 this afternoon
 * @author Tom.Cai
 */
public class CustomDialog extends Dialog {
	private EditText editText;
	private Button positiveButton, negativeButton;
	private TextView title;

	public CustomDialog(Context context) {
		super(context,R.style.CustomDialog);
		setCustomDialog();
	}

	private void setCustomDialog() {
		View mView = LayoutInflater.from(getContext()).inflate(R.layout.dialog_normal_layout, null);
		title = (TextView) mView.findViewById(R.id.title);
		editText = (EditText) mView.findViewById(R.id.number);
		positiveButton = (Button) mView.findViewById(R.id.positiveButton);
		negativeButton = (Button) mView.findViewById(R.id.negativeButton);
		super.setContentView(mView);
	}
	
	public View getEditText(){
		return editText;
	}
	
	 @Override
	public void setContentView(int layoutResID) {
	}

	@Override
	public void setContentView(View view, LayoutParams params) {
	}

	@Override
	public void setContentView(View view) {
	}

	/** 
     * Determine the key. 
     * @param listener 
     */  
    public void setOnPositiveListener(View.OnClickListener listener){  
    	positiveButton.setOnClickListener(listener);  
    }  
    /** 
     * The Cancel button. 
     * @param listener 
     */  
    public void setOnNegativeListener(View.OnClickListener listener){  
    	negativeButton.setOnClickListener(listener);  
    }
}

Method of use:
	// Pop
	private void dialog() {
		dialog = new CustomDialog(WorkspaceActivity.this);
		EditText editText = (EditText) dialog.getEditText();//The method was implemented in CustomDialog
		dialog.setOnPositiveListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				//dosomething youself
				dialog2.dismiss();
			}
		});
		dialog.setOnNegativeListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				dialog2.dismiss();
			}
		});
		dialog.show();
	}
Master bypass! For more information, please see my blog
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download

Posted by Aaron at November 28, 2013 - 5:36 PM