How to Create a Relativelayout Programmatically with Two Buttons One on Top of the Other

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

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

Two button one close to other at the bottom of RelativeLayout - Android

Can u modify your button layout impl. with the below sample and try.

 <LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:weightSum="1">
<Button
android:textColor="#ffffff"
android:layout_width="0dp"
android:id="@+id/back"
android:layout_weight="0.5"
android:layout_height="50dp"
android:text="@string/back"
android:layout_gravity="center"
android:layout_marginBottom="3dip"
android:onClick="goBack"/>

<Button
android:textColor="#ffffff"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_weight="0.5"
android:layout_gravity="center"
android:text="@string/stats"
android:onClick="goBack"
android:id="@+id/stats"
android:layout_marginBottom="3dip"/>
</LinearLayout>

Dynamically adding buttons next to each other - RelativeLayout

You can add 8 same size buttons using weightsum and layoutweight with LienarLayout with horizontal orientations.

see below code it may help you to add same size buttons dynamically.

  /* Add a new Linearlayout as a container for the buttons */
LinearLayout linearLayout = new LinearLayout(this);
linearLayout.setOrientation(LinearLayout.HORIZONTAL);
//Added Weight Sum 8 in LinearLayout
linearLayout.setWeightSum(8);
/* Create a new Buttons in this container, for the status bar */
//below LayoutParams define with weight 1 for buttons.
LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT, 1.0f);
Button button1 = new Button(linearLayout.getContext());
button1.setLayoutParams(param);

Button button2 = new Button(linearLayout.getContext());
button2.setLayoutParams(param);

Button button3 = new Button(linearLayout.getContext());
button3.setLayoutParams(param);

Button button4 = new Button(linearLayout.getContext());
button4.setLayoutParams(param);

Button button5 = new Button(linearLayout.getContext());
button5.setLayoutParams(param);

Button button6 = new Button(linearLayout.getContext());
button6.setLayoutParams(param);

Button button7 = new Button(linearLayout.getContext());
button7.setLayoutParams(param);

Button button8 = new Button(linearLayout.getContext());
button8.setLayoutParams(param);

Layout - one button on top of two buttons

first of all you have way to much relative layouts, much more than you need.
you can try this layout here (I removed the background and hardcoded the text for demonstration purposes)

  <RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp">

<Button
android:id="@+id/apply"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="Top Button"
android:textColor="@android:color/white"
android:textSize="15sp" />

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/apply"
android:orientation="horizontal">

<Button
android:id="@+id/discard"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Bottom Left Button"
android:textColor="@android:color/white"
android:textSize="15sp" />

<Button
android:id="@+id/save"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Bottom Right Button"
android:textColor="@android:color/white"
android:textSize="15sp" />

</LinearLayout>
</RelativeLayout>

you can set the layout margins on the outer relative layout (e.g. android:layout_margin="8dp"

the layout works like this:
in the top row there is just the top Button. Below it there is a horizontal linear layout containing two buttons with

 android:layout_width="0dp"
android:layout_weight="1"

This ensures both buttons span equally horizontal (because they have the same width). If you want the whole layout have a specific width just set the android:layout_width="match_parent" from the relative layout to android:layout_width="320dp"

add view dynamically above two already created button in relative layout in android

You can do it in this way:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="100" >
<LinearLayout
android:id="@+id/your_dinamyc_layout"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="70"
>
</LinearLayout>

<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="30"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true">

<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/btnaddtoprofile"
android:text="Add to Profile"
android:textStyle="bold"
android:textSize="20dp"
android:layout_marginTop="10dp"
android:gravity="center"/>

<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/btncancel"
android:text="Cancel"
android:textStyle="bold"
android:textSize="20dp"
android:layout_below="@+id/btnaddtoprofile"
android:gravity="center"/>
</LinearLayout>
</LinearLayout>

I have not added dinamycally an imageview I added two textviews instead of:

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

LinearLayout your_dinamic_layout=(LinearLayout)findViewById(R.id.your_dinamyc_layout);
TextView tv = new TextView(this);
tv.setGravity(Gravity.CENTER_HORIZONTAL);
tv.setText("code");
tv.setTextSize(25);
your_dinamic_layout.addView(tv);
TextView tv2 = new TextView(this);
tv2.setGravity(Gravity.CENTER_HORIZONTAL);
tv2.setText("code 2");
tv2.setTextSize(30);
your_dinamic_layout.addView(tv2);
}
}

and the result is:

enter image description here

Add a button next to another one, programmatically

You need to call setId with the videoId in the constructor of the VideoButtonView for this to work.

Make sure that setId contains a positive number, so for example if videoIds start with 0, use:

public VideoButtonView(Context context, int videoId) {
super(context);
this.setId(videoId + 1);
// other code to set layout
}


Related Topics



Leave a reply



Submit