4

I am building a soundboard app, where I have some predefined buttons in a tablelayout, which is nested inside a relative layout. The layout is an own fragment (I use multiple tabs for different categories).

(I will post an image here as soon as I have the 10 reps)

Sound and tabs are working fine, now I want to implement the function, that you generate a new button, once you click the "+" button and that the "+" button moves one row beneath the new button.

Is there a way to generate a new tablelayout, with the same attributes as the already existing without having to use blanks inside the xml-file?

4
  • 1
    can you post the layout of this ui Commented Feb 27, 2014 at 11:36
  • the picture is nice but actually it would be better if you had post some code Commented Feb 27, 2014 at 11:41
  • Use two rel layouts one for btns and another for + btn(with property bellow) and add button at first rel layout with property bellow last btns id... Commented Feb 27, 2014 at 11:50
  • I am having trouble adding a new Layout programmatically to the existing one. Commented Feb 27, 2014 at 12:50

3 Answers 3

2

try this simple example and modify as per your requirement

 public class MainActivity extends Activity {
        Context context;
        ScrollView scrollView;
        LinearLayout container;
        LinearLayout layout;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            context = this;
            setContentView(R.layout.activity_main);
             layout = (LinearLayout) findViewById(R.id.LinearAdd);


        }

        public void addButton(View view) {
            Button button  = new Button(MainActivity.this);
            LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT );
            button.setLayoutParams(lp);
            button.setText("ADDED");
            layout.addView(button);
        }

    }

And this your xml

<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"
    tools:context=".MainActivity" >

    <LinearLayout
        android:id="@+id/LinearAdd"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="70"
        android:orientation="vertical" >

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="addButton"
            android:text="click to add" />
    </LinearLayout>

</LinearLayout>
Sign up to request clarification or add additional context in comments.

2 Comments

If I want to put this code outside of the MainActivity into a seperate Activity only for generating buttons, how do I have to call the function from inside my fragment?
Use this xml to set view for your fragment class and also put on click method in to your fragment class. that will work
0

Here is my layout fragment code:

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<TableLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:stretchColumns="*"
    android:id="@+id/tableLayout">


    <TableRow
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_column="1">
        <Button
            android:background="@drawable/button"
            style="@style/button_text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/tab01sound01"
            android:layout_column="0"
            android:text="@string/buttonAlan" />
    </TableRow>

</TableLayout>

<TableLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:stretchColumns="*"
    android:layout_below="@+id/tableLayout2"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:id="@+id/thirdTable">

</TableLayout>

</RelativeLayout>

Comments

0

And this is the code from my first fragment tab

public class Tab1 extends Fragment {

private int buttonAmount = MainActivity.getButtonAmountTab1();
private Button[] button = new Button[buttonAmount + 1];
private Sound sound;
private String packageName = MainActivity.getStringPackageName();
private Button addButton;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    sound = MainActivity.getSound();

    View rootView = inflater.inflate(R.layout.fragment_fun, container, false);

    //This generates the Audio functionality for each button

    for (int i = 1; i < buttonAmount + 1; i++) {
        String buttonID = "tab01sound0" + i;
        int resID = getResources().getIdentifier(buttonID, "id", packageName);
        final int audioID = getResources().getIdentifier(buttonID, "raw", packageName);
        button[i] = (Button) rootView.findViewById(resID);
        registerForContextMenu(button[i]);
        button[i].setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                sound.playSound(audioID);
            }

        });

    }

    //functionality for the "+" button

    addButton = (Button) rootView.findViewById(R.id.addButton);
    addButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            CreateDialog newDialog = new CreateDialog();
            newDialog.AlertBox(getActivity());

        }
    });

    return rootView;

}

}

Comments

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.