Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding LinearLayout programmatically in Android doesn't work

Tags:

The hierarchy is like this:

  • RelativeLayout
    • LinearLayout (vertical)
      • FrameLayout (weight 5)
        • ImageView
      • View (weight 1)

The View is just a dummy view for spacing purpose. I did it in the layout xml and it works. But when I want to do it programmatically, the following codes do not work.

LinearLayout LL = new LinearLayout(this); ImageView ladder = new ImageView(this); FrameLayout ladderFL = new FrameLayout(this); View dummyView = new View(this); ladder.setImageResource(R.drawable.ladder1); LL.setOrientation(LinearLayout.VERTICAL); LayoutParams LLParams = new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT); LinearLayout.LayoutParams ladderFLParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, 0); ladderFLParams.weight = 5f; LinearLayout.LayoutParams dummyParams = new LinearLayout.LayoutParams(0,0); dummyParams.weight = 1f;  FrameLayout.LayoutParams ladderParams = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT, Gravity.BOTTOM); ladder.setLayoutParams(ladderParams); ladderFL.setLayoutParams(ladderFLParams); dummyView.setLayoutParams(dummyParams); LL.setWeightSum(6f); LL.setLayoutParams(LLParams);  ladderFL.addView(ladder); LL.addView(ladderFL); LL.addView(dummyView); ((RelativeLayout) findViewById(R.id.screenRL)).addView(LL); 
like image 328
Tommy Avatar asked Sep 28 '13 10:09

Tommy


People also ask

How do I install LinearLayout?

To create a linear layout in which each child uses the same amount of space on the screen, set the android:layout_height of each view to "0dp" (for a vertical layout) or the android:layout_width of each view to "0dp" (for a horizontal layout). Then set the android:layout_weight of each view to "1" .

What is the difference between Framelayout and LinearLayout in Android?

Frame Layout: This is designed to block out an area on the screen to display a single item. Linear Layout: A layout that arranges its children in a single column or a single row. Relative Layout: This layout is a view group that displays child views in relative positions.

What is RelativeLayout and LinearLayout in Android?

Android Layout TypesLinearLayout : is a ViewGroup that aligns all children in a single direction, vertically or horizontally. RelativeLayout : is a ViewGroup that displays child views in relative positions. AbsoluteLayout : allows us to specify the exact location of the child views and widgets.


1 Answers

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 = 1f;     dummyView.setLayoutParams(dummyParams);     dummyView.setBackgroundColor(Color.RED);        ladderFL.addView(ladder);     LL.addView(ladderFL);     LL.addView(dummyView);     RelativeLayout rl=((RelativeLayout) findViewById(R.id.screenRL));     rl.addView(LL); 

I have just arranged your code for better understanding, also gave it a background color to get the clear picture as I dont know what you want, you can go through it. I Hope it is helpful. You should provide your working xml so we know exactly what you want.

like image 117
Piyush Avatar answered Oct 03 '22 22:10

Piyush