how to set a button layout_marginTop/layout_marginBottom in runtime? - android

I am using a relative layout with 4 buttons (in the center, one below the other).
My problem is to adjust the gap between all buttons so it will be the same between all buttons.
I want to do it dynamically according to the height of the screen (i use Display class to get the actual height).
what is the best way to do it?
Thanks,
Amigal

You can do this via modifying the LayoutParams of your View
Button b;// your button
RelativeLayout.LayoutParams lp=(RelativeLayout.LayoutParams)b.getLayoutParams();
lp.leftMargin=10;//your left margin here
lp.topMargin=10;//your top margin here and do this for other 2 margins if you need to
sometimes you need to call
b.setLayoutParams(lp);
to have the changes applied
also i dont know how you get the screen dimensions, but this works at every API:
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);

Related

Android - How can I create layout as per image attached for different screens?

Android - How can I create layout as per image attached for different screens?
You must make different designs and use the visibility attribute to change the designs:
RelativeLayout oneLayout = findViewById (R.id.one_view);
RelativeLayout otherLayout = findViewById (R.id.other_view);
oneLayout.setVisibility (View.GONE);
otherLayout.setVisibility (View.VISIBLE);
To know what design to use:
DisplayMetrics metrics = context.getResources().GetDisplayMetrics ();
          int height = metrics.heightPixels;
          int width = metrics.widthPixels;
          int density = metrics.densityDpi;
This way you will know the width and height of the screen.
For the design you have chosen, you should read about RelativeLayout and ConstraintLayout.
So there is one option, which is way too sophisticated and going to be fun to implement but could take forever, you draw this on a Canvas with all sort of paths and whatnot.
Another option is not very clever, but I think will do the trick!
Step 1
Split your background into squares, so that your background is a grid. Split it horizontally and vertically, like the images below.
Place those into containers, namely ViewGroups that split the screen correctly .. maybe using weights or ConstraintLayout
Step 2
You can align your items to the edges of those backgrounds you constructed.
Just an idea :)

android set width and height to 100% of a dynamically created button

that might be a silly question but i didn't find any answer on the internet.
I want to create a button that fills the whole screen (width = 100%, height = 100%) when pressing another button.
Therefore i tried this:
Button button = new Button();
button.setText("fills screen");
button.setWidth(100);
button.setHeight(100);
the problem is that the setWidth and setHeight methods are dealing with px and not with percent.
What do i need to add to use percent values instead of px values?
I'll link here a post that details how to set a width programmatically
programmatically set button width to 50 of parent linearlayout
You can utilize that method (except do not divide by 2 JohnEye demonstrates in his answer)
Or you can utilize the LayoutParams class and create a new instance that will have the parameters set and pass those parameters to your object.
LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
button.setLayoutParams(lp); // Assuming you've already initiated it of course as above
MATCH_PARENT will fill up the view. If the root view is the highest view on the hierarchy then this will work, as it will fill up the entire view. If not, the button would need to be moved there. (It may be your linearLayout or some other view)
Hope this helps.

How do I create a ViewPager without margins?

Using the ViewPager view from the Android support library, the default setup shows one page at a time, with a large margin between each item - ie, if your view is about half the width of your activity there's space on either side, and as you swipe the next one in there's space there too.
ViewPagers have a method, setPageMargin(), that lets you specify an offset to adjust the margin size between pages, and I'm using it to specify a negative margin so that it pulls the pages closer together. However, obviously the amount you need to pull in these margins varies according to the screen dimensions.
So, I'm looking for a smarter way: is there a way to tell the ViewPager "I want no margins at all, making the views in my ViewPager butt up against each other"?
Thank you!
I don't know if there's a smarter way, but your way should work if all pages have the same width. Try something like this:
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
// getActivity().getWindow... if inside a Fragment
yourViewPager.setPageMargin((yourPageWidth - dm.widthPixels) / 2);

move the screen content to the left outside the screen android

is there a way to move the whole screen content outside the screen bounds, with titlebar and all, on the left for example, i tried
content = ((LinearLayout) act.findViewById(android.R.id.content)
.getParent());
to get the screen content and add a margin to it
FrameLayout.LayoutParams pr = (android.widget.FrameLayout.LayoutParams) content
.getLayoutParams();
pr.rightMargin = xOffset;
content.setLayoutParams(pr);
but it doesnt work, the margin appears but the content is not moved just resized to fit the screen, anyone know how to do this the simple way?
you can set the margin in negative(-) values.
for example if your want the layout to move upper use this:
android:marginTop = "-50dip or more u can put"
Are you trying to flip between views? As in, moving that layout off the screen to show another one? If so you should look at using a widget called ViewFlipper which will do exactly as you need. It can also be animated etc.
http://developer.android.com/reference/android/widget/ViewFlipper.html

Android: scaling a button with a background image

I'm probably just being daft but my Google searches are not working out well.
I have a bunch of buttons i add in code that all have dynamic text. I've set a background image for each of these buttons since the default greybutton doesn't work well for my application.
This works perfectly and when the text size (or content) changes, the button automatically grows to accommodate the expanded text. What doesn't work is that I'd like the button to scale proportionally - i.e. if the background image is round, i'd like it to stay round rather than oval as the button gets bigger.
With an imagebutton, there is a property "Adjust view bounds" that does exactly this but I cant put text on an imagebutton. Is there something equivalent for a regular button?
or am I going about this wrong?
i also tried setting the width of the button in code, but I can't seem to determine the new height (button.getHeight() returns 0)
ok i found one way to do it...
I modified the patch-9 to have its expandable area be the maximum on both axis.
then used the DisplayMetrics like this:
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
buttonView.setGravity(Gravity.CENTER);
int buttonSize=(int) Math.floor(metrics.density*1.6*fontSize);
buttonView.setWidth(buttonSize);
buttonView.setHeight(buttonSize);
where fontSize is the size of the font in DIP that i'm placing on each button. In this case, since I only have a single letter on each button, i don't need to worry about the text length, but one could obviously tweak this to handle that situation as well.

Categories

Resources