I provided a sample of android code to create a custom dialog in android. Save the code in different java and xml files as given below before running the code:
cdialog.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="200dip"
android:orientation="vertical"
android:padding="5dip"
android:layout_height="fill_parent">
<TextView
android:layout_width="fill_parent"
android:text="Dialog Title"
android:gravity="center_vertical"
android:layout_height="20dip">
</TextView>
<TextView
android:layout_width="fill_parent"
android:layout_marginTop="5dip"
android:background="#FFFFFF"
android:layout_height="0.5sp">
</TextView>
<TextView
android:layout_width="fill_parent"
android:text="Dialog Message"
android:gravity="center_vertical"
android:layout_height="50dip">
</TextView>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="OK"
android:id="@+id/btnOk"
android:layout_gravity="center">
</Button>
</LinearLayout>
main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btnDialog"
android:text="ShowDialog">
</Button>
</LinearLayout>
CustomDialog.java:
package com.customdialog.home;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.Window;
public class CustomDialog extends Dialog
{
public CustomDialog(Context context)
{
super(context);
}
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.cdialog);
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="200dip"
android:orientation="vertical"
android:padding="5dip"
android:layout_height="fill_parent">
<TextView
android:layout_width="fill_parent"
android:text="Dialog Title"
android:gravity="center_vertical"
android:layout_height="20dip">
</TextView>
<TextView
android:layout_width="fill_parent"
android:layout_marginTop="5dip"
android:background="#FFFFFF"
android:layout_height="0.5sp">
</TextView>
<TextView
android:layout_width="fill_parent"
android:text="Dialog Message"
android:gravity="center_vertical"
android:layout_height="50dip">
</TextView>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="OK"
android:id="@+id/btnOk"
android:layout_gravity="center">
</Button>
</LinearLayout>
main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btnDialog"
android:text="ShowDialog">
</Button>
</LinearLayout>
CustomDialog.java:
package com.customdialog.home;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.Window;
public class CustomDialog extends Dialog
{
public CustomDialog(Context context)
{
super(context);
}
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.cdialog);
}
}
Home.java:
package com.customdialog.home;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class Home extends Activity
{
/** Called when the activity is first created. */
Button btnClick;
CustomDialog cd;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btnClick = (Button)findViewById(R.id.btnDialog);
btnClick.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View arg0)
{
cd = new CustomDialog(Home.this);
cd.show();
Button btnDimiss = (Button)cd.findViewById(R.id.btnOk);
btnDimiss.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View arg0)
{
cd.dismiss();
}
});
}
});
}
}
No comments:
Post a Comment