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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T03:37:07+00:00 2026-06-02T03:37:07+00:00

I have a horizontalscrollview containing many textviews that are added dynamically. When I press

  • 0

I have a horizontalscrollview containing many textviews that are added dynamically. When I press on a textview, I would like its background to stay blue, until I click on another textview. In a listview, you would use android:state_activated, but Android wont let me use it for a textview. I have also tried setting the background in the onclicklistener, but it didnt work either.

How could I do this?

activity

public class HorizontaltextscrollerActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        LinearLayout menu = (LinearLayout) findViewById(R.id.menu);

        for (int i = 0; i < 20; i++) {
            TextView t = (TextView) getLayoutInflater().inflate(R.layout.text, null);
            t.setId(i);
            t.setText("item" + i);
            t.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    Toast.makeText(getApplicationContext(), "hi " + v.getId(), Toast.LENGTH_SHORT).show();
                    v.setBackgroundColor(R.color.background_selected);
                }
            });

            menu.addView(t);
        }

    }
}

main.xml

<?xml version="1.0" encoding="utf-8"?>
<HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="#000000" >

    <LinearLayout
        android:id="@+id/menu"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"

        android:orientation="horizontal" >
    </LinearLayout>

</HorizontalScrollView>

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/text_selector"
    android:textSize="24sp"
    android:padding="30dp"
    android:clickable="true" >

</TextView>

text_selector.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:drawable="@color/selected" android:state_pressed="true"/>

</selector>

EDIT1

public class HorizontaltextscrollerActivity extends Activity {
    TextView[] t = new TextView[20];

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        LinearLayout menu = (LinearLayout) findViewById(R.id.menu);

        for (int i = 0; i < 20; i++) {
            t[i] = (TextView) getLayoutInflater().inflate(R.layout.text, null);

            t[i].setId(i);
            t[i].setText("item" + i);
            t[i].setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    Toast.makeText(getApplicationContext(), "hi " + v.getId(), Toast.LENGTH_SHORT).show();
                    t[v.getId()].setBackgroundColor(R.color.background_selected);
                }
            });

            menu.addView(t[i]);
        }
    }
}
  • 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-02T03:37:08+00:00Added an answer on June 2, 2026 at 3:37 am

    create an array of TextViews…

      TextView[] t=new TextView[20];
    

    instead of this..

     TextView t = (TextView) getLayoutInflater().inflate(R.layout.text, null);
    

    use

     t[i] = (TextView) getLayoutInflater().inflate(R.layout.text, null);
      t[i].setId(i);
     //similarly..
      t[i].setOnClickListener(new OnClickListener() {
         for(int m=0;m<20;m++){
       if(m!=i){
        t[m].setBackgroundColor(R.color.putdefaultcolor here)
        }
    
         }
        t[i].setBackgroundColor(R.color.background_selected);
    
     .....
    

    I think then it will work fine..

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

Sidebar

Related Questions

I have a HorizontalScrollView in Android that looks like the following in the xml.
I have HorizontalScrollView with few TextViews. Each TextView might contain text of different length.
I have a custom View that is nested inside ScrollView and HorizontalScrollView like this:
I have a bunch of ListViews inside of a HorizontalScrollView. I would like to
I have a HorizontalScrollView that has lots of views and images inside it. The
I have created one custom view that contains the horizontalscrollview. Now when i changes
I have created one custom view that contains the horizontalscrollview. Now when i changes
i have a HorizontalScrollView, and i am adding a bunch of ListView as its
In my example activity, I have - a ListView containing - multiple HorizontalScrollView containing
I have a HorizontalScrollView with a LinearLayout inside. This LinearLayout is populated (dynamically) with

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.