Reputation: 75
I have two layouts. The first only has a TextView
and the second has the below code.
I want to merge these two layouts in a way where the first layout displays the aforementioned TextView
with the id "arry" of the second layout. I do this using a FrameLayout
but for this I have to set top margin for the first layout.
I do not want to do this because the first layout will move up and down according to the screen size change. Can anybody tell me how to do this without setting such margin? I want to get the position of the TextView
and after that I want to set the first layout programmatically. Here is the aforementioned code:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.example.user_17.hashmapbasics.MainActivity">
<ImageView
android:id="@+id/img"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="" />
<TextView
android:id="@+id/arry"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="" />
<TextView
android:id="@+id/arry1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="" />
</LinearLayout>
Upvotes: 1
Views: 1106
Reputation: 122
you can use "include" for merge two different layout
<include
android:id="@+id/layout"
layout="@layout/first_layout"
android:layout_gravity="center" />
Upvotes: 0
Reputation: 758
Have you tried include
layout?
If your given layout xml file is, suppose "your_layout.xml
"
Then you can go to you the layout where the TextView
is then just below that add
this line:
<include layout="@layout/your_layout"/>
For Example:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:id="@+id/your_parent_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/app_bg"
android:gravity="center_horizontal">
<TextView android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
android:padding="10dp" />
<include layout="@layout/your_layout"/>
</LinearLayout>
For more to know, go to https://developer.android.com/training/improving-layouts/reusing-layouts.html
Upvotes: 0
Reputation: 13343
Try this one:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.example.user_17.hashmapbasics.MainActivity">
<ImageView
android:id="@+id/img"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="" />
<include
android:id="@+id/first_layout_id"
layout="@layout/first_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/arry"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="" />
<TextView
android:id="@+id/arry1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="" />
</LinearLayout>
where first_layout
- your first layout xml
file.
Update:
For insert layout programmatically You can use this answer of antonyt user:
When adding a
View
to aViewGroup
, you can specify an index which sets the position of the view in the parent.You have two views and so (counting from zero) you would want to add at the 1st position; just call
ll.addView(view, 1)
; to have it placed in between the two TextViews.
In your case You should use secondLayout.addView(firstLayout, 2)
, where firstLayout
is Layout
object for your first layout:
LayoutInflater inflater = LayoutInflater.from(context);
View firstLayout= inflater.inflate(R.layout.first_layout, null, false);
secondLayout.addView(firstLayout, 2);
Upvotes: 2