Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - How to store an extra hidden information(s) (by tag) in layout

I have a layout like this-

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <!-- Linearlaout which acts as ListView in this case -->
    <LinearLayout
        android:id="@+id/Main_View_Reference"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        android:contentDescription="This is a container reference for main list items"

    </LinearLayout>
</ScrollView>

And another layout for inflating the "LinearLayout" part of this layout like this,

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/white"
    android:id="@+id/Level_3_Layout"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/Level_3_Item_Name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/background_light"
        android:text="Itame Name"
        android:textColor="@android:color/black"
        android:textSize="18sp" />

    <TextView
        android:id="@+id/Level_3_Item_Price"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/background_light"
        android:text="Item Price"
        android:textColor="@android:color/black"
        android:textSize="13sp" />

    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="@android:color/darker_gray" />

</LinearLayout>

What I want to do is I want to assign a tag value for the dynamic inflated layouts created from second part.

So, I want to set extra hidden information for each created (inflated) layout with Java to store some extra information so that I can retrive them in next time when I need them like HTML's hidden field.

Is there any way in android?

Thanks for helping.

like image 402
Abrar Jahin Avatar asked Feb 09 '23 16:02

Abrar Jahin


2 Answers

I'm not rally sure what did you mean by 'dynamic', but I assume you want to assign not just the value, but also the key for that 'hidden field' of yours. If my assumptions is correct, then you could utilize this method:

yourView.setTag(int id, Object object);

I know you're reluctant to use setTag but in this use case, I think it is the most suited thing to do. Do note the int id though, using that you could assign as may tag as you want (as long they all possess different id) thus making it pretty dynamic. Should you want to retrieve a tag you had assigned using the aforementioned method, you call this:

yourView.getTag(int id);

Hope this helps!

If this is still not what you wanted, I'm afraid the only option would be to extend your desired View and then customizing it to your needs.

like image 79
Hadi Satrio Avatar answered May 07 '23 16:05

Hadi Satrio


For layouts use android:tag="", e.g.

....
<View 
    android:tag="YOUR_HIDDEN_THINGIE"
    .../>
like image 34
Dimitar Genov Avatar answered May 07 '23 17:05

Dimitar Genov