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 LinearLayout Gradient Background

Ok I have managed to solve this using a selector. See code below: main_header.xml: <?xml version=”1.0″ encoding=”utf-8″?> <LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android” android:layout_width=”fill_parent” android:layout_height=”50dip” android:orientation=”horizontal” android:background=”@drawable/main_header_selector”> </LinearLayout> main_header_selector.xml: <?xml version=”1.0″ encoding=”utf-8″?> <selector xmlns:android=”http://schemas.android.com/apk/res/android”> <item> <shape> <gradient android:angle=”90″ android:startColor=”#FFFF0000″ android:endColor=”#FF00FF00″ android:type=”linear” /> </shape> </item> </selector> Hopefully this helps someone who has the same problem.

Setting a maximum width on a ViewGroup

One option which is what I did is to extend LinearLayout and override the onMeasure function. For example: public class BoundedLinearLayout extends LinearLayout { private final int mBoundedWidth; private final int mBoundedHeight; public BoundedLinearLayout(Context context) { super(context); mBoundedWidth = 0; mBoundedHeight = 0; } public BoundedLinearLayout(Context context, AttributeSet attrs) { super(context, attrs); TypedArray a = … Read more

How to add border around linear layout except at the bottom?

Create an XML file named border.xml in the drawable folder and put the following code in it. <?xml version=”1.0″ encoding=”utf-8″?> <layer-list xmlns:android=”http://schemas.android.com/apk/res/android”> <item> <shape android:shape=”rectangle”> <solid android:color=”#FF0000″ /> </shape> </item> <item android:left=”5dp” android:right=”5dp” android:top=”5dp” > <shape android:shape=”rectangle”> <solid android:color=”#000000″ /> </shape> </item> </layer-list> Then add a background to your linear layout like this: android:background=”@drawable/border” EDIT … Read more

Scrolling with Multiple ListViews for Android

Forward touch event from touched view to other views. All your views will be synchronized expand/collapsed too. OnTouchListener mOnTouch = new OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { MotionEvent newEvent = MotionEvent.obtain(event); switch(event.getAction()) { case MotionEvent.ACTION_MOVE: if(mTouched == null) { mTouched = v; } mMovingFlag = true; break; case MotionEvent.ACTION_UP: if(mMovingFlag==false) { … Read more

How to show shadow around the linearlayout in Android?

There is also another solution to the problem by implementing a layer-list that will act as the background for the LinearLayoout. Add background_with_shadow.xml file to res/drawable. Containing: <?xml version=”1.0″ encoding=”utf-8″?> <layer-list xmlns:android=”http://schemas.android.com/apk/res/android”> <item > <shape android:shape=”rectangle”> <solid android:color=”@android:color/darker_gray” /> <corners android:radius=”5dp”/> </shape> </item> <item android:right=”1dp” android:left=”1dp” android:bottom=”2dp”> <shape android:shape=”rectangle”> <solid android:color=”@android:color/white”/> <corners android:radius=”5dp”/> </shape> </item> … Read more

How to add (vertical) divider to a horizontal LinearLayout?

use this for horizontal divider <View android:layout_width=”1dp” android:layout_height=”match_parent” android:background=”@color/honeycombish_blue” /> and this for vertical divider <View android:layout_width=”match_parent” android:layout_height=”1dp” android:background=”@color/honeycombish_blue” /> OR if you can use the LinearLayout divider, for horizontal divider <?xml version=”1.0″ encoding=”utf-8″?> <shape xmlns:android=”http://schemas.android.com/apk/res/android” > <size android:height=”1dp”/> <solid android:color=”#f6f6f6″/> </shape> and in LinearLayout <LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android” android:layout_width=”match_parent” android:layout_height=”match_parent” android:divider=”@drawable/divider” android:orientation=”vertical” android:showDividers=”middle” > If you … Read more

Android: How to Programmatically set the size of a Layout

Java This should work: // Gets linearlayout LinearLayout layout = findViewById(R.id.numberPadLayout); // Gets the layout params that will allow you to resize the layout LayoutParams params = layout.getLayoutParams(); // Changes the height and width to the specified *pixels* params.height = 100; params.width = 100; layout.setLayoutParams(params); If you want to convert dip to pixels, use this: … Read more