so what i want to do is add buttons to a LinearLayout programaticalls, this works fine. I want them to apper as one line of buttons in a horizontal order so i set the LinearLayout like this:
<LinearLayout
android:id="#+id/button_frame"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</LinearLayout>
Then i add the buttons programatically:
for (String text: textlist) {
Button cbut = new Button(context);
cbut.setText(text);
cbut.setLayoutParams(new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
cbut.setOnClickListener(new Button.OnClickListener() {
#Override
public void onClick(View arg0) {
Log.d(LOGTAGNAME, "TEST: " + buttonText);
}
});
button_frame.addView(cbut);
button_frame.invalidate();
}
This works until the buttons extend the screen width. So what i want to happen is that there is a horizontal scollbar if the buttons extend the screen width. As an alternative there could be a "line break" for the buttons inside the LinearLayout.
I've tried different setups including a scrollview around the listview but i never saw a scrollbar.
So maybe my problem is that the LinearLayout is not resizing correctly? What do i have to do to make the LinearLayout recalculate width every time after a view is added? invalidate() has no effect at all.
Thanks for any help.
Try putting the linearlayout inside a horiztonalscrollview. That should provide the scrollbar once the button exceeds the screen width.
You trying a simple scrollview or http://developer.android.com/reference/android/widget/HorizontalScrollView.html ? If you don't trying HorizontalScrollView, I think that must help.
try HorizontalScrollView and add Buttons to it . this will scroll automatically whenever needed . for more help share complete layout code
LinearLayout provide only "linear" positioning of views ;) I mean you can do like this:
[btn1][btn2][btn3][btn4]
or like this:
[btn1]
[btn2]
[btn3]
[btn4]
Difference between two variants is in android:orientation param. For more complicated views you should use TableLayout or RelativeLayout.
If you want to do scrollable variant of linear layout creta this structure:
<HorizontalScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
...any other params...>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horisontal"
...any other params.../>
</HorizontalScrollView>
and add buttons to linear layout like you do it now.
Related
I have the following method which I call a few times to create a list of buttons. It works well and creates the buttons.
public void CreateButton(int i) {
LinearLayout btnLayout = (LinearLayout)findViewById(R.id.btnLayout);
Button btn = new Button(this);
btn.setId(i);
btn.setText(String.valueOf(i+1));
btnLayout.addView(btn);
}
But each created button is fitting the screen in width, and I would want it to stay side by side, two buttons per row. I managed to set the button to half the screen size using this:
int displaywidth= getResources().getDisplayMetrics().widthPixels;
btn.setLayoutParams(new LayoutParams((int)(displaywidth/2), LayoutParams.WRAP_CONTENT));
It makes the button's width to be half the screen's size, but I can't figure out how to place them side by side. Any help is appreciated.
Change your orientation in your LinearLayout to horizontal.
<LinearLayout
...
android:orientation="horizontal"
... >
...
</LinearLayout>
If you only have a single LinearLayout that is to hold multiple side-by-side buttons you can make horizontal LinearLayouts to hold your button pairs and either nest them in the main veritical layout or utilize another layout, for example RelativeLayout, to get the desired results.
Try using weight property of LinearLayout. If in each row you want only two buttons then give
android:weightSum="1"
to LinearLayout and
android:layout_weight="0.5"
to each button or you can set weight dynamically in your code by
LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT, xf);
where x represents the float value of layout weight of your button.
for more details about layout weight http://developer.android.com/guide/topics/ui/layout/linear.html
One way to solve the problem is by using their weight and width. Make sure the Linear Layout has a horizontal orientation. Then, use Layout_params to set their width to "Wrap_Content" and their weight to "1". Then both will automatically take up the same amount of space.
I am trying to achieve a dynamic list of textviews like in the image below :-
Here is my code :-
LayerDrawable dashboardResShape_community= (LayerDrawable) getResources().getDrawable(R.drawable.upcomingtask_tags_shape);
// The background effect is by the layer list drawable from the above code
LinearLayout tags_view2=(LinearLayout)findViewById(R.id.tags_view);
LayoutParams lp = new LayoutParams( LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
lp.setMargins(10, 2, 2, 2);
TextView[] tx = new TextView[15];
for(int i=0; i<15; i++) {
tx[i] = new TextView(getActivity());
tx[i].setPadding(8, 4, 8, 4);
tx[i].setBackground(dashboardResShape_community);
tx[i].setLayoutParams(lp);
tx[i].setText("Tag"+i);
tags_view2.addView(tx[i]);
}
and in my xml there is only a linear layout :-
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/tags_view"
android:orientation="horizontal" >
</LinearLayout>
This is what i achieve :-
When i am adding 15 textviews, only 8 are shown like below, the rest should come in the next line but they are not.
If i add more textviews, it goes out of screen but i want to add the textview in the second line when the first line is full. What i am doing wrong here?
Its LinearLayout's limitation.
If you want the explained behavior than
You have to make your own Layout/View refer this link or
Impliment LinearLayout Horizontal orientation with wrapping children like this
you cannot get more text views on next line after linear layout is filled( screen width ), you already the made linear layout orientation as horizontal. Better solution add one more linear layout or use relative (do some child count coding and set parameters). The best solution i prefer for u is table layout. Easier to code code and handle
What you can do is add as many textviews as will fit on the screen to your linearlayout, but then when a textview would go off the screen, you could add another linearlayout below the one that you already had, and then add on to that. You could keep doing that and you would end up with no textviews goind off the screen. You could also try using a gridview.
Here is what this layout looks like:
http://developer.android.com/guide/topics/ui/layout/gridview.html
And here is the documentation:
http://developer.android.com/reference/android/widget/GridView.html
I have a linearlayout as a container for two relativelayouts. Both relativelayouts appear on the screen but they are side by side. I want them to be top and bottom. It looks as if the linearlayout initialization defaults to Horizontal. I have tried using setorientation to Vertical but the screen blanks out.
The following code is an example of what I am trying to do:
LinearLayout layoutContainer = new LinearLayout(this);
layoutContainer.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.FILL_PARENT));
//layoutContainer.setOrientation(LinearLayout.VERTICAL);
// Arguments here: width, height, weight
LinearLayout.LayoutParams childLp = new LinearLayout.LayoutParams(0,
LayoutParams.FILL_PARENT, 1);
layoutTop = new RelativeLayout(this);
layoutContainer.addView(layoutTop, childLp);
layoutBot = new RelativeLayout(this);
layoutContainer.addView(layoutBot, childLp);
layoutTop.setBackgroundColor(GREEN);
layoutBot.setBackgroundColor(Color.BLUE);
setContentView(layoutContainer);
It looks as if the linearlayout initialization defaults to Horizontal.
That is exactly right.
I have tried using setorientation to Vertical but the screen blanks out.
You do need to set orientation to vertical to get this effect. As for it "blanks out", I see several things wrong such as setting the width to "0". There is no width so it won't show anything. I think you would want something like LinearLayout.WRAP_CONTENT. Also, you are using LinearLayout params for your RelativeLayout which may or may not make a difference in this case.
If there isn't a necessary reason to create the layout in Java it is much easier to do this in the xml.
I think you are complicating your layouts by trying to programmatically manipulate them. Set the orientation to Vertical and do the following:
<LinearLayout
------
------
android:orientation="vertical">
Your first Relative layout
<RelativeLayout
android:id="#+id/rel1"
android:layout_alignParentTop="true"
----------------------
---------------------
/>
Your next Relative layout
<RelativeLayout
android:id="#+id/rel2"
android:layout_below="#id/rel1"
----------------------
---------------------
/>
Folks, I have the following layout: http://dpaste.com/hold/755261/
It has two seekbar widgets. Both are not showing. If I move them to the root linearlayout, they appear, otherwise not. I need them to be inside the first linearlayout. Anyone could help me how to achieve that?
It looks like you need to put the following in your third LinearLayout:
android:layout_weight="1"
Change the second LinearLayout height to wrap_content. If the content doesn't fit on the screen, then maybe you have to use a ScrollView.
scrollview = (ScrollView)findViewById(R.id.detailedScrollView);
for (Quotation quotation : object.quotes){
TextView quote = new TextView(this);
quote.setText(quotation.getQuote());
quote.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
scrollview.addView(quote);
}
Let's say there are three quotes, then I want to have three textViews. However, the code above crashes my app. Any obvious mistakes? Here's the error I'm getting:
11-06 17:35:53.214: E/AndroidRuntime(1430): java.lang.IllegalStateException: ScrollView can host only one direct child
You can't add views directly inside a scrollview. A scrollview can only contain a single layout object. What you have to do is to add a linearlayout in your scrollview, then add the textview to the linearlayout
Layout container for a view hierarchy that can be scrolled by the user, allowing it to be larger than the physical display. A ScrollView is a FrameLayout, meaning you should place one child in it containing the entire contents to scroll; this child may itself be a layout manager with a complex hierarchy of objects. A child that is often used is a LinearLayout in a vertical orientation, presenting a vertical array of top-level items that the user can scroll through.
The TextView class also takes care of its own scrolling, so does not require a ScrollView, but using the two together is possible to achieve the effect of a text view within a larger container. Please more detail
With best regards,
Psycho
You need to add a "LinearLayout" (or "RelativeLayout") inside ScrollView.
Say you have the layout xml as follows:
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="#+id/linearlayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
</LinearLayout>
</ScrollView>
And now you want to add the 'TextView' programmatically, which is as follows:
LinearLayout linearLayout =(LinearLayout) this.findViewById(R.id.linearlayout1);
for (Quotation quotation : object.quotes){
TextView quote = new TextView(this);
quote.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
quote.setPadding(4, 0, 4, 0); //left,top,right,bottom
quote.setText(quotation.getQuote());
linearLayout.addView(quote);
}