Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting onClickListeners for buttons in scenes programmatically

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?

like image 500
Droidekas Avatar asked Dec 05 '22 22:12

Droidekas


2 Answers

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.

like image 92
AllDayAmazing Avatar answered Feb 06 '23 20:02

AllDayAmazing


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>
like image 25
Kasra Avatar answered Feb 06 '23 21:02

Kasra