Here ima using two views.This code will cover,how to expand firstview based on second view size..
In second view we enter data,based on the entered text second view will expand..and every time first view also changed based on second view height..
i implemented this in my AddJobScreen class of SnailMail(Latterly) Project
Dash_LinesActivity.java:-
package org.Dharani.Dash_Lines;
import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnKeyListener;
import android.widget.EditText;
import android.widget.Toast;
public class Dash_LinesActivity extends Activity {
EditText edtText1,edtText2;
//LinedEditText et;
static int textHeight,lineHight;
int previousLinecount,currentLinecount=1;
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(keyCode == KeyEvent.KEYCODE_ENTER)
{
edtText1.setHeight(textHeight+lineHight);
return super.onKeyDown(keyCode, event);
}
return super.onKeyDown(keyCode, event);
}
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
edtText1 = (EditText) findViewById(R.id.fromBodyEdtTxt1);
edtText2= (EditText) findViewById(R.id.frommiddleNameEdtTxt);
edtText2.setOnKeyListener(new OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
// If the event is a key-down event on the "enter" button
if (keyCode == KeyEvent.KEYCODE_ENTER) {
edtText1.setHeight(textHeight+lineHight);
// Perform action on key press
Toast.makeText(Dash_LinesActivity.this, edtText2.getText()+""+textHeight+lineHight, Toast.LENGTH_SHORT).show();
}
/*else
if (keyCode == KeyEvent.KEYCODE_DEL) {
if(edtText1.getHeight()>edtText2.getHeight()){
edtText1.setHeight(edtText2.getHeight());
System.out.println("-------delete key code------"+edtText1.getHeight()+" "+edtText2.getHeight());
}
}*/
return false;
}
});
// et.setMinLines(6);//Text("The name of our country is Bangladesh. I am proud of my country :)");
// et.setLayoutParams(textViewLayoutParams);
/* LinearLayout ll = (LinearLayout)findViewById(R.id.fromlastNameEdtTxt);
ll.setOrientation(LinearLayout.VERTICAL);
LayoutParams textViewLayoutParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);*/
//final LinedEditText et = new LinedEditText(this, null);
/*et.setText("The name of our country is Bangladesh. I am proud of my country klas jdlkasjld asd sad lasdlas dlas dasjd lasjd as dllas dlasd las ldldjasjd:)");
et.setLayoutParams(textViewLayoutParams);
et.setKeyListener(null);
ll.addView(et);*/
edtText2.addTextChangedListener(new TextWatcher()
{
@Override
public void afterTextChanged(Editable s) {
/*System.out.println("-------afterTextChanged------");
edtText1.setHeight(textHeight);
if(edtText1.getHeight()>edtText2.getHeight()){
edtText1.setHeight(edtText2.getHeight());
System.out.println("-------afterTextChanged------"+edtText1.getHeight()+" "+edtText2.getHeight());
}*/
// TODO Auto-generated method stub
/*float scale = getApplicationContext().getResources().getDisplayMetrics().density;
int dipvalue=(int) (textHeight * scale + 0.5f);
System.out.println("getDip method???dip???"+dipvalue+"???px value???"+textHeight);
edtText1.setHeight(textHeight);*/
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
/*System.out.println("-------beforeTextChanged------");
// TODO Auto-generated method stub
edtText1.setHeight(textHeight);
if(edtText1.getHeight()>edtText2.getHeight()){
edtText1.setHeight(edtText2.getHeight());
System.out.println("-------beforeTextChanged------"+edtText1.getHeight()+" "+edtText2.getHeight());
}*/
}
@Override
public void onTextChanged(CharSequence s, int start, int before,int count) {
// TODO Auto-generated method stub
previousLinecount=currentLinecount;
currentLinecount=edtText2.getLineCount();
Log.e("total line num is : =>", ""+edtText1.getLineCount()+" "+edtText2.getLineCount()+" "+count);
Log.e("line counts : =>", "previ"+previousLinecount+" "+currentLinecount);
Log.e("line counts : =>", "line hight:"+lineHight+" Text Hight "+textHeight);
if(previousLinecount>currentLinecount){
edtText1.setHeight(edtText2.getHeight()-lineHight);
System.out.println("-------onTextChanged------"+edtText1.getHeight()+" "+edtText2.getHeight());
}else{
edtText1.setHeight(textHeight);
}
}
});
//this.setContentView(ll);
}
}
main.xml:-
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@color/white">
<org.Dharani.Dash_Lines.LinedEditText
android:id="@+id/fromBodyEdtTxt1"
android:gravity="left"
android:minHeight="150dip"
android:layout_width="50dip"
android:layout_height="wrap_content"
android:textColor="@color/redColor"
android:background="@color/transparent"
android:focusable="false"
android:clickable="false"
android:hint="Body"
/>
<org.Dharani.Dash_Lines.LinedEditText
android:id="@+id/frommiddleNameEdtTxt"
android:minHeight="150dip"
android:gravity="left"
android:layout_toRightOf="@+id/fromBodyEdtTxt1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="@color/redColor"
android:background="@color/transparent"
android:hint="Body"
/>
</RelativeLayout>
LinedEditText.java:-
package org.Dharani.Dash_Lines;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.DashPathEffect;
import android.graphics.Paint;
import android.graphics.PathEffect;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.widget.EditText;
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.STROKE);
mPaint.setColor(Color.GRAY);
PathEffect effects = new DashPathEffect(new float[]{3,3,3,3},1);
mPaint.setPathEffect(effects);
}
/*@Override
protected void onDraw(Canvas canvas) {//works good..
int left = getLeft();
int right = getRight();
int paddingTop = getPaddingTop();
int paddingBottom = getPaddingBottom();
int paddingLeft = getPaddingLeft();
int paddingRight = getPaddingRight();
int height = getHeight();
int lineHeight =getLineHeight();
System.out.println("--------lineHeight--------------"+lineHeight);
int count =(height-paddingTop-paddingBottom) / lineHeight;
for (int i = 0; i < count; i++) {
int baseline = lineHeight * (i+1) + paddingTop-20;
canvas.drawLine(left+paddingLeft, baseline, right-paddingRight, baseline, mPaint);
//canvas.drawLine(35, 0, 0, 35, mPaint); //this is a second line that will cross the first line
}
super.onDraw(canvas);
}*/
@Override
protected void onDraw(Canvas canvas) {//works good..
//int count = getLineCount();
int height = getHeight();
Dash_LinesActivity.textHeight=height;
int line_height = getLineHeight();
Dash_LinesActivity.lineHight=line_height;
int count = height / line_height;
if (getLineCount() > count)
count = getLineCount();
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);
//canvas.drawText("Hello, World", 30, 30, paint);
baseline += getLineHeight();//next line
}
super.onDraw(canvas);
}
/*private Paint mPaint = new Paint();
public LinedEditText(Context context) {
super(context);
initPaint();
}
public LinedEditText(Context context, AttributeSet attrs) {
super(context, attrs);
initPaint();
}
public LinedEditText(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
initPaint();
}
private void initPaint() {
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setColor(0x80060000);
}
*//**
* 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 left = getLeft();
int right = getRight();
int paddingTop = getPaddingTop()+20;
int paddingBottom = getPaddingBottom();
int paddingLeft = getPaddingLeft();
int paddingRight = getPaddingRight();
int height = getHeight();
int lineHeight = getLineHeight();
int count =(height-paddingTop-paddingBottom) / lineHeight;
for (int i = 0; i < count; i++) {
int baseline = lineHeight * (i+1) + paddingTop;
canvas.drawLine(left+paddingLeft, baseline, right-paddingRight, baseline, mPaint);
}
super.onDraw(canvas);
}*/
/*@Override
protected void onDraw(Canvas canvas)
{
int height = canvas.getHeight();
int curHeight = 20;
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);
}*/
}
ReplyDeleteHere in Above code there is no need to handle enter press key listener separatly,
because TextWatcher onTextChanged() method will handle all kinds of user operations..
Means dont use below code of :
bodyEditTxt.setOnKeyListener(new OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
// If the event is a key-down event on the "enter" button
if (keyCode == KeyEvent.KEYCODE_ENTER) {
if(bodyEditTxt.getLineCount()>6)//7-its value used in layout..
bodyTxtView.setLines(bodyEditTxt.getLineCount());
}
return false;
}
});
Instead of using the above KeyEvent.KEYCODE_ENTER code just handle these operations in
onTextChanged() method like below..it works for me better.
bodyEditTxt.addTextChangedListener(new TextWatcher()
{
@Override
public void afterTextChanged(Editable s) {
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before,int count) {
// TODO Auto-generated method stub
previousLinecount=currentLinecount;
currentLinecount=bodyEditTxt.getLineCount();
Log.e("total line num is : =>", ""+bodyTxtView.getLineCount()+" "+bodyEditTxt.getLineCount()+" "+count);
Log.e("line counts : =>", "previ"+previousLinecount+" "+currentLinecount);
//Log.e("line counts : =>", "line hight:"+SharedVariables.lineHight+" Text Hight "+textHeight);
if(previousLinecount>currentLinecount && bodyEditTxt.getLineCount()>=6){//min 6-horizontal lines need to display ,i specified it in my .xml file
bodyTxtView.setHeight(bodyEditTxt.getHeight()-SharedVariables.lineHight);
System.out.println("-------onTextChanged------"+bodyTxtView.getHeight()+" "+bodyEditTxt.getHeight());
}else
if(bodyEditTxt.getLineCount()>=6){
bodyTxtView.setLines(bodyEditTxt.getLineCount());
}
}
});