In Android programming you want to custom dialog for your interface to be beautiful, but you don't know how, today I will introduce you a pretty simple custom dialog in Android with the following steps:
Step1 : create layout_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:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:background="@android:color/transparent"
android:orientation="vertical">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:ems="10"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/white"
android:inputType="textMultiLine"
android:layout_marginTop="20dp"
android:lines="8"
android:maxLines="10"
android:minLines="6"
android:scrollbars="vertical" />
<ImageView
android:layout_marginTop="0dp"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_gravity="center|top"
android:src="@drawable/ic_launcher" />
</FrameLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:background="@android:color/white"
android:weightSum="2">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@android:color/black"
android:text="Submit"
android:textColor="#FFFFFF" />
<View
android:layout_width="4dp"
android:layout_height="match_parent"
android:background="@android:color/transparent" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@android:color/black"
android:text="Cancel"
android:textColor="#FFFFFF" />
</LinearLayout>
</LinearLayout>
Step 2: Create class activity custom dialog
public class CustomDialogClass extends Dialog implements
android.view.View.OnClickListener {
public Activity c;
public Dialog d;
public Button yes, no;
public CustomDialogClass(Activity a) {
super(a);
// TODO Auto-generated constructor stub
this.c = a;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.custom_dialog);
yes = (Button) findViewById(R.id.btn_yes);
no = (Button) findViewById(R.id.btn_no);
yes.setOnClickListener(this);
no.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_yes:
c.finish();
break;
case R.id.btn_no:
dismiss();
break;
default:
break;
}
dismiss();
}
}
No comments:
Post a Comment