Relative Layout width/height is not work programmatically? - android-layout

I want to doing a relative layout programmatically and set android:layout_widht=60 android:layout_height=60.When i doing programmatically it is fill all screen? How can i do that?
my code:`
RelativeLayout relativeLayout=new RelativeLayout(getContext());
RelativeLayout.LayoutParams rel_btn = new RelativeLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
rel_btn.height = 60;
rel_btn.width = 60;
relativeLayout.setLayoutParams(rel_btn);
this.setBackgroundResource(com.example.R.drawable.line);`
screen:

btn.getLayoutParams().width = width;
btn.getLayoutParams().height = height;
in fact, just use
RelativeLayout.LayoutParams rel_btn = new RelativeLayout.LayoutParams(
60, 60);
that's OK. the problem maybe the background pic stretch the button

Related

Get Navigation bar height in xamarin forms?

I am working on a project where i need to get the navigation bar height.I am getting the whole screen height using:
Helpers.ApplicationContext.ScreenHeight = (int)Resources.DisplayMetrics.HeightPixels / Resources.DisplayMetrics.Density;
Helpers.ApplicationContext.ScreenWidth = (int)Resources.DisplayMetrics.WidthPixels / Resources.DisplayMetrics.Density;
But I am not able to get the navigation bar height.
Can any one please suggest an idea to get the navigation bar height of my page.
Thanks in advance.
For iOS This may help you :
var navbarHeight = YourViewControllerInstance.NavigationController?.NavigationBar.Frame.Height;
For Android :
TypedArray styledAttributes = this.Theme.ObtainStyledAttributes(
new int[] { Android.Resource.Attribute.ActionBarSize });
var actionbarHeight = (int) styledAttributes.GetDimension(0, 0);
styledAttributes.recycle();
Above example gives Height in Pixel. Please find more refined sample as below which provide height in Pixel as well as DP:
TypedValue tv = new TypedValue();
int actionBarHeightInPixel = 0;
int actionBarHeightInDP = 0;
DisplayMetrics metrics = Resources.DisplayMetrics;
if (Theme.ResolveAttribute(Resource.Attribute.actionBarSize, tv, true))
{
actionBarHeightInPixel = TypedValue.ComplexToDimensionPixelSize(tv.Data, metrics);
actionBarHeightInDP = actionBarHeightInPixel / (int)metrics.Density;
}

Positioning of Section HeaderView elements in MonoTouch Dialog

I am trying to customize the position of text in Section element headers in MonoTouch Dialog. I read up on how you are supposed to create you own UILabel to create styled text and then assign that to the HeaderView of the Section object. That part works great.
The problem that I am faced with now is: how can I get a similar text offset as the offset used in the default unstyled Section element (see comparison in attached image). I cannot seem to find a way to get the "Styled Section" text moved away from the left edge of the screen no matter what I do. I tried changing the x coordinate specified in the RectangleF declaration, but whatever I specify appears to be disregarded when the view is rendered.
Here is the backing code for the screenshot:
Root = new RootElement ("Login2Screen");
var labelHeader = new UILabel();
labelHeader = new UILabel(new RectangleF(0, 0, 320, 48));
labelHeader.Text = "Styled
labelHeader.TextColor = UIColor.Blue;
labelHeader.BackgroundColor = UIColor.Clear;
var styledSection = new Section(labelHeader);
styledSection.Add(new EntryElement("Username", string.Empty, string.Empty));
styledSection.Add(new EntryElement("Password", string.Empty, string.Empty));
Root.Add(styledSection);
var defaultStyleSection = new Section("Default Section");
Root.Add (defaultStyleSection);
Add the labelHeader into an UIView and then set the x-coordinate of your UILabel to 10.
var viewLabelHeader = new UIView(new RectangleF(0, 0, 320, 48));
var labelHeader = new UILabel(new RectangleF(10, 0, 320, 48));
labelHeader.Text = "Styled section";
labelHeader.TextColor = UIColor.Blue;
labelHeader.BackgroundColor = UIColor.Clear;
viewLabelHeader.AddSubview (labelHeader);
var styledSection = new Section(viewLabelHeader);
Result:
Haven't read anything about the position of text in Section element, but
labelHeader = new UILabel(new RectangleF(0, 0, 320, 48));
You are positioning it at 0,0? if you make it 10,0 it should move. i.e. move x position to 10
labelHeader = new UILabel(new RectangleF(10, 0, 320, 48));

raphael js, calculate setViewBox width height to fix window

My div content canvas in first width : 1300, height = 500
Then I resize div to width = 800, height = 500
And I resize canvas to width = 800, height = 500 to fix window
But Some Elements in canvas is hide (because My canvas width now is 800px)
So I use setViewBox to zoom it to fix width my new width, height
Result: mouse not fix with element when I drag them (I think I calculate wrong width-height for setViewBox)
Other question: Have any way to let canvas height auto extend when drag element down?
Thanks for help :)
try this code (you have to include jquery + raphael js 2.x) :
var original_width = 777;
var original_height = 667;
var zoom_width = map_width*100/original_width/100;
var zoom_height = map_height*100/original_height/100;
if(zoom_width<=zoom_height)
zoom = zoom_width;
else
zoom = zoom_height;
rsr.setViewBox($("#"+map_name).offset().left, $("#"+map_name).offset().top, (map_width/zoom), (map_height/zoom));

How to zoom all elements pof canvas with one click?

I m using fabric.js in one of my projects.
I m using zooming functionalities . I would like to know if there a simple way to zoom all the canvas (with all the elements ) in on click.
Try out this jsfiddle demonstrating zoom onclick of a button: http://jsfiddle.net/cmontgomery/H7Utw/1/. In essence you get all objects on the canvas and scale/move them by the same factor, giving the visual appearance of zoom.
var objects = canvas.getObjects();
for (var i in objects) {
var scaleX = objects[i].scaleX;
var scaleY = objects[i].scaleY;
var left = objects[i].left;
var top = objects[i].top;
var tempScaleX = scaleX * SCALE_FACTOR;
var tempScaleY = scaleY * SCALE_FACTOR;
var tempLeft = left * SCALE_FACTOR;
var tempTop = top * SCALE_FACTOR;
objects[i].scaleX = tempScaleX;
objects[i].scaleY = tempScaleY;
objects[i].left = tempLeft;
objects[i].top = tempTop;
objects[i].setCoords();
}
canvas.renderAll();
If you use zoom with fabricjs without modifying all objects use: https://github.com/wojtek-krysiak/fabricjs-viewport.
Example: http://wojtek-krysiak.github.io/fabricjs-viewport/

Buttons in LinearLayout not filling entire space programmatically

So I have a LinearLayout in horizontal mode which I add buttons to at random times in code and I like them all to fill up the space equally.
This is my current code so far:
layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
layoutParams.gravity = Gravity.FILL;
and I add it to my view here:
linearLayout.addView(button, layoutParams);
All it does is add the buttons as if it was wrap_content and not expanding their width to fill up the available space as in the buttons look left justified.
I also have tried linearLayout.setGravity(Gravity.FILL_HORIZONTAL); and
linearLayout.setWeightSum(++weightSum);
linearLayout.addView(button, layoutParams);
where layoutParams in this case is:
layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT, 1);
I also tried setting the layout width to zero per answers to other questions.
Am I missing another technique?
Edit: I figured it out, I forgot to set the width of the LinearLayout to match_parent instead of wrap_content.
Button's background has "margins" so they will always have gaps even if there's no space between buttons. You have to change background to your own image which doesn't have these margins.
EDIT: You should use weights for buttons.
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
0, LayoutParams.WRAP_CONTENT);
params.weight = 1;
for( int i = 0; i < 5; ++i) {
Button button = new Button(this);
button.setText("Button" + (i + 1));
linearLayout.addView(button, params);
}

Resources