1

Firstly, my apologies if this answer is already on here, as I've been searching for a few weeks and haven't found anything yet.

I am working on an Android app which needs to allow the user to create and remove buttons. I know how to normally create buttons statically through adding the button the XML file and creating it's functionality in the JAVA file.

Instead, I have a static button which I'll refer to as "Create Button". When the user presses on the Create Button, they should be given the option to add a new button to the current activity, allowing them to change the title of said button etc. When they close the app and open it back up; the button they added should still be there. Similarly, they should be given an option to remove buttons.

Can someone point me in the right direction? Most of the sources that I've come across only explain how to statically create buttons, like I first mentioned.

Thanks for the help!

EDIT: I was able to figure some stuff out based off of the feedback I've been given.

So far I have the following code in the onOptionsItemSelected( ) method:

    if (id == R.id.add_button) 
        {
        Button myButton = new Button(this);
        myButton.setText("Push Me");
        //myButton.setVisibility(View.VISIBLE);
        return true;    
        }

I am still a little confused about how this can get added to the layout. Mainly, I am confused about the findViewById call:

    RelativeLayout layout = (RelativeLayout)findViewById(R.id.?);

Which id should I be using? In the app's main XML file, there is no ID for the layout itself. It's just a "RelativeLayout".

EDIT2:

Actually, I solved the problem. Thanks for the advice! I just needed to give my layout an ID in the XML file. I knew that I could give buttons etc an ID, but never knew that I was able to do so for the actual layout itself!

  • This is a very general question.This shows that you haven't researched before asking a question.You can find the answer way easily if you try to search rather than asking it straight away on SO – Haresh Chhelana Apr 20 '15 at 07:06
  • See this: http://stackoverflow.com/questions/14826822/adding-multiple-views-to-a-view/14827165#14827165 I have posted a code where you can see how dynamically views can be added, modified and removed. – Sandeep Kharat Apr 20 '15 at 07:12

2 Answers2

1

Creating a button -

Button myButton = new Button(this);

Adding text to it -

myButton.setText("Push Me");

To make the button visible, you need to add it to a view like this. You can also add it to a statically created view -

LinearLayout ll = (LinearLayout)findViewById(R.id.buttonlayout);
LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);

ll.addView(myButton, lp);

Removing button -

ll.removeView(myButton);

For additional customizations, check documentation.


If you are creating multiple buttons, then I recommend setting id. This example makes it clear.


For making buttons visible after closing the app, you need to store the data on memory. The simplest way to do this is to maintain a record of the buttons and their specifications and storing them before closing the app. After opening the app, you can read the stored data and create the buttons accordingly.

For more details, check Data Storing.

Community
  • 1
  • 1
Confuse
  • 5,646
  • 7
  • 36
  • 58
0
ViewGroup mViewGroup = (ViewGroup) findViewById(R.id.main_layout_id);
mViewGroup.addView(yourButton, 0, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
Leebeedev
  • 2,126
  • 22
  • 31