UndeadLeech
UndeadLeech

Reputation: 133

LinearLayout's width and height are zero

I have the problem, that if I try to get my height and width with LinearLayout.getWidth() and LinearLayout.getHeight(), the int I get is 0.

This is my onCreat:

@Override
protected void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    View mainView = getLayoutInflater().inflate(R.layout.activity_game, null);
    LinearLayout container = (LinearLayout) mainView.findViewById(R.id.container);
    view = new GameLayout(this, container.getWidth(), container.getHeight());
    container.addView(view);
    setContentView(mainView);       
    view.setOnClickListener(this);
}

If you have any idea, what I am doing wrong, pls help.

I need this for getting the size of my Layout, because I want, to position my Bitmap at the same percental place on every device (for example every time at the exact center of the screen).

EDIT:

Solution:

@Override
protected void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    View mainView = getLayoutInflater().inflate(R.layout.activity_game, null);
    final LinearLayout container = (LinearLayout) mainView.findViewById(R.id.container);
    mainView.post(new Runnable() 
    {
        @Override
        public void run() 
        {
            view = new GameLayout(GameActivity.this, container.getWidth(), container.getHeight());
            container.addView(view);
            view.setOnClickListener(GameActivity.this);
        }
    } );

    setContentView(mainView);   
}

Upvotes: 0

Views: 5538

Answers (4)

Rufatet
Rufatet

Reputation: 51

LinearLayout container = (LinearLayout) mainView.findViewById(R.id.container); Object container is created. But View have not loaded so, you can not get real size of parameters. Need Load View, then get height and width. You can try it in onResume() method. Result will be the same. I solved creating AsyncTask class and call it after view loaded. So achieved as you wanted.

LinearLayout linearLayout;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_notes);



    new SizeS().execute();
}

private class SizeS extends AsyncTask<Void, Void, Void> {

    @Override
    protected Void doInBackground(Void... params) {
        try {
            Thread.sleep(1);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        linearLayout = (LinearLayout) findViewById(R.id.linearLayoutBookAlbums);
        int k = linearLayout.getWidth();
        Log.d("testA", Integer.toString(k));
        super.onPostExecute(result);
    }
}

Upvotes: 1

Blackbelt
Blackbelt

Reputation: 157437

it is returning zero because the size of your layout has not been calculated. To overcame this situation you can post a Runnable on the mainview queue this way:

  mainView.post(new Runnable() {
        @Override
        public void run() {
            view = new GameLayout(this, container.getWidth(), container.getHeight());
        } );

the runnable is going to be executed as last element of the mainview`s queue. At that time, the size of container should be already calculated.

Edit. container has to be marked as final to be accessed in the inner runnable

Upvotes: 9

Tenfour04
Tenfour04

Reputation: 93561

The height and width will be 0 until after the layout is calculated, which happens after onResume(), I think. Definitely after onCreate(). When you set your GameLayout's size, use LayoutParams.MATCH_PARENT instead.

When you set the content view, that only queues it up to be drawn to the screen. The dimensions are only calculated when it's time to draw the views to the screen, which doesn't happen until after onResume().

Upvotes: 0

Shani Goriwal
Shani Goriwal

Reputation: 2104

you should use this:

new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,getWindowManager().getDefaultDisplay().getHeight()-100));

or this one also:

ll.setLayoutParams(new android.view.ViewGroup.LayoutParams(
        android.view.ViewGroup.LayoutParams.FILL_PARENT,
        android.view.ViewGroup.LayoutParams.WRAP_CONTENT));

Upvotes: 0

Related Questions