I have RelativeLayout on all height and it has two View:
1) 'mainItem' is View to fix height;
2) 'listView' from rubber(or not fix) height;
I want that 'mainItem' stay on top and 'listView' was below 'mainView' and it stretched at all the rest height RelativeLayout.
I do so:
mainItem = new ItemList(getContext());
mainItem.setId(0);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
params.addRule(ALIGN_PARENT_TOP);
addView(mainItem, params);
listView = new ListView(getContext());
listView.setAdapter(adapter);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
params.addRule(BELOW, mainItem.getId());
addView(listView, params);
I get 'listView' stay on top and stretched at all width and height.
A 'mainItem' stay on top too but under 'listView'. ('mainView' not visible)
Please tell me, what I'm doing wrong?
Do not set the id to be 0. Put some other number and see if that works?
If you have a look at the docs for setID() you will see that it says "The identifier should be a positive number". Zero is not a positive number.
http://developer.android.com/reference/android/view/View.html#setId(int)
Related
I want to create vertical LinearLayout with couple of Button children, where each child has width of widest of them.
However depending on using MATCH_PARENT or WRAP_CONTENT for children width, I get either LinearLayout taking whole screen's width, or Buttons not filling LinearLayout. Screenshots below (fill/wrap):
Example Activity code:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
RelativeLayout mainView = new RelativeLayout(this);
mainView.setBackgroundColor(Colors.WHITE);
String[] buttonsNames = new String[] { "Short", "Looooooong", "Medium" };
View buttonsView = getButtonsView(buttonsNames);
mainView.addView(buttonsView, new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT));
setContentView(mainView);
}
private View getButtonsView(String[] buttonNames) {
LinearLayout buttonsView = new LinearLayout(this);
buttonsView.setOrientation(LinearLayout.VERTICAL);
buttonsView.setBackgroundColor(Colors.BLACK);
for (int i = 0; i < buttonNames.length; i++) {
Button button = new Button(this);
button.setText(buttonNames[i]);
///////////// HERE LAYS THE PROBLEM //////////
buttonsView.addView(button, new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
//LinearLayout.LayoutParams.WRAP_CONTENT, // neither of them works
LinearLayout.LayoutParams.WRAP_CONTENT));
View redLineDivider = new View(this);
redLineDivider.setBackgroundColor(Colors.RED);
buttonsView.addView(redLineDivider, new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT, 2));
}
return buttonsView;
}
As you can see on second screenshot, red lines actually take whole width without stretching LinearLayout - it is because at least one view has set width.
Potential fix I have came up with is to find widest button (with longest text) and make it use WRAP_CONTENT, while all the rest use MATCH_PARENT, which gives me expected result:
Code:
buttonsView.addView(button, new LinearLayout.LayoutParams(
isLongestText(i) ? LinearLayout.LayoutParams.WRAP_CONTENT
: LinearLayout.LayoutParams.FILL_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT));
It doesn't feel like elegant solution though - is there any intended mechanism for situation like this, that I am missing out?
Following is the trick:
Mention the width of the LinearLayout containing the buttons (buttonsView in your code) as WRAP_CONTENT.
Mention the width of each button as MATCH_PARENT
Your program should give you the expected result if you do not include the redLineDivider View. There seems to be some issue with setting the width of redLineDivider. As an alternative you can declare it as a LinearLayout to make your code work perfectly.
// View redLineDivider = new View(this);
// Instead declare it as a LinearLayout
LinearLayout redLineDivider = new LinearLayout(this);
Hope this will be useful.
I am getting stucked on the following problem:
For a questions app I want to implement a nice output for a survey showing the possible answers and the percentage of people answered the specific question. Therefore, I want to add a "bar" to my app that displays the percentage. I try to solve this by using views and weighting them in a linear layout.
I want to add the different answers programmatically, this is the code I've got so far.
My problems are, that I am not getting closer to weighting the views and resizing them.
/* Add all questions */
RelativeLayout my_root = (RelativeLayout) findViewById(R.id.ownerRL);
/* Add a new Linearlayout as a container for the question */
LinearLayout A = new LinearLayout(this);
A.setOrientation(LinearLayout.HORIZONTAL);
my_root.addView(A);
/* Create a new View in this container, for the status bar */
View new_view = new View(getBaseContext());
new_view.setBackgroundColor(Color.YELLOW);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(50, 20, 3);
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) A.getLayoutParams();
params.addRule(RelativeLayout.BELOW, R.id.question1);
A.addView(new_view);
View new_view2 = new View(getBaseContext());
new_view.setBackgroundColor(Color.GREEN);
ViewGroup.LayoutParams lp2 = new ViewGroup.LayoutParams(50, 20);
RelativeLayout.LayoutParams params2 = (RelativeLayout.LayoutParams) A.getLayoutParams();
params2.addRule(RelativeLayout.BELOW, R.id.question1);
params2.addRule(RelativeLayout.LEFT_OF, new_view.getId());
A.addView(new_view2);
The Green view should actually be right of the yellow one (which is not visible).
How do get it managed such that it creates a red/white bar that has a height of 4px and the red and weight parts are weighted?
Thanks for your help!
check this link, Set Linear Layout weight it like this
LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT, 1.0f);
The last parameter is weight.Hope this help.
I have tried to make a dynamic listview adapter for my data, I don't know exactly how many items I will have so I make a loop creating textviews and adding them to an horizontal linearlayout, everything works, except the size, it seems there is no way to know the list width from within the adapter so I tried to set the layoutparams of the textviews to work with weight, but either it does nothing or everything dissapears.
adapter getView:
LinearLayout m = (LinearLayout) view.findViewById(R.id.grid_lo);
String[] s = filtered[position];
for(String i:s)
{
TableRow.LayoutParams params = new TableRow.LayoutParams(0, ViewGroup.LayoutParams.WRAP_CONTENT,1.0f);
TextView tv = new TextView(context);
tv.setText(i);
m.addView(tv,params);
}
this creates all the textviews but they are all just drawed one after another.
I have tried with the LinearLayout.LayoutParams but it's the same.
Any other ideas I might try?
in the first write custom adapter the get the layout parameter
and then calculate the height:
AccountCustomAdapter Adt = new AccountCustomAdapter(this,account);
ViewGroup.LayoutParams params = accountLV.getLayoutParams();
params.width = LayoutParams.FILL_PARENT;
params.height = Adt.getGroupCount() * 70;
I have a method that creates several buttons
public Button[] generaBottoniRisposta(int numeroBottoni, Context context){
Button bottoni[]= new Button[numeroBottoni];
/*genero un tot di bottoni in base a numeroBottoni, รจ necessario avere il context*/
for(int i=0; i < bottoni.length;i++){
bottoni[i] = new Button(context);
bottoni[i].setId(i);
bottoni[i].setText(String.valueOf(i+1));
LayoutParams param = new LinearLayout.LayoutParams(50, 50);
bottoni[i].setLayoutParams(param);
}
return bottoni;
}
and then another method that add them to a gridlayout.
I want to set the width of those buttons, but i'm not able to do it.
I tried a lot of stuff, setWidth(), setMaxWidth(), invalidate() etc.
Something weird happens. If I try to make the button bigger than its default size it works, if i try to make the button smaller than its default size it doesn't work!
How should I do? thank you
Try using LayoutParams, something like..
RelativeLayout.LayoutParams rel_bottone = new RelativeLayout.LayoutParams(buttonWidth, buttonHeight);
button.setLayoutParams(rel_bottone);
And the layout depends on the parent layout that contains the buttons..
I think setlayoutparams should do what you want. Like in the answer in this thread.
Set View Width Programmatically
try this:
LinearLayout.LayoutParams params = button.getLayoutParams();
params.width = 100;
button.setLayoutParams(params);
one more thing LinearLayout.LayoutParams is used when parent is a LinearLayout and when parent is RelativeLayout use RelativeLayout.LayoutParams.
Try something like this :
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(width, height);
button.setLayoutParams(params);
My buttons are inside a gridlayout. I tried to use GridLayout.LayoutParams giving the width of the cells, instead of trying to set the width of the button, and now it seems to work.
this is the code I use to add the buttons to the GridLayout
Spec row = GridLayout.spec(numeroRiga, 1);
Spec colspan = GridLayout.spec(numeroColonna, 1);
GridLayout.LayoutParams gridLayoutParam = new GridLayout.LayoutParams(row,colspan);
gridLayoutParam.width=50;
gridLayoutParam.height=50;
gridLayout.addView(button,gridLayoutParam);
But i'm wondering if i can set the width of the buttons in a similar way.
Can anyone help me how to set the width of TextView to wrap_content through code and not from XML?
I am dynamically creating a TextView in code ,so is there anyway to how to set its width to wrap_content through code?
TextView pf = new TextView(context);
pf.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
For different layouts like ConstraintLayout and others, they have their own LayoutParams, like so:
pf.setLayoutParams(new ConstraintLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
or
parentView.addView(pf, new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
There is another way to achieve same result. In case you need to set only one parameter, for example 'height':
TextView textView = (TextView)findViewById(R.id.text_view);
ViewGroup.LayoutParams params = textView.getLayoutParams();
params.height = ViewGroup.LayoutParams.WRAP_CONTENT;
textView.setLayoutParams(params);
Solution for change TextView width to wrap content.
textView.getLayoutParams().width = ViewGroup.LayoutParams.WRAP_CONTENT;
textView.requestLayout();
// Call requestLayout() for redraw your TextView when your TextView is already drawn (laid out) (eg: you update TextView width when click a Button).
// If your TextView is drawing you may not need requestLayout() (eg: you change TextView width inside onCreate()). However if you call it, it still working well => for easy: always use requestLayout()
// Another useful example
// textView.getLayoutParams().width = 200; // For change `TextView` width to 200 pixel
A little update on this post: if you are using ktx within your Android project, there is a little helper method that makes updating LayoutParams a lot easier.
If you want to update e.g. only the width you can do that with the following line in Kotlin.
tv.updateLayoutParams { width = WRAP_CONTENT }
I am posting android Java base multi line edittext.
EditText editText = findViewById(R.id.editText);/* edittext access */
ViewGroup.LayoutParams params = editText.getLayoutParams();
params.height = ViewGroup.LayoutParams.WRAP_CONTENT;
editText.setLayoutParams(params); /* Gives as much height for multi line*/
editText.setSingleLine(false); /* Makes it Multi line */
I think this code answer your question
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)
holder.desc1.getLayoutParams();
params.height = RelativeLayout.LayoutParams.WRAP_CONTENT;
holder.desc1.setLayoutParams(params);