user1301568
user1301568

Reputation: 2143

How to place layout at the center of screen programmatically?

layout1 is composed of status bar at the top and toolbar at the bottom of screen. And it is defined in xml. I want to place layout2 between layout1's status bar and toolbar programmatically.

setContentView(R.layout.layout1);
layout2 = new MyLayout(this);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
    LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.leftMargin = 0;
params.addRule(RelativeLayout.BELOW, R.id.statusbar); // this does not work.
addContentView(layout2, params);

This code puts layout2 at the top of screen so it hides layout1's status bar. How can I place layout2 at the location I want? Is there other way not using addContentView?

Upvotes: 0

Views: 611

Answers (2)

ca android
ca android

Reputation: 51

LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
    LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

params.gravity = Gravity.CENTER;

I tried this work.

Upvotes: 2

Arvind Kanjariya
Arvind Kanjariya

Reputation: 2097

Set gravity in linear layout programmatically.

LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
        LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

    params.gravity = Gravity.CENTER;

Upvotes: 0

Related Questions