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

  • SEARCH
  • Home
  • 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 8001939
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T16:13:46+00:00 2026-06-04T16:13:46+00:00

I have defined an Activity in an Android app containing 9 button. Each button

  • 0

I have defined an Activity in an Android app containing 9 button. Each button have to listen to the same event listener.

Looking at generated R class, their Ids are defined :

 public static final int btn1=0x7f040001;
 ...
 public static final int btn1=0x7f040009;

My question is: it is ok to add the listeners cycling the ID in a for loop?

for (int i= R.id.btn1; i<=R.id.btn9;i++)
{
   Button button = (Button)findViewById(i);
   button.setOnTouchListener(new View.OnTouchListener() {
        @Override public boolean onTouch(View v, MotionEvent event) {
             ...
        }
   });
}

Or there in some situation in which Id get reassigned by the compiler, breaking my code?

I’m using Intellij Idea environment to build the app.

  • 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-04T16:13:48+00:00Added an answer on June 4, 2026 at 4:13 pm

    I fear this would be a very bad idea in the event your R file is regenerated with non-consecutive IDs.. your better option would be to store an int array containing your IDs and loop through that

    ex.

    int[] views = new int[] { R.id.btn1, R.id.btn2, R.id.btn3, ...... R.id.btn9 };
    
    for (int i= 0; i< views.length ;i++)
    {
       Button button = (Button)findViewById(views[i]);
       button.setOnTouchListener(new View.OnTouchListener() {
            @Override public boolean onTouch(View v, MotionEvent event) {
                 ...
            }
       });
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In my app I have the main activity defined in the manifest.xml file like
I have an Android app with 2 activities defined below. In the MainMenu.oncreate() ,
I have application A defined as below: <application android:icon=@drawable/icon android:label=@string/app_name> <activity android:name=com.example.MyExampleActivity android:label=@string/app_name> <intent-filter>
i have a simple activity program in android. basically the class just extends Activity.
The main activity of my app is defined in the Android manifest file with
I have defined a config section in my app.config in the following way: <?xml
I have defined a class A and derived a new class B from A
I have built an activity to handle gestures in my Android game and want
I have a simple HelloWorld Activity that I try to test with an Android
I have an Android app that uses ActionBar Sherlock. I create a menu that

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.