MoreLess_SampleProj.java:-
package org.dharani.MoreLessProj;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MoreLess_SampleProj extends Activity {
static TextView textView;
static TextView textView1;
Button btn;
static int contentHight;
boolean flag=false;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
textView=(TextView)findViewById(R.id.value);
textView1=(TextView)findViewById(R.id.value1);
btn=(Button)findViewById(R.id.expand);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(flag){
btn.setText("Less");
textView.setMaxHeight(contentHight);
flag=!flag;
}else{
btn.setText("More");
textView.setMaxHeight(52);
flag=!flag;
}
}
});
}
}
ExpandablePanel.java:-
package org.dharani.MoreLessProj;
import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;
public class ExpandablePanel extends LinearLayout {
//private final int mHandleId;
private final int mContentId,mContentId1;
int count=0;
//private View mHandle;
private View mContent,mContent1;
//private boolean mExpanded = true;
//private int mCollapsedHeight = 0;
private int mContentHeight = 0,mContentHeight1 = 0;
public ExpandablePanel(Context context) {
this(context, null);
}
public ExpandablePanel(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray a = context.obtainStyledAttributes(attrs,
R.styleable.ExpandablePanel, 0, 0);
// How high the content should be in "collapsed" state
//mCollapsedHeight = (int) a.getDimension(R.styleable.ExpandablePanel_collapsedHeight, 0.0f);
int contentId = a.getResourceId(R.styleable.ExpandablePanel_content, 0);
int contentId1 = a.getResourceId(R.styleable.ExpandablePanel_content1, 0);
if (contentId == 0) {
throw new IllegalArgumentException(
"The content attribute is required and must refer "
+ "to a valid child.");
}
if (contentId1 == 0) {
throw new IllegalArgumentException(
"The content attribute is required and must refer "
+ "to a valid child.");
}
// mHandleId = handleId;
mContentId = contentId;
mContentId1 = contentId1;
a.recycle();
}
@Override
protected void onFinishInflate() {
super.onFinishInflate();
//mHandle = findViewById(mHandleId);
/*if (mHandle == null) {
throw new IllegalArgumentException(
"The handle attribute is must refer to an"
+ " existing child.");
}*/
mContent = findViewById(mContentId);
mContent1 = findViewById(mContentId1);
if (mContent == null) {
throw new IllegalArgumentException(
"The content attribute is must refer to an"
+ " existing child.");
}
if (mContent1 == null) {
throw new IllegalArgumentException(
"The content attribute is must refer to an"
+ " existing child.");
}
// mHandle.setOnClickListener(new PanelToggler());
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
if (mContentHeight == 0) {
// First, measure how high content wants to be
mContent.measure(widthMeasureSpec, MeasureSpec.UNSPECIFIED);
mContentHeight = mContent.getMeasuredHeight();
MoreLess_SampleProj.contentHight=mContentHeight;
//code to get the TextView height
System.out.println("---------mContentHeight------"+mContentHeight);
//code to get the line count of TextViews
System.out.println("---------mCollapsedHeight------"+MoreLess_SampleProj.textView.getLineCount());
//LinearLayout.LayoutParams adaptLayout = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
//adaptLayout.setMargins(0, mContentHeight,0 ,0 );
//MoreLess_SampleProj.textView.setHeight(50);//LayoutParams(adaptLayout);
}
if(mContentHeight1==0){
mContent1.measure(widthMeasureSpec, MeasureSpec.UNSPECIFIED);
mContentHeight1 = mContent1.getMeasuredHeight();
//MoreLess_SampleProj.contentHight=mContentHeight;
//code to get the TextView height..
System.out.println("---------mContentHeight1------"+mContentHeight1);
//code to get the line count of TextView
System.out.println("---------mCollapsedHeight------"+MoreLess_SampleProj.textView1.getLineCount());
}
// Then let the usual thing happen
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}
main.xml:-
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:example="http://schemas.android.com/apk/res/org.dharani.MoreLessProj"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<org.dharani.MoreLessProj.ExpandablePanel
android:orientation="vertical"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
example:content="@+id/value"
example:content1="@+id/value1"
>
<TextView
android:id="@id/value"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="16dip"
android:text="ldjas ;ldasljdl asljdlasl jdl asljdlasjld jlas ldjl alskjd jasl kdllas kldklaskldlalsdaskldkla skl ljasl jdlkas ld jlas lk ldjl kasd alskldjlasldljasljdl al l lk laskld las jdklas ld las kld lasjdl lasdl askld jlas jldal jdljas kld lkj lasj dlajskldj laskl jdla sldj alsjd la ldka klasjkld las jljasljdlasjdljasl djlasjldjlasjldjasldjlajsld d lasldlasdlasl dlas ldl asldjlasjldjlj"/>
<TextView
android:id="@id/value1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="13dip"
android:text="ldjas ;ldasljdl asljdlasl jdl aldklajasldjlajsld d lasldlasdlasl dlas ldl asldjlasjldjlj"/>
<Button
android:id="@+id/expand"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="More" />
</org.dharani.MoreLessProj.ExpandablePanel>
</LinearLayout>
attrs.xml:- ///it must be placed in res/values/attrs.xml folder
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="ExpandablePanel">
<attr name="content" format="reference" />
<attr name="content1" format="reference" />
</declare-styleable>
</resources>
Comments
Post a Comment