How to inflate one view with a layout
I'm not sure I have followed your question- are you trying to attach a child view to the RelativeLayout? If so you want to do something along the lines of:
RelativeLayout item = (RelativeLayout)findViewById(R.id.item);
View child = getLayoutInflater().inflate(R.layout.child, null);
item.addView(child);
Inflate a view / layout into another layout?
There is ViewStub
but I never used it and I think it can't be used more than once.
You can inflate the menu layout and attach it to the main layout:
AbsoluteLayout mainLayout = (AbsoluteLayout) findViewById(R.id.your_main_layout);
LayoutInflater inflater =
(LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View menuLayout = inflater.inflate(R.layout.your_menu_layout, mainLayout, true);
then when you want to change you can remove it:
mainLayout.removeView(menuLayout);
and add another the same way.
This will work because you want to add the layout as the last child of the parent layout. If you want to add it, say, at 1st position, you can inflate your layout without attaching it to the parent (use false
as last arg), and adding it manually specifying the index:
mainLayout.addView(menuLayout, 0);
How to inflate view in other layout?
You have to create a new box
View
on each step.
Something like this:
for (int i=0; i<idx; i++ ){
View box = getLayoutInflater().inflate(R.layout.box, null);
LinearLayout ll_box = (LinearLayout)box.findViewById(R.id.ll_box);
for(int j=0; j<3; j++){
ll_box.addView(LayoutInflater.from(this).inflate(R.layout.item, null));
}
ll_list.addView(box); //here add `box` instead of `ll_box`!!
}
Inflate layout programmatically within another layout
You could use something like
LayoutInflater inflater = LayoutInflater.from(context);
//to get the MainLayout
View view = inflater.inflate(container_destacado, null);
...
//Avoid pass null in the root it ignores spaces in the child layout
View inflatedLayout= inflater.inflate(R.layout.yourLayout, (ViewGroup) view, false);
containerDestacado.addView(inflatedLayout);
How to show a layout into another view by programming?
I think your intention is to inflate the target_view_layout.xml into your activity layout view with id targetViewId, if that's the case try,
private View targetView;
targetView = findViewById(R.id.targetViewId);
LayoutInflater layoutInflater = getLayoutInflater();
View child = layoutInflater.inflate(R.layout.target_view_layout,null);
targetView.add(child);
android: inflate views into a layout
I checked your code by creating a project. I am using Android Studio. I did not find any of that problem that you mentioned. Just have a look at my code and try to change yours accordingly.
MainActivity:
public class MainActivity extends Activity {
private LinearLayout mContainer;
private View view;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mContainer = (LinearLayout)findViewById(R.id.inflate_container);
findViewById(R.id.add).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
view = LayoutInflater.from(MainActivity.this).inflate(R.layout.expanded_record,mContainer,false);
mContainer.addView(view,mContainer.getChildCount());
}
});
findViewById(R.id.change_color).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
view.setBackgroundColor(Color.BLUE);
}
});
}
Main XML:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
<LinearLayout
android:id="@+id/inflate_container"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_margin="20dp">
<TextView
android:text="add" android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/add"
android:clickable="true"/>
<TextView
android:id="@+id/change_color"
android:clickable="true"
android:text="change_color" android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</RelativeLayout>
and the XML that I am inflating
<?xml version="1.0" encoding="utf-8"?>
<View xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="10dp"
android:background="#000000"
android:layout_gravity="center">
</View>
This is the result I got
See that I can add black views and change them to blue and then again black
http://postimg.org/image/ggeotcz91/
dynamically include a layout to another layout file inside a fragment
You are getting Error becauseaddView(View child)
need parameter a view
and you are adding FrameLayout
which is ViewGroup
Try this way
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View myView = inflater.inflate(R.layout.sub_layout, null);;
flContainer = (FrameLayout) v.findViewById(R.id.flContainer);
flContainer.addView(myView);
inflate another layout below in android
you got 2 chocies here...
1.Replace RealtiveLayout with LinearLayout in file Feed Fragment (LinearLayout must take the whole screen too so it will insert them one under the other)
2.instand of ScrollView and RelativeLayout use ListView and Adapter which is much useful here.
ListView Example
Related Topics
Send Touch Events to a Device via Adb
Extending Relativelayout, and Overriding Dispatchdraw() to Create a Zoomable Viewgroup
Determine When a Viewpager Changes Pages
Remove Vertical Padding from Horizontal Progressbar
Getting "Debuggable" Value of Androidmanifest from Code
How to Import Eclipse Project to Android Studio
Rotate View Hierarchy 90 Degrees
How to Restart Adb Manually from Android Studio
Why Does It Take So Long for Android's Mediaplayer to Prepare Some Live Streams for Playback
How to Install Android Market App on the Emulator
Why Is My Android Emulator Keyboard in Chinese Character Mode
How to Add an Image File into JSON Object
Android: Application-Wide Font-Size Preference