Following is my custom TextView Class:
public class MontTextView extends android.support.v7.widget.AppCompatTextView{
public MontTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
public MontTextView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public MontTextView(Context context) {
super(context);
init();
}
public void init() {
Typeface tf = Typeface.createFromAsset(getContext().getAssets(), "fonts/Montserrat-Regular.ttf");
setTypeface(tf ,1);
}
}
And in XML I use it like this:
<com.minimalist.gorakh.customviews.MontTextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="#dimen/_20sdp"
android:text="Text"
android:textColor="#fff"
android:textSize="#dimen/_25ssp"
app:layout_constraintBottom_toBottomOf="#+id/betaView"
app:layout_constraintStart_toStartOf="#+id/betaView"
app:layout_constraintTop_toTopOf="#+id/betaView" />
This is working in pre-lollipop(v-19) like this:
And on a marshmallow emulator:
I copied the custom TextView code from a previous project, but couldn't get what am I doing wrong here.
Try this :
import android.widget.*;
import android.util.*;
import android.content.*;
import android.graphics.*;
public class MontTextView extends TextView{
public MontTextView (Context context) {
super(context);
init();
}
public MontTextView (Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public MontTextView (Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
private void init() {
setTypeface(Typeface.createFromAsset(getContext().getAssets(), "fonts/Montserrat-Regular.ttf"));
}
}
Related
i created simple widget as custom checkBox as this below code, in this code setting custom font work fine but i can't check or uncheck CheckBox
public class CustomFontCheckBox extends AppCompatCheckBox {
public CustomFontCheckBox(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
setTypeface(FontManager.getInstance(getContext()).loadFont("fonts/my_font.ttf"));
}
public CustomFontCheckBox(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
#SuppressWarnings("unused")
private void internalInit(Context context, AttributeSet attrs) {
}
}
xml layout:
<com.myapp.test.Widgets.CustomFontCheckBox
android:id="#+id/ch1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="#dimen/sickness_depth_marginRight"
android:button="#null"
android:buttonTint="#color/colorAccent"
android:drawableRight="?android:attr/listChoiceIndicatorMultiple"
android:text="#string/diabetes"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#color/body_text_color"/>
Well I think you are messing with its constructor and its style use it like this
public class CustomFontCheckBox extends AppCompatCheckBox {
public CustomFontCheckBox(Context context) {
super(context);
init();
}
public CustomFontCheckBox(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public CustomFontCheckBox(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
private void init(){
//set your typeface here.
// setTypeface("");
}
}
I need to set my shape as default background for all of my texts and I don't know how to do this, I want to add many TextView to my project so it's really important for me to do this, I've tried searching the internet but no luck :(
try this code
import android.content.Context;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.widget.TextView;
public class CustomTextView extends TextView{
public CustomTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
public CustomTextView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public CustomTextView(Context context) {
super(context);
init();
}
public void init() {
if(!isInEditMode()){
setBackgroundResource("Your shape file id ");// like r.drawable.shape_textview
}
}
}
I am using custom Edittext like this
public class DosisEdittextFont extends TextView {
public DosisEdittextFont(Context context) {
super(context);
init(context);
}
public DosisEdittextFont(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
public DosisEdittextFont(Context context, AttributeSet attrs,
int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context);
}
private void init(Context context) {
Typeface t = Typeface.createFromAsset(context.getAssets(),
"fonts/Dosis-Regular.otf");
this.setTypeface(t);
}
}
But because of that cursor is not showing in EditText. how to set cursor in custom EditText?
You may extends EditText instead of TextView
Try This
public class CustomEditText extends EditText {
public CustomEditText(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
if(!isInEditMode())
init(attrs);
}
public CustomEditText(Context context, AttributeSet attrs) {
super(context, attrs);
if(!isInEditMode())
init(attrs);
}
public CustomEditText(Context context) {
super(context);
if(!isInEditMode())
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_fontFamily);
if (fontName!=null)
{
Typeface myTypeface = Typeface.createFromAsset(getContext().getAssets(), "fonts/"+fontName);
setTypeface(myTypeface);
}
a.recycle();
}
}
}
To Use In Your Layout
<CustomEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/etEmailId"
android:hint="#string/email"
android:textColor="#color/input_text_color"
android:textColorHint="#color/input_text_color"
customfont:fontFamily="YOUR FONT NAME.ttf"
/>
Reason for not appearance of cursor in edittext are many like your theme appearance
i.e. theme style or edittext background and one is that you extending TextView,
so try extending EditText in place of TextView as:
import android.content.Context;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.widget.EditText;
public class CustomEditText extends EditText {
private Context context;
private AttributeSet attrs;
private int defStyle;
public CustomEditText(Context context) {
super(context);
this.context = context;
init();
}
public CustomEditText(Context context, AttributeSet attrs) {
super(context, attrs);
this.context = context;
this.attrs = attrs;
init();
}
public CustomEditText(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
this.context = context;
this.attrs = attrs;
this.defStyle = defStyle;
init();
}
private void init() {
Typeface font = Typeface.createFromAsset(getContext().getAssets(), "fonts/montserrat_bold.ttf");
this.setTypeface(font);
}
#Override
public void setTypeface(Typeface tf, int style) {
tf = Typeface.createFromAsset(getContext().getAssets(), "fonts/montserrat_bold.ttf");
super.setTypeface(tf, style);
}
#Override
public void setTypeface(Typeface tf) {
tf = Typeface.createFromAsset(getContext().getAssets(), "fonts/montserrat_bold.ttf");
super.setTypeface(tf);
}
}
and in you layout xml
<com.example.CustomEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/edittext"
android:hint="text"
android:textColor="#000000"
android:textColorHint="#000000"/>
So, try replacing textview with edittext , if still not resolve, this
occurs due to your activity theme or your edittext background
I have a class which extends LinearLayout. This class has a method called addSubview and the warning message "Method 'actionXYZ' is never used" shows, but the method have public modifier!
Furthermore, this method does not appear when this class is instantiated in a activity, for example.
Why?
public class dummyLayout extends RelativeLayout {
LayoutInflater mInflater;
RelativeLayout rootItem;
public dummyLayout(Context context) {
super(context);
init(context);
}
public dummyLayout(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
public dummyLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context);
}
private void init(Context ctx) {
mInflater = LayoutInflater.from(ctx);
rootItem = (RelativeLayout) mInflater.inflate(R.layout.dummyLayout, this, true);
}
public void addSubView(View v) {
rootItem.addView(v);
}
}
I've written a subclass of EditText. Here is that subclass:
package com.package.foo;
import android.content.Context;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.widget.EditText;
public class FuturaEditText extends EditText{
public FuturaEditText(Context context) {
this(context, null, 0);
}
public FuturaEditText(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public FuturaEditText(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
if(!isInEditMode()) {
setTypeface(Typeface.createFromAsset(context.getAssets(), "fonts/futura.ttf"));
}
}
}
yet it doesn't behave like an EditText, but a TextView. In particular, the soft keyboard isn't rising on focus and it has no EditText formatting. Why?
For completeness - from the source:
public EditText(Context context, AttributeSet attrs) {
this(context, attrs, com.android.internal.R.attr.editTextStyle);
}
So, if the constructor is called with no default style, it defaults to editTextStyle
This implementation, with each constructor calling the matched super constructor:
package com.package.foo;
import android.content.Context;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.widget.EditText;
public class FuturaEditText extends EditText{
public FuturaEditText(Context context) {
super(context);
if(!isInEditMode()) {
setTypeface(Typeface.createFromAsset(context.getAssets(), "fonts/futura.ttf"));
}
}
public FuturaEditText(Context context, AttributeSet attrs) {
super(context, attrs);
if(!isInEditMode()) {
setTypeface(Typeface.createFromAsset(context.getAssets(), "fonts/futura.ttf"));
}
}
public FuturaEditText(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
if(!isInEditMode()) {
setTypeface(Typeface.createFromAsset(context.getAssets(), "fonts/futura.ttf"));
}
}
}
works. I can only assume that EditText(context, attrs, 0) is not the same as EditText(context, attrs).
You should not invoke the super constructor completing the missing arguments, otherwise there's no way for a client to invoke a constructor with less than 3 arguments (i.e. super.(Context context, AttributeSet attrs)).
The second version is the correct one.