Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9250189
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T10:23:23+00:00 2026-06-18T10:23:23+00:00

I have 24 buttons in my layout, all these buttons do something similar so

  • 0

I have 24 buttons in my layout, all these buttons do something similar so I want to create a generic function. But first I need to know the name (xml id) of he button.

This the XML code of the button:

  <Button
      android:id="@+id/add_04"
      android:layout_width="42dp"
      android:layout_height="wrap_content"
      android:layout_gravity="center"
      android:layout_marginLeft="15dp"
      android:background="@xml/zbuttonshape"
      android:onClick="onClick"
      android:text="@string/mas" />

I set android:onClick="onClick" for all the buttons.

In my activity I’ve create a new function onClick:

This the code I’ve tried:

public void onClick(View v) {
        String name = v.getContext().getString(v.getId());
        String name2 = context.getString(v.getId());
        String name3 = getString(v.getId());
        String name4 = getResources().getString(v.getId()); 
}

But when I try to get the name (in this case "add_04") I always get "false".

Finally I’ve found a solution with the following code:

import java.lang.reflect.Field;

String name5 = null;
Field[] campos = R.id.class.getFields();
for(Field f:campos){
     try{
        if(v.getId()==f.getInt(null)){
            name5 = f.getName();
            break;
        }
       }
       catch(Exception e){
        e.printStackTrace();
    }
}

Is there an easier way to get this ID?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T10:23:24+00:00Added an answer on June 18, 2026 at 10:23 am

    The approach is misguided to begin with. If you want to associate a piece of arbitrary data (e. g. a string) with a view, that’s what tag is for. The ID is numeric and it better stay that way. A word of caution though, tags are not unique in Android, watch for accidental tag collisions within the same view tree.

    EDIT much later: the OP’s issue was a case of an XY problem. That said, the question title alone is a legitimate question in its own right.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've set android:layout_weight=1 to all 3 buttons, but doesn't work. My layout is something
I have many buttons in my layout and they work well. I want to
I have 4 buttons in my layout - they are switchers between 4 possible
I have two buttons in my layout say b1 and b2. By pressing b1
I have two buttons in my xml layout as shown bellow. I used an
I have a linear layout with several buttons in it. The button images are
How to align the buttons bottom of the screen.In the layout i have header
I am trying something very basic: I have a list of 5 buttons. They
What i have: I have a calculator layout, with many buttons. And each button
Using eclipse, first of all. I'm building an Android calculator. I have all my

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.