0

I have a layout as follows

Horizontal Scroll View: id - container
    Scroll View: width & height - match_parent
        Relative Layout: id - wrapper, width & height - match_parent
            View
            LinearLayout: id - timeline, width & height - match_parent
            LinearLayout: id - body, width & height - match_parent

The "container" has an initial width. I want to add a LinearLayout inside "body" which has the width equal to the container. This linear layout has multiple text views inside it. But I want to increase the width of the "container" and also the linear layout when the text views exceed the initial width so that text views are not clipped when added. Here's how I'm doing it:

This is the LinearLayout im trying to add into the "body"

public class Layer extends LinearLayout {
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        setMeasuredDimension(MeasureSpec.getSize(widthMeasureSpec), 100);
    };

    public void addTextView(CharSequence text) { 
        TextView tv = new TextView(text);
        addView(tv);
        if(this.getWidth() < tv.getX() + tv.getWidth())  {
            RelativeLayout wrapper = (RelativeLayout) findViewById(R.id.wrapper);
            wrapper.setMinimumWidth(size);
            wrapper.invalidate();
        }
    }

I'm getting a NullPointerException at this line 'wrapper.setMinimumWidth(size);'. Is there any other better way to do it? I'm kind of stuck. Any help would be appreciated!

Abdul Qadir
  • 129
  • 10
  • I guess findByViewId(R.id.wrapper) returns null. That might be the bug. – Lajos Arpad Jun 28 '14 at 13:27
  • Yes you're right. findViewById is returning null. Do you have any idea why would it return null, it's working fine when I refer it in my main activity. – Abdul Qadir Jun 28 '14 at 18:08
  • I also tried getRootView().setMinimumWidth() and getRootView().invalidate() ... but the size remains the same! – Abdul Qadir Jun 28 '14 at 18:31
  • Kindly read this, it might help you: http://stackoverflow.com/questions/3264610/findviewbyid-returns-null – Lajos Arpad Jun 28 '14 at 19:05
  • I read all the answers but none seemed to match my situation. I have a custom view (whose code is in the original question) in my fragment as a private member variable and on `onCreateView()` method of the fragment, I just returned that custom view variable. So I've not used the `inflater` or `setContentView()` anywhere in my fragment. Am I doing it wrong? What should I do? Any help would be appreciated!! – Abdul Qadir Jun 28 '14 at 22:31

1 Answers1

0

Ok so I found the solution. I had a fragment which contained my custom view as a private member variable. onCreateView() method of the fragment returned the custom view variable. Inside my custom view class, findViewById() was returning null. So I passed the context by getActivity() from my fragment to my custom view class and casted it into Activity to make it work!!

((Activity)context).findViewById()

Hope this helps!!

Abdul Qadir
  • 129
  • 10