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

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 … Read more

What are the differences between LinearLayout, RelativeLayout, and AbsoluteLayout?

LinearLayout means you can align views one by one (vertically/ horizontally). RelativeLayout means based on relation of views from its parents and other views. ConstraintLayout is similar to a RelativeLayout in that it uses relations to position and size widgets, but has additional flexibility and is easier to use in the Layout Editor. WebView to … Read more

Overlapping Views in Android

Android handles transparency across views and drawables (including PNG images) natively, so the scenario you describe (a partially transparent ImageView in front of a Gallery) is certainly possible. If you’re having problems it may be related to either the layout or your image. I’ve replicated the layout you describe and successfully achieved the effect you’re … Read more

Create a new TextView programmatically then display it below another TextView

If it’s not important to use a RelativeLayout, you could use a LinearLayout, and do this: LinearLayout linearLayout = new LinearLayout(this); linearLayout.setOrientation(LinearLayout.VERTICAL); Doing this allows you to avoid the addRule method you’ve tried. You can simply use addView() to add new TextViews. Complete code: String[] textArray = {“One”, “Two”, “Three”, “Four”}; LinearLayout linearLayout = new … Read more

How to set RelativeLayout layout params in code not in xml?

Just a basic example: RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT); params.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE); Button button1; button1.setLayoutParams(params); params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT); params.addRule(RelativeLayout.RIGHT_OF, button1.getId()); Button button2; button2.setLayoutParams(params); As you can see, this is what you have to do: Create a RelativeLayout.LayoutParams object. Use addRule(int) or addRule(int, int) to set the rules. The first method is used to … Read more

How to programmatically set the layout_align_parent_right attribute of a Button in Relative Layout?

You can access any LayoutParams from code using View.getLayoutParams. You just have to be very aware of what LayoutParams your accessing. This is normally achieved by checking the containing ViewGroup if it has a LayoutParams inner child then that’s the one you should use. In your case it’s RelativeLayout.LayoutParams. You’ll be using RelativeLayout.LayoutParams#addRule(int verb) and … Read more

Is a RelativeLayout more expensive than a LinearLayout?

In a talk at Google I/O 2013 (Writing Custom Views for Android), Romain Guy clarified the misunderstanding that caused everyone to start using RelativeLayouts for everything. A RelativeLayout always has to do two measure passes. Overall it is negligible as long as your view hierarchy is simple. But if your hierarchy is complex, doing an … Read more

Differences between ConstraintLayout and RelativeLayout

The intention of ConstraintLayout is to optimize and flatten the view hierarchy of your layouts by applying some rules to each view to avoid nesting. The Rules are similar to RelativeLayout, for example setting the bottom edge to the bottom of some other view. app:layout_constraintBottom_toBottomOf=”@+id/view1″ Unlike RelativeLayout, ConstraintLayout offers a bias value that is used … Read more