Reputation: 199
I am trying to dynamically add textviews inside a LinearLayout
. once all the width of all the textviews is bigger than the width of the screen I need to create another LinearLayout
below the first one and continue adding textviews. I am basically splitting a String
into multiple textviews, because I need to check if some of the words have a '#' in front. If so I need to change the color. The problem I am having is that the first LinearLayout
is very tall (Height does not wrap content). Any help?
LinC is is the linearLayout counter. It get incremented once the widths of the textviews is greater than the screen
public void TextMod()
{
LinearLayout.LayoutParams layoutParams,layoutParams2;
layoutParams= new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
layoutParams= new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
llay= new LinearLayout(this.c);
llay.setOrientation(LinearLayout.HORIZONTAL);
llay.setBackgroundColor(Color.GREEN);
llay2= new LinearLayout(this.c);
llay2.setBackgroundColor(Color.MAGENTA);
llay2.setOrientation(LinearLayout.HORIZONTAL);
Boolean addl =true;
String parts[]= this.Dis.split(" ");
size=parts.length;
int i=0;
while( i<size)
{
//
if(textview_wid < this.w )
{
TextView valueTV = new TextView(c);
String d= parts[i] + " ";
valueTV.setText(d);
valueTV.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED);
textview_wid +=valueTV.getMeasuredWidth();
Log.d(" des", " TEEXT WIDTH " + valueTV.getMeasuredWidth());
if(d.charAt(0)=='#')
{
valueTV.setTextColor(Color.MAGENTA);
}
if(addl)
{
if(LinC==0)
{
llay.setLayoutParams(layoutParams);
this.Lin.addView(llay);
}
if(LinC==1)
{
llay2.setLayoutParams(layoutParams);
this.Lin.addView(llay2);
}
addl=false;
}
valueTV.setLayoutParams(layoutParams);
if(LinC==0)llay.addView(valueTV);
if(LinC==1)llay2.addView(valueTV);
i++;
}
else
{
Log.d(" des", " TOTAL TEXTVIE WIDE " + textview_wid);
textview_wid =0;
LinC++;
addl=true;
}
// valueTV.setLayoutParams(layoutParams);
}
}
OnCreate
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
height =metrics.heightPixels;
width=metrics.widthPixels;
L=(LinearLayout)findViewById(R.id.ll);
//layoutParams= new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
sep = new DescriptionStringSep(s,L,this,width);
sep.TextMod();
}
XML
<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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/ll"
android:orientation="vertical"
>
</LinearLayout>
</RelativeLayout>
Upvotes: 1
Views: 1322
Reputation: 1893
What you're doing wrong
setContentView(R.layout.activity_main);
This sets the content view to the xml layout passed as a parameter. Since you're creating a dynamic layout in the TextMod
method, call it later on in your code and set the resultant layout in your activity. You should do this
onCreate(){
...
LinearLayout mLayout;
mLayout = sep.TextMod(); // make TextMod return the root view
setContentView(mLayout);
}
Upvotes: 1