Edittext cursor not visible in android - android

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

Related

Custom TextView not visible in 5.0 and higher devices

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"));
}
}

Android custom checkbox for set custom font

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("");
}
}

Stroke in textview text android?

I need to style the text for textview as per the below image
The font being used is CARTER ONE font
This is my textview class
public class CustomTextViewCarter extends TextView {
private static Typeface mTypeface;
public CustomTextViewCarter(final Context context) {
this(context, null);
}
public CustomTextViewCarter(final Context context, final AttributeSet attrs) {
this(context, attrs, 0);
}
public CustomTextViewCarter(final Context context, final AttributeSet attrs, final int defStyle) {
super(context, attrs, defStyle);
if (!isInEditMode()) {
if (mTypeface == null) {
mTypeface = Typeface.createFromAsset(context.getResources().getAssets(), "carter_one.ttf");
}
setTypeface(mTypeface);
}
}
}
But the the same kind of text doesn't appear. How can it be done?
Use this code for show custom TextView
Step-1:
CustomTextView.java
public class CustomTextView extends TextView {
private static final String TAG = "TextView";
public CustomTextView(Context context) {
super(context);
}
public CustomTextView(Context context, AttributeSet attrs) {
super(context, attrs);
setCustomFont(context, attrs);
}
public CustomTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
setCustomFont(context, attrs);
}
private void setCustomFont(Context ctx, AttributeSet attrs) {
TypedArray a = ctx.obtainStyledAttributes(attrs,R.styleable.CustomTV);
String customFont = a.getString(R.styleable.CustomTV_customFont);
setCustomFont(ctx, customFont);
a.recycle();
}
public boolean setCustomFont(Context ctx, String asset) {
Typeface tf = null;
try {
tf = Typeface.createFromAsset(ctx.getAssets(), asset);
} catch (Exception e) {
Log.e(TAG, "Could not get typeface: "+e.getMessage());
return false;
}
setTypeface(tf);
return true;
}
}
Step-2:
After that write below code in your styles.xml file
<declare-styleable name="CustomTV">
<attr name="customFont" format="string"></attr>
</declare-styleable>
Step-3:
Use CustomTextView in your xml file
Here com.app.demo is my package name.. use your package name instead of that,
<com.app.demo.CustomTextView
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="hello"
android:textColor="#color/text"
android:textStyle="bold"
app:customFont="carter_one.ttf" />
Try this.
You can set ttf by making your own textview implementation.
It will works above API 15 (Ice cream sandwich).
https://stackoverflow.com/a/5185587/850347
<com.lht.ui.MyTextView
android:text="Hello friends"
lht:ttf_name="ITCBLKAD.TTF"
/>

Set textcolor and other attribute in textview extends

How can i set text color and text size in the class below
public class MyTextView extends TextView {
public MyTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
public MyTextView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public MyTextView(Context context) {
super(context);
init();
}
public void init() {
Typeface tf = Typeface.createFromAsset(getContext().getAssets(), "font/chiller.ttf");
setTypeface(tf ,1);
}
}
In your init use setTextColor and setTextSize
public void init() {
Typeface tf = Typeface.createFromAsset(getContext().getAssets(), "font/chiller.ttf");
setTypeface(tf ,1);
this.setTextColor(Color.RED);
this.setTextSize(20f);
}
If you are looking for custom attributes check this
Setting color of a Paint object in custom view

Android expandable list view items font style

I have followed the tutorial given in below link:
http://www.androidhive.info/2013/07/android-expandable-list-view-tutorial/
How can I give custom font(typeface) to textviews in list_group.xml and list_item.xml.
I have copied Arial.ttf file into assets folder.
Please help me.
You can create a custom textview as I have done here by overriding the TextView Class and then instead of using TextView in your xml you use CustomTextView:
public class CustomTextView extends TextView {
public CustomTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init(context, attrs);
}
public CustomTextView(Context context, AttributeSet attrs) {
super(context, attrs);
init(context, attrs);
}
public CustomTextView(Context context) {
super(context);
}
private void init(Context context, AttributeSet attrs) {
}
#Override
public void setTypeface(Typeface tf, int style) {
if (!isInEditMode()) {
Typeface normalTypeFace = Typeface.createFromAsset(getContext()
.getAssets(), "Esans.ttf");
Typeface boldTypeFace = Typeface.createFromAsset(getContext()
.getAssets(), "EsansBol.ttf");
Typeface lightTypeFace = Typeface.createFromAsset(getContext()
.getAssets(), "EsansLig.ttf");
Typeface mediumTypeFace = Typeface.createFromAsset(getContext()
.getAssets(), "EsansMed.ttf");
//Modifying styles to support the different styles of custom font
switch (style) {
case Typeface.NORMAL:
super.setTypeface(normalTypeFace);
break;
case Typeface.BOLD:
super.setTypeface(boldTypeFace);
break;
case Typeface.ITALIC:
super.setTypeface(lightTypeFace);
break;
case Typeface.BOLD_ITALIC:
super.setTypeface(mediumTypeFace);
break;
default:
super.setTypeface(normalTypeFace);
break;
}
}
}
}
You can try this to add typeface in xml
public class TextViewPlus extends TextView {
private static final String TAG = "TextView";
public TextViewPlus(Context context) {
super(context);
}
public TextViewPlus(Context context, AttributeSet attrs) {
super(context, attrs);
setCustomFont(context, attrs);
}
public TextViewPlus(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
setCustomFont(context, attrs);
}
private void setCustomFont(Context ctx, AttributeSet attrs) {
TypedArray a = ctx.obtainStyledAttributes(attrs, R.styleable.TextViewPlus);
String customFont = a.getString(R.styleable.TextViewPlus_customFont);
setCustomFont(ctx, customFont);
a.recycle();
}
public boolean setCustomFont(Context ctx, String asset) {
Typeface tf = null;
try {
tf = Typeface.createFromAsset(ctx.getAssets(), asset);
} catch (Exception e) {
Log.e(TAG, "Could not get typeface: "+e.getMessage());
return false;
}
setTypeface(tf);
return true;
}
}
in your xml
<com.icl.examples.TextViewPlus
android:id="#+id/textViewPlus1"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:text="#string/info_text"
android:textColor="#color/White"
android:textStyle="bold"
android:textSize="50sp"
foo:customFont="Bamini.ttf">
</com.icl.examples.TextViewPlus>
add this to resources, res->values-attrs.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="TextViewPlus">
<attr name="customFont" format="string"/>
</declare-styleable>
</resources>

Categories

Resources