How to show textview left alignment in android - android

In my application i want to show VIEW ALL to left align like below image:-
But I found this type of layout and more thing i also do all these layout programmatically.
My SourceCode:-
private void printFrontCategory(){
for(int i=0;i<main.size();i++) {
View v = new View(MainActivity.this);
v.setLayoutParams(new RadioGroup.LayoutParams(RadioGroup.LayoutParams.FILL_PARENT, 5));
v.setBackgroundColor(Color.rgb(51, 51, 51));
/* View v1 = new View(MainActivity.this);
v1.setLayoutParams(new RadioGroup.LayoutParams(RadioGroup.LayoutParams.FILL_PARENT,5));
v1.setBackgroundColor(Color.rgb(255, 255, 255));*/
HorizontalScrollView horizontalScrollView = new HorizontalScrollView(MainActivity.this);
horizontalScrollView.setHorizontalScrollBarEnabled(false);
TextView textView = new TextView(MainActivity.this);
String content = front_category_catetory_name.get(i);
content = content.replace("'", "");
content = content.replace("&","");
textView.setText(content);
textView.setGravity(Gravity.RIGHT);
// textView.setGravity(Gravity.CENTER);
textView.setTextSize(20);
TextView textView2 = new TextView(MainActivity.this);
textView2.setText("VIEW ALL");
textView2.setTextColor(Color.BLUE);
textView2.setGravity(Gravity.LEFT);
LinearLayout linearLayout2 = new LinearLayout(MainActivity.this);
linearLayout2.setOrientation(LinearLayout.HORIZONTAL);
linearLayout2.addView(textView,0);
linearLayout2.addView(textView2,1);
LinearLayout linearLayout = new LinearLayout(MainActivity.this);
linearLayout.setOrientation(LinearLayout.HORIZONTAL);
for (int j = 0; j < main.get(i).size(); j++) {
LinearLayout linearLayout1 = new LinearLayout(MainActivity.this);
linearLayout1.setOrientation(LinearLayout.VERTICAL);
ImageView image = new ImageView(MainActivity.this);
TextView nameProduct = new TextView(MainActivity.this);
TextView priceProduct = new TextView(MainActivity.this);
TextView special_discount = new TextView(MainActivity.this);
/* Log.d("counter val",cnt+"");
Log.d("thumb ",front_category_thumb.size()+"");
Log.d("image", front_category_thumb.get(52));*/
new SetImageView(image).execute(main.get(i).get(j).get(1));
nameProduct.setText(main.get(i).get(j).get(2));
if (!String.valueOf(main.get(i).get(j).get(5)).equals(null)) {
priceProduct.setText(main.get(i).get(j).get(3));
special_discount.setText(main.get(i).get(j).get(5));
priceProduct.setPaintFlags(nameProduct.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
priceProduct.setGravity(Gravity.CENTER);
special_discount.setGravity(Gravity.CENTER);
nameProduct.setGravity(Gravity.CENTER);
linearLayout1.addView(image);
linearLayout1.addView(nameProduct);
linearLayout1.addView(priceProduct);
linearLayout1.addView(special_discount);
} else if (!String.valueOf(main.get(i).get(j).get(4)).equals(null)) {
priceProduct.setText(main.get(i).get(j).get(3));
special_discount.setText(main.get(i).get(j).get(4));
priceProduct.setPaintFlags(nameProduct.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
priceProduct.setGravity(Gravity.CENTER);
special_discount.setGravity(Gravity.CENTER);
nameProduct.setGravity(Gravity.CENTER);
linearLayout1.addView(image);
linearLayout1.addView(nameProduct);
linearLayout1.addView(priceProduct);
linearLayout1.addView(special_discount);
} else {
priceProduct.setText(main.get(i).get(j).get(3));
priceProduct.setGravity(Gravity.CENTER);
nameProduct.setGravity(Gravity.CENTER);
linearLayout1.addView(image);
linearLayout1.addView(nameProduct);
linearLayout1.addView(priceProduct);
}
linearLayout.addView(linearLayout1, j);
}
horizontalScrollView.addView(linearLayout);
// linearLayoutmens.addView(textView);
// linearLayoutmens.addView(v1);
linearLayoutmens.addView(linearLayout2);
linearLayoutmens.addView(horizontalScrollView);
linearLayoutmens.addView(v);
}
}
I am new in android programming. Please help me!

If you haven't found solution yet, then you can try the following
textView.setGravity(Gravity.RIGHT); This basically act as Gravity for text within TextView for more info refer So basically it is counter part of android:Gravity. Hence we need LayoutParam
Your problem it can be solved in two ways using LinearLayout and RelativeLayout change the below code from
TextView textView = new TextView(MainActivity.this);
String content = front_category_catetory_name.get(i);
content = content.replace("'", "");
content = content.replace("&","");
textView.setText(content);
textView.setGravity(Gravity.RIGHT);
// textView.setGravity(Gravity.CENTER);
textView.setTextSize(20);
TextView textView2 = new TextView(MainActivity.this);
textView2.setText("VIEW ALL");
textView2.setTextColor(Color.BLUE);
textView2.setGravity(Gravity.LEFT);
LinearLayout linearLayout2 = new LinearLayout(MainActivity.this);
linearLayout2.setOrientation(LinearLayout.HORIZONTAL);
linearLayout2.addView(textView,0);
linearLayout2.addView(textView2,1);
Solution1 Using LinearLayout
TextView textView = new TextView(MainActivity.this);
String content = front_category_catetory_name.get(i);
content = content.replace("'", "");
content = content.replace("&","");
textView.setText(content);
textView.setGravity(Gravity.START); //Gravity.Left also works
textView.setTextSize(20);
TextView textView2 = new TextView(MainActivity.this);
textView2.setText("VIEW ALL");
textView2.setTextColor(Color.BLUE);
textView2.setGravity(Gravity.END); //Gravity.Right also works
LinearLayout linearLayout2 = new LinearLayout(MainActivity.this);
linearLayout2.setOrientation(LinearLayout.HORIZONTAL);
LinearLayout.LayoutParams linearLayoutParam = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT,2);
linearLayout2.setLayoutParams(linearLayoutParam);
LinearLayout.LayoutParams viewParam = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT,1);
linearLayout2.addView(textView, 0, viewParam);
linearLayout2.addView(textView2, 1, viewParam);
In above solution I have assigned weightSum = 2 to LinearLayout and weight=1 to each TextView.
Solution2 Using RelativeLayout
TextView textView = new TextView(MainActivity.this);
String content = front_category_catetory_name.get(i);
content = content.replace("'", "");
content = content.replace("&","");
textView.setText(content);
//textView.setGravity(Gravity.START); //Gravity.Left also works
textView.setTextSize(20);
TextView textView2 = new TextView(MainActivity.this);
textView2.setText("VIEW ALL");
textView2.setTextColor(Color.BLUE);
//textView2.setGravity(Gravity.END); //Gravity.Right also works
RelativeLayout relativeLayout2 = new RelativeLayout(MainActivity.this);
RelativeLayout.LayoutParams relativeLayoutParam1 = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
relativeLayoutParam1.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
relativeLayout2.addView(textView, 0, relativeLayoutParam1);
RelativeLayout.LayoutParams relativeLayoutParam2 = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
relativeLayoutParam2.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
relativeLayout2.addView(textView2, 1, relativeLayoutParam2);
In this case textView.setGravity(Gravity.RIGHT); is optional.
Update Based on comment
If you want underlined text as in SAMPLE below then check one of the answer here
<u>Sample</u>
But if you want a black line then it is view and you need to add it after adding linearlayout2 or relativelayout2 depending upon the solution in the main view.

Related

Display a Linear Layout programmatically in a loop

Hello everyone please i need help. I'm a biginner in Android programming so since i was trying to solve this problem it took me all my days that why i want you to help me please. I'm trying to display a Linear layout according to the loop condition but the issue is that when i'm doing that it's just displaying the last one and i'm not able to see others when it's displaying. Here are my codes
ScrollView sv = new ScrollView(this);
sv.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT));
sv.setPadding(5, 5, 5, 5);
sv.setBackgroundColor(getResources().getColor(R.color.colorWhite));
LinearLayout ll = null;
for (int i=0; i < 2; i++){
try {
//Here iam defining the LinearLayout
ll = new LinearLayout(this);
ll.setId(i);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
ll.setLayoutParams(params);
ll.setOrientation(LinearLayout.VERTICAL);
ll.setBackground(getDrawable(R.drawable.circle_view));
//Here iam defining the RelativeLayout
RelativeLayout Rl = new RelativeLayout(this);
RelativeLayout.LayoutParams param = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
Rl.setLayoutParams(param);
//Defining a layout params for widgets
RelativeLayout.LayoutParams image = new RelativeLayout.LayoutParams(100, 90);
image.addRule(RelativeLayout.ALIGN_LEFT);
//Creating widgets
ImageView im = new ImageView(this);
im.setImageResource(R.mipmap.airtel);
im.setId(R.id.image1);
im.setLayoutParams(image);
//----------------
RelativeLayout.LayoutParams txtone = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
txtone.addRule(RelativeLayout.RIGHT_OF, R.id.image1);
txtone.setMargins(0, 5, 0, 45);
TextView txtVone = new TextView(this);
txtVone.setText("ArkaL"+i);
//txtVone.setTextSize(R.dimen.MainTextSize);
txtVone.setTextSize(15);
txtVone.setTypeface(txtVone.getTypeface(), Typeface.BOLD);
txtVone.setLayoutParams(txtone);
//------------------------------------------------
RelativeLayout.LayoutParams txttwo = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
txttwo.addRule(RelativeLayout.RIGHT_OF, R.id.image1);
txttwo.setMargins(0, 45, 0, 0);
TextView txtVtwo = new TextView(this);
txtVtwo.setText("République Démocratique du Congo");
txtVtwo.setTypeface(txtVtwo.getTypeface(), Typeface.SERIF.getStyle());
txtVtwo.setTextSize(10);
txtVtwo.setLayoutParams(txttwo);
Rl.addView(im);
Rl.addView(txtVone);
Rl.addView(txtVtwo);
RelativeLayout.LayoutParams recycler = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
RecyclerView rv = new RecyclerView(this);
rv.setLayoutParams(recycler);
ll.addView(Rl);
ll.addView(rv);
}catch (Exception e){
}
}
//Here for assigning Linear to ScrollView
sv.addView(ll);
this.setContentView(sv);
Please help me.
your problem is that you created one LinearLayout and changed it in a loop and then added it to a ScrollView so at the end you will have just one LinearLayout. as you may know an ScrollView should have just one View as a child view so you should add a LinearLayout for a ScrollView and add as many LinearLayout as you want to the primary LinearLayout. your code can be like this:
LinearLayout llMain = new LinearLayout(this);
LinearLayout.LayoutParams params2 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
llMain.setLayoutParams(params2);
llMain.setOrientation(LinearLayout.VERTICAL);
sv.addView(llMain);
LinearLayout ll = null;
for (int i = 0; i < 2; i++) {
try {
//Here iam defining the LinearLayout
ll = new LinearLayout(this);
ll.setId(i);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
ll.setLayoutParams(params);
ll.setOrientation(LinearLayout.VERTICAL);
...
llMain.addView(ll);
} catch (Exception e) {
...
note that the Height of primary linearlayour should wrap its content;

Dynamically create textview in center

I have a list of items and a relative layout. I want to add as many relative layouts as the no of items in list and textview in each on them.
My problem is, I am not able to position the textview in the centre of the layout.
Below in my code
for(int i = 0; i < Names.size(); i++)
{
textView = new TextView(this);
textView.setText(Names.get(i));
rt=new RelativeLayout(this);
rt.setBackgroundResource(R.drawable.mednamedash);
int curTextViewId = prevTextViewId + 1;
int curRLId = prevRLId + 1;
textView.setId(curTextViewId);
rt.setId(curRLId);
final RelativeLayout.LayoutParams params =
new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.BELOW, prevTextViewId);
rt.setLayoutParams(params);
//textView.setLayoutParams(params);
rt.addView(textView);
textView.setGravity(Gravity.CENTER);
prevTextViewId = curTextViewId;
prevRLId=curRLId;
noon.addView(rt, params);
}
Here rt is the dynamiclly created RelativeLayout and textView is the dynamically created TextView... noon is the RelativeLayout I am adding everything into.
Try this:
final RelativeLayout.LayoutParams tvParams =
new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
tvParams.addRule(RelativeLayout.CENTER_IN_PARENT);
rt.addView(textView, tvParams);
Try with textView.setGravity(Gravity.CENTER); before its addition to the rt (RelativeLayout)

My Android layout is not working as expected

I want to design an UI with 3 elements, one TextView tvStart, one SeekBar sbProgress and one TextView tvEnd.
I want tvStart aligned left and tvEnd aligned right, with sbProgress at the center.
And I want to do everything programmatically!
I have this code:
public void MyLayout() {
LinearLayout layout = new LinearLayout(this);
TextView tvStart = new TextView(this);
SeekBar sbProgress = new SeekBar(this);
TextView tvEnd = new TextView(this);
tvStart.setText("0");
tvEnd.setText("100");
sbProgress.setMax(100);
layout.addView(tvStart);
layout.addView(sbProgress);
layout.addView(tvEnd);
setContentView(layout);
}
But it seems the 3 elements are aligned from left to right and the SeekBar is very short.
Is it possible to do such tings with LinearLayout or do I need to use RelativeLayout?
public void MyLayout() {
LinearLayout layout = new LinearLayout(this);
TextView tvStart = new TextView(this);
SeekBar sbProgress = new SeekBar(this);
TextView tvEnd = new TextView(this);
tvStart.setText("0");
tvEnd.setText("100");
sbProgress.setMax(100);
LinearLayout.LayoutParams startParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
LinearLayout.LayoutParams sbParams = new LinearLayout.LayoutParams(0, LayoutParams.WRAP_CONTENT);
sbParams.weight = 1;
LinearLayout.LayoutParams endParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
layout.addView(tvStart, tvParams);
layout.addView(sbProgress, sbParams);
layout.addView(tvEnd, endParams);
setContentView(layout);
}

Relative Layout dynamic textView

RelativeLayout layout = (RelativeLayout)findViewById(R.id.rl);
for(int i = 1; i <= count; i++)
{
final TextView textViewUtente = new TextView(mContext);
final TextView textViewMessaggio = new TextView(mContext);
final TextView textViewData = new TextView(mContext);
textViewUtente.setText(sepUser[i]);
textViewMessaggio.setText(sepMessage[i]);
textViewData.setText(sepDate[i]);
int curTextViewIdUtente = 1000+i;
int curTextViewIdMessaggio = 2000+i;
int curTextViewIdData = 3000+i;
textViewUtente.setId(curTextViewIdUtente);
textViewMessaggio.setId(curTextViewIdMessaggio);
textViewData.setId(curTextViewIdData);
final RelativeLayout.LayoutParams params =
new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
params.topMargin= 100+i*150;
textViewUtente.setLayoutParams(params);
layout.addView(textViewUtente, params);
params.topMargin= 250+i*150;
textViewMessaggio.setLayoutParams(params);
layout.addView(textViewMessaggio, params);
params.topMargin= 100+i*150;
params.rightMargin=200;
textViewData.setLayoutParams(params);
layout.addView(textViewData, params);
}
I need to create 3 textview (that create the post on the blog from the user) every for loop like this:
Post 1:
TextViewUSer TextViewDate
TextViewMessage
Post 2:
TextViewUSer TextViewDate
TextViewMessage
I'm able to make one blog's post(a group of text view) above the other.
I just can position them, like this, from eachother. they stuck one above the other.
I also trued with addRules the .ABOVE and others but it's even worst cause they stuck all on the top left corner
You can add Rules to your textviews like this
tv1 = new TextView(this);
tv1.setId((int)System.currentTimeMillis());
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
lp.addRule(RelativeLayout.BELOW, recent.getId());
tv1.setText("Time: "+System.currentTimeMillis());
rr.addView(tv1, lp);
Egs.
lp.addRule(RelativeLayout.ALIGN_PARENT_TOP);
lp.addRule(RelativeLayout.RIGHT_OF, tv1.getId());
lp.addRule(RelativeLayout.BELOW, someOtherView.getId())
lp.addRule(RelativeLayout.ALIGN_PARENT_LEFT)

Create multiple linear layout and divider programmatically

In my app. there is activity contain multiple linear layout and divider which created programmatically , its run fine ,
but i have to duplicate the linear layout and divider 5 times ,and all are the same except two things :
1- each linear layout has different string .
2- first divider margin differ than others divider margin .
is there's better approach to do that with more clean and shorter code .
any help will be much appreciated .
public class Dreams extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Boolean customTitleSupported =
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.trip);
if (customTitleSupported) {
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,R.layout.custom_title);
}
TextView tv = (TextView) findViewById(R.id.title);
tv.setTypeface(FontFactory.getOldEnglish(getBaseContext()));
tv.setText("Dreams");
LinearLayout ll = (LinearLayout)findViewById(R.id.linearLayout);
// add text view
TextView tv1 = new TextView(this);
tv1.setGravity(Gravity.RIGHT);
tv1.setTextSize(30);
tv1.setTypeface(FontFactory.getOldEnglish(getBaseContext()));
ll.addView(tv1);
tv1.setText(Html.fromHtml(getString(R.string.dreams)));
ImageView divider1 = new ImageView(this);
LinearLayout.LayoutParams lp1 =
new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, 5);
lp1.setMargins(40, 0, 40, 0);
divider1.setLayoutParams(lp1);
divider1.setBackgroundColor(Color.RED);
ll.addView(divider1);
TextView tv2 = new TextView(this);
tv2.setGravity(Gravity.RIGHT);
tv2.setTextSize(30);
tv2.setTypeface(FontFactory.getOldEnglish(getBaseContext()));
ll.addView(tv2);
tv2.setText(Html.fromHtml(getString(R.string.dream_1)));
ImageView divider2 = new ImageView(this);
LinearLayout.LayoutParams lp2 =
new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, 5);
lp2.setMargins(10, 10, 10, 10);
divider2.setLayoutParams(lp2);
divider2.setBackgroundColor(Color.RED);
ll.addView(divider2);
TextView tv3 = new TextView(this);
tv3.setGravity(Gravity.RIGHT);
tv3.setTextSize(30);
tv3.setTypeface(FontFactory.getOldEnglish(getBaseContext()));
ll.addView(tv3);
tv3.setText(Html.fromHtml(getString(R.string.dream_2)));
ImageView divider3 = new ImageView(this);
LinearLayout.LayoutParams lp3 =
new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, 5);
lp3.setMargins(10, 10, 10, 10);
divider3.setLayoutParams(lp3);
divider3.setBackgroundColor(Color.RED);
ll.addView(divider3);
TextView tv4 = new TextView(this);
tv4.setGravity(Gravity.RIGHT);
tv4.setTextSize(30);
tv4.setTypeface(FontFactory.getOldEnglish(getBaseContext()));
ll.addView(tv4);
tv4.setText(Html.fromHtml(getString(R.string.dream_3)));
ImageView divider4 = new ImageView(this);
LinearLayout.LayoutParams lp4 =
new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, 5);
lp4.setMargins(10, 10, 10, 10);
divider4.setLayoutParams(lp4);
divider4.setBackgroundColor(Color.RED);
ll.addView(divider4);
TextView tv5 = new TextView(this);
tv5.setGravity(Gravity.RIGHT);
tv5.setTextSize(30);
tv5.setTypeface(FontFactory.getOldEnglish(getBaseContext()));
ll.addView(tv5);
tv5.setText(Html.fromHtml(getString(R.string.dream_4)));
ImageView divider5 = new ImageView(this);
LinearLayout.LayoutParams lp5 =
new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, 5);
lp5.setMargins(10, 10, 10, 10);
divider5.setLayoutParams(lp5);
divider5.setBackgroundColor(Color.RED);
ll.addView(divider5);
TextView tv6 = new TextView(this);
tv6.setGravity(Gravity.RIGHT);
tv6.setTextSize(30);
tv6.setTypeface(FontFactory.getOldEnglish(getBaseContext()));
ll.addView(tv6);
tv6.setText(Html.fromHtml(getString(R.string.dream_5)));
ImageView divider6 = new ImageView(this);
LinearLayout.LayoutParams lp6 =
new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, 5);
lp6.setMargins(10, 10, 10, 10);
divider6.setLayoutParams(lp6);
divider6.setBackgroundColor(Color.RED);
ll.addView(divider6);
}
}
Since all that is changing is the TextView setText() you can make this a for loop with a list of String inputs. For example:
LinearLayout ll = (LinearLayout)findViewById(R.id.linearLayout);
String[] textEntries = { getString(R.string.dream),
getString(R.string.dream_1),
getString(R.string.dream_2),
getString(R.string.dream_3),
getString(R.string.dream_4),
getString(R.string.dream_5)
};
for ( int i = 0; i < textEntries.length; i++)
{
TextView tv = new TextView(this);
tv.setGravity(Gravity.RIGHT);
tv.setTextSize(30);
tv.setTypeface(FontFactory.getOldEnglish(getBaseContext()));
ll.addView(tv);
tv.setText(Html.fromHtml(textEntries[i]));
ImageView divider = new ImageView(this);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, 5);
lp.setMargins(10, 10, 10, 10);
divider.setLayoutParams(lp);
divider.setBackgroundColor(Color.RED);
ll.addView(divider);
}
first of all it would be easier if you define your layouts in XML instead of adding them programmatically. You will profit from the benefits of the UI editor as well. :)
Second, you may want to use ListView and an Adapter to fill the list, since you do not want do duplicate the same tasks for each layout.
Maybe these two links are helpful:
1. http://developer.android.com/guide/topics/ui/declaring-layout.html
2. http://developer.android.com/guide/topics/ui/layout/listview.html
So, to finally answer your question, I would do the following:
Create a file, e.g. list_item.xml, with something like:
<LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="10dp"><TextView your attributes.../></LinearLayout>
Create another layout, for instance main.xml, which contains a ListView. You can change the color of the divider like described here How to change the divider color in the listview?.
In your code (activity or fragment) add the main.xml as content view via setContentView().
Also in your code you should then add an adapter to the ListView which then populates the list for you. Here is an example How to customize listview using baseadapter
Finally, and since you separate the concerns (design and code), you could achieve what you want with just a few lines in your activity (the layout stuff would be in the xml and the population could be moved to a separated class like MyAdapter.java...)
Hope that helps...

Categories

Resources