I think this would not work. TextView extends View already... And If I
put the TextView within a LinearLayout e.g. and inflate it in the
constructor of my custom view, I have to set the attributes manually
too. It seems I have to retrieve all my TextViews and set the Typeface
for each separate in the code. The idea of an extended TextView would
be the best and clean way, but I think its not possible as long as I
add the android based attributes to the "declare-stylable set" too...
I think this is really ugly... If I forget one - someone wonders why
their set attributes not act for my custom view.

Best regards,
-Danny S.


On 30 Nov., 12:33, "Danny S." <danny.schi...@googlemail.com> wrote:
> Note: I renamed it from MemoryTextView to MyTextView but got not all
> of them. Don't mind about the different names. In principle it works,
> but I cant use the android default attributes for my custom text
> view...
>
> On 30 Nov., 12:26, "Danny S." <danny.schi...@googlemail.com> wrote:
>
>
>
>
>
>
>
> > Hi,
>
> > I want to create a custom TextView that uses custom Typeface. I do
> > this, cause I won't set Typeface for each TextView within the code. I
> > created a class:
>
> > public class MyTextView extends TextView {
> >         public MemoryTextView(Context context, AttributeSet attrs) {
> >                 super(context, attrs);
>
> >                 // retrieve attributes
> >                 TypedArray array = mContext.obtainStyledAttributes(attrs,
> > R.styleable.MemoryTextView);
>
> >                 String font = 
> > array.getString(R.styleable.MemoryTextView_ttfName);
> >                 // set font if defined, otherwise use standard font
> >                 if (font != null)
> >                         setFont(font);
>
> >                 // free up memory
> >                 array.recycle();
> >         }
>
> > }
>
> > I created the attributes for my view within the attrs.xml. And I can
> > use the view and see my custom font.
>
> > In Layout I use my view this way:
> > <my.package.structure.views.MyTextView
> >   android:layout_width="wrap_content"
> >   android:layout_height="fill_parent"
> >   myTextView:ttfName="AldrichRegular"
> >   myTextView:text="@string/app_name"
> >   android:textColor="@color/white"
> >   android:textStyle="bold"
> >   android:textSize="18sp"
> >   android:gravity="center" />
>
> > Now the Problem: android based attributes I set does not work. My text
> > is not bold, is not sized 18sp and gravity is not set like I would. If
> > I add the android attribute to my "declare-styleable" it is used (e.g.
> > the gravity).
>
> > <declare-styleable name="MemoryTextView">
> >     <attr name="android:gravity" />
> >     ... other custom attributes
> > </declare-styleable>
>
> > But I can not list all the possible TextView styles to this list. It
> > seems not to be the clear way. Where is my mistake? I only want a
> > TextView with all of it's behavior and additional functionality...
>
> > Thanks a lot,
> > -Danny

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to