How to open an activity in a popup window? - android

I have a ListActivity which shows list of items. I prepared another layout for detailed view that contains items' name, address, phone number and image. I want to show these items detailed in a popup window if one is clicked without closing my ListActivity.
How can i do that?

You can use AlertDialog to do this. Look here http://developer.android.com/guide/topics/ui/dialogs.html. And scroll to Creating a Custom Dialog. Example is:
AlertDialog.Builder builder;
AlertDialog alertDialog;
Context mContext = getApplicationContext();
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.custom_dialog,
(ViewGroup) findViewById(R.id.layout_root));
TextView text = (TextView) layout.findViewById(R.id.text);
text.setText("Hello, this is a custom dialog!");
ImageView image = (ImageView) layout.findViewById(R.id.image);
image.setImageResource(R.drawable.android);
builder = new AlertDialog.Builder(mContext);
builder.setView(layout);
alertDialog = builder.create();

You can use a quickAction like the Twitter app or start a new Activity with android:theme="#android:style/Theme.Dialog" specified in your manifest.

Creating dialogs is described on this page: http://developer.android.com/guide/topics/ui/dialogs.html

Related

Create AlertDialog using inside layout of a xml

I want to create a alert dialog using a layout which is in a xml. I tried this:
AlertDialog.Builder builder = new AlertDialog.Builder(this);
LayoutInflater inflater = getLayoutInflater();
View v = inflater.inflate(R.id.optionsmenu, null);
AlertDialog dialog = builder.setView(v).create();
dialog.show();
It does not work. optionsmenu is the layout that i want to use to create alert dialog. Can i set alert dialog view to this inner layout ?
here is the image's url . You can see the layout that i want to use.
I dont really know how you expect your dialog to look like but you should create it like this:
Dialog dialog = new Dialog(this);
dialog.setContentView(R.layout.activity_main);
dialog.setTitle("");
//to use a view inside the xml (i.e. a button)
Button button = (Button)dialog.findViewById(R.id.optionsmenu);
dialog.show();
Note that you were trying to inflate not the layout but the view (R.id.optionsmenu) it should have been (R.layout.activity_main)
alertDialog.builder is used to create a dialog without using a xml layout

android dialog with list and imageview

I am new to android. I have a layout that contains an imageview and a textview. I also have a list that contains some data information.
I would like to add the list in the dialog on the textview position. Imageview is set to a default image and I would like to leave it like that for the momenet. Later i will have a new list with imageview that I would like to set to my imageview from the layout.view pos.
I would like to ask how to set the list data to the textview and also how to obtain the clicked element from the dialog
I don't know how to add the list in the dialog on the text
builder = new AlertDialog.Builder(getApplicationContext());
LayoutInflater li = (LayoutInflater) getApplicationContext().getSystemService(LAYOUT_INFLATER_SERVICE);
View promptsView = li.inflate(R.layout.contact_filster_list,(ViewGroup) findViewById(R.id.layout_root));
builder.setView(promptsView);
final ImageView userInput = (ImageView) promptsView.findViewById(R.id.dialog_icon);
final TextView userInput1 = (TextView) promptsView.findViewById(R.id.dialog_text);

Android AlertDialog Multi Choice Items with customised items

Does anyone know how to customize items in AlertDialog when multi choice items are set with setMultiChoiceItems(...). I would like to change the text size for the items.
Thanks
Sure, you can use Dialog.setContentView() to set the content of a dialog to be an arbitrary layout.
Dialog dialog = new Dialog(this);
dialog.setContentView(R.layout.yourLayoutId);
dialog.show();
Make yourself a layout file with a components that you want in it and call setContentView on your dialog, passing the name of your layout file.
If you are deadset on AlertDialog you can do something similar with builder.setView()
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.yourLayoutId, (ViewGroup) findViewById(R.id.yourLayoutRoot));
AlertDialog.Builder builder = new AlertDialog.Builder(this)
.setView(layout);
AlertDialog alertDialog = builder.create();
alertDialog.show();
You can create a custom AlertDialog where you can change the text size of any element. A simple generic example: http://android-codes-examples.blogspot.in/2011/03/how-to-display-custom-dialog-and.html

How to use another layout id?

In my application I have used one alert box.. In this alert box, I give another layout to be show.. using setView() method.. I have a problem. How to use layout elements id in my activity?
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.volume, null);
builder.setTitle("Volume");
builder.setView(layout);
builder.setPositiveButton("OK", null);
builder.setNegativeButton("Cancel", null);
AlertDialog alert1 = builder.create();
alert1.show();
Something like this:
In your activity:
....
View layout = inflater.inflate(R.layout.volume, null);
.....
View some = layout.findViewById(YOUR_VIEW_ID);
.....
if you want to refer to view inside your custom layout, then do the following:
EditText editText = (EditText) layout.findViewById(R.id.your_editText);
Button editText = (Button) layout.findViewById(R.id.your_button);
.
.
.
//and so one with other views
This code shows you how to show a custom alert and get a view from that custom layout:
AlertDialog mDialog = null;
Context mContext = getApplicationContext();
final AlertDialog.Builder my_Dialog = new AlertDialog.Builder(this);
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.my_dialog, (ViewGroup) findViewById(R.id.layout_root));
ImageView imageViewInsideLayout = (ImageView) layout.findViewById(R.id.imageviewLayout);
my_Dialog.setView(layout);
mDialog = my_Dialog.create();
mDialog.show();
You can get reference to view by builder.findViewById( R.id.restart );

Android popup dialog

I'm trying to find a way to create a popup screen for some user input which includes radio button, editText, button. I don't want to start a new activity.
what would be a good option? AlertDialog? Spinner?Popup menu?
Thanks
AlertDialog would be fine for this. You can declare a layout.xml file with all of the components you'll need and then inflate it and set it as the content of your dialog.
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.yourLayoutId, (ViewGroup) findViewById(R.id.yourLayoutRoot));
AlertDialog.Builder builder = new AlertDialog.Builder(this)
.setView(layout);
AlertDialog alertDialog = builder.create();
alertDialog.show();

Categories

Resources