4

I have 2 layouts which contain the same buttons

layout_1.xml

  <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
    <Button
        android:id="@+id/button_1"
        android:text="button2"
        android:background="@android:color/black"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    </RelativeLayout>

and layout_2.xml

<RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
    <Button
        android:id="@+id/button_1"
        android:text="button2"
        android:background="@android:color/white"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    </RelativeLayout>

Please assume these are all valid layouts etc.(I am just adding the relevant code.).

So in my fragment ,I inflate and use layout_1.xml in onCreateView.I want to toggle between the 2 scenes using button_1. I can set the listener for button_1 in layout_1.xml during the onCreateView(). The problem is trying to set a listener on that button in the second view.i.e. the listener does not activate for the second scene(with layout_2.xml).And hence i canot toggle between the 2 scenes.Is there a way to achieve this?

Droidekas
  • 3,464
  • 2
  • 26
  • 40

2 Answers2

14

It would actually appear that a proper way to do this would be to on the second scene you define an action to be performed as such:

mSecondScene.setEnterAction(new Runnable() {
        @Override
        public void run() {
                 ((Button) mSecondScene.getSceneRoot().findViewById(R.id. button_1)).setOnClickListener( ... );
    }

This will allow you to set your ClickListener on the View without the data binding to a generic click listener method. Then you can perform the Transition to the second scene and viola.

AllDayAmazing
  • 2,383
  • 1
  • 24
  • 25
  • 1
    You're a godsent. Been looking for this damn solution for ages! "How to bind data into a newly loaded scene." Seems no ones using Scenes so there isn't much info on this! Androids Scene documentation should be updated. Thank you! – vguzzi Feb 20 '18 at 11:32
3

In general, it is not a good idea to have multiple views with the same id. This is what caused the confusion here.

Note: Below is the solution used by OP that was suitable for their specific needs:

One simple solution is to use the onClick attribute in the XML file. You can assign the same onClick method to multiple items. Like this:

And in your activity.java add this:

public void buttonClicked(View v){

    Log.d("TAG","Button clicked!!"
    // do stuff here

}

2nd option:

When you set a listener for one button with the id of button_1, it does not set the listener for both buttons, it only sets it for the first one. If you want to set the same listener for both, all you need to do is to assign these button different ids and then assign them the same listener.

This is what you should do:

Listener myListener = new Listener(){.. blah blah....};

((Button) findViewById(R.id.some_id)).setListerner(myListener);
((Button) findViewById(R.id.some_other_id)).setListerner(myListener);

3rd option:

findViewById(R.id.id_of_layout1).findViewById(R.id.button_1)
findViewById(R.id.id_of_layout2).findViewById(R.id.button_1)

in this case, you need add some id to your layout files, for example: layout_1.xml:

<RelativeLayout
        android:id="+id/id_of_layout1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
    <Button
        android:id="@+id/button_1"
        android:text="button2"
        android:background="@android:color/black"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    </RelativeLayout>
Kasra
  • 3,045
  • 3
  • 17
  • 34
  • I plan to use the fact that it is the same id to animate only certain objects in the scene.Hence the id was specified as same.Both the buttons do not exist during the same instance.And when a scene transition occurs,I needd to set listeners,so is theere a way to get the view from a current scene? – Droidekas Mar 08 '15 at 00:14
  • The xml method does not give me enough dynamicity(if that's a word).I need to add several listeners,hence I would prefer a programamtic approach. – Droidekas Mar 08 '15 at 00:16
  • yes but these are layout files,not inflated views.How do i get the inflated views so that i can call `findViewById()` ? – Droidekas Mar 08 '15 at 00:20
  • You cannot set listener for an not-yet-inflated view – Kasra Mar 08 '15 at 00:23
  • But the thing is that when you use scenes,all tutorials show same ids for to indicate the same element being animated.check [here](http://developer.android.com/training/transitions/scenes.html) – Droidekas Mar 08 '15 at 00:38
  • I think the buttons should have differing id's like @Kasra mentioned, it makes sense for text views to have the same id because they are essentially the same, these 2 buttons are not – Joe Maher Mar 08 '15 at 00:51
  • @JoeMaher If you maintain the same id,the animations provided to that object differ from the rest(it willl be translated instead of fading in etc etc).Hence the same ids are used. – Droidekas Mar 08 '15 at 10:09
  • @Kasra,I had to set it via xml to solve the problem.You have suggested all the answers possible here :).So please add that option as the solution finally used and I shall accept it :) – Droidekas Mar 09 '15 at 06:35
  • Awesome! Did you end up using onClick in XML? – Kasra Mar 09 '15 at 08:23
  • Yes i did.And I am still surprised there si no programmatic way of dealing with this. – Droidekas Mar 11 '15 at 04:31
  • This should not be the accepted answer. This is incorrect as all shared element transitions should use the same ID – AllDayAmazing Mar 09 '16 at 23:56