Android UI Layout Simple Question


Last Updated:

  1. danjartin

    danjartin New Member This Topic's Starter

    Joined:
    Jun 10, 2010
    Messages:
    3
    Likes Received:
    0
    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
     

    Advertisement
  2. dmitriyk

    dmitriyk Active Member

    Joined:
    Apr 17, 2010
    Messages:
    44
    Likes Received:
    10
    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.
     

Share This Page

Loading...