android layout margins with percentage

You can have invisible Views in your LinearLayouts as spacers and use the layout_weight mechanism to assign them relative size. Example: <LinearLayout android:layout_width=”match_parent” android:layout_height=”match_parent” android:layout_weight=”1″> <Thumbnail android:id=”@+id/thumb1″ android:layout_width=”0dp” android:layout_height=”match_parent” android:layout_weight=”4″ /> <View android:layout_width=”0dp” android:layout_height=”0dp” android:layout_weight=”2″ android:visibility=”invisible”/> <Thumbnail android:id=”@+id/thumb2″ android:layout_width=”0dp” android:layout_height=”match_parent” android:layout_weight=”4″ /> </LinearLayout>

ScrollView not scrolling at all

Answer: the ScrollView is not working when used as the root element of an XML layout. It has to be wrapped inside a LinearLayout. Solution then : <LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android” android:layout_width=”match_parent” android:layout_height=”match_parent” > <ScrollView android:id=”@+id/scroll_view” android:layout_width=”match_parent” android:layout_height=”match_parent” android:fillViewport=”true” > <LinearLayout android:id=”@+id/scroll_layout” android:layout_width=”match_parent” android:layout_height=”wrap_content” android:orientation=”vertical” > </LinearLayout> </ScrollView> </LinearLayout>

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

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)