Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - How to add buttons programmatically depending on screen size and button below each other dynamically?

I'm experiencing a little problem to make my Android app. I want to have a responsive and dynamical application.

When the user clicks on a button, i want to add button dynamically, here is my code:

 @Override
public boolean onOptionsItemSelected(MenuItem item) {


    switch (item.getItemId()) {
        case R.id.action_ajouter:

            Button b = new Button(this);


               //numButton counts the number of buttons created by
              //clicking on a button placed in the action bar

                if (numButton % 2 != 0) {
                    //Align to the left

                    RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
                    params.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
                    b.setLayoutParams(params);
                    b.setText("Bouton n°" + numButton);
                    mainRelativeLayout.addView(b);
                    numButton++;

                } else {
                    //Align to the right
                    RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
                    params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
                    b.setLayoutParams(params);
                    b.setText("Bouton n°" + numButton);
                    mainRelativeLayout.addView(b);
                    numButton++;
                }

            break;
        default:
            break;
    }
    return true;


    }

The problem is: I need two buttons to be added side by side and the other ones blow those ones (also side by side). The problem is they are superposing them and not being added below the others.

like image 531
KaraNyyan Avatar asked Dec 09 '25 16:12

KaraNyyan


2 Answers

Your buttons overlapped each others because your layout params is not appropriate:

params.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
                b.setLayoutParams(params);
                b.setText("Bouton n°" + numBoutton);

With above code, all the buttons will align left with parent.

If you want to add buttons next to others, you should better use LinearLayout instead of RelativeLayout.

Suppose that you already have a linear layout with horizontal orientation.

LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
                b.setLayoutParams(params);
                b.setText("Bouton n°" + numBoutton);
                mainLinearLayout.addView(b);
                numBoutton++;

That's all.

Edit: Here are some code, dont know if this is your need or not.

In this app, i have a LinearLayout inside a ScrollView and a Button. When i click the Button, i add new button to the ScrollView.

The MainActivity.java

package com.tiennt.myapplication;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;

public class MainActivity extends AppCompatActivity {

    private LinearLayout llButtons;
    private int buttonCount;

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

        Button btnAdd = (Button) findViewById(R.id.btn_add);
        btnAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                doAddButton();
            }
        });
        llButtons = (LinearLayout) findViewById(R.id.ll_buttons);
    }

    private void doAddButton() {
        Button button = new Button(this);
        button.setText("Button " + ++buttonCount);
        button.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        llButtons.addView(button);
    }
}

The activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.tiennt.myapplication.MainActivity">

    <Button
        android:id="@+id/btn_add"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="ADD BUTTON"
        android:layout_gravity="center_horizontal" />

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <LinearLayout
            android:id="@+id/ll_buttons"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" />

    </ScrollView>

</LinearLayout>

Here are the result: Result

like image 150
Tien Avatar answered Dec 11 '25 05:12

Tien


Check this code :

   // first Button
            RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.id.relativelayout);
            RelativeLayout.LayoutParams param = new RelativeLayout.LayoutParams(
                    RelativeLayout.LayoutParams.WRAP_CONTENT,
                    RelativeLayout.LayoutParams.WRAP_CONTENT);
            Button b1 = new Button(this);
            b1.setText("Hello button 1");
            b1.setLayoutParams(param);
            b1.setId(1);
            relativeLayout.addView(b1);

            // second Button RIGHT_OF
            RelativeLayout.LayoutParams paramRightOf = new RelativeLayout.LayoutParams(
                    RelativeLayout.LayoutParams.WRAP_CONTENT,
                    RelativeLayout.LayoutParams.WRAP_CONTENT);
            Button b2 = new Button(this);
            b1.setText("Hello button 2");
            paramRightOf.addRule(RelativeLayout.RIGHT_OF, b1.getId());
            b2.setLayoutParams(paramRightOf);
            b2.setId(2);
            relativeLayout.addView(b1);

            // add third button BELOW
            RelativeLayout.LayoutParams paramBelow = new RelativeLayout.LayoutParams(
                    RelativeLayout.LayoutParams.WRAP_CONTENT,
                    RelativeLayout.LayoutParams.WRAP_CONTENT);
            Button b3 = new Button(this);
            b1.setText("Hello button 3");
            paramBelow.addRule(RelativeLayout.BELOW, b1.getId());
            b3.setLayoutParams(paramBelow);
            b3.setId(3);
            relativeLayout.addView(b3);
like image 23
Amit Vaghela Avatar answered Dec 11 '25 05:12

Amit Vaghela



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!