How to set dynamic size for Android's view? - android

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

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

Dynamically-generated LinearLayouts overlapping

I'm a newbie to Android development, and I'm very much still learning Java too so be gentle!
I am creating an app that can take information about a task (I'm basing it around a sort of homework planner), store that info and then display it in a list. The program must be able to dynamically generate the list from the background files. I have managed all of this so far, but when I create a basic output for each task, containing the "subject" and "details" variables using a LinearLayout they appear on the screen overlapping. They all seem to be creating correctly, but they are all being put in the same place. Are there attributes I can set to make them display in a vertical list???
Here is the piece of code where I generate the viewgroups and display them. This is called from a loop in another part of the program which finds the number of files in internal storage.
TextView subjView;
TextView detailView;
RelativeLayout displayLayout;
LinearLayout taskDisplay = new LinearLayout(this);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
subjView = new TextView(this);
detailView = new TextView(this);
displayLayout = (RelativeLayout) findViewById(R.id.relative_display_layout);
subjView.setText(subject);
detailView.setText(details);
layoutParams.setMargins(0, 0, 0, 0);
taskDisplay.addView(subjView, layoutParams);
layoutParams.setMargins(10, 0, 0, 0);
taskDisplay.addView(detailView, layoutParams);
displayLayout.addView(taskDisplay);
If I understand correctly, I think your issue is only that you are declaring and then changing the layoutParams margins which sets them both to the same, which is overlapping your TextViews.
Edit
Okay, I am still not 100% sure how you are doing all of this so my example may need to be tweaked. I tried to throw this together quickly so forgive me for any minor mistakes.
New mock up for dynamic layouts:
TextView subjView, detailView;
RelativeLayout displayLayout, rl;
// I am assuming this is your main layout
displayLayout = (RelativeLayout) findViewById(R.id.relative_display_layout);
// Just using a for loop as an example of a loop event, not sure how you are accomplishing this
for(int i = 0; i < data.length(); i++) {
RelativeLayout.LayoutParams rllp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, 100);
if (i > 0) {
int rePositionRule = i;
rllp.addRule(RelativeLayout.BELOW, rePositionRule);
}
RelativeLayout taskDisplay = new RelativeLayout(this);
taskDisplay.setLayoutParams(rllp);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
layoutParams.setMargins(0, 0, 0, 0);
LinearLayout.LayoutParams layoutParams2 = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
layoutParams2.setMargins(10, 0, 0, 0);
subjView = new TextView(this);
detailView = new TextView(this);
subjView.setText(subject);
subjView.setLayoutParams(layoutParams);
detailView.setText(details);
detailView.setLayoutParams(layoutParams2);
taskDisplay.addView(subjView);
taskDisplay.addView(detailView);
displayLayout.addView(taskDisplay);
}
Your displayLayout is a relativeLayout.. A relative layout, as the name implies, places element relative to each other. Normally you'd say "element A should go below element B" etc. Since you aren't providing any of these rules for the items you are creating they are just going to all go to the default position in a relative layout (which is the top of the screen.. hence the overlap)
If you don't want to deal with the hassle of changing your code to place things relatively simply switch your displayLayout to a LinearLayout in your xml and code and set its orientation to vertical. You'll probably want to wrap that in a scroll view if it runs off the screen
However, it sounds like what you really want is a ListView...

How to set a desired layout for an animating fragment?

I have a fragment which is animating from right to left. I want to set it to stop at like 100 units from the left part of the screen. This is what i did so far.
RelativeLayout tempLi;
Display display = MyActivity.context.getWindowManager().getDefaultDisplay();
final int width = display.getWidth();
tempLi.setLayoutParams(new LayoutParams(width-100, LayoutParams.FILL_PARENT));
This line of code is getting the layout 100 units short from the right side. I tried doing -width + 100 , it didn't work.
Any suggestions will be appreciated. TIA
You can set the left-margin to the layout as follows:
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
layoutParams.setMargins(100, 0, 0, 0);
tempLi.setLayoutParams(layoutParams);

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.

android layout - WRAP_CONTENT from right to left possible?

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

Categories

Resources