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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T16:39:39+00:00 2026-06-03T16:39:39+00:00

I have programatically created a set of ImageButton views (with transparent background) as map

  • 0

I have programatically created a set of ImageButton views (with transparent background) as map pins on a static map (ImageView) defined in my app. At certain locations on the map, there are a few ImageButtons that are quite close to each other. I have noticed that in such situation even I am clicking on button 1, the app will think that button 2 is being clicked on and thus proceed to run the activity associated with button 2. Turning off transparency on these buttons reveals that the buttons are actually much bigger than the associated source images and thus some of these buttons have overlapped.

How do I define the imagebuttons such that they are size to the same size as the source images?

Here is a snapshot of relevant code under onCreate():

    ImageButton btnMapLoc = new ImageButton(ResultMapActivity.this);
    RelativeLayout.LayoutParams vp = new RelativeLayout.LayoutParams(
            LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    vp.setMargins(x,y,0,0);  // [left, top, right, bottom] - in pixels
    btnMapLoc.setLayoutParams(vp);
    //btnMapLoc.setBackgroundColor(Color.TRANSPARENT);
    btnMapLoc.requestLayout();

    String imgNameNormal = "map_loc_" + mapLocation + "_normal"; //e.g. map_loc_10_normal
    int idNormal = getResources().getIdentifier(imgNameNormal,"drawable",getPackageName());
    String imgNameClick = "map_loc_" + mapLocation + "_click"; //e.g. map_loc_10_click
    int idClick = getResources().getIdentifier(imgNameClick,"drawable",getPackageName());
    btnMapLoc.setImageResource(idNormal);
    rl.addView(btnMapLoc, vp);

    // Change image as the state of the button changed (normal vs. clicked)
    StateListDrawable states = new StateListDrawable();
    states.addState(new int[] {-android.R.attr.state_pressed},getResources().getDrawable(idNormal)); // Note the "-"
    states.addState(new int[] {android.R.attr.state_pressed},getResources().getDrawable(idClick));
    btnMapLoc.setImageDrawable(states);

Thanks.

  • 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-03T16:39:40+00:00Added an answer on June 3, 2026 at 4:39 pm

    Try this, it works for me.

    Bitmap image = BitmapFactory.decodeResource(getResources(),R.drawable.testbild);            
    
    ImageButton btnMapLoc = (ImageButton) findViewById(R.id.imageButton1);        
    LayoutParams lp = new LayoutParams(image.getWidth(), image.getHeight());
    btnMapLoc.setLayoutParams(lp);
    
    btnMapLoc.setImageBitmap(image);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a button that is programatically created, it's content is a stack panel
I have a custom tableview cell created programmatically that consists of: a background view
I have a WPF app in which I need to programatically manipulate at runtime.
I have a my views and controllers set up like so. A Tab/Bar controller
I have to set the style for a TextView which is created programmatically. How
I have created 10 buttons programmatically and set the titles in the button. Now
I have one ImageButton in my xml layout like that : <ImageButton android:id=@+id/tabsButton android:background=@drawable/button
Say I have a (programatically created) parent LinearLayout parent1 and I add a (programatically
I have created buttons are programmatically and set the button tags. View Did Load:
I have created 5 TextView programmatically, now i want to set few Parameters/Attributes of

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.