I want to create a line edittext and set it's height match parent but when It display have a space with top as shown in below image.
Here is my code :
public class LineEditText extends EditText {
private Rect mRect;
private Paint mPaint;
public LineEditText(Context context, AttributeSet attrs) {
super(context, attrs);
mRect = new Rect();
mPaint = new Paint();
mPaint.setStyle(Paint.Style.FILL_AND_STROKE);
mPaint.setColor(Color.parseColor("#afaaaa"));
}
#Override
protected void onDraw(Canvas canvas) {
int height = getHeight();
int line_height = getLineHeight();
int count = height / line_height;
if (getLineCount() > count)
count = getLineCount();
Rect r = mRect;
Paint paint = mPaint;
int baseline = getLineBounds(0, r);
for (int i = 0; i < count; i++) {
canvas.drawLine(r.left, baseline + 1, r.right, baseline + 1, paint);
baseline += getLineHeight();
}
super.onDraw(canvas);
}
edittext.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<app.lichconggiao.custom.LineEditText
android:id="#+id/edtNote"
android:textSize="20dp"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginTop="10dp"
android:ems="10"
android:textColor="#212121" />
</LinearLayout>
and my screen display
How i can fix it?
Please help me thank alot
#Override
protected void onDraw(Canvas canvas) {
int height = getHeight();
int line_height = getLineHeight();
//int count = height / line_height;
if (getLineCount() > height)
height = getLineCount();
Rect r = mRect;
Paint paint = mPaint;
int baseline = getLineBounds(0, r);
for (int i = 0; i < height; i++) {
float f = (float) (baseline + 1);
canvas.drawLine(r.left, baseline + 1, r.right, baseline + 1, paint);
baseline += getLineHeight();
}
super.onDraw(canvas);
}
Related
I am using Lined EditText to show vertical lines in an android app like notepad. When I add data it is displayed successfully but as data grows bottom lines disappear.
Any help will be appreciated.
Code:
public class LinedEditText extends AppCompatEditText {
private Rect mRect;
private Paint mPaint;
public LinedEditText(Context context, AttributeSet attrs) {
super(context, attrs);
mRect = new Rect();
mPaint = new Paint();
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setColor(0xFF000000);
}
/**
* This is called to draw the LinedEditText object
*
* #param canvas The canvas on which the background is drawn.
*/
#Override
protected void onDraw(Canvas canvas) {
int height = canvas.getHeight();
int curHeight = 0;
Rect r = mRect;
Paint paint = mPaint;
int baseline = getLineBounds(0, r);
for (curHeight = baseline + 1; curHeight < height;
curHeight += getLineHeight()) {
canvas.drawLine(r.left, curHeight, r.right, curHeight, paint);
}
super.onDraw(canvas);
}
}
layout_file.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<com.g26app.LinedEditText
android:id="#+id/note"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/transparent"
android:gravity="top"
android:textSize="22sp" />
</LinearLayout>
This is the code you need based on max4ever
#Override
protected void onDraw(Canvas canvas) {
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);
}
I updated onDraw of LinedEditText and it is working fine:
public void onDraw(Canvas canvas) {
int height = getHeight() / getLineHeight();
if (getLineCount() > height) {
height = getLineCount();
}
Rect rect = this.mRect;
Paint paint = this.mPaint;
int lineBounds = getLineBounds(0, rect);
for (int i = 0; i < height; i++) {
float f = (float) (lineBounds + 1);
canvas.drawLine((float) rect.left, f, (float) rect.right, f, paint);
lineBounds += getLineHeight();
}
super.onDraw(canvas);
}
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?
I was taking a look at the notepad sample in the android SDK see here: http://developer.android.com/resources/samples/NotePad/src/com/example/android/notepad/NoteEditor.html
Thing is it only draws the current line the cursor is on e.g http://cdn2.staztic.com/screenshots/simple-notepad-app-al-1.jpg
But I'd like to display lines that fill up the screen e.g. http://www.itismyworld.info/wp-content/uploads/2010/03/AK-notebook.png
Any suggestions would be great. The relevent bit of code seems to be here:
protected void onDraw(Canvas canvas) {
// Gets the number of lines of text in the View.
int count = getLineCount();
// Gets the global Rect and Paint objects
Rect r = mRect;
Paint paint = mPaint;
/*
* Draws one line in the rectangle for every line of text in the EditText
*/
for (int i = 0; i < count; i++) {
// Gets the baseline coordinates for the current line of text
int baseline = getLineBounds(i, r);
/*
* Draws a line in the background from the left of the rectangle to the right,
* at a vertical position one dip below the baseline, using the "paint" object
* for details.
*/
canvas.drawLine(r.left, baseline + 1, r.right, baseline + 1, paint);
}
// Finishes up by calling the parent method
super.onDraw(canvas);
}
This is the code, based on jkhouws1's suggestion and google's note editor
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(R.color.edit_note_line); //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);
}
}
In Eclipse IDE press Ctrl+Shift+O to add all needed imports
I think this is what you need:
public class LinedEditText extends EditText {
private static Paint linePaint;
static {
linePaint = new Paint();
linePaint.setColor(Color.BLACK);
linePaint.setStyle(Style.STROKE);
}
public LinedEditText(Context context, AttributeSet attributes) {
super(context, attributes);
}
#Override
protected void onDraw(Canvas canvas) {
Rect bounds = new Rect();
int firstLineY = getLineBounds(0, bounds);
int lineHeight = getLineHeight();
int totalLines = Math.max(getLineCount(), getHeight() / lineHeight);
for (int i = 0; i < totalLines; i++) {
int lineY = firstLineY + i * lineHeight;
canvas.drawLine(bounds.left, lineY, bounds.right, lineY, linePaint);
}
super.onDraw(canvas);
}
}
maybe after that for loop, you draw estimated* additional lines.
getHeight() will return EditText's height in pixels
getLineHeight() will height of one standard line
so getHeight/getlineHeight-getCount will be number of lines left to draw.
you can't use getLineBounds, using the above functions you could calculate the position of the remaining lines to draw.
*Estimated since formatting of text could change the line height, but since there is no text in these lines yet that shouldnt be an issue. But for that same reason you should only draw the remaining lines, and not use this to draw all the lines.
<com.example.goh2.pronoornotepad.LinedEditText
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ffffcc4b"
android:gravity="top|left"
android:singleLine="false"
android:text=""
/>
The above XML works with the code from Max4ever's answer:
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(R.color.edit_note_line); //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);
}
}
I have to draw a custom view below A banner for that like the pick attached PICK
below DaTTab banner I have to draw the Rectangle view my xml
main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/relative_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<include android:id="#+id/banner" layout="#layout/upper_border" />
<com.example.ui_1.Border_Portrait
android:id="#+id/custom_display_view1"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_below="#+id/banner"
/>
Draw_Border.jav
class Border_Portrait extends View{
private static final int DEFAULT_SIZE = 100;
int mViewHeight , mViewWidth ;
Paint mPaint ;
Canvas mCanvas ;
int margin ;
float [] mPoints ;
int mLogoHeight ;
int mLogoWidth ;
int dy,dx ;
public Border_Portrait(Context context) {
super(context);
}
public Border_Portrait(Context context, AttributeSet attrs) {
super(context, attrs);
margin = 7 ;
mPaint = new Paint();
mPaint.setColor(Color.rgb(0,188,226));
mPaint.setStrokeWidth(3);
mLogoHeight = ScreenUtils.convertDIPToPixels(getContext(), 50 + margin );
mLogoWidth = ScreenUtils.convertDIPToPixels(getContext(), 66 + margin);
dy = ScreenUtils.convertDIPToPixels(getContext(),1) ;
dx= ScreenUtils.convertDIPToPixels(getContext(),1) ;
}
#Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
super.onDraw(canvas);
canvas.save();
mPoints = new float [] {
margin + mLogoWidth ,margin,//1
getWidth()-margin ,margin ,//1
getWidth()-margin ,margin ,//2
getWidth()-margin , getHeight()-margin ,//2
getWidth()-margin , getHeight()-margin , //3
margin , getHeight()-margin , //3
margin , getHeight() - margin , //4
margin , mLogoHeight + dy ,//4
margin , mLogoHeight + dy , // 5
margin + mLogoWidth + dx ,mLogoHeight + dy, //5
margin + mLogoWidth ,mLogoHeight + dy, //6
margin + mLogoWidth ,margin, //6
} ;
canvas.drawLines(mPoints, mPaint);
canvas.restore();
}
#Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int parentWidth = MeasureSpec.getSize(widthMeasureSpec);
int parentHeight = MeasureSpec.getSize(heightMeasureSpec);
this.setMeasuredDimension(parentWidth, parentHeight);
}
private int calculateMeasure(int measureSpec) {
return 0 ;
}
}
my question is how to measure the Width of the border at run time I have refer
THIS
and other links too but till now no luck
Just got the answer add margin in main.xml
<com.example.ui_1.Border_Portrait
android:id="#+id/custom_display_view1"
android:layout_marginTop="50dp"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
I am creating an application, in which a camera is showing with an option on screen to hide and show grids on the screen. I created this grid by CustomView and it was working fine. but when I hide the grid and click show then horizontal lines are not showing... please help...
custom view
<com.***.GridLinesView
android:id="#+id/cameraView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" />
OnClick Hide/Unhide Button
#Override
public void onClick(View v) {
// get an image from the camera
View grid = (View)findViewById(R.id.cameraView1);
if(grid.getVisibility() == View.VISIBLE)
grid.setVisibility(View.INVISIBLE);
else
grid.setVisibility(View.VISIBLE);
}
Custom View
public class GridLinesView extends View {
private Paint p;
int width = 0;
int height = 0;
int pass = 0;
int xpos = 0;
int ypos = 0;
public GridLinesView(Context context) {
super(context);
// TODO Auto-generated constructor stub
p = new Paint();
p.setColor(Color.WHITE);
p.setStyle(Paint.Style.STROKE);
p.setStrokeWidth(1);
p.setAntiAlias(true);
}
public GridLinesView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
p = new Paint();
p.setColor(Color.WHITE);
p.setStyle(Paint.Style.STROKE);
p.setStrokeWidth(1);
p.setAntiAlias(true);
}
public GridLinesView(Context context, AttributeSet attrs) {
super(context, attrs);
p = new Paint();
p.setColor(Color.WHITE);
p.setStyle(Paint.Style.STROKE);
p.setStrokeWidth(1);
p.setAntiAlias(true);
}
#Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
super.onDraw(canvas);
width = getWidth();
height = getHeight();
int ratio = 5;
if(width < 300 || height < 300){
ratio = 3;
}
xpos = width / ratio;
ypos = height / ratio;
p.setStyle(Style.STROKE);
for (int i = 0; i < ratio; i++) {
p.setColor(Color.argb(100, 255, 255, 255));
canvas.drawLine(0, (ypos * pass) + ratio, width, (ypos * pass) + ratio, p);
pass++;
}
for (int i = 0; i < ratio; i++) {
p.setColor(Color.argb(100, 255, 255, 255));
canvas.drawLine(xpos + (xpos * i), 0, xpos + (xpos * i), height, p);
}
}
}
Any help will be appreciable... Thank you.