• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Android UI Layout Simple Question

danjartin

Lurker
Hello All,

I am a java novice and a android newbie. I am just wondering if you can nest XML based layouts with layouts specified in java code together. I would for sure think that it is possible but I cannot find any information about it and so far whatever I try does not work. This is some basic code that I have been working with so far:

LinearLayout mLinearLayout;
LinearLayout sLinearLayout;
private Button closebutton;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

LinearLayout mLinearLayout = (LinearLayout) this.findViewById(R.layout.main);

setContentView(R.layout.main);
this.closebutton = (Button) this.findViewById(R.id.button);



// Create a LinearLayout in which to add the ImageView



// Instantiate an ImageView and define its properties
ImageView i = new ImageView(this);
i.setImageResource(R.drawable.bmfrac1);
//Button button = (Button) findViewById(R.layout.main..);

i.setAdjustViewBounds(true); // set the ImageView bounds to match the Drawable's dimensions
i.setLayoutParams(new Gallery.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

// Add the ImageView to the layout and set the layout as the content view
//mLinearLayout.addView(i);
//mLinearLayout.setVisibility(1);
// setContentView(mLinearLayout);


}



I have a linearlayout specified in XML but I would also like to nest it with another layout in the program using java code. Android can handle this, correct?

Thanks,

Dan
 
Android can handle this, but you can (or should) call setContentView once. You can add a ViewGroup layout (such as a LinearLayout) to your XML layout, get it by using findViewById, and call addView on it.
 
Back
Top Bottom