Reputation: 953
I have a requirement where there are 2 programatically generated screens and 2 xml layouts. Now i need to on the fly combine, these layouts multiple times.
For ex, i have screen 1 - programatically created, screen 2 - programatically created, screen 3- from a xml layout, screen 4 - from a xml layout
My final layout design should be a single screen with screen1, screen2, screen 3, screen 4, screen 2... with all screens sharing equal screen space based on the number of screen i input. Please let me know the approach. Some screens are having relative layout and some linear ones. So it should combine these.
Upvotes: 1
Views: 784
Reputation: 2570
You could look into Fragments. They seem to do exactly what you need. Here are the links to the Training and API Guides on them.
In your xml file, you can specify 4 child layouts inside a LinearLayout
parent, each with an attribute android:layout_weight="1"
, so each child layout would only take up the same amount of space. If in portrait orientation, it is suggested to set android:layout_width="match_parent
and android:layout_height="0dp"
Now, you can label the id's of each child layout as id1, id2, id3, etc, but you can also label the two layouts you will create as something likeandroid:id="@+id/fragment_container_first
and android:id="@+id/fragment_container_second
.
In the Java code, you would set the contentView as the id of the xml file (setContentView(R.layout.myXMLLayout);
), create two instances of a Fragment by following the Training guide link I provided above, and add those views to the containers you setup earlier inside your xml files by using something like getSupportFragmentManager().beginTransaction()
.add(R.id.fragment_container_first, firstFragment).commit();
and getSupportFragmentManager().beginTransaction()
.add(R.id.fragment_container_second, secondFragment).commit();
(If you are using the support library, which is what the training guides use).
I really hope this helps you out. You can build a really flexible UI with Fragments. For instance, later on, you can replace the first two fragments with other fragments at runtime, increasing flexibility. You can even setup different UIs for different screen sizes, with a more compact view on a phone, but with much more to offer on a larger screen like a tablet.
I'd love to hear back if this helped you out!
Upvotes: 0
Reputation: 635
You'll need to invoke addView()
on the primary layout. Once the primary layout is built (which holds all the other layouts), the addView()
method will add new views to the existing primary layout.
To add the new layout, you'll need to inflate it first.
LinearLayout primaryLayout;
LayoutInflater layoutInflater = (LayoutInflater)this.getSystemService( Context.LAYOUT_INFLATER_SERVICE );
LinearLayout newLayout = (LinearLayout)layoutInflater.inflate(R.layout.your_new_layout, null, false);
primaryLayout.addView(newLayout);
AddView also provides an index option to place the new layout at a specific point in the primary layout.
Try starting with a blank, XML layout (say called primary_layout):
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/primaryLayout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</RelativeLayout>
Then, as your activity starts, set that first, then inflate and add as desired:
setContentView(R.layout.primary_layout);
LinearLayout primaryLayout = (LinearLayout) findViewById(R.id.primaryLayout);
Then you can add your new views to that one. As for adding multiple times, I believe that it's done by reference, so it only sees a single view. Try building the view in a method, and just returning the view. Such as:
private View buildNewView(){
LayoutInflater layoutInflater = (LayoutInflater)this.getSystemService( Context.LAYOUT_INFLATER_SERVICE );
LinearLayout newView = (LinearLayout)layoutInflater.inflate( R.layout.my_new_view null, false );
return newView ;
}
And call it via primaryLayout.addView(buildNewView();
.
Upvotes: 3