Create buttons on Android programmatically

4

I'm doing a project on Android and I want to add some buttons in one of my activitys, which must be added as the users create them, so I must be able to create them dynamically.

I would also like them to change color according to certain user states and I would also like them to change their position, that one or the other button appears first, as the case may be.

What Android tools do you recommend I use?

Greetings.

    
asked by manduinca 18.03.2016 в 17:39
source

1 answer

5

You must create buttons dynamically, using a custom layout for your button, you can also modify the properties of the button as required, for example you can modify the text, its color, the icon, even its position ...

This would be a code that shows what I'm saying:

    LinearLayout btnsContainer = new LinearLayout(getApplicationContext());
    btnsContainer.setLayoutParams(new LinearLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.WRAP_CONTENT));
    btnsContainer.setOrientation(LinearLayout.VERTICAL);
    btnsContainer.setGravity(Gravity.CENTER);
    //Crea botons dinamicamente.
    for (int i = 0; i < 100; i++){
        final LinearLayout buttonContainer = (LinearLayout) LayoutInflater.from(getApplicationContext()).inflate(R.layout.item_button,null);
        ImageView btnImg = (ImageView) buttonContainer.findViewById(R.id.btn_image);
        TextView btnTxt = (TextView) buttonContainer.findViewById(R.id.btn_text);
        btnTxt.setText("mi Botón no." +  i);
        btnTxt.setBackgroundColor(getRandomColor());
        btnImg.setImageResource(R.mipmap.ic_launcher);
        buttonContainer.setTag(i);

        buttonContainer.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                   Toast.makeText(getApplicationContext(), " Listener botón " + v.getTag() , Toast.LENGTH_SHORT).show();
            }
        });
        //Va agregegando botones al contenedor.
        btnsContainer.addView(buttonContainer);
    }
    //Crea contenedor para agregar contenedor de botones.
    FrameLayout.LayoutParams paramsContainer = new FrameLayout.LayoutParams(400, 1500, Gravity.CENTER);
    //Agrega contenedor con botones.
    addContentView(btnsContainer, paramsContainer);

Function to obtain random colors:

public int getRandomColor(){
   Random rnd = new Random();
   return Color.argb(255, rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256));
}

This would be the layout of the custom button item_button.xml :

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:padding="10px"
    android:orientation="vertical" >

    <ImageView
        android:id="@+id/btn_image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:contentDescription="@null"/>

    <TextView
        android:id="@+id/btn_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="12sp"
        android:textColor="#F0F0F0" />

</LinearLayout>
    
answered by 18.03.2016 / 18:56
source