android layout - WRAP_CONTENT from right to left possible? - android

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);

Related

Align image and text on textview

In my app, I populate a linearlayout with a text and a little icon (like a listview with a custom adapter).
I need the text and the picture to have the same size. Is there a way to do that?
Here's the code:
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT);
LinearLayout LayoutPadre = new LinearLayout(this);
TextView Tag = new TextView(this);
ImageView img = new ImageView(this);
Tag.setText(data);
if(stato.equalsIgnoreCase("presente")){
img.setImageResource(R.drawable.ic_presente);
}else{
img.setImageResource(R.drawable.ic_assente);
}
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(75, LayoutParams.WRAP_CONTENT); //Here I need the img to have same size as the text, but Tag.getHeight() returns 0
layoutParams.setMargins(50, 0, 0, 0);
img.setLayoutParams(layoutParams);
LayoutPadre.setLayoutParams(params);
LayoutPadre.addView(Tag);
LayoutPadre.addView(img);
The problem is that the image is not horizontally centered like the img.
Look at the screen
Thanks to all.
keep the layout_height of text and image as Match_Parent and not Wrap_Content... By that
If you are preserving the android:layout_height="wrap_content" or it did not work, you can also do android:gravity="center"

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);

How to set dynamic size for Android's view?

I am trying to set the minimum sizes, but still one view is taking over completely. My normal view looks like this:
These two values are coming from the server. But if the size of the "hello" changes to something longer then the number string disappears.
My code looks like this:
if (jsonDataViewType.get(i).toString().equals("editBox")) {
editText = new EditText(context);
editText.setMinLines(1);
editText.setMinimumWidth(10);
linearLayoutHorizontal.addView(editText);
editText.setText(jsonDataValue.get(i));
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
// RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
params.weight = 1.0f;
editText.setGravity(Gravity.CENTER_HORIZONTAL);
editText.setLayoutParams(params);
}
else if(jsonDataViewType.get(i).toString().equals("button")) {
helloButton = new Button(context);
// helloButton.setMinLines(1);
linearLayoutHorizontal.addView(helloButton);
helloButton.setText(jsonDataValue.get(i));
// testing
// helloButton.setText("hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhheeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeelllllllllllllllllllllllllllooooooooooo");
}
I am setting both the minLines and minWidth, but as you can see its still not working.
Try to set layout_width=0 for both EditView andButton`. Then set layout_weight to 0.8 and 0.2
So theese two view will fill parents width. Now your EditView is out of the screen left side.
So your problem is that setting setMinLines(1) is a lower bound setting meaning that at minimum make this one line long. If you want to control how much is being shown you need to setMaxLines(1); and then the same for width setting setMinWidth(10) means at least let this be 10 wide so do setMaxWidth(10)
Then you may want to consider adding ellipsize to the buttons text if you want it to show that there is more text but its cut off.
Update
If you want them to sit next to each other:
// Linear Layout has weight sum of 3
linearLayoutHorizontal.setWeightSum(3.0f);
// Lets EditText take up 2/3 of view
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 2.0f);
editText.setLayoutParams(params);
// Button gets the other 1/3
LinearLayout.LayoutParams params2 = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1.0f);
helloButton.setLayoutParams(params2);

How to maximize the width of middle cell of linear layout in android?

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);

android position TextViews programmatically

how do I position two programmatically created TextViews in a LinearLayout BESIDE each other? I tried the code below, but that way the "number" TextView is placed one line deeper compared to the "value" TextView, so the height of nlap LinearLayout changes. I need both TextViews to be at the same height, the "number" TextView should be on the left side an centered vertically. Any help is appreciated.
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
layoutParams.setMargins(10, 1, 10, 1);
LinearLayout nlap = new LinearLayout(this);
nlap.setOrientation(LinearLayout.VERTICAL);
nlap.setLayoutParams(layoutParams);
TextView value = new TextView(this);
value.setText("Test");
value.setTextColor(Color.parseColor("#A60101"));
value.setTextSize(23);
value.setGravity(Gravity.CENTER);
value.setTypeface(font);
TextView number = new TextView(this);
number.setTextColor(Color.parseColor("#FFFFFF"));
number.setText("01");
nlap.addView(value);
nlap.addView(number);
You should use nlap.setOrientation(LinearLayout.HORIZONTAL); to achieve having textViews beside each other, if you use VERTICAL as you do, second one will always be below the first.

Categories

Resources