I have a custom view extending the RelativeLayout. In the init(Context context, AttributeSet attrs) method of this class I set the layout file by:
inflate(getContext(), R.layout.in_session_view_layout, this);
I am using this custom view in a list item like this:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:verizon="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:layout_margin="5dp">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center">
<ImageView
android:id="@+id/flag"
android:layout_width="60dp"
android:layout_height="160dp"
android:layout_below="@id/txt"
android:layout_gravity="center"
android:layout_margin="5dp" />
</FrameLayout>
<com.widgets.FreeBeeNotificationView
android:id="@+id/freebee_pre_session_view"
android:layout_width="wrap_content"
android:layout_height="35dp"
android:layout_gravity="right|top"
android:layout_marginBottom="6dp"
android:layout_marginRight="6dp"
android:layout_marginTop="6dp"
verizon:textSize="12sp" />
</FrameLayout>
Now, the requirement is that, this layout file needs to be set in the custom view dynamically based on an user input. But, I receive the user input after the init() method of the custom view has already been called as this method gets called as soon as the FrameLayout given above gets loaded.
How do I dynamically set the layout file to the custom view based on user input? Can someone kindly help me.
Thanks.
Hope I have understood your requirement correctly. If not please explain further. You can do this.
In your custom view make a method called setLayout(int layoutId)
public void setLayout(int layoutId)
{
removeAllViews();
LayoutInflater inflater = LayoutInflater.from(getContext());
inflater.inflate(getContext(), layoutId, this);
}
When you have your user input you call
yourCustomView.setLayout(R.layout.new_layout);
Disclaimer: All this is from memory and not tested so might not compile.