You might also want to create your buttons with a loop in your code instead
of in the layout file

On Aug 8, 2016 5:41 AM, "Thomas Fazekas" <[email protected]> wrote:

> My bad... thanks for setting me straight.
>
> On Sunday, August 7, 2016 at 5:18:52 PM UTC+2, Steve Gabrilowitz wrote:
>>
>> 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/ms
>> gid/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/2ed4bd2b-dab2-404d-bc33-
> bbe5ac7a5bf6%40googlegroups.com
> <https://groups.google.com/d/msgid/android-developers/2ed4bd2b-dab2-404d-bc33-bbe5ac7a5bf6%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/CABfabRi8rXh1Z5Q6KnjM%3D3zO6Bfg6SWToW-Dp1jX5rnK4x0gyw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to