How to make a custom TextView?
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.widget.TextView;
public class FontTextView extends TextView {
public FontTextView(Context context) {
super(context);
Typeface face=Typeface.createFromAsset(context.getAssets(), "Helvetica_Neue.ttf");
this.setTypeface(face);
}
public FontTextView(Context context, AttributeSet attrs) {
super(context, attrs);
Typeface face=Typeface.createFromAsset(context.getAssets(), "Helvetica_Neue.ttf");
this.setTypeface(face);
}
public FontTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
Typeface face=Typeface.createFromAsset(context.getAssets(), "Helvetica_Neue.ttf");
this.setTypeface(face);
}
protected void onDraw (Canvas canvas) {
super.onDraw(canvas);
}
}
and in xml:
<com.util.FontTextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/accountInfoText"
android:textColor="#727272"
android:textSize="18dp" />
How to create our own custom text view in Android?
Whatever you want:
public class SimpleTextView extends TextView
{
private static int color=Color.RED;
public SimpleTextView(Context context)
{
super(context);
this.setTextColor(color)
}
public SimpleTextView(Context context, AttributeSet attrs)
{
super(context, attrs);
this.setTextColor(color)
}
public SimpleTextView(Context context, AttributeSet attrs, int defStyle)
{
super(context, attrs, defStyle);
this.setTextColor(color)
}
public static void setGlobalColor(int gcolor)
{
color=gcolor;
}
}
So anytime you can change color of your TextViews globally.
android set custom value to a custom textview programmatically
define variable startColor
and endColor
and also setters for it
like
public void setStartColor(int color) {
this.startColor= color;
--- do your logic----
invalidate();
}
Reference Link
How to make custom textview in android?
In Android there is a method setRotation(float), which you can use it
textview.setRotation(float);
NOTE: this method was added in API 11
so if you want to support it you can use this
if (Build.VERSION.SDK_INT < 11) {
RotateAnimation animation = new RotateAnimation(oldAngel, newAngel);
animation.setDuration(100);
animation.setFillAfter(true);
watermarkText.startAnimation(animation);
} else {
watermarkText.setRotation(progress);
}
EDIT: Here's my solution:
Here's my full activity_main.xml
:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tool="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorPrimary">
<RelativeLayout
android:id="@+id/item"
android:layout_width="200dp"
android:layout_height="200dp"
android:background="#FFFFFFFF"
>
<TextView
android:layout_width="match_parent"
android:layout_height="40dp"
android:textColor="#FFFFFFFF"
android:text="HP EliteBook 1212x"
android:textAlignment="gravity"
android:gravity="center"
android:background="@color/colorPrimaryDark"
/>
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="40dp"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_margin="0dp"
android:src="@drawable/laptop"/>
<ImageView
android:layout_width="175dp"
android:layout_height="175dp"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_margin="0dp"
android:padding="0dp"
android:src="@drawable/triangle"/>
<LinearLayout
android:id="@+id/prices"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:orientation="vertical"
android:padding="7dp"
android:rotation="-45.0"
android:textAlignment="center">
<TextView
android:id="@+id/old_price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="200$"
android:textColor="#FFFFFFFF"
/>
<TextView
android:id="@+id/new_price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/old_price"
android:layout_gravity="center"
android:text="400$"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#FFFFFFFF"/>
</LinearLayout>
</RelativeLayout>
</RelativeLayout>
For creating triangle, create triangle.xml
file in this directory:
your_app_name/app/src/main/res/drawable
and as content put this code:
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item>
<rotate
android:fromDegrees="-45"
android:toDegrees="0"
android:pivotX="150%"
android:pivotY="20%" >
<shape
android:shape="rectangle" >
<solid android:color="#ff0000" />
</shape>
</rotate>
</item>
</layer-list>
In your Java MainActivity
class put:
TextView oldPrice = (TextView) findViewById(R.id.old_price);
oldPrice.setPaintFlags(oldPrice.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
to add to your old price text strike-through effect.
If you have a question, please free to ask.
Hope it help
Implement a custom TextView with custom textSize
This is your problem:
Caused by: java.lang.RuntimeException: Failed to resolve attribute at index 12
at android.content.res.TypedArray.getDimensionPixelSize(TypedArray.java:569)
at com.my.view.TextViewFont.<init>(TextViewFont.java:29)
Possible solution: place this into your contructor of TextViewFont.java
TypedArray a = context.obtainStyledAttributes(
attrs,
R.styleable.FieldLayout,
0, 0);
try {
final int fontSize = a.getDimensionPixelSize(R.styleable.FieldLayout_right_detail_text_size, 0);
this.setTextSize(TypedValue.COMPLEX_UNIT_PX, fontSize);
}
finally {
a.recycle();
}
or
TypedArray a = context.obtainStyledAttributes(
attrs,
R.styleable.FieldLayout,
0, 0);
try {
final int fontSize = a.getDimension(R.styleable.FieldLayout_right_detail_text_size, 0);
this.setTextSize(fontSize);
}
finally {
a.recycle();
}
How to create a custom view for TextView and change text dynamically
Just to give a rough idea, you can do something like this:
<LinearLayout
android:orientation="horizontal">
<ImageView
android:id="@+id/imageView"
android:layout_height="40dp"
android:layout_width="40dp"
android:tint="@color/green"/>
<TextView
android:id="@+id/textView"
android:textColor="@color/green"/>
</LinearLayout>
Then in your Activity/Fragment:
textView.setOnClickListener {
textView.textColor = // New color
imageView.imageResource = // New image
imageView.tint = // New tint
// For the delay you can use a normal Handler
Handler(Looper.getMainLooper()).postDelayed(
{
// Animate these changes however you wish
textView.textColor = // Original color
imageView.imageResource = // Original image
imageView.tint = // Original tint
},
5_000 // 5 sec delay
)
}
(I don't remember all the properties so might have used some wrong property names here, but this should give you the general idea)
For the fade in/out part, you can use the common view animation apis. Checkout this and this SO threads to get some idea on how to animate views.
How can we create custom class of TextView with custom font in kotlin?
create a folder "font" inside "res" folder and copy your font
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="@font/muli_font"
/>
programmatically
view.setTypeface(ResourcesCompat.getFont(context, R.font.muli_font))
you can download ".ttf" font from here
Related Topics
Android: Binding Data from a Database to a Checkbox in a Listview
Eclipse Error "Adb Server Didn't Ack, Failed to Start Daemon"
How to Programmatically Turn Off Wifi on Android Device
Calling Startactivity() from Outside of an Activity
Locationsettingsrequest Dialog to Enable Gps - Onactivityresult() Skipped
Create Blurry Transparent Background Effect
Why Is My Button Text Forced to All Caps on Lollipop
Android Push Notification (Gcm), Is There Any Daily Limit
How to Maintain Multi Layers of Imageviews and Keep Their Aspect Ratio Based on the Largest One
Manifest Merger Fails for Appcomponentfactory
Error: Fix the Version Conflict (Google-Services Plugin)
Android Support Repo 46.0.0 with Android Studio 2.3
How to Capture an Image and Store It with the Native Android Camera
Android Command Line Tools Sdkmanager Always Shows: Warning: Could Not Create Settings
Get Current Location of User in Android Without Using Gps or Internet