I found some similar questions here, but all of their were either really confusing for such a newbie like me or didn't help to solve my problem:
I have a TextView tv. By click on it I want to display a custom AlertDialog with EditText + Cancel- and Update-Buttons. By click on Update-Button of my Dialog I want to replace the text of the same TextView (tv) with the value of EditText from Dialog. Here is one of my attepmts:
...
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final TextView tv̶̶C̶̶l̶̶i̶̶c̶̶k̶̶e̶̶d̶ = (TextView) findViewById(R.id.tv_id);
tv.setText("Initial value");
tv.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
UpdateDialogFragment updDiag = new UpdateDialogFragment();
updDiag.show(getFragmentManager(), "dialog");
tv.setText(updDiag.value); // I try to get the value of EditText like this, but it doesn't work
}
});
}
public class UpdateDialogFragment extends DialogFragment {
String value; // I try to get the value of EditText like this, but it doesn't work
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = getActivity().getLayoutInflater();
builder.setView(inflater.inflate(R.layout.edit_tv, null))
.setPositiveButton("Update", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int id) {
EditText et = (EditText) UpdateDialogFragment.this.getDialog().findViewById(R.id.et_tv);
value = et.getText().toString(); // I try to get the value of EditText like this, but it doesn't work
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
UpdateDialogFragment.this.getDialog().cancel();
}
});
return builder.create();
}
}
And my edit_tv.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="#+id/et_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:layout_marginTop="40dp"
android:hint="Enter text ..."
android:layout_gravity="center_horizontal">
<requestFocus />
</EditText>
</LinearLayout>
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.yev.tabletasting.MainActivity">
<TextView
android:id="#+id/tv_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
In my case the Text of tv will be setted to "" (probably null) after click on "Update".
I would be thankful for every advice, thanking you in anticipation!
This is what you should have in your TextView onClickListener:
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Write new value for TextView");
final EditText editText = new EditText(this);
builder.setView(editText);
builder.setPositiveButton("Update", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int id) {
}
});
AlertDialog dialog = builder.create();
dialog.show();
dialog.getButton(DialogInterface.BUTTON_POSITIVE).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String newValue = editText.getText().toString();
if(newValue.lenght() == 0)
Toast.makeText(this, "You need to type something in the editText", Toast.LENGTH_LONG).show();
}else{
tv.setText(newValue);
dialog.dismiss();
}
});
Maybe some minor changes need to be done because I wrote the code down in here. Hope it helps. Wish you luck :)
LE: you don't need the extra Fragment and xml files for the dialog editText.
Okay, so you haven't really mentioned as to why you prefer to use the DialogFragment rather than just an AlertDialog as what Victor Holotescu answered. But here goes, I tried out your code and managed to receive a NullPointerException when I try to click the Update Button. So I looked at it and modified the code, here it is:
MainActivity
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final TextView tv = (TextView) findViewById(R.id.tv_id);
tv.setText("Initial value");
tv.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
UpdateDialogFragment updDiag = new UpdateDialogFragment().newInstance(tv); // Passed the TextView here
updDiag.show(getFragmentManager(), "dialog");
}
});
}
public static class UpdateDialogFragment extends DialogFragment {
String value; // I try to get the value of EditText like this, but it doesn't work
TextView tvToEdit;
public UpdateDialogFragment newInstance(TextView tvToEdit){
UpdateDialogFragment udf = new UpdateDialogFragment();
udf.tvToEdit = tvToEdit;
return udf;
}
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = getActivity().getLayoutInflater();
builder.setView(inflater.inflate(R.layout.edit_tv, null))
.setPositiveButton("Update", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int id) {
EditText et = (EditText) UpdateDialogFragment.this.getDialog().findViewById(R.id.et_tv);
value = et.getText().toString();
tvToEdit.setText(value);
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
UpdateDialogFragment.this.getDialog().cancel();
}
});
return builder.create();
}
}
}
I added some TODO comments (labeled with Read comments below. :)) inside the code, check them out. I tested it and it runs properly. Hope this was able to help you in some way. For more information regarding AlertDialogs and DialogFragments, here's a good post -- DialogFragment advantages over AlertDialog.
EDIT
Okay. So I edited it where you just pass a TextView to the UpdateDialogFragment by creating a newInstance method -- referenced it here SetText of TextView in DialogFragment from Calling Activity -- Hope this helps. :)
Related
after doing a lot of research and trying (i think) every lifecycle method i could find, i´m really stuck:
What i want: Basically i have this DialogFragment ("ProfileUsernameDialogFragment")which should present my layout file ("fragment_profile_header_username_dialog").
in this layout file i have some TextViews and some EditTexts. I want to set the text of these EditTexts to some value i obtain in my programm, so setting it beforehand is not possible.
What i have:
public class ProfileUsernameDialogFragment extends DialogFragment {
...
#Override
public void onActivityCreated(Bundle savedInstanceState) {
EditText usernameText = (EditText) this.getView().findViewById(R.id.username_field);
usernameText.setText("TEST");
}
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
Log.d(AppController.LOG_STRING, this.getClass().toString() + " - onCreateDialog - START");
AlertDialog.Builder builder = new AlertDialog.Builder(this.getActivity());
View view = this.getActivity().getLayoutInflater().inflate(R.layout.fragment_profile_header_username_dialog, null);
builder.setView(view);
builder.setPositiveButton(SAVE_SETTINGS, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int whichButton) {
// Tried this too:
// EditText usernameText = (EditText)
// ProfileUsernameDialogFragment.this.getView().findViewById(R.id.forename_label);
// usernameText.setText("TEST");
}
});
builder.setNegativeButton(ABORT_ACTION, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int whichButton) {
dialog.dismiss();
}
});
builder.setTitle(CHANGE_NAME_SETTINGS);
Dialog dialog = builder.create();
return dialog;
}
}
What i get: is a NullPointerException, no matter which position for the findViewById(R.id.username_field) i tried.
(I also tried onCreate and some other methods, i start thinking i might have another problem than the code location, but i really hav no clue) I tried to follow at least 6 similar questions about NullPointers in DialogFragments so i guess i need advanced help ^^
i should add i´m completely new to android so please be nice, but convention tipps etc. are very welcome! thx in advance :)
Following is what you need:
mAlertDialog.setOnShowListener(new DialogInterface.OnShowListener() {
#Override
public void onShow(final DialogInterface dialog) {
final Button b = mAlertDialog.getButton(DialogInterface.BUTTON_POSITIVE);
b.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(final View view) {
v.findViewById(R.id...)
}
});
}
});
});
}
});
Another option in addition to #David's answer is to use the onStart() method.
It looks like you were thinking along these lines in your question, except you used the onActivityCreated() method.
So for example:
#Override
public void onStart()
{
super.onStart();
EditText usernameText = (EditText) this.getDialog().findViewById(R.id.username_field);
usernameText.setText("TEST");
}
Android documentation has good example
So, if You wont set text in Your layout You need set it in onCreateView() method, for example:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup cont,
Bundle savedInstanceState) {
// Inflate the layout to use as dialog or embedded fragment
View view = inflater.inflate(R.layout.app_info, cont, false);
TextView title = (TextView)view.findViewById(R.id.dialog_header);
TextView content = (TextView)view.findViewById(R.id.dialog_content);
Button button = (Button)view.findViewById(R.id.dialog_button);
title.setText(this.dialogTitle);
content.setText(this.dialogContent);
button.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view){
getDialog().dismiss();
}
});
return view;
}
The original code has been deleted, the new working code is shown. The idea behind the code is to create a new textView within a layout that has a custom name to it that the user provides. Previously, a NPE error was happening. This is a fix. Any questions, please feel free to ask.
EDIT: Found the solution
The fix needs to be as followed:
accountEdit = new EditText(this); // accountEdit needs to be a global variable
then within the builder.setPositiveButton
builder.setPositiveButton(R.string.btn_save, new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dInterface, int whichButton)
{
LinearLayout lineLayout = (LinearLayout)findViewById(R.id.linear_layout);
String newAccountName = accountEdit.getText().toString();
newTextView = new TextView( getBaseContext() );
newTextView.setVisibility(View.VISIBLE);
newTextView.setText( newAccountName );
newTextView.setId(id);
newTextView.setTextSize(35);
newTextView.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
onClickNew(view);
}
});
newTextView.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View view) {
Toast.makeText(getBaseContext(), "Testing" , Toast.LENGTH_LONG).show();
return true;
}
});
This will create the button, as well as set the name of the button to the information that is in the EditText within the Dialog Box. Previously, the EditText was from another activity, and was being called wrong, which caused the NPE. Thank you for all the help.
As Wenhui metioned, you call the finViewById inside the onclick listener of the button, so the wrong context is used. Do it like in the following example:
final EditText accountEdit = (EditText)findViewById(R.id.newAccountButton);
final String newAccountName = accountEdit.getText().toString();
final LinearLayout lineLayout = (LinearLayout)findViewById(R.id.linear_layout);
builder.setPositiveButton(R.string.btn_save, new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dInterface, int whichButton)
{
newTextView = new TextView(getBaseContext());
newTextView.setVisibility(View.VISIBLE);
newTextView.setText("Test");
newTextView.setId(id);
newTextView.setTextSize(35);
newTextView.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
onClickNew(view);
}
});
lineLayout.addView(newTextView);
id++;
}
});
i am new to developing apps for android and i want to create a simple Conterter app, just for the start. In my view i have a edittext and a Button. If i click the button, it will open a AlertDialog with list of strings. I cant figure out how to manage this: When i click on one item in the AlertView i want to set the text of the button to the selected string and dismiss the AlertDialog. Can somebody please help me ?
public class VypocetDlzkyActivity extends Activity {
EditText HodnotaDlzka;
Button prevodDlzkaZtlacidlo;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_vypocet_dlzky);
}
public void zmenPrevodZ(View view){
final String[] jednotkyDlzky = {"milimeter", "centimeter", "decimeter", "meter", "kilometer", "svetelny rok"};
AlertDialog.Builder builder = new AlertDialog.Builder(VypocetDlzkyActivity.this);
builder.setTitle("Vyberte jednotku");
builder.setItems(jednotkyDlzky,null);
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
String value = jednotkyDlzky[item].toString();
prevodDlzkaZtlacidlo.setText(value);
dialog.cancel();
}
};
final AlertDialog alert = builder.create();
alert.show();
}
You need to set the values of these 2 member variables in your onCreate() method, like this:
HodnotaDlzka = (EditText)findViewById(R.id.xxxx);
prevodDlzkaZtlacidlo = (Button)findViewById(R.id.yyyy);
xxxx is the ID you gave the EditText in activity_vypocet_dlzky.xml and yyyy is the ID you gave to the Button.
Also, after a button is clicked in the AlertDialog, the dialog is automatically dismissed, so you don't need to call dialog.cancel().
Problem is you didnt add any onClick listnerz.On clicking on button you need to call the required method.
public class MainActivity extends Activity implements OnClickListener {
EditText HodnotaDlzka;
Button prevodDlzkaZtlacidlo;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
HodnotaDlzka = (EditText) findViewById(R.id.e1);
prevodDlzkaZtlacidlo = (Button) findViewById(R.id.b1);
prevodDlzkaZtlacidlo.setOnClickListener(this);
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
final String[] jednotkyDlzky = {"milimeter", "centimeter", "decimeter", "meter", "kilometer", "svetelny rok"};
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("Vyberte jednotku");
builder.setItems(jednotkyDlzky,new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
String value = jednotkyDlzky[item].toString();
prevodDlzkaZtlacidlo.setText(value);
}
});
enter code here
final AlertDialog alert = builder.create();
alert.show();
}
}
I have searched for the answer and dont find it, so please help me :)
I have a custom class:
public class CustomClass {
private final Context ctx;
public CustomClass(Context ctx) {
this.ctx = ctx;
}
public boolean setDialog(int head, int text) {
final boolean value;
final Dialog d = new Dialog(ctx);
d.requestWindowFeature(Window.FEATURE_NO_TITLE);
d.setContentView(R.layout.custom2_dialog);
TextView txtHead = (TextView) d.findViewById(R.id.custom2_txtHead);
txtHead.setText(ctx.getResources().getString(head));
TextView txtText = (TextView) d.findViewById(R.id.custom2_txtText);
txtText.setText(ctx.getResources().getString(text));
Button btnOK = (Button) d.findViewById(R.id.custom2_btnOK);
btnOK.setText("OK");
btnOK.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
value = true;
d.dismiss();
}
});
Button btnNO = (Button) d.findViewById(R.id.custom2_btnNO);
btnNO.setText("NO");
btnNO.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
value = false;
d.dismiss();
}
});
d.show();
return value;
}
}
You can see that i have a custom dialog created in my custom class because i dont want to create in every activity a dialog. Now when i use it in an Activity:
CustomClass cC = new CustomClass(this);
if(cC.setDialog(R.string.head, R.string.text)) {
// user checked OK
} else {
// user checked NO
}
How to know if user checked OK or NO, because the return true, false value dont work in custom class, the dialog wont wait before the user clicks, it automatic return a value.
Here is the solution to your question.
First of all, if you are creating the Custom dialog then you have to to extend the Dialog class to read the response and implement the OnClickListener.
class CustomizeDialog extends Dialog implements OnClickListener
{
// some code
public CustomizeDialog(Activity activity, String title, String msg, int i, Typeface typeface)
{
super(activity);
this.activity = activity;
intFlag = i;
setContentView(R.layout.relative);
dialogButtonYes = (Button) findViewById(R.id.buttonCustomDialogYes);
dialogButtonNo = (Button) findViewById(R.id.buttonCustomDialogNo);
textView = (TextView) findViewById(R.id.textViewCustomTitle);
this.msg = msg;
dialogButtonNo.setOnClickListener(this);
dialogButtonYes.setOnClickListener(this);
}
}
#Override
public void onClick(View v) {
if (v == dialogButtonYes)
{
Intent intent =new Intent(activity, CallForOne.class);
activity.startActivity(intent);
dismiss();
}
else
dismiss();
}
else if(intFlag == 2)
{
if (v == dialogButtonYes)
{
Intent intent =new Intent(activity, CallMe.class);
activity.startActivity(intent);
dismiss();
}
else
dismiss();
}
And the relative.xml file
<TextView
android:id="#+id/textViewCustomTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/textViewCustomTitle"
android:orientation="horizontal"
android:paddingBottom="1.0dip"
android:paddingLeft="4.0dip"
android:paddingRight="4.0dip"
android:paddingTop="5.0dip" >
<Button
android:id="#+id/buttonCustomDialogYes"
style="#style/styleNormalButton"
android:layout_width="0.0dp"
android:layout_height="fill_parent"
android:layout_weight="1.0"
android:text="#string/yes" />
<Button
android:id="#+id/buttonCustomDialogNo"
style="#style/styleNormalButton"
android:layout_width="0.0dp"
android:layout_height="fill_parent"
android:layout_weight="1.0"
android:text="#string/no" />
</LinearLayout>
And the main class
class Main implements OnClickListener
{
onCreate()
{
//your code
}
public void on click()
{
customizeDialog = new CustomizeDialog(CustomDialogExample.this,getString(R.string.title_for_one),getString(R.string.msg_for_one),1,"String");
customizeDialog.show();
}
}
I was facing the same problem here, then I found one solution, to open an Activity from button click in a custom dialog. But you can use this logic for another actions too.
My custom method:
public class Extension extends Activity{
public void showDialogConfirma(Activity localActivity, Class destActivity, String titulo, String mensagem){
final Dialog dialog = new Dialog(localActivity);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.dialog_um_btn);
TextView textTitulo = dialog.findViewById(R.id.txtTitle);
textTitulo.setText(titulo);
Button btnOk = dialog.findViewById(R.id.btnOk);
btnOk.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
dialog.dismiss();
// here I can open any activity from any class
localActivity.startActivity(new Intent(localActivity, destActivity));
}
});
dialog.show();
}
}
You call this method like this:
Extension extension = new Extension();
// here you gonna pass your parameters to your Dialog on create it
// on my case the activity that I want open
extension.showDialogConfirma(ThisActivity.this, DestActivity.class, titulo, mensagem);
First of all the comment from #user370305 is the way to do it right. There can be other ways no doubts about that.
The problem you are facing is because when you are calling a method of custom class
if(cC.setDialog(R.string.head, R.string.text));
you are getting the value even though nothing has been clicked
return value;
as false which is the default value set when you are declaring
final boolean value;
Instead you should listen for the Cancel and Ok clicks
I've been playing around with alert dialogs. I want to show a dialog that shows particular information about a list item in listview. Just like the android's file manager's detail dialog.
Picture: https://dl.dropbox.com/u/20856352/detailsbox.jpg
Interesting thing about this Details dialog is that it shows list items which are very similar to Preference item in a Preferences Screen. They can be clicked upon, they're showing a very nicely laid out two-line item listitem.
I need to create a similar dialog box but I've no clue how to accomplish this. I've played around a bit. Preference XML cannot be used as alertdialog's layout. And I'm unable to develop a layout that looks similar to the above pic. Need help / guideline how to achieve this.
Faraz Azhar
You probably don't want to use a custom dialog because it will be difficult to replicate the look of the AlertDialog. An AlertDialog can display a list of items using AlertDialog.setListAdapter. You can customize the list of items to show two rows of text per item by using a custom implementation of ListAdapter. The attached screenshot was produced by the below code and xml.
public class Temp extends Activity
{
private String[] listItemsFirstRow = {"item 1", "item 2", "item 3"};
private String[] listItemsSecondRow = {"item 1", "item 2", "item 3"};
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setAdapter(new MyAdapter(), null);
builder.setTitle("Title");
builder.setPositiveButton(android.R.string.ok, new OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
}
});
builder.show();
}
class MyAdapter extends BaseAdapter
{
#Override
public int getCount()
{
return listItemsFirstRow.length;
}
#Override
public Object getItem(int position)
{
//this isn't great
return listItemsFirstRow[position];
}
#Override
public long getItemId(int position)
{
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
if(convertView == null)
{
convertView = getLayoutInflater().inflate(R.layout.main, null);
}
((TextView)convertView.findViewById(R.id.text1)).setText( listItemsFirstRow[position]);
((TextView)convertView.findViewById(R.id.text2)).setText( listItemsSecondRow[position]);
return convertView;
}
}
}
main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:minHeight=![enter image description here][2]"?android:attr/listPreferredItemHeight"
android:orientation="vertical"
android:gravity="center_vertical"
android:paddingLeft="15dip"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:textAppearance="?android:attr/textAppearanceLarge"
android:id="#+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="?android:attr/textColorSecondary"
android:id="#+id/text2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
Just made your xml file same as regular screen/page
then put this code on your onCreate()
AlertDialog.Builder builder;
LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.toast_info,
(ViewGroup) findViewById(R.id.toast_layout_root));
builder = new AlertDialog.Builder(this);
builder.setView(layout);
alertDialog = builder.create();
which r.layout.toast_info is your xml file
and r.id.toast_layout_root is your root xml id (eg. '<'linearlayout android:id="+#id...."'>' )
and when you want to show it just write this line
alertDialog.show();
My suggestion would be to use an Activity as a dialog. This way it is pretty easy to create a custom dialog. Here is a small example which I think you can build upon.
**Activity**
public class CustomDialogEx extends Activity implements OnClickListener {
private Button button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.your_layout);
button = (Button) findViewById(R.id.button1);
button.setOnClickListener(this);
}
#Override
public void onClick(View v) { // pass your string data via this intent to the custom view
// show the custom dialog
Intent i = new Intent();
// i.putExtra(<your key/value pairs here>
i.setClass(this, DialogActivity.class);
startActivity(i);
}
}
****************************************************************************
**Custom Dialog**
// The Activity will serve as the Dialog
public class DialogActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.your_dialog_layout);
Intent i = new Intent();
Bundle b = getIntent().getExtras();
b.getString(<your key>)
}
}
*****************************************************************************
**AndroidManifest**
<activity
android:name="DialogActivity"
android:configChanges="keyboardHidden|orientation"
android:theme="#android:style/Theme.Dialog" >
</activity>
this may also help you
public class ShareDialog extends Dialog implements android.view.View.OnClickListener{
Context mcontContext;
Button btnok;
Listview lstview;
public ShareDialog(Context context) {
super(context);
mcontContext= context;
//pls replace with your dialog.xml file
setContentView(R.layout.sharedialog);
bindComponent();
addListener();
}
private void bindComponent() {
// TODO Auto-generated method stub
lstview=(Listview) findViewById(R.id.lstdetail);
btnok=(Button) findViewById(R.id.btnok);
//bind here listview with your adpter
}
private void addListener()
{
btnshareviwifi.setOnClickListener(this);
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btnok:
dismiss();
break;
default:
break;
}
}
}
and where you want to show
ShareDialog shobje=new ShareDialog (context);
shobje.show()