Thursday, January 29, 2015

Set Android View's Bound in Java Class(RelativeLayout)

Continue from previous lesson Set Third-party Font in Android, in this post I will show you how to set dimension and align a view in Java Class that extends RelativeLayout.

If we create the XML Layout, you can use design tab and just drag and drop the view. Then, the code will auto generate and update.
But, in java you need use RelativeLayout.LayoutParams to adjust dimension and position of the view.



For dimension, there exist three constant provide in LayoutParams class,

FILL_PARENT This constant for height or width, the dimension of height or width will same as the screen of device.
This constant already deprecated(replace with MATCH_PARENT)
MATCH_PARENT This constant for height or width, the dimension of height or width will same as the screen of device.
WRAP_CONTENT This constant for height or width, the dimension of height or width depend on content.


For alignment, have 22 constant variables,

ABOVE Rule that aligns a child's bottom edge with another child's top edge.
ALIGN_BASELINE Rule that aligns a child's baseline with another child's baseline.
ALIGN_BOTTOM Rule that aligns a child's bottom edge with another child's bottom edge.
ALIGN_END Rule that aligns a child's end edge with another child's end edge.
ALIGN_LEFT Rule that aligns a child's left edge with another child's left edge.
ALIGN_PARENT_BOTTOM Rule that aligns the child's bottom edge with its RelativeLayout
parent's bottom edge.
ALIGN_PARENT_END Rule that aligns the child's end edge with its RelativeLayout
parent's end edge.
ALIGN_PARENT_LEFT Rule that aligns the child's left edge with its RelativeLayout
parent's left edge.
ALIGN_PARENT_RIGHT Rule that aligns the child's right edge with its RelativeLayout
parent's right edge.
ALIGN_PARENT_START Rule that aligns the child's start edge with its RelativeLayout
parent's start edge.
ALIGN_PARENT_TOP Rule that aligns the child's top edge with its RelativeLayout
parent's top edge.
ALIGN_RIGHT Rule that aligns a child's right edge with another child's right edge.
ALIGN_START Rule that aligns a child's start edge with another child's start edge.
ALIGN_TOP Rule that aligns a child's top edge with another child's top edge.
BELOW Rule that aligns a child's top edge with another child's bottom edge.
CENTER_HORIZONTAL Rule that centers the child horizontally with respect to the
bounds of its RelativeLayout parent.
CENTER_IN_PARENT Rule that centers the child with respect to the bounds of its
RelativeLayout parent.
CENTER_VERTICAL Rule that centers the child vertically with respect to the
bounds of its RelativeLayout parent.
END_OF Rule that aligns a child's start edge with another child's end edge.
LEFT_OF Rule that aligns a child's right edge with another child's left edge.
RIGHT_OF Rule that aligns a child's left edge with another child's right edge.
START_OF Rule that aligns a child's end edge with another child's start edge.

Reference : RelativeLayout | Android

addRule() function use to align the view, one axis only can have one rule. If there are two rule for Y-axis, then the application will use the most top alignment .

Let's say in you app's design , TextView should be at top of the screen and center. So, the code is like this.

//This line to set view's dimension
LayoutParams layoutParams=new           
                LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);

//This line to set view's alignment
layoutParams.addRule(RelativeLayout.ALIGN_PARENT_TOP,TRUE);
layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT,TRUE);


Then, use setLayoutParams() to assign LayoutParams for TextView,

//This line to set LayoutParams
textView.setLayoutParams(layoutParams);

Here full code for MainLayout.java,


Sample Running:




Futhermore, you also can set margin(or absolute position) in LayoutParams.
Have four variables for set margin,
  • leftMargin
  • rightMargin
  • topMargin
  • bottomMargin
And three functions for set margin.
  • setMargins(left,top,right,bottom)
  • setMarginStart(start)
  • setMarginEnd(end)
Below example code,





Sample Running :





Now, I will show you the code which one of the views's position depend on other view's position.




Sample Running :



firstly, need assign id for textView1 at line 67, then the application can inform textView2 which view it should follow by adding a rule, at line 111.


To download full code in .ZIP format, just click on link below,

Tutorial 3.zip

So, this is the end of the post,
If you have problem or something to ask something related to java...
you can email or comment on this post...
I will try to find the solution and share it with you..












you can test it by your self..
i hope you like this souce code...
and please give some comment....
                                                                                                                                                      Created By : Z-man, 2015

No comments:

Post a Comment