Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add custom string attribute in my custom android View

I have a cusom view derived from TableLayout, and I need to declare String property Title, like this:

<com.mycompany.controls.NavigationControllerView
xmlns:app="http://schemas.usetech.com/apk/res/onroad"
title="@string/nav_title"
...
/>

(to be able specify value via resource reference). I've specified this property in values/attrs.xml:

<declare-styleable name="NavigationControllerView">
    <attr name="title" format="string"/>
    ...
</declare-styleable>

In my code I'm trying:

public class NavigationControllerView extends TableLayout {
public NavigationControllerView(Context context, AttributeSet attrs) {
    super(context, attrs);
    View.inflate(getContext(), R.layout.inc_header, this);
    TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.NavigationControllerView);
    CharSequence title = a.getString(R.styleable.NavigationControllerView_title);
    if (title != null)
        setTitle(title.toString());
}
...

but no luck, title is always null. Do you see where I'm wrong?

like image 814
ZlobnyiSerg Avatar asked Nov 05 '22 19:11

ZlobnyiSerg


1 Answers

You should use

<com.mycompany.controls.NavigationControllerView
    xmlns:app="http://schemas.android.com/apk/res/onroad" 
    app:title="@string/nav_title" 
... /> 

do not miss app: prefix. Also correct namespace uri should be used.

like image 191
woodshy Avatar answered Nov 11 '22 09:11

woodshy