Re: [android-developers] button onClick handler retrieve custom attribute value declared in activity_main.xml

2016-08-08 Thread Steve Gabrilowitz
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"  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"  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
>>
>> > android:orientation="horizontal"
>> android:layout_width="match_parent"
>> android:layout_height="wrap_content"
>> android:gravity="center">
>>
>> > android:layout_width="48dp"
>> android:layout_height="48dp"
>> android:text="o"
>> android:id="@+id/led0"
>> android:tag="paramValue1"
>> android:onClick="sendURL" />
>>
>> > android:layout_width="48dp"
>> android:layout_height="48dp"
>> android:text="o"
>> android:tag="paramValue2"
>> android:id="@+id/led1"
>> android:onClick="sendURL"/>
>>
>> 
>> 
>>
>> 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-d...@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/ms
>> gid/android-developers/fa4ae215-281e-4fcf-b929-005b59f2ef2f%
>> 40googlegroups.com
>> 
>> .
>>
>> 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 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/2ed4bd2b-dab2-404d-bc33-
> bbe5ac7a5bf6%40googlegroups.com
> 
> .
> 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 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/CABfabRi8rXh1Z5Q6KnjM%3D3zO6Bfg6SWToW-Dp1jX5rnK4x0gyw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Re: [android-developers] button onClick handler retrieve custom attribute value declared in activity_main.xml

2016-08-08 Thread Thomas Fazekas
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"  > 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
>
>  android:orientation="horizontal"
> android:layout_width="match_parent"
> android:layout_height="wrap_content"
> android:gravity="center">
>
>  android:layout_width="48dp"
> android:layout_height="48dp"
> android:text="o"
> android:id="@+id/led0"
> android:tag="paramValue1"
> android:onClick="sendURL" />
>
>  android:layout_width="48dp"
> android:layout_height="48dp"
> android:text="o"
> android:tag="paramValue2"
> android:id="@+id/led1"
> android:onClick="sendURL"/>
>
> 
> 
>
> 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-d...@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.
>
>
>

-- 
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/2ed4bd2b-dab2-404d-bc33-bbe5ac7a5bf6%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [android-developers] button onClick handler retrieve custom attribute value declared in activity_main.xml

2016-08-07 Thread Steve Gabrilowitz
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"  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










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.

-- 
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/CABfabRgeWAZaagA%3DPCZoTDjvp9t8B_U_FxM6e%2Bbn3BgN0VwiQw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


[android-developers] button onClick handler retrieve custom attribute value declared in activity_main.xml

2016-08-07 Thread Thomas Fazekas
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










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.


[android-developers] button onClick handler retrieve custom attribute value declared in activity_main.xml

2016-08-07 Thread Thomas Fazekas
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 :

actiity_main.xml













and then in the 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){
...




urlText.setText("http://"+hostText.getText().toString()+":"+portText.getText().toString()+"/"+view.getId());
}
}

-- 
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/85e842f5-c237-46be-b283-35a7bde7c300%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.