combining wrap_content on parent and fill_parent on child

In theory what you are describing should not work (“Because it the parent gets it’s height from the childs and vice-versa”.) However, we made it work in LinearLayout because it was a very common use case. I recently added similar support to FrameLayout (this feature should be part of Honeycomb.) What you are doing is … Read more

selected item on custom listview with contextual action bar

I’ve only ever tested this in CHOICE_MODE_SINGLE, but in that situation it works by doing the following. When you select a list item, in code, call “setItemChecked(position, checked)” method (on the ListView instance) for that item in the list. Add this to the XML for individual ListView items: android:background=”?android:attr/activatedBackgroundIndicator”

Scale background image to wrap content of layout

I had the same problem (I think), and the only solution I could find was this: <RelativeLayout android:layout_width=”match_parent” android:layout_height=”wrap_content” > <View android:layout_width=”wrap_content” android:layout_height=”wrap_content” android:background=”@drawable/some_image” android:layout_alignTop=”@+id/actual_content” android:layout_alignBottom=”@id/actual_content” android:layout_alignLeft=”@id/actual_content” android:layout_alignRight=”@id/actual_content” /> <LinearLayout android:id=”@id/actual_content” android:layout_width=”match_parent” android:layout_height=”wrap_content” android:layout_alignParentTop=”true” > <!– more stuff … –> </LinearLayout> </RelativeLayout>

Creating LinearLayout Programmatically/Dynamically with Multiple Views

You want that hierarchy programmatically. – LinearLayout(horizontal) – ImageView – LinearLayout(vertical) – TextView – TextView – TextView – TextView Ok lets start with Parent LinearLayout LinearLayout parent = new LinearLayout(context); parent.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT)); parent.setOrientation(LinearLayout.HORIZONTAL); //children of parent linearlayout ImageView iv = new ImageView(context); LinearLayout layout2 = new LinearLayout(context); layout2.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT)); layout2.setOrientation(LinearLayout.VERTICAL); parent.addView(iv); parent.addView(layout2); //children … Read more

Android: ClassCastException when adding a header view to ExpandableListView

Ok, I figured this one out. I got rid of the runtime error by programatically setting the View’s LayoutParams to ListView LayoutParams, like so: headerView.setLayoutParams(new ListView.LayoutParams(ListView.LayoutParams.FILL_PARENT, ListView.LayoutParams.WRAP_CONTENT)); before adding the view. The reason being is found in the Android docs: http://developer.android.com/reference/android/view/View.html#setLayoutParams(android.view.ViewGroup.LayoutParams) which states that: These supply parameters to the parent of this view specifying how … Read more

Android LinearLayout fill-the-middle

Turned out (Thanks Mark Murphy for the answer) that all I was looking for was to set middle row to layout_height=”0px” and layout_weight=”1″ If, after all the wrap_content and fixed-sized items are allocated for along an axis (horizontal or vertical), there is still room on that axis left over, LinearLayout then allocates the remaining space … Read more

How to set Ripple effect on a LinearLayout programmatically?

You can use this way. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { // If we’re running on Honeycomb or newer, then we can use the Theme’s // selectableItemBackground to ensure that the View has a pressed state TypedValue outValue = new TypedValue(); this.getTheme().resolveAttribute(android.R.attr.selectableItemBackground, outValue, true); textView.setBackgroundResource(outValue.resourceId); }