android - Custom widget does not fill available space in width -
created custom widget textview. created custom component works fine when declared through xml. when try add layout dynamically through code , not fill available width.
can 1 me how make occupy full width.
my xml layout below :
<scrollview 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" > <linearlayout android:orientation="vertical" android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/llayout" >
in activity file , try add custom widget linear layout below :
string yourtext = "lorem ipsum dolor sit amet, consectetur adipiscing elit. " + "ut volutpat interdum interdum. nulla laoreet lacus diam, vitae " + "sodales sapien commodo faucibus. vestibulum et feugiat enim. donec " + "semper mi et euismod tempor. sed sodales eleifend mi id varius. nam " + "et ornare enim, sit amet gravida sapien. quisque gravida et enim vel " + "volutpat. vivamus egestas ut felis blandit. vivamus fringilla " + "dignissim mollis. maecenas imperdiet interdum hendrerit. aliquam" + " dictum hendrerit ultrices. ut vitae vestibulum dolor. donec auctor ante" + " eget libero molestie porta. nam tempor fringilla ultricies. nam sem " + "lectus, feugiat eget ullamcorper vitae, ornare et sem. fusce dapibus ipsum" + " sed laoreet suscipit. "; linearlayout llayout = (linearlayout)findviewbyid(r.id.llayout); expandabletextview expandabletextview = new expandabletextview(getapplicationcontext()); layoutparams lpview = new layoutparams(layoutparams.match_parent, layoutparams.wrap_content); expandabletextview.setlayoutparams(lpview); expandabletextview.settextcolor(color.magenta); expandabletextview.settextsize(25); expandabletextview.settext(yourtext); llayout.addview(expandabletextview);
expandabletextview
/** * */ package com.andr.expandabletextviewwidget; import com.andr.kamal56.r; import android.content.context; import android.content.res.typedarray; import android.text.spannablestringbuilder; import android.util.attributeset; import android.view.view; import android.widget.textview; public class expandabletextview extends textview{ private static final int default_trim_length = 200; private static final string ellipsis = "....."; private charsequence originaltext; private charsequence trimmedtext; private buffertype buffertype; private boolean trim = true; private int trimlength; public expandabletextview(context context) { super(context); // todo auto-generated constructor stub } public expandabletextview(context context, attributeset attrs) { super(context, attrs); typedarray typedarray = context.obtainstyledattributes(attrs, r.styleable.expandabletextview); this.trimlength = typedarray.getint(r.styleable.expandabletextview_trimlength, default_trim_length); typedarray.recycle(); setonclicklistener(new onclicklistener() { @override public void onclick(view v) { trim = !trim; settext(); requestfocusfromtouch(); } }); } private void settext() { super.settext(getdisplayabletext(), buffertype); } private charsequence getdisplayabletext() { return trim ? trimmedtext : originaltext; } @override public void settext(charsequence text, buffertype type) { originaltext = text; trimmedtext = gettrimmedtext(text); buffertype = type; settext(); } private charsequence gettrimmedtext(charsequence text) { if (originaltext != null && originaltext.length() > trimlength) { return new spannablestringbuilder(originaltext, 0, trimlength + 1).append(ellipsis); } else { return originaltext; } } public charsequence getoriginaltext() { return originaltext; } public void settrimlength(int trimlength) { this.trimlength = trimlength; trimmedtext = gettrimmedtext(originaltext); settext(); } public int gettrimlength() { return trimlength; } }
try explicitly using linearlayout.layoutparams
in activity code. there many different layoutparams
classes , can end using wrong 1 mistake because wrong 1 being imported.
linearlayout.layoutparams lpview = new linearlayout.layoutparams( layoutparams.match_parent, layoutparams.wrap_content);
Comments
Post a Comment