I'm trying to inflate a layout and show it to a view group. How can I do this?
I have my custom Layout file target_view_layout.xml in resource/layout directory:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<androidx.cardview.widget.CardView
android:id="#+id/targetCardViewId"
...
In the activity.xml file I have a view:
...
<View
android:id="#+id/targetViewId"
android:layout_width="0dp"
android:layout_height="150dp"
...
Inside activity java file I tried this:
private View targetView;
targetView = findViewById(R.id.targetViewId);
LayoutInflater layoutInflater = getLayoutInflater();
targetView = layoutInflater.inflate(R.layout.target_view_layout,null);
how can I attach the inflated view into targetView ?
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);
Your current code only just assigns the inflated view to the variable targetView, what you wanna do is add the inflated view inside the targetView
Use View.add(View view) function to achieve that
private View targetView;
private View view;
targetView = findViewById(R.id.targetViewId);
LayoutInflater layoutInflater = getLayoutInflater();
view = layoutInflater.inflate(R.layout.target_view_layout,null);
targetView.add(view)'
Related
I have a TextView inside this layout inflater which i want to customize. What can be the easy way other than implementing getView()
private ViewGroup buildHeader() {
LayoutInflater infalter = getLayoutInflater();
ViewGroup header = (ViewGroup) infalter.inflate(R.layout.listitem_header, getListView(), false);
//TEXT VIEW SET COLOR
header.setEnabled(false);
return(header);
}
What inflater does is inflate the layout you specified to the view hierarchy.
In other words the inflater builds the objects (views) located in the layout specified, so they can be then used.
Once that is done you can find the views located in that layout with findViewById and manipulate them.
So if you have a layout that consists of:
<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:id="myTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
You can get and use your TextView like this:
TextView textView = (TextView) header.findViewById(R.id.myTextView);
textView.setText("Something"):
textView.setColor(Color.RED);
I am trying to create a custom view (which extends RelativeLayout), which wraps a lot of other views.
I would like to create that child-views in a xml layout file. Now I wonder how I could inflate that layout and use it in my custom view. Something like this would be great (inside my custom view):
RelativeLayout rootLayout = (RelativeLayout) inflater.inflate(my xml file)
this.setContenView(rootLayout);
Unfortunatelly this is only possible in activities. Is there something similar for views?
EDIT:
I don't want to use View.addView(rootLayout) cause that adds another view hierachy, which is not needed.
You could try to use the <merge> tag as the root element in your layout and inflate it in your custom RelativeLayout with this as the parent and attachToRoot set to true. You do not have to call addView then.
Here is a similar example with a LinearLayout (bottom of page), should work with RelativeLayout too.
Use the below
View v =getLayoutInflater().inflate(R.layout.mylayout,null);
// inflate mylayout.xml with other views
CustomRelativeLayout cs = new CustomRelativeLayout(this);
// CustomRelativeLayout is a class that extends RelativeLayout
cs.addView(v); // add the view to relative layout
setContentView(cs); // set the custom relative layout to activity
Example :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="111dp"
android:text="TextView" />
</RelativeLayout>
SView
public class SView extends RelativeLayout {
Paint p,paint;
public SView(Context context) {
super(context);
TextView tv = new TextView(context);
tv.setText("hello");
this.addView(tv);
}
}
In MainActivtiy
View v =getLayoutInflater().inflate(R.layout.mylayout,null);
SView cs = new SView(this);
cs.addView(v);
setContentView(cs);
Snap
Edit:
If you wish to inflate in CustomRelative layout
In the constructor
LayoutInflater inflater = LayoutInflater.from(context);
View v =inflater.inflate(R.layout.mylayout,null);
TextView tv = new TextView(context);
tv.setText("hello");
this.addView(tv);
this.addView(v);
Within your view you can get layout inflater from the context, inflate children and add them to this (sub-class of RelativeLayout)
final LayoutInflater inflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View child = inflater.inflate(R.layout.custom_layout, this, false);
// Then add child to this (subclass of RelativeLayout)
this.addView(child);
Edit:
The code above shows how to inflate children inside a custom view.
This link shows how to insert the custom view itself into XML layout.
I've a XML File that acts like a frame for the activities. I want to set the View I've added in the XML, but when I try it nothing is shown.
<View
android:id="#+id/marco_container"
style="#style/wrapFull"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
This is my Activity code:
setContentView(R.layout.marco);
LinearLayout li = (LinearLayout) findViewById(R.id.marco_container);
View view = View.inflate(getApplicationContext(), R.layout.prueba, null);
li.addView(view);
How can I add the View to the XML?
You must use LayoutInflater. Something like:
setContentView(R.layout.marco);
LinearLayout li = (LinearLayout) findViewById(R.id.marco_container);
View view = getLayoutInflater().inflate(R.layout.prueba, li, false);
li.addView(view);
I've a View declared in the XML file, and I want to define it by code, but when I establish it nothing is shown. Can you help me?
This is my XML file:
[...]
<View
android:id="#+id/marco_container"
style="#style/wrapFull"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
[...]
And I define it by this way:
setContentView(R.layout.marco);
View view = (View) findViewById(R.id.marco_container);
view.inflate(getApplicationContext(), R.layout.prueba, null);
I tried to declare it by this way too:
view = View.inflate(getApplicationContext(), R.layout.prueba, null);
This is prueba.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView android:text="Prueba" style="#style/wrapContent"
android:layout_gravity="center"/>
</FrameLayout>
It's not a View view. It's your custom View, just create a custom view that extends FrameLayout and inside the constructor inflate your view.
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER...);
View view = inflater.inflate(getApplicationContext(), R.layout.prueba, this);
and inside your main xml put :
<com.my.path.CustomView
android:id="#+id/marco_container"
style="#style/wrapFull"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
And in your class :
setContentView(R.layout.marco);
CustomView view = (CustomView) findViewById(R.id.marco_container);
You need to do it this way:
View view2 = view.inflate(this, R.layout.prueba, null);
((ViewGroup)view).addView(view2);
Of course view must be some kind of ViewGroup (RelativeLayout, LinearLayout, etc.)
setContentView(R.layout.marco);
LinearLayout li = (LinearLayout) findViewById(R.id.marco_container);
View view = getLayoutInflater().inflate(R.layout.prueba, li, false);
li.addView(view);
I have layout and I want to inflate that
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<EditText
android:id="#+id/editText1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
</EditText>
</LinearLayout>
like
LinearLayout ll=(LinearLayout)findViewById(R.id.llContainer);
View view;
LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.question_free_text, null);
ll.addView(view);
where ll is
<LinearLayout
android:id="#+id/llContainer"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginTop="20dp"
android:layout_marginBottom="20dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
>
</LinearLayout>
in other xml, but problem is when it inflate it shows but height is big (fill_parent, it looks like wrap_content, but there is no wrap_content in layout). Can anybody help me ?
As Yashwanth Kumar correctly mentioned in the comments, the second parameter of the inflate-method should be the root view in which the new view will be inserted:
LinearLayout ll = (LinearLayout) findViewById(R.id.llContainer);
View view;
LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.question_free_text, ll);
If a root view is provided in the inflate-call, the LayoutInflator calls the generateLayoutParams(ViewGroup.LayoutParams p) method of this root view to get some LayoutParams (basically containing information about how big a view can/should be) which will be passed to the new view.
Note that if you provide a root view, the inflated view will be automatically added to the root view via root.addView(View child, LayoutParams params).
You can also pass a third parameter to the inflate-method (boolean attachToRoot), if this value is false, the new view will not be added automatically to the root view, but the LayoutParams are still set with setLayoutParams(params). You can use this if you wand to add you view manually to the root view (e.g. at a specific position/index):
LinearLayout ll = (LinearLayout) findViewById(R.id.llContainer);
View view;
LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.question_free_text, ll, false); // the LayoutParams of view are set here
ll.addView(view, 2);