0

Here is the code using layout resource file:

<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:clipChildren="false"
          android:clipToPadding="false"
          tools:context=".MainActivity">


<LinearLayout
    android:id="@+id/layout1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:clipChildren="false"
    android:clipToPadding="false"
    >

    <Button
        android:id="@+id/im11"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:src="@drawable/munshee_logo"
        android:padding="1dp"
        android:scaleType="centerCrop"
        android:cropToPadding="true"
        android:clipChildren="false"
        android:clipToPadding="false"
        android:background="@drawable/munshee_logo"/>

    <Button
        android:id="@+id/im12"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:padding="1dp"
        android:scaleType="centerCrop"
        android:cropToPadding="true"
        android:clipChildren="false"
        android:clipToPadding="false"
        android:background="@drawable/munshee_logo"

        android:src="@drawable/munshee_logo" />

    <Button
        android:id="@+id/im13"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:padding="1dp"
        android:scaleType="centerCrop"
        android:cropToPadding="true"
        android:clipChildren="false"
        android:clipToPadding="false"
        android:background="@drawable/munshee_logo"
        android:src="@drawable/munshee_logo" />

    <Button
        android:id="@+id/im14"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:padding="1dp"
        android:scaleType="centerCrop"
        android:cropToPadding="true"
        android:clipChildren="false"
        android:clipToPadding="false"
        android:background="@drawable/munshee_logo"
        android:src="@drawable/munshee_logo" />
    <Button
        android:id="@+id/im15"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:padding="1dp"
        android:scaleType="centerCrop"
        android:cropToPadding="true"
        android:clipChildren="false"
        android:clipToPadding="false"
        android:background="@drawable/munshee_logo"
        android:src="@drawable/munshee_logo" />


</LinearLayout>

how do I convert this into java code? The outcome should be one row of buttons horizontally spread across the screen. also, each button should scale itself according to the dimensions of the screen. Please help me implement this.

3
  • Inflate this layout, and add these in one layout. Set id for those on run time Commented Oct 4, 2016 at 9:43
  • So what is the issue? Commented Oct 4, 2016 at 9:56
  • 1
    Possible duplicate of Android - Dynamically Add Views into View Commented Oct 5, 2016 at 5:21

2 Answers 2

1

Adding views and components on layout in java is mostly similar to xml. Simple Example method is below:-

public LinearLayout createRow() {
        LinearLayout objLinearLayout = new LinearLayout(mContext);
        LinearLayout.LayoutParams objLayoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
        objLinearLayout.setWeightSum(3);
        objLinearLayout.setOrientation(LinearLayout.HORIZONTAL);
        objLinearLayout.setLayoutParams(objLayoutParams);

        Button objButton = new Button(mContext);
        LinearLayout.LayoutParams objButonLayoutParams = new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.MATCH_PARENT);
        objButonLayoutParams.weight = 1;
        objButton.setText("Add Button");
        objButton.setBackgroundColor(Color.LTGRAY);
        objButton.setTextColor(Color.BLACK);
        objButton.setLayoutParams(objButonLayoutParams);

        objLinearLayout.addView(objButton);

        /*
        * Here you can add other views like Textview,Spinner,etc
        * Every components has same method like in xml.*/


        return objLinearLayout;//This layout can display where you want.
    }
Sign up to request clarification or add additional context in comments.

Comments

0

You can try this, Add buttons dynamically:

<?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="horizontal"
tools:context="com.xanadutec.testviews.MainActivity">

</LinearLayout>

In Java Class:

HorizontalScrollView horizontalScrollView = new HorizontalScrollView(this);
LinearLayout ll = new LinearLayout(this);
horizontalScrollView.addView(ll);
ll.setOrientation(LinearLayout.HORIZONTAL);

for(int i = 0; i < 20; i++) {
   Button cb = new Button(this);
   cb.setText("I'm dynamic!");
   ll.addView(cb);
 }

  this.setContentView(horizontalScrollView);

EDITED:

ScrollView scrollView =new ScrollView(this);
        LinearLayout layout = new LinearLayout(this);
        layout.setOrientation(LinearLayout.VERTICAL);
        scrollView.addView(layout);
        for (int i = 0; i < 10; i++) {
            LinearLayout row = new LinearLayout(this);
            row.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));

            for (int j = 0; j < 3; j++) {
                Button btnTag = new Button(this);
                btnTag.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
                btnTag.setText("Button " + (j + 1 + (i * 3)));
                btnTag.setId(j + 1 + (i * 3));
                row.addView(btnTag);
            }

            layout.addView(row);
        }
        super.setContentView(scrollView);

3 Comments

Hello! this produces the buttons in one row... I wanted a grid view with multiple rows one after the other...is that achievable?
@ Sanjana, Check Edited Answer.
Probably better to add the buttons to an Adapter, not a ScrollView

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.