Building LinearLayout programmatically - android

I'm trying to get my layout to look like this:
My class containing this code extends Linelayout itself and adds the different elements with addView
setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
LinearLayout line = new LinearLayout(context);
line.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
viewVerLabels = new VerLabelsView(context);
// add y axis linearlayout & graphview
line.addView(viewVerLabels);
line.addView(new GraphViewContentView(context), new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT, 1));
line.setOrientation(LinearLayout.HORIZONTAL);
// add to main linearlayout
addView(line);
LinearLayout line2 = new LinearLayout(context);
line2.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
viewHorLabels = new HorLabelsView(context);
setOrientation(LinearLayout.VERTICAL);
// add x axis labels
line2.addView(viewHorLabels);
// add to main linearlayout
addView(line2);
My XML file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/graph2"
android:layout_width="fill_parent"
android:layout_height="250dp"
android:orientation="vertical" />
With this code I get the Y axis labels and the graph to appear, but the X axis labels are missing
I can't seem to get what I want, I've tried several combinations, what am I doing wrong

This seems to be a size issue where when adding the first Layouts to the parent, they fill up the entire area and the others are just now shown.
As other mentions, recommendations (I would try either one independent)
- A scroll View in top of everything.
- Create a separate Control that extends from LinearLayout with its respective layout.xml that just inflates inside. And on this Activity when showing just instantiate this control and add just one Layout that has them all.
- Create a New LinearLayout in top of everything, add all to it and then add that one to the actual View.
If any work or you got a solution post it so others can see and benefit.

Hi just try this code I have changed some values, views and setBackground to colors, it gives same output as you desire.
LinearLayout MLayoutTm = (LinearLayout) findViewById(R.id.graph2);
LinearLayout line = new LinearLayout(this);
line.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, 0, 1));
line.setBackgroundColor(Color.BLUE);
TextView viewVerLabels = new TextView(this);
viewVerLabels.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT));
viewVerLabels.setText("Nimsih");
viewVerLabels.setBackgroundColor(Color.RED);
line.addView(viewVerLabels);
TextView GraphViewContentView = new TextView(this);
GraphViewContentView.setBackgroundColor(Color.MAGENTA);
GraphViewContentView.setText("Nimsih2");
GraphViewContentView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.MATCH_PARENT, 1));
line.addView(GraphViewContentView);
line.setOrientation(LinearLayout.HORIZONTAL);
MLayoutTm.addView(line);
LinearLayout line2 = new LinearLayout(this);
line2.setBackgroundColor(Color.RED);
line2.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
TextView viewHorLabels = new TextView(this);
viewHorLabels.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT,
1));
viewHorLabels.setBackgroundColor(Color.CYAN);
viewHorLabels.setText("Nimsih3 ");
MLayoutTm.setOrientation(LinearLayout.VERTICAL);
line2.addView(viewHorLabels);
MLayoutTm.addView(line2);

i am acheving x-axis lable and y-axis lable by using code of arnodenhond. you can get from
http://android.arnodenhond.com/components
my emulator screen shot

try to set different background colors to the layouts you created to see their real areas. Most likely some layouts occupied all the available height so the X axis labels are out the border.
You may want to assign some kind of weight for the layouts, like the total weight sum as 5, assign 4 for the drawing area and set 1 to the layout that holds the X-axis labels

Here's my code that allowed me to get round the problem
graphViewStyle = new GraphViewStyle();
paint = new Paint();
graphSeries = new ArrayList<GraphViewSeries>();
setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
setOrientation(LinearLayout.VERTICAL);
LinearLayout line = new LinearLayout(context);
line.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
line.setOrientation(LinearLayout.HORIZONTAL);
LinearLayout line2 = new LinearLayout(context);
line2.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
line2.setOrientation(LinearLayout.HORIZONTAL);
txt = new TextView(mContext);
addView(txt);
GraphViewContentView graphContentView = new GraphViewContentView(context);
graphContentView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, 260, 1));
line.addView(graphContentView);
viewVerLabels = new VerLabelsView(context);
viewVerLabels.setLayoutParams(new LayoutParams(30, 260));
viewHorLabels = new HorLabelsView(context);
viewHorLabels.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, 15));
line.addView(viewVerLabels);
addView(line);
line2.addView(viewHorLabels);
TextView v = new TextView(context);
v.setLayoutParams(new LayoutParams(30, 15));
line2.addView(v);
addView(line2);

Related

Programmatically LinearLayout Weight

I'm trying to develop similar to grid but using LinearLayout. I would like to have 3 images and exact bottom text after images in single row.
What I have tried:
LinearLayout layout = new LinearLayout(context);
layout.setOrientation(LinearLayout.VERTICAL);
ImageView icon = new ImageView(context);
Item item = getItem(page, index);
Log.e("tag", item.getDrawable());
imageLoader.displayImage(item.getDrawable(), icon, R.drawable.ic_launcher);
icon.setPadding(15, 15, 15, 15);
layout.addView(icon);
TextView label = new TextView(context);
label.setTag("text");
label.setText(item.getName());
label.setTextColor(Color.BLACK);
label.setGravity(Gravity.TOP | Gravity.CENTER_HORIZONTAL);
label.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
layout.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
layout.setWeightSum(3f);
layout.addView(label);
I've a view method which returns Viewso I return return layout; at the end of the method.
Here I 've given weight 3 button this is not working for me. And code show more then 3 images in row with text but would like to have weight 3 images and bottom text .
LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT, 1.0f);
or
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
params.weight = 1.0f;
Button button = new Button(this);
button.setLayoutParams(params);
I would define a TableLayout with as many TableRows as you need. On each of those TableRows, I'd add a LinearLayout with VERTICAL orientation consisting of the two Views you need: ImageView and TextView.
This is the LinearLayout where you should set a weight of 1 (to all of them). You will have to get the screen's width and see whether the new LinearLayout to be added still fits the current row. If not, simply start a new TableRow.
To get the width of the screen you can use this:
final Display display = getWindowManager().getDefaultDisplay();
final Point size = new Point();
display.getSize(size);
final WindowManager.LayoutParams params = getWindow().getAttributes();
The screen's width might be accessed via the params.width attribute, just compare it with your LinearLayout's .getWidth() method and keep an incremental account (a float) of the current TableRow's width to help you determine if the current item should be placed in an existing row or a new one.
Programmtically setting Linear Layout weight
LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT, 1.0f);

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...

LinearLayout won't fill the width of the screen

I've been working through a bunch of examples on StackOverflow to create a dynamic layout.
I've made some headway but have now stalled on a particular bug.
How do I make this code fill the width of the parent?
LinearLayout masterLayout = (LinearLayout) findViewById(R.id.button_views);
for(int i=0; i<(launchers.length/3+1); i++){
layout[i] = new LinearLayout(getBaseContext());
layout[i].setLayoutParams(new LinearLayout.LayoutParams(
ViewGroup.LayoutParams.FILL_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
layout[i].setWeightSum(3);
for(int i1=0; i1<(3); i1++){
launchers[i1] = new Button(this);
launchers[i1].setText(titles[i1]);
launchers[i1].setLayoutParams(new LinearLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
launchers[i1].setOnClickListener(this);
layout[i].addView(launchers[i1]);
}
masterLayout.addView(layout[i]);
The code works MOSTLY as desired, the part i'm missing is the buttons being an even width and being spaced evenly across the full length of the screen.
Figured out that LinearLayout.LayoutParams has an option for weight at the end of it. Here is the final code for a dynamic button layout system that fits the width of the screen.
LinearLayout masterLayout = (LinearLayout) findViewById(R.id.button_views);
for(int i=0; i<(launchers.length/3+1); i++){
layout[i] = new LinearLayout(getBaseContext());
layout[i].setLayoutParams(new LinearLayout.LayoutParams(
ViewGroup.LayoutParams.FILL_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
layout[i].setWeightSum(3);
for(int i1=0; i1<(3); i1++){
launchers[i1] = new Button(this);
launchers[i1].setText(titles[i1]);
launchers[i1].setLayoutParams(new LinearLayout.LayoutParams(
ViewGroup.LayoutParams.FILL_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT, 1.0f));
launchers[i1].setOnClickListener(this);
layout[i].addView(launchers[i1]);
}
masterLayout.addView(layout[i]);
What would help would be a way to allow the buttons to always be square if anyone wants to have a go at that...

Android: Programmatically Setting width of Child View using Percentage

I've read this article:
Setting width of Child View using Percentage
So far it works great if we set the weight and width in XML but I want to do it programmatically. I've a table in my layout (XML) and I am adding rows to it programmatically. In each row, I am adding 2 Text Views. I tried doing like this:
TableLayout tl = (TableLayout)findViewById(R.id.myTable);
for(int i = 0; i < nl.getLength(); i++){
NamedNodeMap np = nl.item(i).getAttributes();
TableRow trDetails = new TableRow(this);
trDetails.setBackgroundColor(Color.BLACK);
TextView tvInfo = new TextView(this);
tvInfo.setTextColor(Color.WHITE);
tvInfo.setGravity(Gravity.LEFT);
tvInfo.setPadding(3, 3, 3, 3);
tvInfo.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 10);
tvInfo.setText("info");
TextView tvDetails = new TextView(this);
tvDetails.setTextColor(Color.WHITE);
tvDetails.setGravity(Gravity.LEFT);
tvDetails.setPadding(3, 3, 3, 3);
tvDetails.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 10);
tvDetails.setText(np.getNamedItem("d").getNodeValue());
trDetails.addView(tvInfo, new LinearLayout.LayoutParams(0, LayoutParams.WRAP_CONTENT, 0.50f));
trDetails.addView(tvDetails, new LinearLayout.LayoutParams(0, LayoutParams.WRAP_CONTENT, 0.50f));
tl.addView(trDetails,new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
}
but with no luck, it shows blank screen but if I do it in XML then it shows perfectly. Please help me sorting out this issue where I am doing wrong?
Thanks
You should use TableRow.LayoutParams and not Linearlayout.LayoutParams
trDetails.addView(tvInfo, new TableRow.LayoutParams(0, LayoutParams.WRAP_CONTENT, 0.50));
trDetails.addView(tvDetails, new TableRow.LayoutParams(0, LayoutParams.WRAP_CONTENT, 0.50));

Programmatically RelativeLayout layouting issue

I have an Achievement dialog, and would like this:
And this is my code, each achievement is a relative layout, and all are contained in a LinearLayout (pnlAchievement):
RelativeLayout pnlItem = new RelativeLayout(Parent);
pnlItem.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
TextView txtItemName = new TextView(Parent);
txtItemName.setText(ACHIEVEMENTS_NAME[i]);
txtItemName.setLayoutParams(new LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
TextView txtAchived = new TextView(Parent);
txtAchived.setText(Achived[i] ? "Achieved!" : "Not Achieved!");
RelativeLayout.LayoutParams rlpAchived = new RelativeLayout.LayoutParams(
new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
rlpAchived.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
rlpAchived.addRule(RelativeLayout.ALIGN_RIGHT, txtItemName.getId());
txtAchived.setLayoutParams(rlpAchived);
TextView txtDescription = new TextView(Parent);
txtDescription.setText(ACHIEVEMENTS_DESCRIPTION[i]);
txtDescription.setHorizontallyScrolling(false);
RelativeLayout.LayoutParams rlpDesc = new RelativeLayout.LayoutParams(
new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
rlpDesc.addRule(RelativeLayout.ALIGN_BOTTOM, txtItemName.getId());
rlpDesc.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
txtDescription.setLayoutParams(rlpDesc);
pnlItem.addView(txtItemName);
pnlItem.addView(txtAchived);
pnlItem.addView(txtDescription);
pnlAchievement.addView(pnlItem);
It work well list all the achievements, but the description is NOT below the Name, it is OVER the Name, like this:
And, can you please tell me how to add the line between every item without using Canvas (this is not very important, but if there is, it's better)?
You need to use RelativeLayout.BELOW
For the line you can use a Label with a backgroundcolor.

Categories

Resources