1

As the title states, I am looking to find out how to create a button dynamically when another button in another activity is pressed. This is being done with the Android SDK.

Basically, I have two activities, MainActivity and SecondaryActivity, within the SecondaryActivity you enter some information, title, text, id, so on and so forth. When you click the "Save" button it sends some, information to MainActivity(not the issue). As well as sending the information, I need to create an entirely new button within the MainActivity.

Any suggestions on how this should be accomplished?

Thanks.

Edit 1

public void CreateNewButton(View view)
{

    LinearLayout lineLayout = (LinearLayout)findViewById(R.id.linear_layout);
    TextView newTextView = new TextView(this);
    int id = rand.nextInt(100);
    int newId;

    newTextView.setVisibility(View.VISIBLE);
    newTextView.setId( R.id.new_button + id );
    newTextView.setText("New Item");
    newTextView.setTextSize(35);
    newTextView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            intent = new Intent(getBaseContext(), SecondActivity.class);

            startActivity(intent);

        }
    });

    lineLayout.addView(newTextView);
}

This code generates the new TextView( Decided to change it up ) but now the issue I have, is newTextView.setText(); needs to get the text from the other activity

newTextView.setText(i.getData().toString());

putting this in the CreateNewButton(View view) methods causes an error since technically there is no data in the field that it is trying to grab from.

The problem at hand is I need to create the new TextView field WITH the name of the new account that has yet to be created. If that makes any sense.

6
  • 2
    Would it be possible to change the Visibility of the button rather than adding it dynamically? Commented Jul 17, 2013 at 21:49
  • Yes and No. The issue is, when the user creates a new account, the button needs to be created with the correct name. So, setting the visibility to true after pressing the button would work, but a new button needs to be created for each new account Commented Jul 18, 2013 at 1:40
  • Oh I see, so it's like a sign-in as different user sort of thing. Commented Jul 18, 2013 at 10:49
  • yeah kind of, essentially I need my second activity to call a method in the main activity after I hit the Save button in the second activity, however, android does not act like java and it crashes when i go to call it Commented Jul 18, 2013 at 16:21
  • 1
    please post the code in question. Commented Jul 18, 2013 at 16:29

1 Answer 1

2

I'm going to assume you want to add this button to a LinearLayout:

LinearLayout linearLayout = (LinearLayout) findViewById(R.id.linearLayout1);
Button button = new Button(this);
button.setText("I'm a button!");
// add whatever other attributes you want to the button
linearLayout.addView(button);
Sign up to request clarification or add additional context in comments.

3 Comments

Yes, that is what I want to do, and I have that exact code in the onCreate() method. However, it tends to crash upon loading. When I put that code into another method ( OnCreateNewButton() ), the button does not get created. I have one method which calls the new Activity, and another one that creates the button.
What does LogCat say when your application crashes?
java.lang.IllegalStateException: Could not execute method of the activity is what i get when i click the button with the second activity being executed in the method ( startActivity(intent) ) then I have the actual button creation in front of it.

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.