3

Is there a way to get the parameters from a XML view, modify some stuff on it and then use it as content view ?

Let's say I have a normal LinearLayout and I want to make that work:

LinearLayout layout = (LinearLayout) findViewById(R.layout.main);
setContentView(layout);

Instead of :

setContentView(R.layout.main);
1
  • 1
    and by Pragmatically you mean programatically, i assume? Commented Sep 15, 2011 at 9:29

2 Answers 2

4

Yes.

To be more specific, we need more specific info from you.

Edit

You can, for example, do the following. Say you have in your xml specification a TextView:

<RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

<TextView
    android:id="@+id/mytv"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="22sp"
    android:textStyle="bold"/>

</RelativeLayout>

Now you want to center horizontal the TextView programmatically:

setContentView(R.id.main);
TextView myTV = (TextView) findViewById(R.id.mytv);
RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) myTV.getLayoutParams();
lp.addRule(RelativeLayout.CENTER_HORIZONTAL);
myTV.setLayoutParams(lp);

You just set the contentview at the start, and don't need to set it again when you change the variables.

Sign up to request clarification or add additional context in comments.

Comments

1

You can do anyything you want to the layouts even after setContentView. When you do operations like add items to a layout or set a background, the views in the screen are redrawn.

onCreate method is where you can modify layouts as it it about to begin drawing on to a screen.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.