How to Inflate One View with a Layout

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

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`!!
}

android how to inflate one view defined in a layout file (not whole file)

Can they put into one layout file and inflated separately?

No. When you inflate a layout resource, you are inflating everything inside that file.

Different view components(e.g. Button, TextView) will be created (inflated) depending on different conditions.

It sounds like you might be able to achieve what you want by using ViewStub tags.

To do this, you would create your main layout file as normal. However, anywhere you want to have dynamic content, you add a <ViewStub> tag. Then, in Java/Kotlin, after you inflate your main layout, you can inflate different views into the ViewStub.

  • main_layout.xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Header"/>

<ViewStub
android:id="@+id/stub"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Footer"/>

</LinearLayout>
  • button.xml
<Button
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CLICK ME">
  • text.xml
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Can't click me">
  • MainActivity.java
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_layout);

ViewStub stub = findViewById(R.id.stub);

if (isUseButton) {
stub.setLayoutResource(R.layout.button);
} else {
stub.setLayoutResource(R.layout.text);
}

stub.inflate();
}

Android Adding view from inflate layout and how to update particular textview value in layout

To update a view property you have to store that view reference.
In your case store the views you have created from inflating separate layout.

View view1 = LayoutInflater.from(context).inflate(layoutId, parent, false);
View view2 = LayoutInflater.from(context).inflate(layoutId, parent, false);
View view3 = LayoutInflater.from(context).inflate(layoutId, parent, false);

Then when you receive response from server, get reference the child view you want to update by findViewById() method and update child's property.

Example:

TextView textView1 = view1.findViewById(viewId);
textView1.setText("Updated Text");

TextView textView2 = view2.findViewById(viewId);
textView2.setText("Updated Text");

Edit

// declare it as global
View views[] = new View[size]

for (int i=0; i<AccNoList.size; i++) {
LayoutInflater inflater = (LayoutInflater)getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
views[i] = inflater.inflate(R.layout.my_table_row, myTable, true);
//inflater.setId(i);
ImageButton balancebtn = (ImageButton) views[i].findViewById(R.id.balancebtn);
TextView textview_accnumber = (TextView) views[i].findViewById(R.id.textview_accnumber);
textview_accnumber.setText("Text for this row");
// balancebtn.setId(i); // So that when it is clicked we know which one has been clicked!
balancebtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// once received value from server, i want to update the same row textview balance.
checkBalance(i);
}
});
//textview_accnumber.setId(i);
//tr.setId(i);
textview_accnumber.setText(AccNoList.get(i));

linearlayout.addView(inflater);
}

checkBalance method

public void checkBalance(final int position){
// .................
// ..............
// after received response
updateStatus(position);
}

updateStatus method

private void updateStatus(int position, String text){
if(position < views.length){
TextView textview_accnumber = (TextView)views[i].findViewById(R.id.textview_accnumber);
if(textview_accnumber != null)
textview_accnumber.setText(text);
}
}

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/

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 use View Binding on custom views

Just inform the root, and whether you want to attach to it

init { // inflate binding and add as view
binding = ResultProfileBinding.inflate(LayoutInflater.from(context), this)
}

or

init { // inflate binding and add as view
binding = ResultProfileBinding.inflate(LayoutInflater.from(context), this, true)
}

which inflate method to use will depend on the root layout type in xml.



Related Topics



Leave a reply



Submit