I have a horizontal LinearLayout and I want to have 50 pixels of blank space between its child views. After some research I've found that dividers can do this job. I don't want to use XML but create everything programmatically. Here is my code:
LinearLayout parent = new LinearLayout(getContext());
parent.setOrientation(LinearLayout.HORIZONTAL);
ColorDrawable divider = new ColorDrawable(Color.TRANSPARENT);
divider.setBounds(0, 0, 50, 0);
parent.setDividerDrawable(divider);
parent.setShowDividers(LinearLayout.SHOW_DIVIDER_MIDDLE);
....children are added here to parent...
Unfortunately, it doesn't work. No blank space appears between the children.
Does anyone have an idea what's wrong there?
To answer my own question, ColorDrawable was the culprit. When using ShapeDrawable instead it works fine, i.e. like this:
LinearLayout parent = new LinearLayout(getContext());
parent.setOrientation(LinearLayout.HORIZONTAL);
ShapeDrawable divider = new ShapeDrawable();
divider.setIntrinsicWidth(50);
divider.setAlpha(0);
parent.setDividerDrawable(divider);
parent.setShowDividers(LinearLayout.SHOW_DIVIDER_MIDDLE);
Related
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.
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"
now I use canvas(ondraw()) to draw images of my app and if I want to show some
list in center of my app. What should I suppose to do? I have 2 ideas.
Add ListView in Dialog, but the screen is dark, which I don't want it to be.
Add ListView in LinearLayout and make it to Bitmap which I can't draw image, my
code is this following:
ListView modeList = new ListView(context);
modeList.setAdapter(new ImageAdapter(context, objects));
linearlayout = new LinearLayout(context);
linearlayout.setOrientation(LinearLayout.VERTICAL);
linearlayout.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
linearlayout.addView(modeList);
linearlayout.layout(0, 0, 200, 200);
linearlayout.measure((int)Define.getScreenWidth(), (int)Define.getScreenHeight());
linearlayout.setDrawingCacheEnabled(true);
linearlayout.buildDrawingCache(true);
and when I draw
protected void onDraw(Canvas canvas) {
canvas.drawBitmap(linearlayout.getDrawingCache(), 200, 200, null);
}
However, it draws nothing :(
First way deffenately better, you could style Dialog to become borderless and with translucenbt background. Check this link.
+1 vote for the first way. Style your dialog and disable dark background.
Second way is overcomplicated. You better add listview as a child directly to a current window, so it will be drawn on top of all other views, or place all your views in framelayout and add listview to that framelayout, so it will be on top of all previously added views.
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.
I'm trying to build an android application that features a graphical display drawn within a RelativeLayout. I want to place "+" and "-" buttons next to several of the parameters, which are drawn at various points on the canvas. The positions are free-form don't seem to conform to any of the standard XML layouts.
I know how to create the buttons programmatically, but I don't know how to place them over the canvas where I need them to be. I'm assuming that this would be done in the view thread's doDraw() method, after all the graphics have been drawn, but how?
I struggled with the same problem, and found out great solution.
RelativeLayout rules like "leftOf" or "rightOf" can be implemented programmatically like this:
RelativeLayout container = new RelativeLayout(getApplicationContext());
Button weight = new Button(getApplicationContext());
final int WEIGHT_ID = 0;
weight.setId(WEIGHT_ID);
weight.setText("0.0");
LayoutParams wrapBoth =
new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
container.addView(weight, wrapBoth);
Button increaseWeight = new Button(getApplicationContext());
increaseWeight.setText("+");
// Note the difference: RelativeLayout.LayoutParams in spite of LayoutParams
RelativeLayout.LayoutParams toBeRightOfWeight =
new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
container.addView(parameter,wrapBoth);
// Sweet part
clearAirParams.addRule(RelativeLayout.RIGHT_OF, WEIGHT_ID);
container.addView(increaseWeight, toBeRightOfWeight);
So, in code you can create a 'container' RelativeLayout, then add several Views with unique ID's and, finally, create RelativeLayout.LayoutParams object to achieve sweet-like-sugar methods for alignment, like in XML.