I have a big library of custom components that I want to use in my Android app.
All components use custom attributes to customize its content.
This is a sample component declared in a XML file:
<myapp.CustomTextView
android:id="#+id/text_view_1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="25dp"
android:layout_marginRight="25dp"
android:layout_marginBottom="8dp"
custom:textContent="This is my text" />
This is the custom attributed declared:
<declare-styleable name="CustomTextView">
<attr name="textContent" format="string" />
</declare-styleable>
And this is the implementation of the custom component:
public class CustomTextView extends TextView {
private Context context;
public CustomTextView(Context context) {
this(context, null);
}
public CustomTextView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public CustomTextView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
readAttrs(context, attrs);
}
private void readAttrs(Context context, AttributeSet attrs) {
TypedArray array = context.getTheme().obtainStyledAttributes(attrs, R.styleable.CustomTextView,0, 0);
try {
String content = array.getString(R.styleable.CustomTextView_textContent);
initContent(context, content);
} finally {
array.recycle();
}
}
private void initContent(Context context, String content) {
...
}
}
The problem is that I'm using data binding in my app, so if I do this, the app doesn't compile:
<myapp.CustomTextView
android:id="#+id/text_view_1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="25dp"
android:layout_marginRight="25dp"
android:layout_marginBottom="8dp"
custom:textContent="#{mainView.content}" />
After reading a lot, the solution seems to be to create a bindingadapter for that custom attribute, but in my case, create a binding adapter for every custom attribute would be a huge work because there are tons of components and attributes.
Is there a way to reduce this amount of work or to adapt data binding to these custom attributes?
Related
How can i change those font attributes of a SwitchPreference?
I already tried to use app:layout attribute with the following layout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="#android:id/title"
style="#android:style/TextAppearance.Widget.TextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="MY-FONT"
android:text="Title" />
<TextView
android:id="#android:id/summary"
style="#android:style/TextAppearance.Widget.TextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="MY-FONT"
android:text="Summary" />
</LinearLayout>
That doesn't work well, because the switch was missing and everything looks a bit messy.
What is an easy (and working) way to customize the fontfamily and textSize?
You need to create a Custom Switch Preference by extending its main class
public class CustomSwitchPreference extends SwitchPreference {
public CustomSwitchPreference(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
public CustomSwitchPreference(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
public CustomSwitchPreference(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CustomSwitchPreference(Context context) {
super(context);
}
#Override
public void onBindViewHolder(PreferenceViewHolder holder) {
super.onBindViewHolder(holder);
TextView title1= (TextView) holder.findViewById(android.R.id.title);
title1.setTypeface(ResourcesCompat.getFont(getContext(),R.font.noto_sans_regular));
TextView title2= (TextView) holder.findViewById(android.R.id.summary);
title2.setTypeface(ResourcesCompat.getFont(getContext(),R.font.noto_sans_regular));
}
}
and use this is in your preferences xml
from
<SwitchPreference
.....
/>
to
<com.example.myapp.CustomSwitchPreference
....
/>
This Worked for me
To change the font family in android first you need to have the font in your app something like this
Your font goes in the font directory under res
Now to use it, your going in the right way
<Button
...
android:fontFamily="#font/nameOfFont"
... />
Let me now if it works for you
I need to create my own ImageView.
This is my class:
public class Brick extends ImageView implements Serializable{
public Brick(Context context) {
super(context);
}
public Brick(Context context, AttributeSet attrs) {
super(context, attrs);
}
public Brick(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
}
When I try to use my ImageView inside a xml layout file (as you can see below) I have a problem. I can see a black shape, but there is not the image (the drawable called d) inside it.
<com.myapp.Brick
android:id="#+id/myBrick"
app:srcCompat="#drawable/d"
android:background="#000000"
android:layout_width="300dp"
android:layout_height="300dp" />
What's my error?
You should call android:src="#drawable in your XML Section .
<com.myapp.Brick
android:id="#+id/myBrick"
app:srcCompat="#drawable/d"
android:background="#000000" // showing Black Shape Background
android:layout_width="300dp"
android:layout_height="300dp"
android:src="#drawable/add_image /> // Add android:src
You should add app:srcCompat or android:src in your XML Section
I extended the TextView class to "CustomTextView" that's why I needed to set a custom font. So it's the result:
public class CustomTextView extends TextView {
public CustomTextView(Context context, AttributeSet attrs) {
super(context, attrs);
this.setTypeface(Typeface.createFromAsset(context.getAssets(), "fonts/nn.otf"));
this.setTextSize(30);
}
}
You can see that I setted a default textSize that it's 30:
When I want this CustomTextView, I use this code:
<com.calendar.CustomTextView
android:id="#+id/edData"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="DOM 21 GIU"
android:textSize="45dp"/>
If you notice, I setted the textSize value to 45dp, but the it remains 30 (from the custom class).
How do I set a different textSize? Also, for bold style?
you should remove the this.setTextSize(30); from the constructor, because the xml layout do the resize in during the super(context, attrs) call, and the bold font should be included in the otf file (usually are different otf files for different styles)
This is my solution: (I made tests with textColor and not textSize, but it's the same).
I edited the res/values/atrs.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="CustomTextView">
<attr name="textColor" format="string" />
</declare-styleable>
</resources>
Then, I edited my class like:
public class CustomTextView extends TextView {
public CustomTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init(attrs);
}
public CustomTextView(Context context, AttributeSet attrs) {
super(context, attrs);
init(attrs);
}
public CustomTextView(Context context) {
super(context);
init(null);
}
private void init(AttributeSet attrs) {
if (attrs != null) {
TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.CustomTextView);
String fontName = a.getString(R.styleable.CustomTextView_fontName);
if (textColor != null) {
this.setTextColor(Color.parseColor(textColor));
} else {
this.setTextColor(Color.parseColor("#000000"));
}
a.recycle();
}
}
}
So this work:
<com.imgspa.listviewadapter.CustomTextView
android:id="#+id/ed"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
listviewadapter:textColor="#FF0000"/>
<com.imgspa.listviewadapter.CustomTextView
android:id="#+id/edRis"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="35dp" />
The first custom textview text is red, the second one is black
To set the style just use android:textStyle="bold". For your other problem, you can check the textSize from the attributes I believe using attrs.getAttribute(NAME OF ATTRIBUTE) method and then you can set it to that value or set it to 30 depending on what you want.
Typeface fontRobo = Typeface.createFromAsset(context.getAssets(), "fonts/Roboto-Black.ttf");
viewTotalValue.setText(total.toString());
You could create your own TextView by overriding the TextView like this:
public class MyTextView extends TextView {
public MyTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
setType(context);
}
public MyTextView(Context context, AttributeSet attrs) {
super(context, attrs);
setType(context);
}
public MyTextView(Context context) {
super(context);
setType(context);
}
private void setType(Context context){
this.setTypeface(Typeface.createFromAsset(context.getAssets(),
"foo.ttf"));
this.setShadowLayer(1.5f, 5, 5, getContext().getResources().getColor(R.color.black_shadow));
}
}
And use it like this:
<com.your.project.package.MyTextView
android:id="#+id/oppinfo_mtv_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:text="Player 1"
/>
You could create a custom class that extends TextView, let's say FontTextView.
Define a special string attribute for that class, let's say "font".
Then, in your FontTextView constructor based on the value of the font attribute, choose the appropriate Typeface from your assets.
See:
Creating a View Class Google Tutorial
Defining custom attributes SO Post
Extending TextView just for setting font looks so expensive and not good.
The most clear way is to use Android Data-Binding Framework and BindingAdapter:
#BindingAdapter("bind:font")
public static void setTypeface(TextView textView, int index) {
Typeface myTypeface = //retrieve typeface from cache, based on some font index
textView.setTypeface(myTypeface);
}
declaration in xml:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:font="#{R.attr.Proxima_Nova_Regular}"
/>
attrs.xml:
<attr name="Proxima_Nova_Regular"/>
<attr name="Proxima_Nova_Black"/>
<attr name="Proxima_Nova_Bold"/>
or use resources integers same way
In your cache/creator helper determine dependencies between R.attr.Your Font and instance of typeface.
I am using a TextSwitcher with a TextView factory. I would like to pass the style I set on the TextSwitcher down to the TextViews.
TextSwitcher does not have a 3 arg constructor.
Is it possible to get the style attribute from the attribute set?
Xml
<com.my.TextSwitcher
style="#style/My.TextView.Style"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
Java
public class MyTextSwitcher extends TextSwitcher {
public MyTextSwitcher(Context context, AttributeSet attrs) {
super(context, attrs);
int style = attrs.getAttributeIntValue("", "style", 0); // I tried this to no avail
setFactory(new MyTextViewFactory(context, attrs, style));
}
private static class MyTextViewFactory implements ViewFactory {
private final Context context;
private final AttributeSet attrs;
private final int style;
public MyTextViewFactory(Context context, AttributeSet attrs, int style) {
this.context = context;
this.attrs = attrs;
this.style = style;
}
#Override
public View makeView() {
return new TextView(context, attrs, style);
}
}
}
is the only answer to make my own custom attribute of int that will pass the style? I can't use the built in style tag?
You can retrieve the style attribute(and pass it to the inner views) using:
attrs.getStyleAttribute()
or its equivalent(as the docs mention):
getAttributeResourceValue(null, "style")
One alternative is to declare the TextView's in XML, however this gives less flexibility to the amount of child TextView's I can have.
<com.my.TextSwitcher
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="#style/My.TextView.Style"
android:text="#string/some_text" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="#style/My.TextView.Style"
android:text="#string/some_other_text" />
</com.my.TextSwitcher>