0

So a fragment on my Alarm application calls an activity and in the activity you set up information. My question is how can I take that information and create, lets say, a button that displays all that information. and how can I add that button to the fragment during runtime?

TheLaz
  • 17
  • 6

1 Answers1

0

Well, I'm assuming you have EditTexts that you're pulling the information from.

First, you'll want to pull that info. Making sure that you initialized your TextViews in the onCreate() method:

String thing1 = thingOneTextView.getText().toString();
String thing2 = thingTwoTextView.getText().toString();
String thing3 = thingThreeTextView.getText().toString();

Then, if you need to get this info back to your original activity (I'm not sure if you do - you weren't too clear on this in your original question), you'll want to use an Intent and addExtra():

Intent intent = new Intent(this, OriginalActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("firstThing",thing1);
intent.putExtra("secondThing",thing2);
intent.putExtra("thirdThing",thing3);
startActivity(intent);

Then, in your original activity, in your onCreate(), you'll want to read this data in.

String thing1, thing2, thing3;

Intent intent = getIntent();
if(intent != null && intent.getStringExtra(firstThing") != null) {
    thing1 = intent.getStringExtra("firstThing","-1");
    thing2 = intent.getStringExtra("secondThing","-1");       
    thing3 = intent.getStringExtra("thirdThing","-1");
}

Now, to add the button. This is how you do it dynamically:

Button b = new Button(this);
b.setText(thing1 + " " + thing2 + " " + thing3);
LinearLayout linLay = (LinearLayout)findViewById(R.id.my_linear_layout_from_the_xml);
LayoutParams layParams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
linLay.addView(b, layParams);

You can read more about adding the button dynamically on this great StackOverflow question.

Community
  • 1
  • 1
Alex K
  • 8,269
  • 9
  • 39
  • 57