Set a view determined previously in the XML file - android

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

Related

How to show a layout into another view by programming?

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

How to customize TextView inside LayoutInflater

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

Define a view of a XML by code

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

Inflater inflate but height is small ( looks like wrap_content and need fill_parent)

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

Android - Dynamically Add Views into View

I have a layout for a view -
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="0px"
android:orientation="vertical">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/items_header"
style="#style/Home.ListHeader" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/items_none"
android:visibility="gone"
style="#style/TextBlock"
android:paddingLeft="6px" />
<ListView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/items_list" />
</LinearLayout>
What I want to do, is in my main activity with a layout like this
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="0px"
android:id="#+id/item_wrapper">
</LinearLayout>
I want to loop through my data model and inject multiple views consisting of the first layout into the main layout. I know I can do this by building the controls completely within the code, but I was wondering if there was a way to dynamically build the views so that I can continue using a layout instead of putting everything in code.
Use the LayoutInflater to create a view based on your layout template, and then inject it into the view where you need it.
LayoutInflater vi = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = vi.inflate(R.layout.your_layout, null);
// fill in any details dynamically here
TextView textView = (TextView) v.findViewById(R.id.a_text_view);
textView.setText("your text");
// insert into main view
ViewGroup insertPoint = (ViewGroup) findViewById(R.id.insert_point);
insertPoint.addView(v, 0, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT));
You may have to adjust the index where you want to insert the view.
Additionally, set the LayoutParams according to how you would like it to fit in the parent view. e.g. with FILL_PARENT, or MATCH_PARENT, etc.
See the LayoutInflater class.
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
ViewGroup parent = (ViewGroup)findViewById(R.id.where_you_want_to_insert);
inflater.inflate(R.layout.the_child_view, parent);
It looks like what you really want a ListView with a custom adapter to inflate the specified layout. Using an ArrayAdapter and the method notifyDataSetChanged() you have full control of the Views generation and rendering.
Take a look at these tutorials
http://www.softwarepassion.com/android-series-custom-listview-items-and-adapters/
http://developerlife.com/tutorials/?p=327
http://www.androidguys.com/2008/07/14/fancy-listviews-part-one/
To make #Mark Fisher's answer more clear, the inserted view being inflated should be a xml file under layout folder but without a layout (ViewGroup) like LinearLayout etc. inside. My example:
res/layout/my_view.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/i_am_id"
android:text="my name"
android:textSize="17sp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"/>
Then, the insertion point should be a layout like LinearLayout:
res/layout/activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/aaa"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="#+id/insert_point"
android:layout_width="match_parent"
android:layout_height="match_parent">
</LinearLayout>
</RelativeLayout>
Then the code should be
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_shopping_cart);
LayoutInflater inflater = getLayoutInflater();
View view = inflater.inflate(R.layout.my_view, null);
ViewGroup main = (ViewGroup) findViewById(R.id.insert_point);
main.addView(view, 0);
}
The reason I post this very similar answer is that when I tried to implement Mark's solution, I got stuck on what xml file should I use for insert_point and the child view. I used layout in the child view firstly and it was totally not working, which took me several hours to figure out. So hope my exploration can save others' time.
// Parent layout
LinearLayout parentLayout = (LinearLayout)findViewById(R.id.layout);
// Layout inflater
LayoutInflater layoutInflater = getLayoutInflater();
View view;
for (int i = 1; i < 101; i++){
// Add the text layout to the parent layout
view = layoutInflater.inflate(R.layout.text_layout, parentLayout, false);
// In order to get the view we have to use the new view with text_layout in it
TextView textView = (TextView)view.findViewById(R.id.text);
textView.setText("Row " + i);
// Add the text view to the parent layout
parentLayout.addView(textView);
}

Categories

Resources