Dear all,

here is what I want to do : declare a whole bunch of buttons in the 
activity_main.xml each of them having one custom tag (in this case it's 
called "tag").
All the buttons would share the same onClick handler, where I would use 
this custom property value to construct some strings (a URL actually).

Code excerpt :

activity_main.xml

<LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center">

        <Button
            android:layout_width="48dp"
            android:layout_height="48dp"
            android:text="o"
            android:id="@+id/led0"
            android:tag="paramValue1"
            android:onClick="sendURL" />

        <Button
            android:layout_width="48dp"
            android:layout_height="48dp"
            android:text="o"
            android:tag="paramValue2"
            android:id="@+id/led1"
            android:onClick="sendURL"/>

....
</LinearLyout>

and then in the ActivityMain.java I would use this value :

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void sendLed(View view){
        ...
        String buttStr = view.getTag().toString();
    ...
    }
}

Now , is that in anyway possible ?
The above construct gets me an NPE.

I am aware that I could use a switch statement (to compare the view.getId() 
with the individual ids of the buttons) but that seems a lot of comparisons 
for nothing (I would use that additional value "blindly", i.e. it doesn't 
matter which button has been pressed) 

-- 
You received this message because you are subscribed to the Google Groups 
"Android Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to android-developers+unsubscr...@googlegroups.com.
To post to this group, send email to android-developers@googlegroups.com.
Visit this group at https://groups.google.com/group/android-developers.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/android-developers/fa4ae215-281e-4fcf-b929-005b59f2ef2f%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to