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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T17:39:52+00:00 2026-05-23T17:39:52+00:00

I have a Gallery with text items. I can change the selected item with

  • 0

I have a Gallery with text items. I can change the selected item with the UI or programmatically with setSelection(position). However when I call this method, sometimes the item background is not changed to a selected state. I noticed that if the item called by setSelection is already drawn on the screen then its background is not updated.

Here is the code. Any help is welcome.

public class Test3 extends Activity {

  private static String[] items = {"0", "1", "2", "3", "4", "5"};

  public void onCreate (Bundle savedInstanceState) {
    super.onCreate (savedInstanceState);
    LinearLayout layout = new LinearLayout (this);
    layout.setOrientation (LinearLayout.VERTICAL);
    setContentView (layout);
    final Gallery gallery = new Gallery (this);
    layout.addView (gallery);
    gallery.setSpacing (0);
    gallery.setAdapter (new Adapter (this));
    gallery.setSelection (0);
    ListView list = new ListView (this);
    layout.addView (list);
    list.setAdapter (new ArrayAdapter <String> (this,
    android.R.layout.simple_list_item_1, items));
    list.setOnItemClickListener (new OnItemClickListener () {
      public void onItemClick (AdapterView <?> parent, View view, int position,
      long id)
      {
        gallery.setSelection (position);
      }
    });
  }

  private class Adapter extends ArrayAdapter <String> {

    public Adapter (Context context) {
      super (context, android.R.layout.simple_gallery_item, items);
    }

    public View getView (int position, View convertView, final ViewGroup parent)
    {
      TextView view = new TextView (getContext ());
      view.setText (getItem (position));
      view.setBackgroundResource (R.drawable.gallery_background);
      view.setGravity (Gravity.CENTER);
      view.setLayoutParams (new Gallery.LayoutParams (Test3.this
      .getWindowManager ().getDefaultDisplay ().getWidth () / 3,
      Gallery.LayoutParams.FILL_PARENT));
      return view;
    }
  }
}

gallery_background.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:state_selected="true" android:drawable="@drawable/highlight_selected" />
  <item android:state_checked="true" android:drawable="@drawable/highlight_selected" />
  <item android:state_focused="true" android:drawable="@drawable/highlight_selected" />
  <item android:state_pressed="true" android:drawable="@drawable/highlight_pressed" />
  <item android:drawable="@drawable/highlight_disabled" />
</selector>
  • 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-23T17:39:53+00:00Added an answer on May 23, 2026 at 5:39 pm

    Try invalidating the gallery or notifyDataSetChanged the galleries adapter.

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

Sidebar

Related Questions

I have a Gallery with ScrollViews as child items. Scroll view can contain text,
I have a Gallery that contains a ScrollView. I can scroll through the text
I have created a XML image gallery, which displays text in between each slide.
Jquery, AJAX and URL changes... I have a gallery, and when the image change
I have used FrameLayout within a Gallery as the Gallery Item. There are 2
We have to implement a vertical Gallery of objects (say static text) with fixed
Basicly I have a photo gallery which has graphic above the photos with text
I have about 100 channel names(text) and a image gallery. And a button on
I'm trying to create a dynamic portfolio gallery where you can hide show items
I have custom gallery. Gallery represents items that are frame layout. There are one

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.