How do I create marginless button bar in Android - android

I want my application to have button bar at the bottom, that is similar to this one (Motorola Defy+):
How can this be achieved? I've tried ButtonBar style with regular buttons in it, but they always have margin in this case.

By code:
Make the bar LinearLayout horizontal. Let´s say its name in code will be bbll.
LinearLayout.LayoutParams params = bbll.LayoutParams();
params.leftMargin=0;
params.rightMargin=0;
... or params.setMargins(0, 0, 0, 0);
bbll.setLayoutParams(params);
By layout simply set each margin to 0. That's all. If you have done it and it won't work, put the code here, please.
But you should set bbll.setPadding(0,0,0,0) , too. Or you'll see empty space between the bar (that is invisible) and the buttons. aqs has a good thought.

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.

Set NavigationDrawer's ListView margin programmatically

I am currently struggling to design my application the way i want to.
In my app i am using a NavigationDrawer and different fragments. By clicking on an item in the NavigationDrawer i swap out the fragment that is currently active.
There is one main fragment which shows a map and doesn't show a toolbar. When I switch to another fragment I want to show my toolbar and let the user interact with it.
Now when I show the toolbar I have to set the top margin of the NavigationDrawer to the size of the toolbar so it doesn't get overlapped.
When I am showing the toolbar I set the margin of the NavigationDrawer's listview like this:
ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams) mDrawerListView.getLayoutParams();
params.setMargins(0, drawerMarginTop, 0, 0);
mDrawerListView.setLayoutParams(params);
mDrawerListView.requestLayout();
The outcome is like the complete opposite of what i expect. It seems like the margin is applied to the bottom of the view.
Screenshot:
Another thing that annoys me is that the toggle-arrow of the toolbar is not centered correctly. It's a little bit higher than it should be, so it overlaps the system bar in the top and doesn't fill the whole size of the toolbar. I tried to make this clear in the following picture:
If you need any xml or code just let me know and I will edit my question.
Thank you in advance!
EDIT 1+2:
My toolbar style:
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/toolbar_navigation"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#color/my_color"
app:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light">
</android.support.v7.widget.Toolbar>
EDIT 3:
The Problem with the toggle-arrow not being centered is fixed now. Thanks to Alchete.
Unfortunately the NavigationDrawer is still buggy. I found out that if I open and close the NavigationDrawer many times it somehow changes its layout at one time and the margin is set correctly... Is there any way to force this top happen immediately?
After many times of opening and closing the drawer it looks like this: (Exactly what I want it to look like)
There must be a way to force this immediately, right?
Your alignment issue is most likely due to your toolbar height. You should be setting the toolbar height as follows:
<android.support.v7.widget.Toolbar
android:layout_height="?attr/actionBarSize">
Here's the same issue for reference: android.support.v7.widget.Toolbar icon alignment issue
I would also be using Google's IOSched app for reference on how to set these items up properly. You can find all the code on Github.
Here are their layout files. Scroll down to see their toolbar/navdrawer layouts: https://github.com/google/iosched/tree/dfaf8b83ad1b3e7c8d1af0b08d59caf4223e0b95/android/src/main/res/layout
And, also note that Google's reference design is to OVERLAP the toolbar with the navdrawer -- which is not how you have it. And, the right margin should be equivalent to the toolbar height.
See here: http://www.google.com/design/spec/patterns/navigation-drawer.html
I'm not 100% about this but it looks like you're setting the ViewGroup layout params to be of type MarginLayoutParams. Instead, set the margin on a 'normal' root ViewGroup type e.g. RelativeLayout and pass that to the View:
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,RelativeLayout.LayoutParams.MATCH_PARENT);
params.setMargins(0, drawerMarginTop, 0, 0);
mDrawerListView.setLayoutParams(params);
mDrawerListView.requestLayout();
You may want to change MATCH_PARENT to WRAP_CONTENT depending on your xml.

Set Android Tabs with different width

I have 4 tabs currently and they are all the same width. I would like the last tab to be less wide than the others. I tried using LayoutParams to specify the width but this didn't seem to work.
TextView view = new TextView(this);
view.setText("Hello");
LayoutParams lp = new LinearLayout.LayoutParams(10,10);
view.setLayoutParams(lp);
actionBar.addTab(actionBar.newTab().setCustomView(view).setTabListener(this));
This is what it currently looks like
I want the fourth tab to be an overflow button. Something like the image is what I'm going for. If there was a way to have only 3 tabs and then an overflow button at the edge that would be perfect.
It will be possible using fragments.

Custom layout addView issue

I am doing custom layout. But I want to seem like in the screen button, image, text.
See Setting parameters on child views of a RelativeLayout .I want to add a button on the left side. Can anybody help me?
My code is:
RelativeLayout.LayoutParams lp1=new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
addView(pic,lp1);
RelativeLayout.LayoutParams lp2=new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
lp2.addRule(RelativeLayout.RIGHT_OF,pic.getId());
addView(name,lp2);
RelativeLayout.LayoutParams lp3=new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
lp3.addRule(RelativeLayout.LEFT_OF,pic.getId());
addView(deletebutton,lp3);
What is wrong?
Look carefully at what you do.
First, you add "pic" to your layout, which will be aligned with your relative layout on the left side (since you didn't provide any rules for it).
At the end, you are trying to add a view at the left side, but there is no space for it, since your pic is left aligned.
You have couple of ways out.
1) Add rule for a 'pic' to be centered in parent horizontally. The there might be free space on the left to fit button.
2) Add button first (it will be aligned left), then add pic and set rule to be on the buttons' right and then add name like you did.

Android: Adding button to custom title bar

I have created a custom title bar as shown in this example
http://staticallytyped.wordpress.com/2011/03/18/android-dynamic-and-custom-title-bars/
"A custom title bar" - half way down.
On some activities I would like to place a button on the right hand side of the titlebar (same as facebook app). I have attempted to add a button to the view as follows, but it doesn't appear.
Custom title bar is displayed as follows
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.maintabhost);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.headerbar_include);
Attempting to add button as follows. The button will eventually be an ImageButton and aligned to right of custom titlebar-if I get it working. (just realised I've too many layoutparams now, but this isnt affecting the button display)
LinearLayout layout = (LinearLayout) findViewById(R.id.headerbar);
Button searchButton = new Button(this);
searchButton.setText("info");
LayoutParams layoutParams = new LinearLayout.LayoutParams
(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
searchButton.setLayoutParams(new LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
layout.addView(searchButton, layoutParams);
layout.invalidate();
I could just create another custom titlebar with the button already embedded, but a dynamic solution would be better.
Cheers
First of all, thanks for the link to my blog. Second, let me see if I can't answer that for you. One of the reasons you're having trouble adding another button when you want to add a button is that in that example I left you with no way of retrieving the Title Bar View through the usual channels. Hence, let's fix it (and potentially let me write another blog post this coming weekend.)
Starting with the xml file, add an id attribute:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:orientation="horizontal"
android:id="#+id/title_complex">
<!-- Stuff -->
</LinearLayout>
and here's code to show you how to get that button in there within your Activity (you'll have to add all the flair later):
LinearLayout layout = (LinearLayout) getWindow().findViewById(R.id.title_complex);
layout.addView(new Button(this));
and if you take a look, there's a non-descript button in the Title Bar (like I said, you'll have to add your own flair):
Due note, however, that I can't guarantee that the button will remain or won't remain on subsequent Activities. I haven't investigated it yet but this should get you started. And if you have any more questions, feel free to ask them here (more eyeballs) or on my blog.
Here's one approach to make sure the button(s) remain in the title bar:
How to Create Custom Window Title in Android
Essentially, wrap the android Activity class, and then extend from that new class.

Categories

Resources