user5425093 user5425093 - 1 month ago 13
Android Question

Adding more buttons when a button in clicked

please look at the following algorithm and advise me if I can achieve it:


  1. create a main_activity

  2. inside the main_activity, create a simple onClick button

  3. the button is labeled as "Add Button"

  4. once the users click/presses the button and additional button is created and placed in the activity.



i.e. in other words
once the user clicks on add button, it should create another button and
place it underneath "Add Button" button.

I apologize in advance as this may be confusing, so please feel free to comment and ask for clarification.

I originally thought about creating a seperate function, in which I would create a ButtonView, but I am not sure how I can physically create a button. Would I need to apply code to .xml file also? I am really confused.

Here is my code:

package inc.fimp;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

private ViewGroup rootView;

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

rootView = (ViewGroup) findViewById(R.id.buttonContainer);

Button addArm = (Button) findViewById(R.id.btnAddArm);
addArm.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
getInfo();
addButton();
}
});
}

public boolean onCreateOptionsMenu(Menu menu){
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.menu_main, menu);



return true;
}

public boolean onOptionsItemSelected(MenuItem item){
int res_id = item.getItemId();
if(res_id==R.id.action_contact)
{
Toast.makeText(getApplicationContext(), "You selected Contacted us option", Toast.LENGTH_SHORT).show();
}

if(res_id==R.id.action_settings){
Toast.makeText(getApplicationContext(), "You selected Settings Option", Toast.LENGTH_SHORT).show();
}
return true;
}

public void getInfo(){

}

public void addButton(){

// create an aditional button

Button button = new Button(MainActivity.this);
button.setText("Button 2");
rootView.addView(button);



ViewGroup.LayoutParams btnParam =image_view.getLayoutParams();
btnParam = below;
Button.setLayoutParams(btnParam);



}
}


xml file code

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/buttonContainer"
android:layout_width="match_parent"
android:layout_height="match_parent"
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="inc.fimp.MainActivity">


<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/add_arm"
android:textStyle="italic"
android:id="@+id/btnAddArm"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:background="@drawable/addarm"
android:singleLine="false" />
</RelativeLayout>

Answer

Start by adding an ID to the parent layout

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/buttonContainer"

Then, get that with findViewById. ViewGroup simply used because that is all you need to get the addView method.

public class MainActivity extends AppCompatActivity {

    private ViewGroup rootView;

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

        rootView = (ViewGroup) findViewById(R.id.buttonContainer);

Then, in the addButton,

Button button = new Button(MainActivity.this); // Need to provide the context, the Activity
// button.setText("Added!"); // for example
rootView.addView(button);

Since you have a RelativeLayout, you can also programmatically put LayoutParams to do layout above/below, etc. other views.