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 8086887
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T18:35:21+00:00 2026-06-05T18:35:21+00:00

I’m developing an app on android and I am generating UI elements in a

  • 0

I’m developing an app on android and I am generating UI elements in a loop. But I need these elements to have an id with letters and numbers, for example "rl1" or "rl2". I was trying to use the method RelativeLayout.setId() but, that method only accepts int. Is there a way I can set an ID as I want without being limited to numbers?

Thanks.

Here is the code I am trying to make work.

for (int i=1; i < 10; i++)
{
    //gets the frameview where the elements will be created.
    String LinearLayoutId = "frameview1";
    int resID = getResources().getIdentifier(LinearLayoutId, "id", "com.myapp.ERS");
    LinearLayout linearLayout = (LinearLayout)findViewById(resID);

    //creates the RelativeLayout that will hold the ImageIcon and the TextView
    RelativeLayout rl = new RelativeLayout(this);
    RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
            RelativeLayout.LayoutParams.MATCH_PARENT,40 );
    rl.setLayoutParams(lp);
    rl.setId("rl"); /// >>>> I would like here to set and ID of "rl1" for example.
    rl.setBackgroundDrawable(getResources().getDrawable(R.drawable.bk36));

    //creates the image icon within the layout at the left side
    ImageView image = new ImageView(this);
    lp = new RelativeLayout.LayoutParams(
            40,RelativeLayout.LayoutParams.MATCH_PARENT );

    image.setLayoutParams(lp);
    String imageicon = "icon_"+i;
    resID = getResources().getIdentifier(imageicon, "drawable", "com.myapp.ERS");
    image.setImageDrawable(getResources().getDrawable(resID));  //sets the icon
    rl.addView(image); //adds the ImageView to the relative layout


    //creates the TextView within the layout with a 40 margin to the left
    TextView tv = new TextView(this);
    lp = new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.MATCH_PARENT,RelativeLayout.LayoutParams.MATCH_PARENT );
    lp.setMargins(40, 0, 0, 0);
    tv.setLayoutParams(lp);
    String textViewID = "tv"+i;
    resID = getResources().getIdentifier(textViewID, "string", "com.myapp.ERS");
    tv.setText(getResources().getString(resID));
    tv.setTextColor(Color.BLACK);
    tv.setTextSize(25);
    rl.addView(tv);//adds the TextView to the relative layout
    rl.setOnClickListener(mAddListener);
    linearLayout.addView(rl);//adds the RelativeLayout to the LinearLayout
}

and then I have the OnCLickListener like this…

private OnClickListener mAddListener = new OnClickListener()
{
    public void onClick(View v){
        Intent intent;
        Bundle bundle;

        String id = getResources().getResourceEntryName(v.getId());
        id = id.replaceAll("\\D+","");
        int value = Integer.parseInt(id);

        intent = new Intent(ERS.this, ShowInfo.class);
        bundle = new Bundle();
        bundle.putInt("key", value);  
        System.out.println(v.getId());
        intent.putExtras(bundle); 
        startActivity(intent);
    }
};

I have tried to set up numeric IDs, but then when I Look for them with:

String id = getResources().getResourceEntryName(v.getId());

It can’t find them.

I had all of this in an xml file to begin with, but it was really long because there are about forty items in the list, and it was complicated for me to go and change a letter for example in all of them. I came up with this idea to generate them at runtime in a for loop. I am testing in the meantime with ten, but I can’t get it to work.

If I am doing something incorrect, then pardon me, but I am new to this.

  • 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-05T18:35:23+00:00Added an answer on June 5, 2026 at 6:35 pm

    Why dont you try using SharedPreferences as an alternative in case you want to access the elements which you give some ID elsewhere in some other activity.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a French site that I want to parse, but am running into
I need to clean up various Word 'smart' characters in user input, including but
I have thousands of HTML files to process using Groovy/Java and I need to
I am trying to loop through a bunch of documents I have to put
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,

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.