I need to have dotted lines in my editext field. Here is what i have tried so far. But didn't reach my goal. It displays nothing. Why dotted lines are missing? Where i'am going wrong?
EditText filed in main.xml
<view
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
class="com.xxx.yyy.MainActivity$LinedEditText"
android:background="#android:color/transparent"
android:inputType="textEmailAddress"
android:maxHeight="30dp"
android:singleLine="true"
android:minHeight="30dp"
android:textSize="13dp" />
Inside MainActivity..
public static class LinedEditText extends EditText {
private Rect mRect;
private Paint mPaint;
// we need this constructor for LayoutInflater
public LinedEditText(Context context, AttributeSet attrs) {
super(context, attrs);
mRect = new Rect();
mPaint = new Paint();
mPaint.setARGB(255, 0, 0,0);
mPaint.setStyle(Style.STROKE);
mPaint.setPathEffect(new DashPathEffect(new float[] {1,2}, 0));
mPaint.setColor(Color.RED);
}
#Override
protected void onDraw(Canvas canvas) {
int count = getLineCount();
for (int i = 0; i < count; i++) {
int baseline = getLineBounds(i, mRect);
canvas.drawLine(mRect.left, baseline + 1, mRect.right, baseline + 1, mPaint);
}
super.onDraw(canvas);
}
}
Add this to your view xml
android:ellipsize="end";
Hope it helps...
Related
public class LinedEditText extends EditText {
private Rect mRect;
private Paint mPaint;
// we need this constructor for LayoutInflater
public LinedEditText(Context context, AttributeSet attrs) {
super(context, attrs);
mRect = new Rect();
mPaint = new Paint();
mPaint.setStyle(Paint.Style.FILL_AND_STROKE);
mPaint.setColor(Color.BLACK); //SET YOUR OWN COLOR HERE
}
#Override
protected void onDraw(Canvas canvas) {
//int count = getLineCount();
int height = getHeight();
int line_height = getLineHeight();
int count = height / line_height;
if (getLineCount() > count)
count = getLineCount();//for long text with scrolling
Rect r = mRect;
Paint paint = mPaint;
int baseline = getLineBounds(0, r);//first line
for (int i = 0; i < count; i++) {
canvas.drawLine(r.left, baseline + 1, r.right, baseline + 1, paint);
baseline += getLineHeight();//next line
}
super.onDraw(canvas);
}
**Im using this class for custom edit text and i am able to change properties in xml but im not getiing a reference to this custom edittext. How can I get reference to this edit text properly in java? my xml look like this **
<com.example.goh2.pronoornotepad.LinedEditText
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:textColor="#ffff1904"
android:background="#ffffcc4b"
android:gravity="top|left"
android:singleLine="false"
android:id="#+id/et_textEditor"
android:text=""
/>
Did you try this?
LinedEditText myview = (LinedEditText) findViewById(R.id.et_textEditor);
If that doesn't work then it's usually an issue of xml hierarchy. Check this post: How to pass view reference to android custom view?
How can i make a layout look like following snap in the case i don't want to use background image.
Create a nine patch image of a single image. Following link is helpful to you for generate nine patch image.
https://romannurik.github.io/AndroidAssetStudio/nine-patches.html
You can create an ImageView like that triangle and put it in the place you need.
Have a look how you can create triangle using xml: Android triangle (arrow) defined as an XML shape
Or even you can create a TextView:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="▼"/>
and put it in the place.
Unicode for the triangle: link
Use below code in Relative Layout. Hope it helps you.
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableRight="#drawable/triangleImage"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"/>
You can also draw custom view:
public class MyBackground extends View{
private Path path = new Path();
private Paint paint = new Paint();
public MyBackground(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public MyBackground(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
paint.setColor(Color.WHITE);
paint.setStyle(Paint.Style.FILL);
paint.setAntiAlias(true);
}
public MyBackground(Context context) {
this(context, null);
}
#Override
protected void onDraw(Canvas canvas) {
canvas.drawColor(Color.GRAY);
int triangleSide = getWidth() / 10; // triangle is 1/10-th of side
int offsetFromRightSide = triangleSide;
int startX = getWidth() - triangleSide - offsetFromRightSide;
path.reset();
path.moveTo(startX, 0);
path.lineTo(startX + triangleSide/2, triangleSide/2);
path.lineTo(startX + triangleSide, 0);
canvas.drawPath(path, paint);
}
}
and use it like this
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/background"
tools:context=".MainActivity">
<your.package.name.MyBackground
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
Or there is one more "code way". Create custom background Drawable
public class Background extends Drawable {
private Path path = new Path();
private Paint paint = new Paint();
public Background(){
paint.setColor(Color.WHITE);
paint.setStyle(Paint.Style.FILL);
paint.setAntiAlias(true);
}
#Override
public void draw(Canvas canvas) {
int triangleSide = getBounds().width() / 10; // triangle is 1/10-th of side
int offsetFromRightSide = triangleSide;
int startX = getBounds().width() - triangleSide - offsetFromRightSide;
path.reset();
path.moveTo(startX, 0);
path.lineTo(startX + triangleSide/2, triangleSide/2);
path.lineTo(startX + triangleSide, 0);
canvas.drawColor(Color.GRAY);
canvas.drawPath(path, paint);
}
#Override public void setAlpha(int alpha) {}
#Override public void setColorFilter(ColorFilter cf) {}
#Override public int getOpacity() {return 0;}
}
And use it like this:
RelativeLayout layout = (RelativeLayout) findViewById(R.id.root);
layout.setBackgroundDrawable(new Background());
I want to have a line between each row in TextView.
Can original TextView do this?
If not, how can I do it?
ANSWER:
Thanks to #Slartibartfast reference and advice. I made a customized TextView. And I get something like this.
This is what I want!
The code:
public class LinedTextView extends TextView {
private Rect mRect;
private Paint mPaint;
public LinedTextView(Context context) {
super(context);
initialize();
}
public LinedTextView(Context context, AttributeSet attrs) {
super(context, attrs);
initialize();
}
public LinedTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
initialize();
}
private void initialize() {
mRect = new Rect();
mPaint = new Paint();
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setColor(0x800000ff);
}
#Override
protected void onDraw(Canvas canvas) {
int cnt = getLineCount();
Rect r = mRect;
Paint paint = mPaint;
for (int i = 0; i < cnt; i++) {
int baseLine = getLineBounds(i, r);
canvas.drawLine(r.left, baseLine + 1, r.right, baseLine + 1, paint);
}
super.onDraw(canvas);
}
}
Use the following line of code below your TextView
<View android:layout_width="fill_parent"
android:layout_height="1px"
android:background="#android:color/background_dark" />
You can configure it according to your need.
You can also use ListView with divider.
is it possible to change the color of the underline from a EditText dynamically?
Like the effect, when i focus it, then it turns into blue.
Hope you understand what i want to do.
Create your own customized EditText control.
Here's an example that I made just for you:
When selected, you just have to change mPaint.setColor(Color.GREEN); to another color
public class CustomEditText extends EditText {
private Rect mRect;
private Paint mPaint;
int widthMsSize;
int heightMsSize;
// we need this constructor for LayoutInflater
public CustomEditText(Context context, AttributeSet attrs) {
super(context, attrs);
mPaint = new Paint();
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setStrokeWidth(5);
mPaint.setColor(Color.GREEN);
System.out.println("constructor");
}
protected void onMeasure(final int widthMeasureSpec,
final int heightMeasureSpec) {
// Extract the Ms (MesaureSpec) parameters
widthMsSize = MeasureSpec.getSize(widthMeasureSpec);
heightMsSize = MeasureSpec.getSize(heightMeasureSpec);
System.out.println("on measure");
// Satisfy contract by calling setMeasuredDimension
setMeasuredDimension(widthMsSize,
heightMsSize);
}
protected void onDraw(Canvas canvas) {
canvas.drawLine(5, heightMsSize - 10, widthMsSize - 5, heightMsSize - 10, mPaint); //draw underline
canvas.drawLine(8, heightMsSize - 10, 8, heightMsSize - 20, mPaint); //draw left corner
canvas.drawLine(widthMsSize - 8, heightMsSize - 10, widthMsSize - 8, heightMsSize - 20, mPaint); //draw right corner
super.onDraw(canvas);
}
}
main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<com.example.testanimationfadeinfadeout.CustomEditText
android:id="#+id/textedit"
android:layout_width="228dp"
android:layout_height="41dp"
android:ems="10"
android:hint="color is changed" />
</LinearLayout>
public static void setEditTextUnderlineColor(final EditText editText, final int focusedColor, final int unfocusedColor) {
editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
editText.getBackground().setColorFilter(focusedColor, PorterDuff.Mode.SRC_ATOP);
return;
}
editText.getBackground().setColorFilter(unfocusedColor, PorterDuff.Mode.SRC_ATOP);
}
});
editText.getBackground().setColorFilter(unfocusedColor, PorterDuff.Mode.SRC_ATOP);
To customize edit Text I did the following way. It worked for me pretty simple way.
public class CardNumberText extends EditText {
boolean isFocus;
Paint mPaint;
Rect mRect;
int widthSize, heightSize;
public CardNumberText(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
initStyle();
}
private void initStyle() {
mRect = new Rect();
mPaint = new Paint();
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setColor(Color.parseColor("#B3B3B3"));
}
public CardNumberText(Context context, AttributeSet attrs) {
super(context, attrs);
initStyle();
}
public CardNumberText(Context context) {
super(context);
initStyle();
}
#Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
Rect rect = mRect;
Paint paint = mPaint;
if (isFocus) {
mPaint.setStrokeWidth(3.0f);
mPaint.setColor(Color.parseColor("#80CBC4"));
} else {
mPaint.setStrokeWidth(1.5f);
mPaint.setColor(Color.parseColor("#B3B3B3"));
}
for (int i = 0; i < getLineCount(); i++) {
int baseline = getLineBounds(i, rect);
canvas.drawLine(rect.left, baseline + 20, rect.right, baseline,
paint);
}
}
#Override
protected void onFocusChanged(boolean focused, int direction,
Rect previouslyFocusedRect) {
super.onFocusChanged(focused, direction, previouslyFocusedRect);
if (focused) {
isFocus = true;
} else {
isFocus = false;
}
}
}
Is there a way to make all lines in a multi-line edittext underlined?
I want all lines to show the line, even if there's no text on it.
This sort of thing is done in the Notepad sample demo. If we look at the editor source, we can see they use a custom text editor, like this :
/**
* A custom EditText that draws lines between each line of text that is displayed.
*/
public static class LinedEditText extends EditText {
private Rect mRect;
private Paint mPaint;
// we need this constructor for LayoutInflater
public LinedEditText(Context context, AttributeSet attrs) {
super(context, attrs);
mRect = new Rect();
mPaint = new Paint();
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setColor(0x800000FF);
}
#Override
protected void onDraw(Canvas canvas) {
int count = getLineCount();
Rect r = mRect;
Paint paint = mPaint;
for (int i = 0; i < count; i++) {
int baseline = getLineBounds(i, r);
canvas.drawLine(r.left, baseline + 1, r.right, baseline + 1, paint);
}
super.onDraw(canvas);
}
}
that they then declare this way in the layout :
<view xmlns:android="http://schemas.android.com/apk/res/android"
class="com.example.android.notepad.NoteEditor$LinedEditText"
android:id="#+id/note"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#android:color/transparent"
android:padding="5dp"
android:scrollbars="vertical"
android:fadingEdge="vertical"
android:gravity="top"
android:textSize="22sp"
android:capitalize="sentences" />