Last active
February 9, 2016 17:51
-
-
Save robbiemu/ff123239a232e2820aca to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Override | |
public void onClick(View v) { | |
Field f; | |
int rid; | |
String name = "unknown View"; | |
try { | |
f = R.id.class.getDeclaredField(Integer.toString(v.getId())); | |
rid = f.getInt(f); | |
name = MainActivity.this.getString(rid); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} finally { | |
Log.v(LOG_TAG, String.format("%s was clicked!", name)); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
xmlns:app="http://schemas.android.com/apk/res-auto" | |
xmlns:tools="http://schemas.android.com/tools" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:paddingBottom="@dimen/activity_vertical_margin" | |
android:paddingLeft="@dimen/activity_horizontal_margin" | |
android:paddingRight="@dimen/activity_horizontal_margin" | |
android:paddingTop="@dimen/activity_vertical_margin" | |
app:layout_behavior="@string/appbar_scrolling_view_behavior" | |
tools:context="xyz.selfenrichment.robertotomas.buttonsandeventlisteners.MainActivity" | |
tools:showIn="@layout/activity_main"> | |
<Button | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:text="@string/button1" | |
android:id="@+id/button1" | |
android:textStyle="italic" | |
android:layout_centerVertical="true" | |
android:layout_centerHorizontal="true" /> | |
<Button | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:text="@string/button2" | |
android:id="@+id/button2" | |
android:layout_above="@+id/button1" | |
android:layout_centerHorizontal="true" /> | |
</RelativeLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Override | |
public void onClick(View v) { | |
String vName = ""; | |
int rid; | |
String name = "unknown View"; | |
try { | |
String vName = getResources().getResourceName(v.getId()); | |
String [] vna = vName.split("/"); | |
vName = vna[vna.length-1]; | |
int rid = getResources().getIdentifier(vName, "string", getPackageName()); | |
name = MainActivity.this.getString(rid); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} finally { | |
Log.v(LOG_TAG, String.format("%s was clicked!", name)); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<resources> | |
<string name="app_name">Buttons and EventListeners</string> | |
<string name="action_settings">Settings</string> | |
<string name="button1">click me!</string> | |
<string name="button2">second button</string> | |
</resources> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
02-09 12:04:04.174 6009-6009/xyz.selfenrichment.robertotomas.buttonsandeventlisteners W/System.err: java.lang.NoSuchFieldException: No field 2131492971 in class Lxyz/selfenrichment/robertotomas/buttonsandeventlisteners/R$id; (declaration of 'xyz.selfenrichment.robertotomas.buttonsandeventlisteners.R$id' appears in /data/app/xyz.selfenrichment.robertotomas.buttonsandeventlisteners-2/base.apk) | |
02-09 12:04:04.174 6009-6009/xyz.selfenrichment.robertotomas.buttonsandeventlisteners W/System.err: at java.lang.Class.getDeclaredField(Native Method) | |
02-09 12:04:04.174 6009-6009/xyz.selfenrichment.robertotomas.buttonsandeventlisteners W/System.err: at xyz.selfenrichment.robertotomas.buttonsandeventlisteners.MainActivity.onClick(MainActivity.java:51) | |
02-09 12:04:04.174 6009-6009/xyz.selfenrichment.robertotomas.buttonsandeventlisteners W/System.err: at android.view.View.performClick(View.java:5198) | |
02-09 12:04:04.174 6009-6009/xyz.selfenrichment.robertotomas.buttonsandeventlisteners W/System.err: at android.view.View$PerformClick.run(View.java:21147) | |
02-09 12:04:04.174 6009-6009/xyz.selfenrichment.robertotomas.buttonsandeventlisteners W/System.err: at android.os.Handler.handleCallback(Handler.java:739) | |
02-09 12:04:04.174 6009-6009/xyz.selfenrichment.robertotomas.buttonsandeventlisteners W/System.err: at android.os.Handler.dispatchMessage(Handler.java:95) | |
02-09 12:04:04.174 6009-6009/xyz.selfenrichment.robertotomas.buttonsandeventlisteners W/System.err: at android.os.Looper.loop(Looper.java:148) | |
02-09 12:04:04.174 6009-6009/xyz.selfenrichment.robertotomas.buttonsandeventlisteners W/System.err: at android.app.ActivityThread.main(ActivityThread.java:5417) | |
02-09 12:04:04.174 6009-6009/xyz.selfenrichment.robertotomas.buttonsandeventlisteners W/System.err: at java.lang.reflect.Method.invoke(Native Method) | |
02-09 12:04:04.174 6009-6009/xyz.selfenrichment.robertotomas.buttonsandeventlisteners W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) | |
02-09 12:04:04.174 6009-6009/xyz.selfenrichment.robertotomas.buttonsandeventlisteners W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) | |
02-09 12:04:04.175 6009-6009/xyz.selfenrichment.robertotomas.buttonsandeventlisteners V/MainActivity: unknown View was clicked! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment