How to display a LinearLayout 100px below the top of the screen? - android

I have a FrameLayout (all the screen is the FL) wich haves a openGLview and a header image on the top of the screen. Now i want to display a menu of two buttons, created with a LinearLayout.
My LL Menu must be floating on the framelayout, 100px below the top of the screen.
How can i achieve that? i tryed with this code, but is not working properly, the Menu is being displayed 100px below the top of the screen but it is painting the upper part of the menu, and i dont want that, i need that the upper part of the menu it's not painted with the colour of the menu. Must be a floating menu.
I'm sure that there is another way to draw the menu 100px below the top of the screen without painting the upper part of the menu with the colour of the menu.
My code (with the upper part colour problem):
///////////////sub menu de shareit////////////////
LinearLayout sharellContainer = new LinearLayout(this);
sharellContainer.setOrientation(LinearLayout.HORIZONTAL);
LinearLayout sharell = new LinearLayout(this);
sharell.setOrientation(LinearLayout.VERTICAL);
sharell.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
//LinearLayout.LayoutParams sharellParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
//sharellParams.gravity=Gravity.CENTER;
sharell.setPadding(10, shareit.getHeight()+80, 10, 10);
sharell.setBackgroundColor(0xFF383838);
//sharell.setLayoutParams(sharellParams);
share= new ImageButton(this);
selector(share, R.drawable.but_share_up,R.drawable.but_share_down);
LinearLayout.LayoutParams shareParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
shareParams.setMargins(0, 0, 0, 10); //dejo un espacio entre este botón y el siguiente
share.setLayoutParams(shareParams);
sharell.addView(share);
web= new ImageButton(this);
selector(web, R.drawable.but_web_up,R.drawable.but_web_down);
sharell.addView(web);
sharellContainer.addView(sharell);
sharellContainer.setGravity(Gravity.RIGHT);
//////////////////////////////////////////////////
.
.
.
fl.addView(squareGLSurfaceView);
fl.addView(rl);
fl.addView(sharellContainer);
setContentView(fl);

The problem is that you are using padding rather than margins. Any padding gets the background color of the view, margins do not.
You will have to add the margins to to the LayoutParams that you give to your view.

This will be very easy if you use an XML layout. You can also view what you are creating and set individual properties. This also allows you to separate your logic from your views and adhere to the MVVM design pattern so future updates are easier to perform, giving you a more flexible system.

Related

Centering multiple textViews programmatically on Android

I have an Open GL ES 2.0 app and am displaying an Android TextView over the top of my GLSurfaceView.
I have the actual textviews displaying OK but now I need to try to centre them.
This is what I have so far:
text1 = new TextView(this);
text1.setText("This is some sample text");
text1.setTextColor(Color.WHITE);
text1.setTextSize(textSize);
text2= new TextView(this);
text2.setText("And this is some more sample text");
text2.setTextColor(Color.WHITE);
text2.setTextSize(textSize);
RelativeLayout.LayoutParams textParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.MATCH_PARENT);
lp.addRule(RelativeLayout.CENTER_HORIZONTAL);
textLayout = new RelativeLayout(this);
textLayout.setLayoutParams(textParams);
textLayout.addView(text1);
textLayout.addView(text2);
mainLayout.addView(textLayout);
However, when I run this, only text1 is centered. Text 2 isn't, and starts at the left side of the first (correctly centered) textView. Try as I might, I can't seem to get both of them centered. The following graphic describes what I mean:
(Please note re vertical placement - in my actual results, both TextViews are at the same vertical/'y' position and therefore overlapping - obviously this is expected as I haven't changed the vertical position of the 2nd TextView, but to make things clearer to illustrate, I've moved the 2nd one down manually.......)
The idea I was going with was creating a 'textLayout' to which I could add all of my textViews, then just add that textLayout to my main layout. Thus facilitating the addition and removal of all of the textViews with a single line of code.
Please note that I am not using, and do not wish to use, XML for this - I would like to know how to do this programmatically.
What am I missing?
Edit Here is how I am creating my Main Layout to which I am adding the textView.....
layout = new RelativeLayout(this);
layout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
When you are adding "RelativeLayout.CENTER_HORIZONTAL" as a rule to your layout params, what you are actually doing is telling textLayout that it should be centered horizontally in your mainLayout (see http://developer.android.com/reference/android/widget/RelativeLayout.LayoutParams.html#attr_android:layout_centerHorizontal). In code you should actually do text1.setGravity(Gravity.CENTER) and text2.setGravity(Gravity.CENTER).

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.

Setting the text of a programatically created button

Basic question regarding setting the text of a programatically created button. As seen in my code below I've done the basics in terms of creating the button but my button appears as seen in my attached image. Basically the text in the button doesn't appear as expected. Any ideas why?
Note: I've declared button as a public instance variable right above my onCreate() and has been added correctly to my relative layout using addView();
// Create User button
btnUserAdmin = new Button(this);
// Customise the UserAdmin button
btnUserAdmin.setBackgroundColor(Color.BLUE);
btnUserAdmin.setTextSize(13.7f);
btnUserAdmin.setTextColor(Color.parseColor("#FFCC00"));
btnUserAdmin.setText("USER ADMINISTRATION");
btnUserAdmin.setGravity(Gravity.LEFT);
Thanks.
You should specify the dimensions of the button, otherwise the size could be unexpected. For instance
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.MATCH_PARENT );
btnUserAdmin.setLayoutParams(lp);
also, you can directly set them when you add the buttom
yourRelativeLatout.addView(btnUserAdmin, lp);
Also remember that numeric values for the dimensions (of the bottom or the layout) usually are evil. As you can, use only WRAP_CONTENT and MATCH_PARENT

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.

Add button to bottom of linear layout programmatically

I have a bunch of buttons displayed using the default gravity. The last button I add to the LinearLayout view is what I'd like to appear at the bottom of the view. How do I add it programmtically to appear at the bottom of the screen? I've tried setting the gravity, but everything falls to the bottom. I just want the one button to fall to the bottom of the screen. Ideally, I won't have to make another view.
Try this:
Button button = new Button(this);
youLinearLayout.addView(button, new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, Gravity.BOTTOM));
edit
sorry, the code above dont work.
You cant do this using an single LinearLayout if orientation==vertical.
You'll need create another layout(RelativeLayout) and add TextView to it.
RelativeLayout relative = new RelativeLayout(this);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams( LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT );
params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
relativeLayout.addView(textView, params);
linearLayout.addView(relativeLayout, new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
If the rest of the screen is not empty, you can give android:layout_weight=0.0 to the Button, and 1.0 to the widget on the top.
In that way, widget with 1.0 weight will expand to fill empty areas, and Button with 0.0 will take only the default space, and being the last item addes to a vertical LinearLayout, it will be sticked to the bottom.

Categories

Resources