Adding table layout to AlertDialog - android

I want to display an AlertDialog (button OK) with the content being a TableLayout.
However, I would like the TableLayout creation to be programatically, by Java, as I need to add rows depeding on some variables.
Any Idea how to do that?
Specifically:
1- Can an AlertDialog have TableLayout view, or does it only expect text so I should go with dialog?
2- How do I create TableLayout programmatically and add rows to it.
I have done it in xml but not sure in java
Help is appreciated
Thanks

Why don't you create a custom Dialog?
1 - You can instantiate a Dialog as in Dialog dialog = new Dialog(context, theme)
2 - Then you can make a dialog.setContentView(layout), find your tablelayout by id (dialog.findViewById)
3 - Then do what you need to do. I'd recommend putting this all in a separated method.

You should be able to set whatever layout you desire to the dialog using it 'setContentView' method.
You can inflate ui from an xml file. Like
LayoutInflater mInflater = LayoutInflater.from(context);
View yourView = mInflater.inflate(R.layout.your_layout, null);
alertDialog.setContentView(yourView);
The above code should work.

Related

In Android, how to I edit views in a layout, when my setContentView is not in that layout?

For example, I might be in R.Layout.activity_main ,
but when I onCreate my activity, I want to change a TextView in another layout, so I would create a TextView object and findViewById from that layout, and then change it.
But this will not work unless I set my contentview to that layout, but is it possible to be able to set the TextView in that layout, without having to setContentView to that layout?
This does not work in all situations, but you could try a LayoutInflator.
LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.your_layout, null);
Then to find views in that layout, just do...
layout.findViewById(R.id.your_view);
It is better to store a reference of desired view in a class as a variable when you create it's parent layout.Then change it's property when you need.
You can see more details in these two questions:
Android: How to declare global variables?
Android, how to access Activity UI from my class?

Creating Custom AlertDialog ? What is the root view?

what i am trying to do:
Create a custom Alert Dialog. Buttons just like any Alert Dialog but above are two TextEdit input boxes. I don't want to create a custom Dialog but a customized Alert Dialog
Here is what I am trying #3:
http://developer.android.com/guide/topics/ui/dialogs.html
It says:
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!");
builder = new AlertDialog.Builder(mContext);
builder.setView(layout);
alertDialog = builder.create();
Documentation says:
View layout = inflater.inflate(R.layout.custom_dialog,
(ViewGroup) findViewById(R.id.layout_root));
where the first parameter is the layout resource ID and the second is the ID of the root View.
Problem is I don't know what the layout root is? this is a dialog I am going to kick of in an Activity. Should I use the layout id if the activity? Is layout_root pulled out of a hat?
Also tried:
View layout = inflater.inflate(R.layout.my_custom_layout,
(ViewGroup) findViewById(android.R.id.content).getRootView());
result null pointer.
Even though an older question, this article might be useful for others who search for this answer:
Layout Inflation as Intended:
If you’ve ever written something like the following code using
LayoutInflater in your Android application:
inflater.inflate(R.layout.my_layout, null);
PLEASE read on, because you’re doing it wrong and I want to explain to
you why.
...BUT...
Every Rule Has An Exception
There are of course instances where you can truly justify a null
parent during inflation, but they are few. One such instance occurs
when you are inflating a custom layout to be attached to an
AlertDialog. Consider the following example where we want to use our
same XML layout but set it as the dialog view:
AlertDialog.Builder builder = new AlertDialog.Builder(context);
View content = LayoutInflater.from(context).inflate(R.layout.item_row, null);
builder.setTitle("My Dialog");
builder.setView(content);
builder.setPositiveButton("OK", null);
builder.show();
The issue here is that AlertDialog.Builder supports a custom view, but
does not provide an implementation of setView() that takes a layout
resource; so you must inflate the XML manually. However, because the
result will go into the dialog, which does not expose its root view
(in fact, it doesn’t exist yet), we do not have access to the eventual
parent of the layout, so we cannot use it for inflation. It turns out,
this is irrelevant, because AlertDialog will erase any LayoutParams on
the layout anyway and replace them with match_parent.
The article has an explanation on why you should supply a parent ViewGroup in most other cases than Dialog building.
Ok. The root view in the documentation refers to the element in the custom layout. So the custom layout will have an outermost view called the root view. You need to give this an Id and than you can pass it in as shown. So first argument is the custom view id, the second argument is id of the root layout element within the custom view.
View layout = inflater.inflate(R.layout.custom_dialog,
(ViewGroup) findViewById(R.id.layout_root));
So in this example given in the documentation above, R.id.layout_root refers to the id you give to say for example the outermost LinearLayout within the custom_dialog layout.
Have you tried this?
View layout = inflater.inflate(R.layout.custom_dialog,null);
builder.setView(layout);
layout.getRootView();
Should give LinearLayout.

Dynamically add UI content in android

I have an android app which asks a question followed by x number of options.
Each option contains a textview, ImageView and a radio button.
The value of x (i.e. the number of options) is not constant. I want to dynamically add UI content to satisfy this requirement.
At the moment I have written the code in the layout's xml to display a maximum of 4 options. If number of options is 2 I hide the options 3 and 4 using something like
tvoption1.setVisibility(View.GONE);
tvoption2.setVisibility(View.GONE);
However this is not very scalable. Can anyone tell me how to add options for java dynamically. Or is there a better approach?
A View can be added at runtime by using the inflater like this:
LinearLayout linearLayout = (LinearLayout)inflater.inflate(R.layout.news_categories_item, null);
TextView categoryValueTextView = (TextView)linearLayout.findViewById(R.id.news_category_item_value);
mMainLinearLayout.addView(categoryValueTextView);
In this example, a LinearLayout containing a TextView is inflated. A reference to the constituent TextView is then obtained, and the TextView is dynamically added (at runtime) to the main linear layout (mMainLinearLayout).
The inflater object may be obtained in an Activity by using getLayoutInflater().
create your row layout separately, from the main xml
Get LayoutInflater service from context:
LayoutInflater inflater=(LayoutInflater)getSystemService(LAYOUT_INFLATE_SERVICE);
use following method to addview to main xml, I assume you have parent layout llParent in xml and you want to add items in this llPaent, from list list.
for(int i=0;i<list.size();i++)
{
LinearLayout llView=(LinearLayout)inflater.inflate(R.layout.row);
//get view id and set values
TextView txt=(TextView)llView.findViewById(R.id.text);
}
A ListView is a good view for displaying several similar items. Here is a tutorial (Other views with adapters are good too, such as GridView or Gallery).
You will probably want to create your own adapter for the list, so you can display all three views (checkbox, image and text) as one item, but there are lots of examples on that available on the net as well as here on SO.

footer for custom dialog with a android default theme

I want to create context menu as follows , i am sure this is not traditional the context menu , its a kind of alert dialog which is being customized.
I tried following code but it adds the footer end of the ListView not to the dialog / context menu reference thread is ContextMenu with footer view (to add checkbox for 'make default' option)
dialog2.getListView().addFooterView(new CheckBox(this))
I have gone through the followig tutorial which has custom context menu , is it possible
to use this code to set a footer
http://www.tanisoft.net/search/label/Tutorial
I want following features , preciously the checkbox in the footer
EDIT
I reached to this part now there are only two issues
1) Dialog Title Icon
2) Dialog Bottom Blue Color ( which is a default
color of android )
and i don;t know how to achieve above two task
Here is my code to create dialog
contactDlg = new Dialog(this);
contactDlg.requestWindowFeature( Window.FEATURE_LEFT_ICON );
contactDlg.setTitle(contactStore.getContactName());
contactDlg.setContentView(R.layout.contact_dialog);
contactDlg.setFeatureDrawableResource(Window.FEATURE_LEFT_ICON, android.R.drawable.bottom_bar);
contactAdapter = new ContactAdapter(this,contactStore.getContactNumber());
modeList = (ListView) contactDlg.findViewById(R.id.contactDlgList);
modeList.setBackgroundResource(android.R.color.white);
modeList.setCacheColorHint(Color.WHITE);
modeList.setAdapter(contactAdapter);
contactDlg.show();
No need to do any thing special for this.This can be achieved easily by creating your own layout containg the list shown in this pic and the footer view in the dialog itself with thye color and style you want.Then just make the property of the dialog named "windowFrame" to be null by < item name="android:windowFrame">#null< / item>Also you can make a separate "theme.xml" in the "values" folder and define this and similar kinda properties in that file.In code, when you instantiate the dialog object, set this theme to it.The basic purpose of doing this is to achieve the total control on what to show and what not in the dialog you create.

Custom AlertDialog - problem with android.R.id.custom

I'm sure I'm missing the point here so I'm hoping someone can explain.
I want to create a popup when a user touches an ImageView. I had a look at AlertDialog and the docs say...
If you want to display a more complex view, look up the FrameLayout called "custom" and add your view to it:
...with the following code...
FrameLayout fl = (FrameLayout) findViewById(android.R.id.custom);
fl.addView(myView, new LayoutParams(MATCH_PARENT, WRAP_CONTENT));
So as a test I tried the following in my onCLick() method...
TextView tv = new TextView(this);
tv.setText("Hello World");
FrameLayout customFrameLayout = (FrameLayout) findViewById(android.R.id.custom);
customFrameLayout.addView(tv, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
The last line of the above where I'm calling addView throws a NullPointerException which makes me think there's a problem with android.R.id.custom.
So the question is, what is wrong with the above and also is there a better way of creating a custom popup (perhaps by using the Dialog class or extending it)?
NOTE: I'm only using TextView in this example as a test, I want to add something more complex for my actual popup.
One option is to create an Activity and style it using the dialog theme:
<activity android:theme="#android:style/Theme.Dialog">
See applying themes for more information.
Checkout Mossila's AlertDialog customization examples. I found them more helpful than Google's examples.
I cut-and-pasted Mossila's code directly into my project and it just worked:-) Then I made a few tweaks to meet my needs.
http://mossila.wordpress.com/2011/05/10/android-dialog-single-choice-items/
I think your problem is because you dont "inflate" the layout. With a FrameLayout you need to use the LayoutInflater
use the following code:
LayoutInflater.from(context).inflate(android.R.id.custom, this, true)
This should work with FrameLayout. Read up more about this at the Android Layout tricks page
Also check out LayoutInflater
edit: i have noticed aswell that there is an identical article to this problem here too: How to implement a custom AlertDialog View

Categories

Resources