1) Create XML in \res\drawable and named it bgdialog.xml .
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<gradient
android:angle="90"
android:endColor="#ffffff"
android:startColor="#ffffff" />
<padding
android:bottom="10dp"
android:left="10dp"
android:right="10dp"
android:top="10dp" />
<stroke
android:width="2dp"
android:color="#000000" />
<corners android:radius="10dp" />
</shape>
2) In main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/tvSearchDialog"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="Search Dialog..."
android:textAppearance="?android:attr/textAppearanceMedium" />
<Button
android:id="@+id/btnDialog"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/tvSearchDialog"
android:text="Show Dialog" />
</RelativeLayout>
3) Create XML in \res\drawable and named it customdialog.xml.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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/bgdialog"
android:orientation="vertical" >
<TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="10dip"
android:text="Custom Dialog"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#000000" />
<EditText
android:id="@+id/etsearch"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:layout_below="@+id/tv"
android:layout_centerHorizontal="true"
android:layout_margin="10dp" >
<requestFocus />
</EditText>
<Button
android:id="@+id/btnsearch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/btncancel"
android:layout_alignBottom="@+id/btncancel"
android:layout_alignLeft="@+id/etsearch"
android:layout_marginLeft="27dp"
android:text=" Search " />
<Button
android:id="@+id/btncancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/etsearch"
android:layout_marginLeft="20dip"
android:layout_toRightOf="@+id/btnsearch"
android:text=" Cancel " />
</RelativeLayout>
4) Open your main activity. In my case,activity name is AndroidCustomDialogActivity.java file
package com.dhruv;
import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class AndroidCustomDialogActivity extends Activity implements
OnClickListener {
/** Called when the activity is first created. */
EditText etSearch;
Button btn, btnSearch, btnCancel;
Dialog dialog;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btn = (Button) findViewById(R.id.btnDialog);
btn.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.btnDialog:
showCustomDialog();
break;
case R.id.btnsearch:
String search = etSearch.getText().toString().trim();
if (TextUtils.isEmpty(search)) {
Toast.makeText(AndroidCustomDialogActivity.this,
"Searching for Nothing", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(AndroidCustomDialogActivity.this,
"Searching for " + search, Toast.LENGTH_LONG).show();
}
dialog.dismiss();
break;
case R.id.btncancel:
dialog.dismiss();
break;
default:
break;
}
}
protected void showCustomDialog() {
dialog = new Dialog(AndroidCustomDialogActivity.this,
android.R.style.Theme_Translucent);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setCancelable(true);
dialog.setContentView(R.layout.customdialog);
etSearch = (EditText) dialog.findViewById(R.id.etsearch);
btnSearch = (Button) dialog.findViewById(R.id.btnsearch);
btnCancel = (Button) dialog.findViewById(R.id.btncancel);
btnSearch.setOnClickListener(this);
btnCancel.setOnClickListener(this);
dialog.show();
}
}
Output
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<gradient
android:angle="90"
android:endColor="#ffffff"
android:startColor="#ffffff" />
<padding
android:bottom="10dp"
android:left="10dp"
android:right="10dp"
android:top="10dp" />
<stroke
android:width="2dp"
android:color="#000000" />
<corners android:radius="10dp" />
</shape>
2) In main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/tvSearchDialog"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="Search Dialog..."
android:textAppearance="?android:attr/textAppearanceMedium" />
<Button
android:id="@+id/btnDialog"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/tvSearchDialog"
android:text="Show Dialog" />
</RelativeLayout>
3) Create XML in \res\drawable and named it customdialog.xml.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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/bgdialog"
android:orientation="vertical" >
<TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="10dip"
android:text="Custom Dialog"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#000000" />
<EditText
android:id="@+id/etsearch"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:layout_below="@+id/tv"
android:layout_centerHorizontal="true"
android:layout_margin="10dp" >
<requestFocus />
</EditText>
<Button
android:id="@+id/btnsearch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/btncancel"
android:layout_alignBottom="@+id/btncancel"
android:layout_alignLeft="@+id/etsearch"
android:layout_marginLeft="27dp"
android:text=" Search " />
<Button
android:id="@+id/btncancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/etsearch"
android:layout_marginLeft="20dip"
android:layout_toRightOf="@+id/btnsearch"
android:text=" Cancel " />
</RelativeLayout>
4) Open your main activity. In my case,activity name is AndroidCustomDialogActivity.java file
package com.dhruv;
import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class AndroidCustomDialogActivity extends Activity implements
OnClickListener {
/** Called when the activity is first created. */
EditText etSearch;
Button btn, btnSearch, btnCancel;
Dialog dialog;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btn = (Button) findViewById(R.id.btnDialog);
btn.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.btnDialog:
showCustomDialog();
break;
case R.id.btnsearch:
String search = etSearch.getText().toString().trim();
if (TextUtils.isEmpty(search)) {
Toast.makeText(AndroidCustomDialogActivity.this,
"Searching for Nothing", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(AndroidCustomDialogActivity.this,
"Searching for " + search, Toast.LENGTH_LONG).show();
}
dialog.dismiss();
break;
case R.id.btncancel:
dialog.dismiss();
break;
default:
break;
}
}
protected void showCustomDialog() {
dialog = new Dialog(AndroidCustomDialogActivity.this,
android.R.style.Theme_Translucent);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setCancelable(true);
dialog.setContentView(R.layout.customdialog);
etSearch = (EditText) dialog.findViewById(R.id.etsearch);
btnSearch = (Button) dialog.findViewById(R.id.btnsearch);
btnCancel = (Button) dialog.findViewById(R.id.btncancel);
btnSearch.setOnClickListener(this);
btnCancel.setOnClickListener(this);
dialog.show();
}
}
Output
Download Full Code From Here
Source Code
Comments
Post a Comment