Unable to display the created CustomView

129 Views Asked by At


Recently I've been trying to create a CustomView.
I am following the tutorial and did as directed but when i tried to run the code my CustomView was not displayed on my android screen.
My code for attrs.xml is:-

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="TimeView">
        <attr name="text" format="string"/>
        <attr name="setColor" format="boolean"/>
    </declare-styleable>

</resources>


Here is the code for my CustomView i.e TimeView.java:-

package com.example.custom;

import java.text.SimpleDateFormat;
import java.util.Calendar;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.util.AttributeSet;
import android.widget.TextView;

public class TimeView
extends TextView{
    public String titleText;
    public boolean color;
    public TimeView(Context context) {
        super(context);
        setTimeView();
        // TODO Auto-generated constructor stub
    }
    public TimeView(Context c,AttributeSet as)
    {
        super(c,as);
        TypedArray ty=c.obtainStyledAttributes(as,R.styleable.TimeView);
        int count=ty.getIndexCount();
        try{
            for(int i=0;i<count;i++)
            {
                int attr=ty.getIndex(i);
                if(attr==R.styleable.TimeView_text)
                {
                    titleText=ty.getString(attr);
                }
                else if(attr==R.styleable.TimeView_setColor)
                {
                    color=ty.getBoolean(attr, false);
                    decorate();
                }
            }
        }
        finally
        {
            ty.recycle();
        }
    }
    public TimeView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);

        setTimeView();

    }
    public void setTimeView()
    {
        SimpleDateFormat sdf=new SimpleDateFormat("hh.mm aa");
        String time=sdf.format(Calendar.getInstance().getTime());
        if(this.titleText!=null)
        {
            setText(this.titleText+" "+time);
        }
        else
            setText(time);
    }
    public void decorate()
    {
        if(this.color==true)
        {
            setShadowLayer(4, 2, 2, Color.rgb(250, 00, 250));
            setBackgroundColor(Color.CYAN);
        }
        else{
            setBackgroundColor(Color.RED);
        }
    }
}


and lastly here is the code for my activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:custom="http://schemas.android.com/apk/res/com.example.custom"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.custom.MainActivity" >

    <com.example.custom.TimeView
        android:id="@+id/tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="40sp"
        custom:text="My View"
        custom:setColor="true"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

</LinearLayout>


I am getting this result:-
Result
I don't know where i am doing mistake.
Please help me!
Thank you in advance.

1

There are 1 best solutions below

1
Moinkhan On BEST ANSWER

Just overwrite your code with my code it's working. You just make mistake while retrieving attributes. Don't forget to add your package name at first line

import java.text.SimpleDateFormat;
import java.util.Calendar;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.util.AttributeSet;
import android.widget.TextView;

public class TimeView
        extends TextView {
    public String titleText;
    public boolean color;

    public TimeView(Context context) {
        super(context);
        setTimeView(context, null);
    }

    public TimeView(Context c, AttributeSet as) {
        super(c, as);
        setTimeView(c, as);
    }

    public TimeView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        setTimeView(context, attrs);
    }

    public void setTimeView(Context c, AttributeSet attrs) {
        TypedArray a;
        if (attrs != null) {
            a = c.getTheme().obtainStyledAttributes(
                    attrs,
                    R.styleable.BLProgress,
                    0, 0);
        } else {
            throw new IllegalArgumentException("Must have to pass the attributes");
        }


        try {
            titleText = a.getString(R.styleable.TimeView_text);
            color = a.getBoolean(R.styleable.TimeView_setColor, false);
        } finally {
            a.recycle();
        }

        SimpleDateFormat sdf = new SimpleDateFormat("hh.mm aa");
        String time = sdf.format(Calendar.getInstance().getTime());
        if (this.titleText != null) {
            setText(this.titleText + " " + time);
        } else
            setText(time);

        decorate();

    }

    public void decorate() {
        if (color) {
            setShadowLayer(4, 2, 2, Color.rgb(250, 00, 250));
            setBackgroundColor(Color.CYAN);
        } else {
            setBackgroundColor(Color.RED);
        }
    }
}

here is the screen shot ..

enter image description here