0

Few details about my code:

I get a JSONObject, then I get the strings from it and using the field "name", I create as many buttons as fields are there in the json.

Now, the thing is I want to add functionality to the buttons too and I am not sure if it will work this way.

Check my code please, I ll comment where I am stuck.

for(int i=0; i<arr.length();i++){

    JSONObject oneObject = arr.getJSONObject(i);

    id = oneObject.getString("Id");
    nume = oneObject.getString("Nume");

    Button btn = new Button(context);
    btn.setId(i);
    btn.setText(nume);
    btn.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
        //STUCK HERE!

        }
    });

    ll.addView(btn);
}   

So, for each button, I want to make a new activity, so a new intent, because each button will need a new "screen"(or so to say).

Is there any ways I can actually do this ,or it's impossible?

NOTE: I could not find anything like this over the internet and my thoughts on this is that it's impossible, but I wanted a few more opinions before I move on.

Mithun
  • 2,075
  • 3
  • 19
  • 26
Vlad
  • 910
  • 1
  • 10
  • 18
  • Activities are tied to the AndoidManifest.xml file aren't they? This looks like a typical Fragment solvable question IMHO. – roarster May 26 '15 at 18:35
  • Yes indeed they are AndroidManifset bound.Anyways, as far as I understand, it's impossible to do that.How would you do it with fragments anyways?I have worked with fragments too and I highly doubt it will work as you need to create a new xml layout and a new java class for each fragment... – Vlad May 26 '15 at 18:38
  • 1
    Well, you'd just need a generic Fragment class that takes in some variables through its constructor. You don't need a new class for each one. It would always have the same layout unless you could send some json to distinguish styles, but that's not a problem - you can load different styles for the Fragment when you create it. – roarster May 26 '15 at 18:41
  • That means my mainactivity will implement Fragment,right? So that I can make the transitions...? – Vlad May 26 '15 at 18:44
  • No, you'd have it extend FragmentActivity so that it can hold Fragments. Then you could use transitions to add/remove Fragments on the fly, making it look to the user like you're changing Activity. – roarster May 26 '15 at 18:45
  • Ok that does make sense.I haven't used FragmentActivity, I usually go with Activity ,anyways, I guess I can handle this task with the information you gave.Feel free to give an answer and after I get some documentation checked, to make sure this is what I need,I ll check your answer as correct. – Vlad May 26 '15 at 18:55

1 Answers1

1

Like we said in the comments, dynamically creating Activities won't work because you can't modify the AndroidManifest.xml at runtime. Instead, this problem is ideally suited to Fragments.

You could use a standard Activity rather than FragmentActivity, but here's a reason why I think you shouldn't. Then you just use that as your root activity and in your onClicks add Fragments programatically:

FragmentManager fragMan = getFragmentManager();
FragmentTransaction fragTransaction = fragMan.beginTransaction();

Fragment fragment = new ImageFragment();
fragTransaction.add(R.id.fragment_container, fragment);
fragTransaction.commit();

There's a nice guide on all that stuff here.

Community
  • 1
  • 1
roarster
  • 4,058
  • 2
  • 25
  • 38
  • I have not tested it yet ,but it is what I need ,I am fairly sure about it.I will check the answer and give a thumbs.Thanks for the suggestions and the actual answer. – Vlad May 27 '15 at 15:34