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

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

Related

Create a CardBuilder with a custom layout

Instead of creating a new CardBuilder with en Enum like CardBuilder.Layout.COLUMNS_FIXED,
I'd like to create them with custom layouts. Is this possible? What other alternative is there?
Actually you CAN embed into a CardBuilder!
View view = new CardBuilder(context, CardBuilder.Layout.EMBED_INSIDE)
.setEmbeddedLayout(R.layout.food_table)
.setFootnote("Foods you tracked")
.setTimestamp("today")
.getView();
TextView textView1 = (TextView) view.findViewById(R.id.text_view_1);
textView1.setText("Water");
This is the correct answer, people should know it is possible.
No, current version of CardBuilder doesn't support a custom layout. But there is plenty of examples on how to use custom layouts with a CardScrollAdapter inside a CardScrollView

How to overlay a small view on a Main view without fully covering main view?

So I need the view of my settings to display over my main page.where there is a "setting"button in Main page which opens this settings view. But I want My main page to be visible beneath my settings view which only covers a half or less of the main view.
I tried adding
<org.example.myCustomView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:theme="#android:style/Theme.Dialog" />
Which I found from stackoverflaw itself. But I cannot do it instead the application corrupts at button click.
I am not sure I got this too correct, Or is there any other clear way to do it?
I replaced myCustomView with my relevent class and created the manifest also but it did not work.
If there is any other alternative way to do this mention to me.
I am not talking about how to place a TextView, Button, EditText on a view
I am talking about completely two layouts.
Below image is only an example to express my question.
I think you need to utilize layoutinflater. Here is a simple example how to use it
setContentView(R.layout.yourMainView);
LayoutInflater mInflater = LayoutInflater.from(getApplicationContext());
View overView = mInflater.inflate(R.layout.yourSmallView, null);
addContentView(overView, new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT));
Use FrameLayout.FrameLayout is like container.In it place first your bluelayout and then your settings layout.It will show as if settings layout is placed on top of your blue layout.
and then hide and show your settings layout on the onclick when required.
eg:
You could use a Dialog Fragment which would be much more simpler and show more complicated stuff on UI with better responsiveness. Have a look at Dialog fragment:
http://developer.android.com/reference/android/app/DialogFragment.html

Add Buttons Into layouts one by one on serval Lines

I want to develop the Messaging application like native Application. The Problem is when i add buttons after adding two or three buttons depending on name size will become like this
My Code is as below
RelativeLayout tr = (RelativeLayout) findViewById(R.id.contacts_div);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
Button b = new Button(MainActivity.this);
b.setText(name);
b.setId(id);
tr.addView(b,params);
if(prev_id!=0)
{
params.addRule(RelativeLayout.RIGHT_OF,prev_id);
}
I want if the enough space is not available the it will add on Next Line.
i try many posts in stack overflow but fail to solve this problem
Thanks in advance
You should use a gridview for this.
try setting your button position through yout code once the text is set then you can see if the total width of your buttons is too big. Then you'll know when to place a button on the next line.
And as #Snicolas said use a GridView, the placement will be easier.
I hope this will help you.
You should use FlowLayout. It's not standart android component, so you should find implementation you like. E.g. this one is nice.

Adding table layout to AlertDialog

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.

What's the "Android way" to dynamically create Views with special styles/properties?

As mentioned in topic, I have some Views, e.g. a TableRow with always the same background used as topic, or a special TableRow containing a TextView with some special styles/properties. These Views are set dynamically, so it's problematic to use a XML for this. As I read it's not possible to set styles programmatically too. So what's the best way to solve that?
Possibility 1:
I use and instance derived Views, like this:
public class TopicTableRow extends TableRow {
public TopicTableRow(Context context) {
super(context);
setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
setBackgroundColor(Color.parseColor("#777777"));
setClickable(false);
}
}
Possibility 2:
I could create a valid xml template with a special layout I never use in the application, containing the needed Views which have already all assigned styles. Afterward I access the needed Views by R.id....
But this method seems to be very dilettante to me.
I don't think that those 2 possibilities are the "real" Android way to do this, so how is this usually done?
If you want to set specific styles for groups of elements, you can use the themes and styles concepts in android.
You can read up on them here: http://developer.android.com/guide/topics/ui/themes.html
It is not possible though to change the style attribute of a view programatically.
Therefore the android way is probably to create the Views you need in XML and use a LayoutInflater to get create an 'java' version of the xml view. This allows you to reuse the component and fill it with apropriate data for as many rows as you would like.
Button view = (Button) LayoutInflater.from(this).inflate(R.layout.textViewFromWeb, null);
I hope this will be of use to you!

Categories

Resources