Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a RelativeLayout programmatically with two buttons one on top of the other?

I'm adding two buttons to the UI, but they appear on top of one another. I want them to appear next to each other. What am I missing in this code?

m_btnCrown = new ImageButton(this); m_btnCrown.setImageResource(R.drawable.king_crown_thumb); m_btnCrown.setAlpha(100);  RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(     RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT); lp.addRule(RelativeLayout.ALIGN_PARENT_TOP);   addContentView(m_btnCrown, lp);   m_btnMonkey = new ImageButton(this); m_btnMonkey.setImageResource(R.drawable.monkey_small); m_btnMonkey.setAlpha(100);  lp = new RelativeLayout.LayoutParams(     RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT); lp.addRule(RelativeLayout.ALIGN_PARENT_TOP); lp.addRule(RelativeLayout.RIGHT_OF, m_btnCrown.getId());     addContentView(m_btnMonkey, lp); 
like image 719
Karthik Murugan Avatar asked Feb 12 '11 16:02

Karthik Murugan


People also ask

Which is better LinearLayout or RelativeLayout?

Relativelayout is more effective than Linearlayout. From here: It is a common misconception that using the basic layout structures leads to the most efficient layouts. However, each widget and layout you add to your application requires initialization, layout, and drawing.

What is RelativeLayout and LinearLayout?

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.

What is a RelativeLayout?

RelativeLayout is a view group that displays child views in relative positions. The position of each view can be specified as relative to sibling elements (such as to the left-of or below another view) or in positions relative to the parent RelativeLayout area (such as aligned to the bottom, left or center).


2 Answers

I have written a quick example to demonstrate how to create a layout programmatically.

public class CodeLayout extends Activity {      @Override     public void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);          // Creating a new RelativeLayout         RelativeLayout relativeLayout = new RelativeLayout(this);          // Defining the RelativeLayout layout parameters.         // In this case I want to fill its parent         RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(                 RelativeLayout.LayoutParams.FILL_PARENT,                 RelativeLayout.LayoutParams.FILL_PARENT);          // Creating a new TextView         TextView tv = new TextView(this);         tv.setText("Test");          // Defining the layout parameters of the TextView         RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(                 RelativeLayout.LayoutParams.WRAP_CONTENT,                 RelativeLayout.LayoutParams.WRAP_CONTENT);         lp.addRule(RelativeLayout.CENTER_IN_PARENT);          // Setting the parameters on the TextView         tv.setLayoutParams(lp);          // Adding the TextView to the RelativeLayout as a child         relativeLayout.addView(tv);          // Setting the RelativeLayout as our content view         setContentView(relativeLayout, rlp);     } } 

In theory everything should be clear as it is commented. If you don't understand something just tell me.

like image 145
Octavian A. Damiean Avatar answered Sep 18 '22 08:09

Octavian A. Damiean


Found the answer in How to lay out Views in RelativeLayout programmatically?

We should explicitly set id's using setId(). Only then, RIGHT_OF rules make sense.

Another mistake I did is, reusing the layoutparams object between the controls. We should create new object for each control

like image 26
Karthik Murugan Avatar answered Sep 18 '22 08:09

Karthik Murugan