i have created a custom view
CameraView extends View{
.....
}
in OnCreate methode i have define as
cameraView = new CameraView(this);
LayoutParams layoutParamsCamera
= new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT);
this.addContentView(cameraView, layoutParamsCamera);
but this use full screen i need to use as
marginBootom= 20dp or paddingBoototm= 20dp
so how i can add this parameter to LayoutParams??
i need help
& sorry for my bad English
Use MarginLayoutParams instead of whatever flavor of LayoutParams you're now using to set view layout margins programmatically.
Related
I have a customView extending from TextureView. I want this customView to always align_parent_right.
My method inside customView has the following code:
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams();
layoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
setLayoutParams(layoutParams);
However, customeView is still aligned left and not right. How can it be change to align right?
Add this way
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams();
layoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
layoutParams.addRule(RelativeLayout.LEFT_OF, R.id.leftViewIdHere);
setLayoutParams(layoutParams);
I have initialized seek bar in Activity Class like this :
SeekBar mySeekBar = new SeekBar(this);
It is too small how can I set dimentions for It ?
scale and setScrollBarSize methods didn't work
When you add it to a ViewGroup, you specify appropriate LayoutParams for it. Try this:
viewGroup.addView(mySeekBar, LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
If you need to something more complicated depending on the layout, you can create a each layout has a LayoutParams inner class that you can create an instance of and make modifications to, then add the view using
LayoutParams params = new LayoutParams(...);
/* do stuff with params */
viewGroup.addView(mySeekBar, params);
I need to implement Dialog for my Android app through Java code, so I can't use XML.
I have root LinearLayout where I implement range seek bar, then I have another LinearLayout under root layout, with horizontal orientation, where I want to add two buttons in same row. So I need to set weight to 1, and width to FILL_PARENT and height to WRAP_CONTENT.
How I can do that with Java code?
LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
p.weight = 1;
rangeSeekBar.setLayoutParams(p);
I'm not sure which view you want to set the layout params on. I just assumed the rangeSeekbar to show an example. Change if you need.
When using the layout params always use the root's param type..
Ex. if you have a View you want to apply params to within a RelativeLayout use RelativeLayout.LayoutParams..
You can pass it in as part of the LinearLayout.LayoutParams constructor:
Did you mean wrap_content?
LayoutParams param = new LinearLayout.LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT, 1.0f);
1.0f is the weight
Is not using XML a requirement? If not, you could always build your layout in XML and then use a LayoutInflater at run-time to build up your view hierarchy and pass that to setView() on your dialog.
For example:
LayoutInflater inflater = LayoutInflater.from(getActivity());
View v = inflater.inflate(R.layout.my_dialog_layout, null);
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setView(v);
an easier way:
public static void setLayoutWeight(View view , float weight)
{
((LinearLayout.LayoutParams) view.getLayoutParams()).weight = weight;
view.requestLayout();
}
I created LinearLayout and Button via class not via XML. I know how to inflate layout view and button view dont know how to set margins of them. I need to set margins of my button and my linearlayout. If I use XML, it is very simple: <android:marginLeft="10px">.
But, what should I do if I want to set margin by class not by XML?
In this we have linear layout in main.xml named lyt1 and we add edittext at runtime and set
left margin value
please use bleow code :
lyt = (LinearLayout)findViewById(R.id.lyt1);
EditText txt = new EditText(WvActivity.this);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT);
lp.leftMargin = 10;
txt.setLayoutParams(lp);
lyt.addView(txt);
lyt.invalidate();
Use:
LinearLayout.LayoutParams params=new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
params.setMargins(left, top, right, bottom);
Working with the XML file was easy as I could specify the parameters as
<android:layout_width="fill_parent" android:layout_height="wrap_content">
But I am confused while specifying it through code. For each view I specify the parameters using
view.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.FILL_PARENT));
I see that I have an option of specifying it as relative layout, frame layout etc.
As of now I am using linear layout for all views such as images, text and also gridview. Should the view parameters be defined based on the layout of the parent element? Or is it OK to specify it as linear layout even if the view is a child of, say, a framelayout? Sorry, but I couldn't find out the difference.
All layout classes (LinearLayout, RelativeLayout, etc.) extend ViewGroup.
The ViewGroup class has two static inner classes: LayoutParams and MarginLayoutParams. And ViewGroup.MarginLayoutParams actually extends ViewGroup.LayoutParams.
Sometimes layout classes need extra layout information to be associated with child view. For this they define their internal static LayoutParams class. For example, LinearLayout has:
public class LinearLayout extends ViewGroup {
...
public static class LayoutParams extends ViewGroup.MarginLayoutParams {
...
}
}
Same thing for RelativeLayout:
public class RelativeLayout extends ViewGroup {
...
public static class LayoutParams extends ViewGroup.MarginLayoutParams {
...
}
}
But LinearLayout.LayoutParams and RelativeLayout.LayoutParams are completely different independent classes. They store different additional information about child views.
For example, LinearLayout.LayoutParams can associate weight value with each view, while RelativeLayout.LayoutParams can't. Same thing with RelativeLayout.LayoutParams: it can associate values like above, below, alightWithParent with each view. And LinearLayout.LayoutParams simply don't have these capability.
So in general, you have to use LayoutParams from enclosing layout to make your view correctly positioned and rendered. But note that all LayoutParams have same parent class ViewGroup.LayoutParams. And if you only use functionality that is defined in that class (like in your case WRAP_CONTENT and FILL_PARENT) you can get working code, even though wrong LayoutParams class was used to specify layout params.
Depending on how many views you want to change the layouts on, I think it's better to create a helper method and pass whatever views you want to change to the method along with the height and width values you want them to change to:
public void setWidthHeight(View v, int width, int height){
LayoutParams lp;
lp = v.getLayoutParams();
lp.width = width;
lp.height = height;
v.setLayoutParams(lp);
}
Remember that setting the width and height here are not going to match the same values in your xml, i.e., android:layout_width="32dp" is not the same as lp.width = 32;
Also, the LayoutParams type variable called lp should be of the type returned by your view... Check what type is returned by the view and import that type in your import statements.