How to set both gravity and layout gravity of a LinearLayout programatically

Short answer Set gravity linearLayout.setGravity(Gravity.CENTER); Set layout gravity // the LinearLayout’s parent is a FrameLayout FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(400, 400); params.gravity = Gravity.TOP|Gravity.RIGHT; linearLayout.setLayoutParams(params); Background Previously, I have explained the difference between ‘gravity’ and `layout_gravity’ for views within a layout. Setting the gravity of a LinearLayout itself changes the location of the views within … Read more

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

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); }

Create Linear Layout programmability on button click in android studio

For this You Need two separate layout, one is parent and another one is child layout. In parent there will be only LinearLayout and Another view will consist a custom layout which you want to add on this. For Example follow this. layout1.xml <LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android” android:layout_width=”fill_parent” android:layout_height=”fill_parent” android:orientation=”vertical” > </LinearLayout> And some other Layout like … Read more

How to use getResource.getIdentifier() to get Layout?

With this: int layoutID = getResources().getIdentifier(“layout”+n, “layout”, getPackageName()); you basically retrieve the id of a layout file that you can inflate. It’s the dynamic version of int layoutID = R.layout.layout1; What you intend to do is retrieve a view from an already inflated layout. That’s how you’d do it: int layoutID = getResources().getIdentifier(“layout”+n, “id”, getPackageName()); … Read more

Adding LinearLayout programmatically in Android doesn’t work

LinearLayout LL = new LinearLayout(this); LL.setBackgroundColor(Color.CYAN); LL.setOrientation(LinearLayout.VERTICAL); LayoutParams LLParams = new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT); LL.setWeightSum(6f); LL.setLayoutParams(LLParams); ImageView ladder = new ImageView(this); ladder.setImageResource(R.drawable.ic_launcher); FrameLayout.LayoutParams ladderParams = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT, Gravity.BOTTOM); ladder.setLayoutParams(ladderParams); FrameLayout ladderFL = new FrameLayout(this); LinearLayout.LayoutParams ladderFLParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, 0); ladderFLParams.weight = 5f; ladderFL.setLayoutParams(ladderFLParams); ladderFL.setBackgroundColor(Color.GREEN); View dummyView = new View(this); LinearLayout.LayoutParams dummyParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,0); dummyParams.weight … Read more

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>