How to set your shape for default background for TextView? - android

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
}
}
}

Related

Hide a Preference-item depending on the state (enabled/disabled) of another Preference in PreferenceScreen

I'm creating a settings screen, and I have a few settings items:
SwitchPreferenceCompat and my CustomPreference.
I need the СustomPreference to disappear(hide) when the SwitchPreferenceCompat is turned off and appear(show) when the SwitchPreferenceCompat is turned on.
Out of the box, only the "dependency" mechanism is available by specifying the "dependency" attribute for some Preferences, which only disables/enables items, and I need to change visibility.
Is there a way to achieve this?
create customPreference and override onDependencyChanged().
package com.paul.ttcapp.p9988030.helper.ui;
import android.content.Context;
import android.util.AttributeSet;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.preference.EditTextPreference;
import androidx.preference.Preference;
public class PaulEditTextPreference extends EditTextPreference {
public PaulEditTextPreference(#NonNull Context context, #Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
public PaulEditTextPreference(#NonNull Context context, #Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
public PaulEditTextPreference(#NonNull Context context, #Nullable AttributeSet attrs) {
super(context, attrs);
}
public PaulEditTextPreference(#NonNull Context context) {
super(context);
}
#Override
public void onDependencyChanged(#NonNull Preference dependency, boolean disableDependent) {
// If the corresponding dependency is disabled, disable (hide) this setting as well.
setVisible(!disableDependent);
super.onDependencyChanged(dependency, disableDependent);
}
}

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

Change color Text from CustomTextView

By default to change textColor programatically is :
textView.setTextColor(Color.RED);
I need to have a custom Textview to change typeface and color by default, How can change textcolor from CustomTextView class, here is my code.
public class CustomTextView extends TextView {
public CustomTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public CustomTextView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CustomTextView(Context context) {
super(context);
}
public void setTypeface(Typeface tf, int style) {
if(!isInEditMode()) {
if (style == Typeface.BOLD) {
super.setTypeface(Typeface.createFromAsset(getContext().getAssets(), "fonts/Lato-Bold.ttf"));
} else if(style == Typeface.ITALIC){ // constant used to set Lato-Light.
super.setTypeface(Typeface.createFromAsset(getContext().getAssets(), "fonts/Lato-Light.ttf"));
}else {
super.setTypeface(Typeface.createFromAsset(getContext().getAssets(), "fonts/Lato-Regular.ttf"));
}
}
}
The below code is the way to set your default text color and typeface.
public class CustomTextView extends TextView {
public CustomTextView(Context context) {
super(context);
init(context);
}
public CustomTextView(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
public CustomTextView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context);
}
private void init(Context context) {
setTypeface(Typeface.createFromAsset(context.getAssets(),"fonts/Lato-Light.ttf"));
setTextColor(Color.RED);
}
}
The init() method gets called every time the text view gets created, and will then set the typeface and color in that. You can manipulate any other variables you want to in there.
Use setTextColor(Color.RED); after each super in each constructor.
Step 1
In the /assets directory (not the /resource directory), create a folder called /fonts. Copy your custom font here. You can use both TTF and OTF fonts.
Step 2
In the /res/values folder, create a new file called attrs.xml. This is how the Android SDK lets you name custom properties for your widgets.
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="MyTextView">
<attr name="fontName" format="string" />
</declare-styleable>
</resources>
Step 3
In /res/layouts, you will need to include your to-be-created custom text view in the activity_main.xml file.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:customfontdemo="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="24sp"
android:padding="12dp"
android:text="Standard Android Font" />
<com.authorwjf.customfontdemo.MyTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="32sp"
android:padding="12dp"
customfontdemo:fontName="pipe_dream.ttf"
android:text="Custom Android Font" />
</LinearLayout>
Step 4
In the /src folder, you will want to create your MyTextView class. It extends the standard text view, plucks the font name from the custom attribute, and applies the type face.
package com.authorwjf.customfontdemo;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.widget.TextView;
public class MyTextView extends TextView {
public MyTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init(attrs);
}
public MyTextView(Context context, AttributeSet attrs) {
super(context, attrs);
init(attrs);
}
public MyTextView(Context context) {
super(context);
init(null);
}
private void init(AttributeSet attrs) {
setTextColor(Color.RED);
if (attrs!=null) {
TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.MyTextView);
String fontName = a.getString(R.styleable.MyTextView_fontName);
if (fontName!=null) {
Typeface myTypeface = Typeface.createFromAsset(getContext().getAssets(), "fonts/"+fontName);
setTypeface(myTypeface);
}
a.recycle();
}
}
}
Step 5
Because the text view is now self-contained, you aren't required to make any modifications to our /src/MainAcitivity.java file.
package com.authorwjf.customfontdemo;
import android.os.Bundle;
import android.app.Activity;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}

Subclass of EditText isn't behaving like EditText

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.

IDEA, Android UI Render error, null pointer

I have a problem with creating custom view. Here is code:
package com.example.App;
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
public class v extends View {
public v(Context context) {
super(context);
}
public v(Context context, AttributeSet attrs) {
super(context, attrs);
}
public v(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
}
I'm trying to use it in layout and getting render error(NullPointerException), here is what IDEA has jenerate in layout code:
<view android:layout_width="wrap_content"
android:layout_height="wrap_content"
class="com.example.App.v" android:id="#+id/view" android:layout_gravity="center"/>
What I'm doing wrong? How to fix it?

Categories

Resources