0

Please look at the following algorithm and tell me if I can achieve it:

  1. create a main_activity
  2. inside the main_activity, create a simple Button
  3. the Button is labeled as "Add Button"
  4. once the users clicks the Button, an additional Button is created and placed in the Activity.

In other words:
once the user clicks on the add Button, it should create another Button and place it under the "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 separate method, 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:

MainActivity.java

    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.widget.Button;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

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

        Button addArm = (Button) findViewById(R.id.btnAddArm);
        addArm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                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 addButton(){

        // create an aditional button



    }
}

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: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>
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Mr.Tanav
  • 107
  • 1
  • 10
  • Possible duplicate of [Android - Dynamically Add Views into View](http://stackoverflow.com/questions/6216547/android-dynamically-add-views-into-view) – OneCricketeer Oct 31 '16 at 23:18
  • Yes, you can achieve it. – DigitalNinja Oct 31 '16 at 23:59
  • @cricket_007 how would having my code help you? Creating a simple onClick button is simple.. I know how to do that, I asking how should i approach in solving my solution. – Mr.Tanav Nov 01 '16 at 00:01
  • @DigitalNinja can you please guide me in how i should approach this? – Mr.Tanav Nov 01 '16 at 00:02
  • 1
    Set it up so that you have your "Add Button" button. When the user clicks "Add Button", you can create a new button underneath programmatically (http://stackoverflow.com/questions/4907609/add-button-to-a-layout-programmatically). No need to mess with the XML. If you perhaps have a limit on how many buttons the user can add then you could use XML and set the visibility of the button accordingly (http://stackoverflow.com/questions/4127725/how-can-i-remove-a-button-or-make-it-invisible-in-android) – DigitalNinja Nov 01 '16 at 00:10
  • @cricket_007 I am well aware that stackoverflow is not a tutorial site, which is why many people come on this site to seek help from individuals who are more educated and experienced. How do you expect some to try something when they do not even know how to go on approaching the problem. I even stated in my post, the approach I thought about taking but did not go ahead with the plan as it I would have generate xml code using my .java file and it would make things very messy. But if you really do disre to see my code... i will post it... – Mr.Tanav Nov 01 '16 at 01:56
  • @DigitalNinja thank you for your input, i will look into it and give it a shot. – Mr.Tanav Nov 01 '16 at 01:57
  • You don't need to generate any XML by Java. You can make a `new Button()`, which is a `View` subclass, and you can `addView` to a `LinearLayout` during an `onClick`. It is useful to see your Activity java and XML code to see what you are working with, so we can help you fill in the blanks. – OneCricketeer Nov 01 '16 at 02:43
  • @cricket_007 Hey, i posted my code. But I will try the way you explained it and see if I have any success. Thank you :) although, how would I declare its id and alignment with in the activity? would it be something along the lines of this :::::::::::::::::::::::: Button buttonTwo = new button(); //to create the button? – Mr.Tanav Nov 01 '16 at 02:56

1 Answers1

0

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

RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
    ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
params.addRule(RelativeLayout.BELOW, R.id.btnAddArm);

// params.addRule ... (there's a bunch you can add)

rootView.addView(button, params);

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

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
  • Okay great, that helped a lot. Can you please check my code for the layoutParams? I i tried doing it but it doesnt seem to set it underneath my button. Just check my original post. – Mr.Tanav Nov 01 '16 at 06:33
  • I don't know what `image_view` is, but that isn't necessary. You should just use LinearLayout, probably, or [ask a new question](http://stackoverflow.com/questions/ask) about the LayoutParams since this answered your initial question. – OneCricketeer Nov 01 '16 at 08:21
  • @user5425093 I've also rolled back your post because your edits invalidated my answer and therefore makes it unclear for future readers. – OneCricketeer Nov 01 '16 at 08:23