user415726
user415726

Reputation: 159

Setting value dynamically to a TextView of included view

I have two views layout_a.xml and layout_b.xml.
Inside layout_a and layout_b, there is an include which include another view layout_header.xml.
That layout_header contains a textView which display login user name.
The user name is stored in a singleton class User.

How can I set the username once but not in every activity of my app that include layout_header.xml?

Upvotes: 2

Views: 155

Answers (1)

Ty Smith
Ty Smith

Reputation: 2594

Subclass TextView and have it initialize the singleton.

Java:

public class UserTextView extends TextView{

    public UserTextView(Context context) {
        super(context);
        CharSequence username = User.getInstance().username
        setText(username);      
    }
}

Layout:

<com.example.UserTextView 
 android:id="@+id/username"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content">

Upvotes: 1

Related Questions