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);
Related
I'm having trouble calculating the weight inside of an scroll view on my android app. I want the app's content to resize proportionally to the screen size. How can I calculate the weight inside a scroll view?
Here some of my code:
LinearLayout layout_principal = (LinearLayout) findViewById(R.id.layout_items);// is horizontal layout and layout of Scrollview
LinearLayout layout_titulo_opcion = new LinearLayout(this);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.FILL_PARENT,
0,
1f);
layout_titulo_opcion.setLayoutParams(layoutParams);
LinearLayout layout_titulo_opcion2 = new LinearLayout(this);
LinearLayout.LayoutParams layoutParams2 = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.FILL_PARENT,
0,
1f);
layout_titulo_opcion2.setLayoutParams(layoutParams2);
layout_principal.addView(layout_titulo_opcion);
layout_principal.addView(layout_titulo_opcion2);
To get/set the weight, use this:
LinearLayout.LayoutParams mylp = (LinearLayout.LayoutParams)myLayout.getLayoutParams();
int myWeight = mylp.weight; // get weight
mylp.weight = 4; // set weight
To set the weight sum, use this:
myLayout.setWeightSum(10);
I am doing an android app that have images which are located besides each other,
every image has its own width, I want to add these images in one line to fit the screen width,
with the same ratio for every one
I wrote code to add this images but still to know how to set the width programmatically
ImageView view = new ImageView(this);
view.setImageResource(R.drawable.a1);
ImageView view1 = new ImageView(this);
view1.setImageResource(R.drawable.a2);
ImageView view2 = new ImageView(this);
view2.setImageResource(R.drawable.a3);
LinearLayout my_root = (LinearLayout) findViewById(R.id.root_layout);
LinearLayout child = new LinearLayout(this);
child.setOrientation(LinearLayout.HORIZONTAL);
child.addView(view);
child.addView(view1);
child.addView(view2);
my_root.addView(child);
only image 1 and 2 appear but the third didn't appear because the width of screen finished
Any help !!
Thank you :)
You must need to use weight parameter to do this.
Try below code:
ImageView view = new ImageView(this);
view.setImageResource(R.drawable.a1);
view.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1f));
ImageView view1 = new ImageView(this);
view1.setImageResource(R.drawable.a2);
view1.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1f));
ImageView view2 = new ImageView(this);
view2.setImageResource(R.drawable.a3);
view2.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1f));
Use Layout Parameters to set height and Width of views at runtime.
like this
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(50, 50);
view .setLayoutParams(layoutParams);
I have a linear layout that's horizontal. It has three textviews in it. How can I set it so that the left and right widths are wrapping contents, but the center cell width is fully maximized, and the textview is left aligned in it? I want to create this in only java.
This is what I have so far, but the middle cell is not being maximized...
Does anyone know how to fix this?
Thanks
LinearLayout titleLL = new LinearLayout(this);
titleLL.setOrientation(LinearLayout.HORIZONTAL);
TextView num = new TextView(this);
num.setText(String.format("%d", index+1));
num.setPadding(5, 5, 5, 5);
num.setTextAppearance(this, R.style.title_numbering);
num.setBackgroundResource(R.drawable.title_numbering_background);
TextView name = new TextView(this);
name.setText(task.name);
name.setPadding(10, 0, 0, 10);
name.setTextAppearance(this, R.style.title);
TextView edit = new TextView(this);
edit.setText("Edit");
titleLL.addView(num);
titleLL.addView(name);
titleLL.addView(edit);
Use weight when you add your middle textview:
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
0, LinearLayout.LayoutParams.WRAP_CONTENT, 1);
titleLL.addView(name, params);
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);
I have a TextView and an ImageButton in a linear layout (horizontal). Total width I have is 300 pixel. Button image is 50x50. Max width I can use for text is 250. The code below works perfect if the text width is less than 250 pixels (WRAP_CONTENT work nice).
// create relative layout for the entire view
LinearLayout layout = new LinearLayout(this);
layout.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
layout.setOrientation(LinearLayout.HORIZONTAL);
// create TextView for the title
TextView titleView = new TextView(this);
titleView.setText(title);
layout.addView(titleView);
// add the button onto the view
bubbleBtn = new ImageButton(this);
bubbleBtn.setLayoutParams(new LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.FILL_PARENT));
layout.addView(bubbleBtn);
Problem comes when the text occupies more than 250 pixels. Button gets pushed out and becomes invisible within that 300 pixel space.
What I want is this: Allocate 50 pixels width for the image. WRAP_CONTENT in the remaining 250 pixels. In other words, instead of filling in from left, fill in from the right. Is Gravity the right thing to use in this context? How and where should I use it in the code?
Or any other better way of doing this?
Use a RelativeLayout instead of a LinearLayout. Set the LayoutParams of each View as follows:
// add the button onto the view
bubbleBtn = new ImageButton(this);
bubbleBtn.setId(1); // should set this using a ids.xml resource really.
RelativeLayout.LayoutParams bbLP = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
bbLP.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
bbLP.addRule(RelativeLayout.CENTER_VERTICAL);
layout.addView(bubbleBtn, bbLP);
// create TextView for the title
TextView titleView = new TextView(this);
titleView.setText(title);
titleView.setGravity(Gravity.RIGHT);
RelativeLayout.LayoutParams tvLP = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
tvLP.addRule(RelativeLayout.LEFT_OF, 1);
tvLP.addRule(RelativeLayout.CENTER_VERTICAL);
layout.addView(titleView, tvLP);