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);
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.
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.
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).
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With