EditText with multiple lines, like a notepad - android

I'm following the example given by max4ever where he showed how to achieve what I'm looking for. I create a custom EditText class:
public class LinedEditText extends AppCompatEditText {
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(getResources().getColor(R.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);
}
}
Then I'm adding it to my layout .xml like this:
<?xml version="1.0" encoding="utf-8"?>
<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"
tools:context="myapp.com.test.NotesLayout">
<myapp.com.test.LinedEditText
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
The problem I'm having is that the EditText only starts in the middle of the view like this:
So, my question is, how can I adjust this custom EditText so that it shows on top/from the start of the view?
HOW I FIXED IT
I changed my xml to this
<myapp.com.test.LinedEditText
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#null"
android:imeOptions="actionNone"
android:gravity="top"
/>
Setting android:gravity="top" is why it was starting in the middle of the view. Thank you for all the contributions.

Set
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
in your custom Edittext in your layout file.This will yield the desired result.

Related

EditText like Note

I am developing Android Application like Note App. All of things were going pretty well.Here my output.
But, I have facing a problem that is the lines in my EditText become unequal with text when I typed Myanmar Language (one of South East Asia Language), here my output,
I googled and try many ways. But, it can't help me. Below are my code. Kindly!
LinedEditText.java
public class LinedEditText extends android.support.v7.widget.AppCompatEditText {
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 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 + 2, r.right, baseline + 2, paint);
baseline += getLineHeight();
}
super.onDraw(canvas);
}
}
actvity_main.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">
<EditText
android:id="#+id/editText"
android:layout_width="match_parent"
android:layout_height="50dp"
android:maxLength="10"
android:ems="10"
android:hint="Title Here" />
<login.fb.ucsy.com.notepad.LinedEditText
android:id="#+id/edit_story"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="20dp"
android:background="#null"
android:inputType="textMultiLine|textNoSuggestions"
android:minLines="10"
android:gravity="top|left"
android:singleLine="false"
android:imeOptions="actionNone"
android:text="Story : \n" />
</LinearLayout>

Drawing multiple line in edittext

I'm trying to draw lines in edittext. I have a scrollview inside which is editText.When number of lines in the editText is less then the total possible line that can be accommodated in view, I still want to draw the left lines after already drawn lines for text present in editText.
For example suppose if the no of lines in the edittext are 3 then only 3 lines are drawn .But I want to draw lines throughout the view in this case.
I've written below code but it is only drawing line upto height of text not beyond that.It will be a great help if anyone could help it .
public class DisplayActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display);
final TextView textView_header = (TextView) findViewById(R.id.header);
final String randomText = getIntent().getExtras().getString("Data");
int index = Integer.parseInt(randomText);
int pos = MainActivity.keyList.get(index);
final String text = MainActivity.settings.getString("data" + pos, "");
final String text_2 = MainActivity.settings_2.getString("data" + pos,
"");
textView_header.setText(text);
LayoutParams textViewLayoutParams = new LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
LinearLayout ll = (LinearLayout) findViewById(R.id.editText_layout);
ScrollView sv = new ScrollView(this);
sv.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT));
LinedEditText et = new LinedEditText(this, null);
et.setLayoutParams(textViewLayoutParams);
et.setText(text_2);
et.setKeyListener(null);
et.setEnabled(false);
et.setTextColor(Color.BLACK);
et.setBackgroundColor(Color.TRANSPARENT);
sv.addView(et);
ll.addView(sv);
}
public class LinedEditText extends EditText {
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(Color.GRAY);
}
#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);
}
}
}
This is my xml layout for the activity
<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/page2"
tools:context="com.example.writenote.DisplayActivity" >
<TextView
android:id="#+id/textView"
android:layout_width="match_parent"
android:layout_height="140px" />
<LinearLayout
android:id="#+id/editText_layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="#+id/line"
android:layout_marginTop="0px"
android:orientation="vertical" />
<TextView
android:id="#+id/line"
android:layout_width="match_parent"
android:layout_height="1px"
android:layout_below="#+id/header"
android:layout_marginLeft="20px"
android:layout_marginRight="5px"
android:layout_marginTop="10dp"
android:background="#a9a9a9" />
<TextView
android:id="#+id/header"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/textView"
android:layout_marginLeft="30px"
android:layout_marginRight="25px"
android:layout_marginTop="30px"
android:text="hello"
android:textColor="#000"
android:textSize="60px" />
<ImageView
android:id="#+id/imageView1"
android:layout_width="260px"
android:layout_height="260px"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:src="#drawable/circleedit" />
</RelativeLayout>
I think the problem is in getHeight() which is in onDraw. It is returning height of editext which consists of three lines. Is there any way to provide the height of linearlayout which encloses edittext? I tried to provide height of linearlayout in onDraw method but there is no effect it's still drawing 3 lines.
I tried to do this in onDraw method
//ll is linearLayout
int height = ll.getHeight();
Your problem comes from the ScrollView, this ViewGroup is made to contain variable content size and add a scrollbar if this content is bigger than the provided space in the layout, thus you cannot use the match_parent configuration inside a ScrollView (your EditText will automatically be set to wrap_content on its height).
You simply have to call sv.fillViewPort(true); in your onCreate to stretch the content if smaller than the ScrollView height, as stated in the android documentation.
<EditText
android:id="#+id/editText"
android:width="wrap_content"
android:height="wrap_content"
android:maxLines="2"/>
maxLines number is your required lines count;
you want multiple lines in edit text ....set edit text width is fixed and height is warap_content then it automaticall enter new line....

EditText with lines like notepad android

I have use custom EditText like notepad from the link Custom EditText
The text entered on the custom EditText doesn't appears correctly on the lines it contains . It some time appears on the line or below the line , unexpected behaviour
My output Click here !
Required output Click here !
Please help me in this case .
LineEditText.java
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.parseColor("#C0C0C0")); //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);
}
}
layout.xml
<RelativeLayout android:id="#+id/rel_edit_story"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/txt_image"
android:padding="10dip"
android:layout_margin="10dip"
android:background="#drawable/relative_lined_edittext_border">
<com.rb.lined.edittext.LinedEditText
android:id="#+id/edit_story"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#null"
android:inputType="textMultiLine|textNoSuggestions"
android:minLines="10"
android:singleLine="false"
android:imeOptions="actionNone"
android:text="Story : " />
</RelativeLayout>
For Desabeling Red underline you can put like
android:inputType="textNoSuggestions"
or
android:inputType="textVisiblePassword"
and for removing blue line this may help.
android:background="#null"
Example
<com.example.lineedittextdemo.LineEditText
android:id="#+id/edit_story"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="20dp"
android:background="#null"
android:inputType="textMultiLine|textNoSuggestions"
android:minLines="10"
android:singleLine="false"
android:imeOptions="actionNone"
android:text="Story : \n" />

android : How to show a text inline another

Is there any solution to have 2 textview like this :
image
in fact i want to show Text2 lines below the Text1 lines. For exmple to show the translation Text1.
Any solution?
You can use setLineSpacing to set a big enough gap in your TextViews and overlap them, then translate it so one shows up in between the lines of the other.
Try this code.
You can set lineSpacingExtra property of TextView and can use relative layout to overlap them. After this you cna set properties of your text view accordingly
In your xml define text view properties like this
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp"
tools:context=".MainActivity" >
<TextView
android:id="#+id/tv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="This is Text 1. This is Text1. This is Text 1. This is Text1. This is Text 1. This is Text1"
android:lineSpacingExtra="20dp"
android:textColor="#5F4C0B"
android:textSize="24sp"
android:textStyle="bold" />
<TextView
android:id="#+id/tv2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="26dp"
android:lineSpacingExtra="26dp"
android:text="This is Text 2. This is Text2. This is Text2. This is Text2. This is Text2. This is Text2"
android:textColor="#B40404"
android:textSize="20sp" />
</RelativeLayout>
You will get output like this
Accept the answer if you found it useful
You can dynamically create this view,
In Main Activity write the following code
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
RelativeLayout rl = new RelativeLayout(this);
RelativeLayout.LayoutParams text1 = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
text1.setMargins(0, 0, 0, 0);
RelativeLayout.LayoutParams text2 = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
text2.setMargins(0, 26, 0, 0);
LinesTextView tv1 = new LinesTextView(this, null);
tv1.setText("This is Text 1. This is Text 1. This is Text 1. This is Text 1. This is Text 1. This is Text 1.");
tv1.setLineSpacing(20.0f, 1.0f);
tv1.setLayoutParams(text1);
tv1.setTextSize(24.0f);
tv1.setTextColor(getResources().getColor(android.R.color.darker_gray));
LinesTextView tv2 = new LinesTextView(this, null);
tv2.setText("This is Text 2. This is Text 2. This is Text 2. This is Text 2. This is Text 2. This is Text 2.");
tv2.setLineSpacing(25.0f, 1.0f);
tv2.setLayoutParams(text2);
tv2.setTextSize(18.0f);
tv2.setTextColor(getResources().getColor(android.R.color.black));
rl.addView(tv1);
rl.addView(tv2);
this.setContentView(rl);
}
}
In LinesTextView class write the following code
public class LinesTextView extends TextView
{
private Rect mRect;
private Paint mPaint;
// we need this constructor for LayoutInflater
public LinesTextView(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);
}
}
Check Screen shot
Set Text color, style, margin and size accordingly.

Custom shape does not appear in RelativeLayout

I have the following problem with my application: I have created a custom View Class:
private class MyCustomPanel extends View {
private int width;
private int height;
public MyCustomPanel(Context context,int width,int height)
{
super(context);
this.height=height;
this.width=width;
}
#Override
public void draw(Canvas canvas) {
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setStyle(Style.STROKE);
paint.setStrokeWidth(2);
paint.setColor(getResources().getColor(R.color.pending));
float radius=(height)/2;
float center_x = (width)/2;
float center_y = (height)/2;
Log.d("DEBUG","R="+radius+" cx="+center_x+" cy="+center_y);
final RectF oval = new RectF();
oval.set(center_x- radius,
center_y - radius,
center_x + radius,
center_y + radius);
//Draw a left semicircle
canvas.drawArc(oval, 90, 180, false, paint);
}
}
This class is an inner class and I am trying to add in it a RelativeLayout in front of a Button.
I have successfully tried to add it to another layout, so it is added and drawn correctly (see here screenshot).
In the previous case the draw function is called and the semicircle is drawn.
In the following though the draw function is not called..
I use this code:
ViewGroup p=(ViewGroup)button.getParent();
MyCustomPanel c=new MyCustomPanel(p.getContext(),p.getWidth(),p.getHeight());
((ViewGroup)p.findViewById(R.id.semicircleFrame)).addView(c);
and the XML in of the RelatedLayout is:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/button_selector"
android:id="#+id/frame">
<Button
android:id="#+id/button"
android:layout_gravity="center"
android:textColor="#FFFFFF"
android:background="#drawable/calendar_button_selector"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
</Button>
<RelativeLayout
android:id="#+id/semicircleFrame"
android:layout_width="match_parent"
android:layout_height="fill_parent"
>
</RelativeLayout>
</RelativeLayout>
The hierarchy viewer shows the following:
screenshot
I have also try to add it directly to the parent frame but the same.
Please Help me
I also had problems with a view in Relative layout thatwas dependent on the Relative layout's size.
I solved this problem problematically by updating the inner view size
private void updateViewSize()
if(rootContainer == null || rootContainer.getLayoutParams() == null)
return;
rootContainer.measure(View.MeasureSpec.makeMeasureSpec(ScalingUtil.getScreenSize().getWidth(), View.MeasureSpec.EXACTLY), View.MeasureSpec.makeMeasureSpec(ViewGroup.LayoutParams.WRAP_CONTENT, View.MeasureSpec.EXACTLY));
RelativeLayout.LayoutParams oldLayoutParams = (RelativeLayout.LayoutParams) rootContainer.findViewById(R.id.deals_card_center_bg).getLayoutParams();
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(rootContainer.getMeasuredWidth(), rootContainer.getMeasuredHeight());
layoutParams.setMargins(oldLayoutParams.leftMargin, oldLayoutParams.topMargin, oldLayoutParams.rightMargin, oldLayoutParams.bottomMargin);
rootContainer.findViewById(R.id.deals_card_center_bg).setLayoutParams(layoutParams);

Categories

Resources