1
public void onClick(View v) {

    int id = v.getId();
    switch(id) {
        case R.id.a :
        String textans = ans.getText().toString();
        ans.setText(textans +id);
    }
}

I am creating an android application in which i have 5 ImageButtons and a TextView.

ImageButtons are having alphabet images i.e A, B, C, D, E and their ID's in XML are a, b, c, d, e as follows

<ImageButton
        android:id="@+id/a"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_marginBottom="170dp"
        android:layout_marginLeft="18dp"
        android:src="@drawable/a" />

    <ImageButton
        android:id="@+id/b"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/imageButton5"
        android:layout_toRightOf="@+id/imageButton5"
        android:src="@drawable/b" />

    <ImageButton
        android:id="@+id/c"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/imageButton2"
        android:layout_toRightOf="@+id/imageButton2"
        android:src="@drawable/c" />

    <ImageButton
        android:id="@+id/d"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/imageButton3"
        android:layout_toRightOf="@+id/imageButton3"
        android:src="@drawable/d" />

    <ImageButton
        android:id="@+id/e"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/a"
        android:layout_toRightOf="@+id/a"
        android:src="@drawable/e" />

What I want is when I click an ImageButton the respective letter must be set in TextView. For this purpose I have used a switch in onClick method and tended to get the ID of the button and set it in TextView but id is something in int.

My question is this "How can I get the name of the ID of button clicked". For example when Iclick button having image A, the id of ImageButton is "a", so it must be stored in a variable and then it is set to TextView.

Hopefully my question is much clear.

Marko
  • 20,385
  • 13
  • 48
  • 64
cute programmer
  • 27
  • 1
  • 11

1 Answers1

2

If you want to set the ID of the clicked button to your TextView you could use something like this

String resourceName = getResources().getResourceEntryName(int resId);

Use in your solution

public void onClick(View v) {

    int id = v.getId();
    switch(id) {
        case R.id.a:
            String resourceName = getResources().getResourceEntryName(id);
            textView.setText(resourceName);
            break;
    }
}
Marko
  • 20,385
  • 13
  • 48
  • 64
  • i used resource thing but i got "com.example.app_name:id/a" – cute programmer Dec 31 '15 at 15:17
  • i only wnt to set TextView with it ID "a". – cute programmer Dec 31 '15 at 15:18
  • You should call `getResourceEntryName()`, instead of `getResourceName()`. – Marko Dec 31 '15 at 15:55
  • you helped me alot.i was stuck there and you show me the way thanks again.well i want to get more help from you. Can i add you on facebook? – cute programmer Jan 01 '16 at 15:23
  • @cuteprogrammer welcome, accept the answer, so that the question is resolved (green arrow next to the answer). I am not using Facebook. You can ask a question here and link it to me and i'll take a look. – Marko Jan 01 '16 at 15:56
  • You can post it to this answer and i'll see it. Or we could talk in stackoverflow chatroom, or skype. – Marko Jan 01 '16 at 22:12
  • I have another question related to this question. – cute programmer Jan 04 '16 at 11:23
  • @cuteprogrammer talk to me. – Marko Jan 04 '16 at 11:25
  • i have accessed ID of ImageButton using reference, now i want to access that ImageButton using the ID. – cute programmer Jan 04 '16 at 11:25
  • @cuteprogrammer check the second answer [here](http://stackoverflow.com/questions/3476430/how-to-get-a-resource-id-with-a-known-resource-name). You can use `getIdentifier` to get the resource reference from resource name. And when you have the resource reference, you can get the ImageButton. – Marko Jan 04 '16 at 11:28
  • What i was doing that when an ImageButton of letter in clicked then the ID of that Button in set in the textView and that button is set to invisible. Now i want when the last character of string in textview is accessed (which is the ID of the ImageButton) that ImageButton must be visible again. – cute programmer Jan 04 '16 at 11:28
  • Easy, use the approach that I explained above your last post. It should work, if you get the resource name, you can get the resource reference, meaning you can get the View, meaning you can set the visibility :). – Marko Jan 04 '16 at 11:33
  • resID= this.getResources().getIdentifier("nameOfResource", "id", this.getPackageName()); should i use this? sorry may be it is a silly question but what to type in parameters of getIdentiifier(" ", " ". "packgename") ??? – cute programmer Jan 04 '16 at 13:26
  • @cuteprogrammer I haven't tried it before, but something like this could work `int resId = getResources().getIdentifier("resourceName", "id", getPackageName());` – Marko Jan 04 '16 at 13:34
  • and what next? plz guide me completely because i am practicing android :( . – cute programmer Jan 04 '16 at 14:02
  • @cuteprogrammer if you get the resource ID reference to your view (with the use of `getIdentifier`), you can call `ImageButton imageButton = (ImageButton) findViewById(resId);` and voila, you should have it. – Marko Jan 04 '16 at 14:07