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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T07:53:18+00:00 2026-06-17T07:53:18+00:00

I have an LinearLayout (with 7 TextView Elements), within a HorizontalScrollView. The HorizontalScrollView is

  • 0

I have an LinearLayout (with 7 TextView Elements), within a HorizontalScrollView.
The HorizontalScrollView is set to fillViewport.
I want only 4 TextView elements to be visible at a time. The user can scroll to view the rest.

Case 1:
I am able get the required layout using layout_weight but then I am not able to scroll, as shown in the attached code. I am assuming the scroll doesn’t work because weights are computed after GUI renders and so the width of the HorizontalScrollLayout doesn’t change. Is that right?

Case 2:
If I fix the width, eg “60dp”, Then it displays as required and I can scroll as well. However, this wont work on other screen sizes.

How can I achieve this effect in a way that it works with different screen sizes.

Here’s the code for Case 1.

Layout:

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:orientation="horizontal" 
        android:weightSum="7">

        <TextView
            style="@style/ViewStyle"
            android:text="1" />

        <TextView
            style="@style/ViewStyle"
            android:text="2" />

        <TextView
            style="@style/ViewStyle"
            android:text="3" />

        <TextView
            style="@style/ViewStyle"
            android:text="4" />

        <TextView
            style="@style/ViewStyle"
            android:text="5" />

        <TextView
            style="@style/ViewStyle"
            android:text="6" />

        <TextView
            style="@style/ViewStyle"
            android:text="7" />
    </LinearLayout>

Style:

<style name="ViewStyle">
    <item name="android:layout_weight">1</item>
    <item name="android:layout_width">0dp</item>
    <item name="android:layout_height">60dp</item>
    <item name="android:layout_centerVertical">true</item>
    <item name="android:layout_centerHorizontal">true</item>
    <item name="android:gravity">center</item>
    <item name="android:textSize">10sp</item>
    <item name="android:textColor">@color/white</item>
</style>
  • 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-17T07:53:19+00:00Added an answer on June 17, 2026 at 7:53 am

    Using the layout_weight in a LinearLayout wrapped in a HorizontalScrollView will not go well for what you want. I suggest you do something like this:

    1. remove the layout_weight attribute from the style, also modify the layout_width to a value(or you could use wrap_content)
    2. post a Runnable on one of your views in the onCreate method to update the text of the TextViews like this:

      // wrapperLinearLayout being your LinearLayout wrapping the 7 TextViews
      wrapperLinearLayout.post(new Runnable() {
      
          @Override
          public void run() {
              // find out the width of the HorizontalScrollView
              HorizontalScrollView hsv = (HorizontalScrollView) wrapperLinearLayout
                      .getParent();
              // the value below will be the new width of all the TextViews so
              // you can see only for initially
              int targetWidth = (hsv.getWidth() / 4) * 7;
              // modify the width of all 7 TextViews
              for (int i = 0; i < wrapperLinearLayout.getChildCount(); i++) {
                  LinearLayout.LayoutParams lpc = (android.widget.LinearLayout.LayoutParams) wrapperLinearLayout
                          .getChildAt(i).getLayoutParams();
                  lpc.width = targetWidth / 7;
              }
          }
      });
      
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a LinearLayout and I want to see when a user has touched
I have created linearlayout with layout:height=fill_parent I have set 3 buttons and one TextView.
I have a LinearLayout view that already contains several elements. I want to add
I want to have two TextView elements appear side by side (in a list
I've a problem I can't solve: inside a ScrollView I only have a LinearLayout.
I have a linearLayout with 2 elements, a textView and a imagebutton, I would
I have a multiline TextView in Android. It is a child of a LinearLayout.
I have multiple textviews in main.xml <LinearLayout... > <textview adndroid:tag=A> ... till to Z.
I have the following XML: <LinearLayout android:id=@+id/linearLayout1 android:layout_width=fill_parent android:layout_height=wrap_content > <TextView android:id=@+id/tv1 android:layout_width=wrap_content android:layout_height=wrap_content
I have a LinearLayout which holds a couple of buttons. To give the user

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.