3

I am adding horizontalScrollView programatically , but when I try to do horizontalScrollView.getMeasuredWidth() it keeps returning 0.

void addCategory(String catTitle) {
    mVideos = mShows.get(catTitle);
    LinearLayout theLayout = (LinearLayout)findViewById(R.id.activitymain);
    TextView textview=(TextView)getLayoutInflater().inflate(R.layout.categorytitle,null);
    textview.setTextColor(Color.CYAN);
    textview.setTextSize(20);
    textview.setText(catTitle);

   HorizontalScrollView horizontalScroll = new HorizontalScrollView (this,null);
    LinearLayout LL = new LinearLayout(this);
    LL.setOrientation(LinearLayout.HORIZONTAL);
    LayoutParams LLParams = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
    LL.setLayoutParams(LLParams);
    HorizontalGalleryAdapter adapter = new HorizontalGalleryAdapter(this,mVideos);
    for (int i = 0; i < adapter.getCount(); i++) {
          View item = adapter.getView(i, null, null);
          LL.addView(item);
    }



    horizontalScroll.addView(LL);
    int maxScrollX = horizontalScroll.getChildAt(0).getMeasuredWidth()-horizontalScroll.getMeasuredWidth();
    AlertDialog alertDialog = new AlertDialog.Builder(this).create();
    alertDialog.setTitle("Reset...");
    String max= String.valueOf(maxScrollX);
aafonso1991
  • 142
  • 9
  • Are you doing this in onCreate()? The measurements are not available just yet. You have to do it after the view is drawn. See http://stackoverflow.com/questions/6939002/if-i-call-getmeasuredwidth-or-getwidth-for-layout-in-onresume-they-return-0 and http://stackoverflow.com/questions/4142090/how-do-you-to-retrieve-dimensions-of-a-view-getheight-and-getwidth-always-r – BVB Dec 27 '13 at 23:12
  • Hi @BVB I'm doing this on a function that gets called after my async task gets completed – aafonso1991 Dec 27 '13 at 23:18
  • Is that function called on the UI thread or in doInBackground() of the AsyncTask? Can the async task ever complete before the UI is fully drawn? – BVB Dec 28 '13 at 10:26
  • its called at the end of onPostExecute() – aafonso1991 Dec 28 '13 at 18:45
  • When do you launch the AsyncTask? Make sure that it can't finish before the views in your Activity are completely drawn. – BVB Dec 29 '13 at 20:18
  • I think i Launch it as soon as my activity gets created – aafonso1991 Dec 30 '13 at 18:20
  • One way is to make sure to start the AsyncTask after the views have loaded. This, of course, would lead it to finish after everything is set up and has a width. – BVB Dec 30 '13 at 18:23
  • I call the asyncTask inside the onCreate method and i Call setContentView right after I called the asynctask execute command – aafonso1991 Dec 30 '13 at 18:30
  • You need to set your content view before calling the AsyncTask. Chances are your task gets done so quickly that the view isn't even set up yet. I would at least move your AsyncTask past the setContentView call. You have all the information needed to solve your issue now. – BVB Dec 30 '13 at 18:32
  • thanks for the help @BVB. I did what you said but unfortunately i still keep getting 0. I setContentView first and called the asyncTask right after that – aafonso1991 Dec 30 '13 at 18:45
  • hey @BVB i'm adding my views programatically. I have an array of "categories", and each category holds an array of videos for that category. I loop through the "categories" Array and for each category I call the function addCategory(string catTItle) wich is the function that I have above. Please look at my edit. I think that my dialog alerts should come up one at a time after each horizontalScrollVIew gets added programatically, but i get the alert dialog boxes right after all the views are loaded. I don't know if this information is helpful but i thought it would be worth sharing – aafonso1991 Dec 30 '13 at 18:58

2 Answers2

2

Ok, I see the problem. You create a HorizontalScrollView, add a child to it, and then immediately try to get its measured width.

You cannot do this. You must add the horizontal scroll view to an existing already-drawn view in your activity first, because otherwise it doesn't have set dimensions yet.

Think about how would it know how many pixels WRAP_CONTENT will set the dimension to before its laid out in your view? If you add it to an existing, already-laid-out view in your activity, then that WRAP_CONTENT will actually get converted to some height.

It looks like you kind-of have a loop - horizontalScroll's dimensions depend on its content (WRAP_CONTENT), yet the content's (LinearLayout's) dimensions depend on the horizontalScroll's dimensions. This does not make sense. Perhaps try MATCH_PARENT for at least the width dimensions of your horizontal scroll view. Then, make sure to not look at dimensions until the view has been drawn.

BVB
  • 5,380
  • 8
  • 41
  • 62
  • and then do a Layout.getChildAt(int).getChildAt().getMeasuredWidth? in which the first "getChildAt" is the horizontalScroll? after I added the horizontalScroll to the layout – aafonso1991 Dec 30 '13 at 19:48
  • Yeah, but honestly, I would just save a reference to the linear layouts. It'd be easier to keep track of. Also, since you are doing a list, why not use a custom ListView? See here for an example tutorial: http://www.androidhive.info/2012/02/android-custom-listview-with-image-and-text/ – BVB Dec 30 '13 at 19:53
1

Have a look into typical usage example for HorizontalScrollView:

// read a view's width
private int viewWidth(View view) {
    view.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
    return view.getMeasuredWidth();
}
....
void getTableRowHeaderCellWidth(){

    int tableAChildCount = ((TableRow)this.tableA.getChildAt(0)).getChildCount();
    int tableBChildCount = ((TableRow)this.tableB.getChildAt(0)).getChildCount();;

    for(int x=0; x<(tableAChildCount+tableBChildCount); x++){

        if(x==0){
            this.headerCellsWidth[x] = this.viewWidth(((TableRow)this.tableA.getChildAt(0)).getChildAt(x));
        }else{
            this.headerCellsWidth[x] = this.viewWidth(((TableRow)this.tableB.getChildAt(0)).getChildAt(x-1));
        }

    }
}

You can also check full details from this nice tutorial: The code of a Ninja.

Gerry Woodberg
  • 336
  • 1
  • 11