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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T00:04:01+00:00 2026-05-16T00:04:01+00:00

So I have made a little piece of code that will insert and manipulate

  • 0

So I have made a little piece of code that will insert and manipulate a few images. Now my main problem is that this is very ugly and long, I was wondering if anything could be done to make it prettier.

    RelativeLayout mRelativeLayout = (RelativeLayout) findViewById(R.id.board);

    ImageView i = new ImageView(this);
    i.setImageResource(R.drawable.blue_1);
    i.setId(400);

    ImageView j = new ImageView(this);
    j.setImageResource(R.drawable.red_1);
    j.setId(401);

    RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(40,62);
    params.addRule(mRelativeLayout.ALIGN_PARENT_BOTTOM);
    RelativeLayout.LayoutParams params1 = new RelativeLayout.LayoutParams(40,62);
    params1.addRule(mRelativeLayout.ALIGN_PARENT_BOTTOM);
    params1.addRule(mRelativeLayout.RIGHT_OF, 400);    

    mRelativeLayout.addView(i, params);
    mRelativeLayout.addView(j, params1);
    setContentView(mRelativeLayout);

For example I have tried with just one layoutparams, but it seem to affect both pictures even if I make changes to it after the first picture is added.

Also I have a function that can return a string such as blue_1 o red_1 as a string to help me print my images, but it wont allow me to put strings into the i.setImageResource to get the picture 🙁

What can i do?

  • 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-05-16T00:04:02+00:00Added an answer on May 16, 2026 at 12:04 am

    The best way to tidy your code up would be to get rid of almost all of it and just define your layout in an XML file. That way you can just call setContentView(R.layout.my_layout_name) instead of manually creating the widgets and layout params yourself. If you’re not too familiar with declaring layouts in XML, have a look at the dev docs. Alternatively, if you need to instantiate different parts of the UI separately, you can inflate the layout with a LayoutInflater.

    For the second question, the Resources class has a getIdentifier method. You can do something like this:

    getResources().getIdentifier("blue_1", "drawable", "com.my.package.here")

    That’ll return the resource ID, which you can then use instead of R.drawable.blue_1.

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

Sidebar

Related Questions

I have made a little program in java that accepts a string as a
I have a little problem... I have made an Android application which extends the
I have made little program for computing pi (π) as an integral. Now I
I have a little but annoying problem. I have made an app where the
I have one little problem...Here is my code..Is there a way to distribute weights
I have made a little app for signing up for an event. User input
I have made some code which exports some details of a journal article to
I have made a new windows service which works fine using barebone code (just
So I have made a webservice that interfaces with a set of data contained
I have a JavaScript class that I have made and put it into its

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.