0

I'm fairly new to android, so don't shoot me right away!

I'm using a linearlayout in android to draw a graph, I'm drawing the graph and set is as background image in a linearlayout.

The linearlayout is child of a relative layout and defined in the layout xml: The relativeLayout contains some other textviews and edittext's as well.

    <RelativeLayout
        android:id="@+id/rl"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >
        <LinearLayout
            android:id="@+id/ll_graph"
            android:layout_width="fill_parent"
            android:layout_height="200dp"
            android:orientation="vertical"
            android:layout_below="@+id/etMemo" >
        </LinearLayout>

    </RelativeLayout>

In OnCreate:

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

Next in OnStart I want to draw the graph, but at that point the ll_graph has a width and height of 0, and therefore displays nothing. If I put a button on the form to execute the same code, the width and height is as expected and the graph is drawn perfectly. What am I missing here?

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
user3432724
  • 11
  • 1
  • 5

4 Answers4

0

The Activity will not necessarily be visible in the onCreate() and onStart() methods. Rather put your drawing code in onwindowFocusChanged(boolean), since the API doc states that "This is the best indicator of whether this activity is visible to the user."

AesSedai101
  • 1,502
  • 2
  • 23
  • 37
  • I think this is more related to rendering pipeline phases (layout, measure, creating display list, executing/drawing) rather than visibility. – stdout Feb 13 '17 at 22:42
0

solution:-

Android lays the view in two phases : measure pass layout pass So This code waits for that phase and when layout has been laid out it will notify you the change.

        ViewTreeObserver vto = ll_graph.getViewTreeObserver();
    vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {



    ll_graph.getViewTreeObserver().removeGlobalOnLayoutListener(
                    this);
        int ll_width = ll_graph.getWidth();
                    int ll_height = ll_graph.getHeight();

        }
    });
Kailash Dabhi
  • 3,473
  • 1
  • 29
  • 47
  • getMeasuredWidth() will not be necessarily the real width of the View. Real width and height is obtained by getWidth() and getHeight(). getMeasuredWidth() returns width of the View that was calculated by onMeasure, but it's parent might set different params on onLayout(). – Yaroslav Mytkalyk Mar 27 '14 at 10:20
  • oh yes you are right.. but i just pasted the code of my application as it was necessary for my app.. but thank you for focusing me to this.:) – Kailash Dabhi Mar 27 '14 at 10:22
0

The Views are not measured at that point yet. But when you press the button, the View

In order to perform drawing when the View has just been measured from outside the View, you can set OnPreDrawListener or OnGlobalLayoutListener to View's ViewTreeObserver. Set this on onCreate() of the Activity.

    ll_graph.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {

        @Override
        public boolean onPreDraw() {
            ll_graph.getViewTreeObserver().removeOnPreDrawListener(this);
            //do your stuff
            return true;
        }
    });
Yaroslav Mytkalyk
  • 16,950
  • 10
  • 72
  • 99
0

You can also post runnuble to a handler of your view. This runnable will execute, when your view is created.

        @Override
        protected void onCreate(final Bundle b) {

        super.onCreate(b);
            ll_graph.post(new Runnable() {
                @Override
                public void run() {
                     ll_graph.setBackgroundResiurce(resId);
                }
            });
    }
rstk
  • 414
  • 5
  • 14
  • In most cases it may work, but it's not reliable method. If the View woun't be laid-out by this time it will fail. It is a better practice to rely on events then on timings. – Yaroslav Mytkalyk Mar 27 '14 at 10:18
  • @doctoror-drive , yes you right. Sorry for wrong answer. Here is good explanation [link](http://stackoverflow.com/questions/4083787/runnable-is-posted-successfully-but-not-run/10457064#10457064) – rstk Mar 27 '14 at 10:28