toString() returns a string representation of an object which is not
necessarily the value of the object.  your view.getTag() returns an object
which is already a String so all you have to do is cast it to string rather
than using toString() on it

String buttStr = (String) view.getTag(); is what you need

On Aug 7, 2016 3:35 AM, "Thomas Fazekas" <[email protected]> wrote:

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 [email protected].
To post to this group, send email to [email protected].
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
<https://groups.google.com/d/msgid/android-developers/fa4ae215-281e-4fcf-b929-005b59f2ef2f%40googlegroups.com?utm_medium=email&utm_source=footer>
.

For more options, visit https://groups.google.com/d/optout.

-- 
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 [email protected].
To post to this group, send email to [email protected].
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/CABfabRgeWAZaagA%3DPCZoTDjvp9t8B_U_FxM6e%2Bbn3BgN0VwiQw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to