1

At the moment I'm trying to create a own view. Now I found the usefull tutorial on developer.android.net. I followed the instroduction, but I have some trouble to use the view.

First, my folder structur

enter image description here

cView.java

package company.firstactivity.customViews;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;

public class cView extends View{
    public cView(Context context, AttributeSet attrs){
        super(context,attrs);
    }
}

cViewAttributes.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="cView">
        <attr name="showText" format="boolean" />
    </declare-styleable>
</resources>

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="company.firstactivity.MainActivity">

</RelativeLayout>

My problem is the namespace to use the view. What is the namespace? Can anyone help me?

Thanks in advance!

4
  • 3
    What do you mean by namespace? Where did that come from? Commented Jan 26, 2016 at 19:43
  • 1
    You mean you want to know how to include your view at xml level? Commented Jan 26, 2016 at 19:45
  • 1
    @PearsonArtPhoto Press on the link and then the point: Define Custom Attributes. -> Instead of belonging to the schemas.android.com/apk/res/android namespace, they belong to schemas.android.com/apk/res/[your package name]. Commented Jan 26, 2016 at 19:45
  • @thetonrifles correct. I want to use the custom view in the xml (with the attribute showText. Commented Jan 26, 2016 at 19:46

3 Answers 3

4

The namespace in the xml can be anything but it should always have the value http://schemas.android.com/apk/res-auto. Please refer to my_custom_namespace in the following example to understand how you use it:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:my_custom_namespace="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="company.firstactivity.MainActivity">

    <company.firstactivity.customViews.cView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        my_custom_namespace:showText="true"/>
</RelativeLayout>

You can change the my_custom_namespace to app or whatever you'd like to have.

On the other hand, if you'd like to use the custom attributes in the java code of your custom view you can read this value this way:

public cView(Context context, AttributeSet attrs) {
    super(context, attrs);
    initView(context, attrs);
}

public cView(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    initView(context, attrs);
}

private void initView(Context context, AttributeSet attrs) {
    if (attrs != null) {
        TypedArray styledAttrs = context.obtainStyledAttributes(attrs, R.styleable.cView);
        boolean shouldShowText = styledAttrs.getBoolean(R.styleable.cView_showText, false);
        styledAttrs.recycle();
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Let me know if it works or if you have other questions regarding this. If not, you can accept the answer so others can take it as a reference.
yeha, your solution worked well. I accepted it. Thanks!
1

To use a custom view in XML, you have to use the fully specified name. This is the combination of the package with the class name. So yours would be company.firstactivity.customViews.cView

Comments

1

you can move content of cViewAttributes.xml to file attire.xml in res/values

then in your layout xml you can declare xmlns

xmlns:app="http://schemas.android.com/apk/res-auto"

and then you can use

app:showText="false"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.