Newbie: set content view which consists of two parts - android

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??

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);

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);

Related

how to create RelativeLayout, do some actions in it, and then remove it in Kotlin android

assume I have this fragment
<android...ConstraintLayout
...>
<ScrollView .../>
<RelativeLayout
android:id=“#+id/fixed”
android:layout_width=“match_parent”
android:layout_height=“match_parent” />
<cl/>
what I would like to do is:
first, I create the RelativeLayout programmatically in kotlin
then, add the attributes to it
then, add it to its proper parent
finally, remove it
Any help will be appreciated
val myLayout = RelativeLayout(context).apply {
layoutParams = *YourParentsLayoutType*.LayoutParams(
*YourParentsLayoutType*.LayoutParams.MATCH_PARENT,
*YourParentsLayoutType*.LayoutParams.MATCH_PARENT)
parentLayout.addView(this)
}
/* ... */
parentLayout.removeView(myLayout)
Replace YourParentsLayoutType with your parent's layout type, for example FrameLayout.
If it's ConstraintLayout, you might need to set its constraints also.

Custom ViewGroup by inflating xml

I am trying to creating a custom view extends RelativeLayout.
To avoid adding view by coding. I prepare a xml with my custom relativelayout.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android">
...
</RelativeLayout>
So in my custom view class, I would inflate the xml by
View.inflate(context, R.layout.custom_view, this);
My question is as my class is already a RelativeLayout, if I doing so, I would have two levels of RelativeLayout. Of course, I can solve it by removing the outer RelativeLayout in xml. But if I am doing so, I cannot see preview in xml editor in eclipse which is the reason I want my custom view inflate from xml.
Even my custom view class extends FrameLayout, there would be one layer more in the view hierachy. How to solve this problem?
To reduce the unnecessary extra layer, you need to use merge tags. Here is a good example on how to use it.
http://android-developers.blogspot.com/2009/03/android-layout-tricks-3-optimize-by.html
<merge xmlns:android="http://schemas.android.com/apk/res/android">
...
</merge>

How to set ImageView as background for any container?

I'm in need to set an ImageView as a background for a let's say LinearLayout. Is that possible to do so?
I wish you wrote more details about what you require. If this is only about setting an image background, you can use the android:background property of LinearLayout. However, to answer your question directly, a view can be placed behind another using FrameLayout:
<FrameLayout>
<ImageView/>
<LinearLayout>
...
...
</LinearLayout>
</FrameLayout>
Yes it should be possible. In the layout xml, try wrapping the ImageView inside the LinearLayout. Then in Activity OnCreate function set the xml as content layout.

Android - Add dynamic xml to layout

I want to merge between layout file and xml file.
But, my problem, is how can I do it dynamically.
I mean that I have general toolbar file that contains: my app icon, the activity name and linearlayout space for button. I want to merge this toolbar to each activity, so that all activity could put its own buttons in the linearlayout in the toolbar.
I have try to do that, I wrote toolbar file that called toolbar.xml in the layout folder.
And each activity included this toobar like that: <include layout="#layout/toolbar.xml" />, but I do not know how to insert the buttons.
Can I do that with the xml of the activity only?
To add buttons from code (which I guess is what you want to do) you just need to have a container where your buttons shall be inserted into. You can just add <LinearLayout> to your layout XML file (ensure your layout got id, i.e.:
<LinearLayout>
...
android:id="#+id/button_container"`
</LinearLayout>
Them, in your code create your button as any other object:
Button myButton = new Button( mContext );
then find your button container:
LinearLayout buttonContainer = findViewById(R.id.button_container);
and add your button to it:
buttonContainer.addView( myButton );
To find out more see ViewGroup documentation

Android: How to add two views to one activity

I have a program where I want to add two views in one activity, like
public class AnimationActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new GraphicsViewForBitmap(this));
setContentView(new GraphicsView(this));
}
}
where GraphicsViewForBitmap & GraphicsView are two classes extends view.
so I want at a time two views should set to an activity.
Is it possible?
Plz give me answer.
Thanks
setContentView() will display only the view that you have set . If you want to display more than one view then you can add both the view in your layout XML file inside any Layout like LinearLayout,RelativeLayout etc. Then you can use setContentView(R.layout.yourXML).
Here is how you can do it in your XML...
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<com.yourpkg.GraphicsView
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
<com.yourpkg.GraphicsViewForBitmap
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
</LinearLayout>
Yes but first you have to put them inside a ViewGroup, for example a LinearLayout, and then set that ViewGroup with setContentView. Because with the existing code you're not just appending the second view with the first, but you are setting another content.
Add the second view to the first view.
LinearLayout childLayout = new LinearLayout(this);
childLayout.setOrientation(LinearLayout.HORIZONTAL);
childLayout.addView(graphicsView);
parentLayout.add(childLayout);
Another way is to create a 2nd layout XML, say main2.xml (the 1st being main.xml). Then you can swap from one to another via, e.g. an ActionBar button, etc. as follows:
setContentView(R.layout.main2); // Pass from layout #1 to layout #2
setContentView(R.layout.main); // Pass from layout #2 back to layout #1
(You can create as many views as you like ...)

Categories

Resources