someone can help me with this?
I didn't understand two things.
one, is that thing:
RelativeLayout.LayoutParams center_ob_l = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
what is the meaning of WARP.CONTENT and why should I do this?
second thing, is the "this":
Button log_b = new Button(this);
why should i send "this" in those brackets?
and why at all I would want to create by myself the buttons and things instead of just go to the visual device and throw the things I want to the screen?
This is answer for "this":
Button btn = new Button(this); what is the use of "this" in this context....?
For understanding WRAP_CONTENT read this: https://developer.android.com/reference/android/view/ViewGroup.LayoutParams.html
WRAP_CONTENT means that the relativelayout establish his size fitting his content.
this (java key) refers to current object, whatever it is, the current istance of the class you are coding.
if you don't need to build programmatically your layout, build it with visual editor, when you will need to create the layout programmatically you will understand it by yourself ;)
LayoutParams is used to tell how the view is going to be drawn, you are using this ViewGroup.LayoutParams(int width, int height) where WRAP_CONTENT is the width and height.
check this
https://developer.android.com/reference/android/view/ViewGroup.LayoutParams.html
For Button log_b = new Button(this);
this keyword in java refers to the current class object.
Button(Context context) 'this' is the context you are passing in the constructor
check this
https://developer.android.com/reference/android/widget/Button.html
wrap_content
which means that the view wants to be just big enough to enclose its content (plus padding)
It's roughly the equivalent of setting a Windows Form Control's Autosize property to true
this
is required by the way android works with Context. Specifically when you are passing this you are basically passing the class that encapsulates this statement.
Related
I read often something like this:
RelativeLayout.LayoutParams lParams = (RelativeLayout.LayoutParams) view.getLayoutParams();
What exactly LayoutParams do?
I've read the Documentary but I wasn't smarter after reading!
Hope someone can explain me what LayoutParams do or pass!
Kind Regards!
LayoutParams are the Java Object representation of all the params you give to your View in the .xml layout file, like layout_width, layout_height and so on. Getting this object from a View allows you to look up those params on runtime, but also to change them in your Java code, when you need to move the View, change it's size etc.
LayoutParams are used by views to tell their parents how they want to be laid out.
The base LayoutParams class just describes how big the view wants to be for both width and height. For each dimension, it can specify one of:
FILL_PARENT (renamed MATCH_PARENT in API Level 8 and higher), which means that the view wants to be as big as its parent (minus padding)
WRAP_CONTENT, which means that the view wants to be just big enough to enclose its content.
That's all folks.
LayoutParams is use for the dynamically change the layout width and height. and also use the create custom view without the xml by using the directly by use of the LayoutParams for Relative or Linear type layout.
Basically when you set an xml with 'match_parent' or anything like layout_something
the android inflator will set the layout param for the child with the appropriate Layout params with the type matching the parent control, you could also do this in code and if you forget or set the wrong type you will get an exception in runtime.
the parent control needs this information to layout the child control correctly and to his liking.
Please see the following: Android Developer site - Layout Params
I think this picture says it all
I'm confronted with the Problem of slow Performance...
Just take a case:
RelativeLayout myLayout = (RelativeLayout) findViewById(R.id.myrlLayout);
//Adding now 100 Buttons with 100 TextViews below each Button(just a example)
for(i=0;i<100;i++) {
Button btn = new Button(this);
btn.setId(i+1); //Should be a positive integer
//set LayoutParams for Button
RelativeLayout.Layoutparams btn_layoutparams = new RelativeLayout.LayoutParams....
....
myLayout.addView(btn, btn_layoutparams);
TextView mytv = new TextView(this);
mytv.setid(101+i);
//set LayoutParams for Button with referenced to the Button(because the Textview Needs to be
of Button)
....
myLayout.addView(mytv, tv-layoutparams);
}
Regarding to the high amount of Views programmatically created, my app starts really slow...
I think it's not because of creating a new View, but because of setting the LayoutParamters each time for the view. I can't find a Workaround because my LayoutParams for the TextView for example Need to reference to the button created before. Due to that i'm not really able to create a XML-layout-file or XML-style-file because i can't reference the tv's layoutparameters anchor in the XML-file to the button which does not exist at the Moment. At least i didn't find a way. I hope somebody got an idea how to appreciable improve the Performance when creating such a amount of views at runtime. Every advise is welcome.
Update regarding answere from rom4ek
The Problem is, that i Need to calculate how much views can i add per row before the Screen-width is fully used. That means i Need second LayoutParams to add the next Button below the first Button from the first row. And i also Need to reference to the img-Button added before in the LayoutParams.. so it's not possible to reference LayoutParams to a Button which doesn't exist before the for-loop.Maybe i completely miss something.. Do you have an idea/solution? Thank you for your respond.
If you're setting the same LayoutParams, what if you move RelativeLayout.Layoutparams btn_layoutparams = new RelativeLayout.LayoutParams.... before the cycle? So you will initialize it one time, and then no need to create new LayoutParams every step.
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
I do a constructer and set some view inside programmatically and add some rule. so When i call constructer i see default view.
Now i want to reach someof this view and change set margin.It is basic i know but i dosent work for me.
Can anybody help?
Part of my constructer:
_photoImgVw = new ImageView(getContext());
_photoImgVw.setImageResource(R.drawable.pic);
_photoImgVw.setAdjustViewBounds(true);
_photoImgVw.setMaxWidth(100);
_photoImgVw.setId(2);
lp2=new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
lp2.setMargins(0,0,0,0);
addView(_photoImgVw,lp2);
LayoutParams lp3=new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
lp3.addRule(RelativeLayout.RIGHT_OF,_photoImgVw.getId());
lp3.setMargins(0,0,0,0);
addView(_nameTxt,lp3);
Call in the activity for change:
facebookPersonView.lp2.setMargins(10,300,100,20);
Make sure that the layout params lp2 are an instance of the class RelativeLayout.LayoutParams instead of MarginLayoutParams, or any other subclass of it named LayoutParams (there are quite a few, see the top of the documentation).
If the params are an instance of the latter, they won't contain any rules for the RelativeLayout. Which means the parent layout doesn't know where to place the view¹ and therefore can't make any sense out of the margins.
It's best to declare that explicitly in a way like this:
RelativeLayout.LayoutParams lp2
= new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
lp2.setMargins(0,0,0,0);
addView(_photoImgVw,lp2);
¹ It will still make it visible in the top-left corner, but it's kind of an undefined state.
I need to create a GUI (layout+views) in my .java activity class (I know it's far more flexible and easier to use .xml layout file, but I don't want to use it for now).
I can't find any setGravity() (but a "Gravity" object I can't figure how to use) or any set setMargin() method for the "View" object.
What is the easiest way to do it ?
Thanx.
For setting the margin on component. The following leaves the existing margins as previously set and sets the left margin as zero.
TextView title = ((TextView)findViewById(R.id.default_panel_title))
final ViewGroup.MarginLayoutParams lpt =(MarginLayoutParams)title.getLayoutParams();
lpt.setMargins(0,lpt.topMargin,lpt.rightMargin,lpt.bottomMargin);
title.setLayoutParams(lpt);
You can add gravity to the "layouts" not to the "controls". Try to set gravity to any of your Linear/Relative or Frame layouts using setGravity(); .
Eg:
LinearLayout lll = (LinearLayout) findViewById(R.id.layoutname);
lll.setGravity(Gravity.CENTER);