Linearlayout programmatically - How to set up a divider? - android

I am creating TextViews in LinearLayout programmatically and I would like to separate them with a divider (just a simple line). I have googled endlessly, what I have found is that I can use .setDividerDrawable, but I don't want to use external images for this.
Any tips?

How to Add Divider to an Android Layout Programmatically
Create a View 1 or 2 pixels tall and width match_parent and set the background color to whatever color you want the divider to be.
Separate the divider from the items above and below with margin settings.
Example:
ImageView divider = new ImageView(this);
LinearLayout.LayoutParams lp =
new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
lp.setMargins(left, top, right, bottom);
divider.setLayoutParams(lp);
divider.setBackgroundColor(Color.WHITE);

You could use a simple drawable in xml for the divider (example here), or use a 9-patch image which barely takes anything.
Then, use the LinearLayoutICS in order to show the divider on most of the devices. you can check out this post i've made about it.

For linear layout you can use this attribute to set divider android:divider="some color"
android:showDividers="middle"

Related

Why are these buttons mis-aligned in a TableLayout?

I'm using a table layout to arrange some buttons. As long as I use the same font for all the labels they are properly aligned in each row.
For some buttons I'd like to use icons from a custom ttf font.
When I use such an icon, the button is placed slightly higher, like so:
(This image is scaled up to make the the problem more evident.)
I took measurements - the buttons appear to be of same height, regardless of the used font.
Why are the buttons not aligned properly?
Does anyone have a suggestion to get them aligned?
Thanks.
Following CommonsWare's advice (thanks for the quick replies!), I tried this:
final LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
layoutParams.gravity = Gravity.CENTER_VERTICAL;
row.setLayoutParams(layoutParams);
This did not work. Will try the base alignment comment next.
Add the following attribute to your TableRow:
android:baselineAligned="false"
By default, the button labels' base lines are vertically aligned which causes the offset you experience.

Android: ListView covers everything

I am using a ListView inside a LinearLayout and below that another LinearLayout, which won't show up because the ListView appears to take up all the space.
Code:
listView = new ListView(this);
listView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
listLayout = new LinearLayout(this);
listLayout.setOrientation(LinearLayout.VERTICAL);
listLayout.addView(listView);
listLayout.addView(new NavigationBar(this, "android.intent.action.MAIN", "android.intent.action.MY_ACTIVITY"));
setContentView(listLayout);
NavigationBar is also a LinearLayout containing some buttons.
If added on its one it just play properly if added after the ListView it doesnt display at all.
You should set the weight attribute for the linear layouts or use fixed height for the listview. Please post the xml layout to help understand better.
Change
listView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
to
listView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, 0, 1));
(assuming you've imported LinearLayout's LayoutParams, otherwise it will be new LinearLayout.LayoutParams)
This will mean the list view itself won't take up any vertical space, so the navigation bar can be laid out to how big it wants to be, but then, any free space will be assigned to the list view because it has a weight.
You should also consider not having the navigation bar at the bottom, that is a very iPhone thing to do.

Adding Textviews Dynamically goes out of focus to a Linear Layout

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

Remove marginetop a relative layout dynamically if another layout is generated in between two relative layout

I have two relative layout say relative layout1 & 2 having an edit text and a button. In order to provide spacing in between I specified margin top of 30dp to relative layout2. Another relative layout 3 is generated dynamically in between them when we click button of the relative layout1. If so I have to remove margin top of relative layout2 programatically.
snapshot is like below
In this case a provide padding in xml. But when another layout is added in between like this
the padding is not preferable. I have to remove padding programmatically. How can i do that by checking whether there is a layout in between and remove that padding. I am a beginner in android coding....
You can set margins, or anything else regarding a layout like this:
RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams)relativeLayout.getLayoutParams();
layoutParams.setMargins(80, 0, 0, 0); // left, top, right, bottom
relativeLayout.setLayoutParams(layoutParams);
You can find more Information on LayoutParams here:
http://developer.android.com/reference/android/widget/RelativeLayout.LayoutParams.html
If you want to remove padding call
relativeLayout.setPadding(0,0,0,0);

ListView background image crop

I have my ListView with image background (it's actually a fragment) but still i have set the background with: getListView().setBackgroundResource(R.drawable.table); I noticed that every time this list shows up bacground image is allways like "fill_parent" also i noticed that listview is fill_parent too but i can fix that with getListView().setPadding() but i cant crop background image. Is there any other way to do it? I need background view with aditional buttons...
the Method setPadding is just affecting the content of the ListView, but not it's container e.g. the Background. So basically the padding produces unused space inside the ListView. If you want to restrict the ListView you can use the margin attribute:
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams (LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
layoutParams.setMargins(30, 20, 30, 0);
Another and in my opinion better way to hava a ListView with a Button below is:
Use a LinearLayout with orientation vertical. Add the ListView and the Button to this layout.
The ListViews height is fill_parent and the Button's wrap_content. Now add to both the layout_weight with value 0 for the Button and 1 for the ListView. The result is that the Button is drawn at the bottom of your screen and the ListView just can expand to the rest of the screen (also its background).

Categories

Resources