Define a view of a XML by code - android

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

Related

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

added footer view to list activity listview but it doesn't show

i have a xml file for footer view with a textview inside it, i have inflated the footer view and extracted textview from it, then i added it to list activity's list view as footer view but it doesn't show when i run it. here is the code:
LayoutInflater inflater=(LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.footer_view, null);
TextView footer=(TextView) view.findViewById(R.id.footer);
getListView().addFooterView(footer);
and here is the xml file content:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/footer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="#string/footer_text"
android:textColor="#0000FF"
android:textSize="20sp" />
what's wrong?
Try adding your footer view like this:
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
TextView footer = (TextView) inflater.inflate(R.layout.footer_view, getListView(), false);
getListView().addFooterView(footer);
Update: Just looked up how I did it in one of my projects, so this should work as well:
TextView footer = (TextView) getLayoutInflater().inflate(R.layout.footer_view, getListView(), false);
getListView().addFooterView(footer);

Custom view: how to set the root layout

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.

Set a view determined previously in the XML file

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

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

Categories

Resources