Question

i want dynamically add a custom component inside a LinearLayout.

This is a piece of code of my activity, where i want insert a custom component:

 <LinearLayout
            android:id="@+id/layout_cartelle_immagini"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/border_bottom"
            android:orientation="vertical" >

        </LinearLayout>

This is my custom component:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/border_bottom"
    android:layout_marginBottom="2dp"
    android:paddingRight="20dp"
    android:paddingLeft="20dp" >

    <TextView
        android:id="@+id/label_pathFolder"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:gravity="center"
        android:padding="20dp"
        android:textSize="25sp" />

    <Spinner
        android:id="@+id/spinner_level"
        android:layout_width="300dp"
        android:layout_height="wrap_content"
        android:layout_toLeftOf="@+id/btn_show_desc_img"
        android:entries="@array/n_level_array"
        android:padding="20dp"
        android:prompt="@string/n_level_prompt" />

    <ImageButton
        android:id="@+id/btn_show_desc_img"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toLeftOf="@+id/btn_remove_folder"
        android:layout_marginLeft="20dp"
        android:layout_centerVertical="true"
        android:background="@drawable/button_press"
        android:contentDescription="@string/showDescImg"
        android:src="@drawable/ic_desc" />

    <ImageButton
        android:id="@+id/btn_remove_folder"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="20dp"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:background="@drawable/button_press"
        android:contentDescription="@string/showDescImg"
        android:src="@drawable/ic_delete" />

</RelativeLayout>

And this is a piece of code that i used to add the component:

LayoutInflater vi = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View root = findViewById(R.id.layout_cartelle_immagini);
View custom = vi.inflate(R.layout.custom_list_folder, null);
...
..
((ViewGroup) root).addView(custom, 0);

All works fine, but the custom component hasn't the same theme of the application, why?? And how can i fix this problem?

Thanks.

Was it helpful?

Solution

This happens because the inflated does not know which activity or application it belongs to. You need to provide this information in the constructor or get a LayoutInflator from your activity instead of a global context. Try calling getLayoutInflator() from your activity and using it to inflate your layout. It will inflate the layout with the same theme as that of your activity.

OTHER TIPS

You can try: View custom = vi.inflate(R.layout.custom_list_folder, root, true); see LayoutInflater Doc

public View inflate (int resource, ViewGroup root, boolean attachToRoot)

Parameters

resource ID for an XML layout resource to load (e.g., R.layout.main_page);

root Optional view to be the parent of the generated hierarchy (if attachToRoot is true), or else simply an object that provides a set of LayoutParams values for root of the returned hierarchy (if attachToRoot is false.)

attachToRoot Whether the inflated hierarchy should be attached to the root parameter? If false, root is only used to create the correct subclass of LayoutParams for the root view in the XML. Returns The root View of the inflated hierarchy. If root was supplied and attachToRoot is true, this is root; otherwise it is the root of the inflated XML file.

You have to use this method to inflate your custom view xml in the root view.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top