android - Unable to display the created CustomView -
i've been trying create customview.
following tutorial , did directed when tried run code customview not displayed on android screen.
code 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 code 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(); } } } { 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); } } }
, lastly here code 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>
getting result:-
don't know doing mistake.
please me!
thank in advance.
just overwrite code code it's working. make mistake while retrieving attributes. don't forget add package name @ 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) { = c.gettheme().obtainstyledattributes( attrs, r.styleable.blprogress, 0, 0); } else { throw new illegalargumentexception("must have pass attributes"); } try { titletext = a.getstring(r.styleable.timeview_text); color = a.getboolean(r.styleable.timeview_setcolor, false); } { 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 screen shot ..
Comments
Post a Comment