Leem.fin
Leem.fin

Reputation: 42602

Newbie: set content view which consists of two parts

I am developing an Android 2.1 app.

I have defined a LinearLayout class:

public class MyTopBar extends LinearLayout {
   ...
}

Then, I have a layout xml file (content.xml):

<LinearLayout>
    ...
</LienarLayout>

I have a RootActivity.java , I would like to set MyTopBar as content in this RootActivity.

Then I have MyActivity which extends RootActivity:

public class MyActivity extends RootActivity{
       //set xml layout as content here    
}

I would like to set the content.xml as content of MyActivity.

As a whole, I would like to use the above way to achieve the layout that MyTopBar should be located on top of the screen always. The other Activities which extend RootActivity will have its content below MyTopBar. How to achieve this??

Upvotes: 0

Views: 2021

Answers (2)

0xC0DED00D
0xC0DED00D

Reputation: 20348

Have a Layout vacant for the TopBar and add Your Topbar in it by using layout.addView(topbarObject); Regarding your second question the setContentView can be called only once, as far as I know. You can however have those two xml files inflated using View.inflate(other_content.xml) and added in the parent xml layout whenever you need it. You can removeView() on parent layout and addView() with the new layout file.

Edit: For the solution of both the question, you can have a parent Layout for eg. like the following:

//Omitting the obvious tags
//parent.xml
<RelativeLayout
    android:id="@+id/parentLayout">
    <RelativeLayout
        android:id="@+id/topLayout">
    </RelativeLayout>
    <RelativeLayout
        android:id="@+id/contentLayout">
    </RelativeLayout>
</RelativeLayout>

Now in your code set the parent layout as content view,make an object of your TopBar layout and add it to the topLayout.

setContentView(R.layout.parent);
MyTopBar topBar=new MyTopBar(this);
RelativeLayout toplayout=(RelativeLayout)findViewByid(R.id.topLayout);
topLayout.addView(topBar); //or you can directly add it to the parentLayout, but it won't work for the first question. So better stick to it.

Now inflate the required xml layout. and add it to contentLayout.

RelativeLayout layout=(RelativeLayout)View.inflate(R.layout.content,null);
contentLayout.addView(layout);//Assuming you've done the findViewById on this.

and when you need to show the other content xml, just call the following code.

contentLayout.removeAllView();
RelativeLayout layout2=(RelativeLayout)View.inflate(R.layout.other_content,null);
contentLayout.addView(layout2);

Upvotes: 0

user
user

Reputation: 87064

1 You could add your custom LinearLayout directly to the xml layout of the MyActivity class like this:

<LinearLayout>
    <com.full.package.MyTopBar 
       attributes here like on any other xml views
    />
    ...
</LinearLayout>

or you could use the include tag to include the layout with the custom view:

<LinearLayout>
    <include layout="@layout/xml_file_containing_mytopbar"
    />
    ...
</LinearLayout>

2 Use :

setContentView(R.layout.other_content);

Upvotes: 1

Related Questions