Create a CardBuilder with a custom layout - android

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

Related

Better to inflate or instantiate controls in Android?

I'm wondering if anyone can shed some insight as to the best practice for dynamically creating controls (inflate vs instantiate).
Inflate:
TextView styledText = (TextView)inflater.inflate(R.layout.styledTextView);
Instantiate:
TextView styledText = new TextView(mContext);
styledText.setTextAppearance(R.style.StyledTextStyle);
The object being created can either contain attributes in the inflated XML file, or be contained in a Style definition which is added to the instantiated object afterwards. (Assume that this styling includes width, background, text color, etc).
Haven't been able to run any time/memory tests of each method, was wondering if anyone knew which was quickest/most efficient.
LayoutInflator has a slight overhead because it has to parse xml in order to build the object. It also temporarily takes more memory for the same reason. Other than that, it builds the View object in the same manner that you would anyway. It may be something to worry about if you call it hundreds of times a second for some reason. 99.9% of the time though you'll never know the difference.
Also to note, any method that accepts an xml resource like "setTextAppearance" will have the same xml parsing overhead. The only difference in the examples you provided is it's not parsing the TextView xml, but it would still have to parse the style attributes.
Though this post asks about controls specifically, I think it's relevant to note that .. for working with a layout you want to dynamically create/add, I found in using the new (aka instantiate) approach , I was not able to get a reference to an inner ImageButton element that was defined in the xml file for which I instantiate the layout object reference.
When I use the inflate approach, the ImageButton was present upon reference.
So in my case:
Works :)
LayoutInflater inflater = LayoutInflater.from(getActivity());
CardView myCardView = (CardView) inflater.inflate(R.layout.my_cardview, null);
ImageView icon = (ImageView) myCardView.findViewById(R.id.iconId);
~~~~~~~~~~~~
Don't Work :( .. variable icon is null in this case
CardView myCardView = new CardView(getActivity());
ImageView icon = (ImageView) myCardView.findViewById(R.id.iconId);

User Interface Design in Android

I want to design the layouts programatically that means without the xml file as per project requirement.
But the terms used programatically is completely different from xml file.Is their any useful tutorial to learn programatically that means without xml file. guide me!
you can create any view you want
a linear layout
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.FILL_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
A Text View
final TextView tv = new TextView(this);
tv.setBackgroundColor(0xFFFF00FF);
tv.setTextColor(0xFF000000);
tv.setTypeface(null, Typeface.BOLD);
tv.setText("Where is Fred?");
tv.setGravity(Gravity.CENTER_HORIZONTAL);
and anything else.
Source
I advise you to spend some time learning about the View class and its popular subclasses such as LinearLayout, RelativeLayout, and so on. (Also, spend a bit of time looking at Drawables.)
When you're creating a layout using XML, you're using XML to define a hierarchy of View objects which, at runtime, are "inflated" into a real hierarchy of View objects that the XML layout file describes. For example, your first XML layout file might be a simple LinearLayout that contains a TextView (note I'm simplifying it for brevity):
<LinearLayout ... >
<TextView ... />
</LinearLayout>
In your Activity you would use this layout using setContentView().
All that XML file is doing is providing a specification, if you like, about the View structure that the system needs to build (or inflate) for you. The end result is that there will be a real LinearLayout object (which is a subclass of View) that has a reference a child TextView (again a subclass of View) together with suitable layout parameters.
To do the above programmatically (i.e. by creating instances of objects and using their methods, rather than inflating from XML) you might do something like (again simplified):
LinearLayout container = new LinearLayout(this);
TextView tv = new TextView(this);
tv.setText("hello");
container.addChild(tv); // Simple example - usually you'd specify layout parameters
setContentView(container);
The basic point I'm making is that, in really simple terms, a layout XML file can be thought of as a kind of 'script' that you can use to tell the system how to create a hierarchy of Views. You can create exactly the same result by programmatically creating instances of View objects and calling appropriate methods on them. Whichever route you take, the end result is the same: a bunch of View objects in memory that represent a View hierarchy.
What you will find is that XML layout attribute names aren't necessarily the same as the corresponding method names, but you can use the relevant API documentation to see what corresponding XML attribute strings are for given methods. For instance, the API documentation for LinearLayout details all of the methods as well as the XML attributes.

How to Bind data to layout objects in Android

I want to create an activity that show informations about a deal, i know how to use JSONParser to retrieve date (like texts and images) in Android but i don't know how to attach these data to the layout tha i've created which is a complicated layout
You need to access TextViews ImageViews etc. which you created in your Layout.
To do so you need to do something like this.
For example if this is your layout object.
<TextView android:id="#+id/my_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Dynamic content gonna get me."/>
You can access it in your calling activity like this.
TextView textView = (TextView) findViewById(R.id.my_textview);
t.setText("Whatever Dynamic data comes from your source");
Also for ImageView you need to do pretty similar stuff.
Assuming that you have your ImageView declared in your layout.
ImageView img = (ImageView) findViewById(R.id.my_imageview);
img.setImageResource(R.drawable.random_image);
Just adding the answer here as I got confused initially after reading the question.
You can attach custom data to Android views using the setTag method. You can store any Object as a tag. To retrieve data use view.getTag and cast it to your stored data type.
There seems to be a nice binding framework for Android:
http://code.google.com/p/android-binding/wiki/ContactManagerDemo
http://www.codeproject.com/Articles/145203/Android-Binding-Introduction
Going to try it now :)

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!

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