I am trying to set layout width as float value programmatically. But it accept only int value.
LinearLayout linearLayout = findViewById(R.id.parent_layout);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
layoutParams.width = 200.75; // Here I am getting error.
But if I set by xml I am able to give float value.
<LinearLayout
android:id="#+id/parent_layout"
android:orientation="vertical"
android:layout_width="200.75dp"
android:layout_height="200dp"/>
But not able to set programmatically. Please let me some idea to resolve this.
DP (DIPs) are not pixels and "layoutParams.width=" requires Pixels.
200.75dp is not equal to 200.75px because it's Display Density dependant.
200.75dp could be more than 600px on some display.
You can convert PX in DP and vice-versa using simple methods (please search online).
However an internal conversion and Math.round() is done when "layout_width="200.75dp"" is executed in Runtime, and you can simulate it using appropriated conversion methods.
Related
This is my final technology issue that i have to complete my 4 month of work on my app.
I will try to be simple as i can,because i need specify solution.
My problem:
In short: i need the stretch my glSurfaceView
I am using custom glsurfaceView,that implement OnMeasure method to set the actual width and height to the surface.
I set the width and height for 1280 and 720 for instance.
then i use openGL and get that pixels with glReadPixel() method to encode video
so far so good.
i add the view in that way :
this.addContentView(this.mRenderSurfaceView, BaseGameActivity.createSurfaceViewLayoutParams());
when :
protected static LayoutParams createSurfaceViewLayoutParams() {
final LayoutParams layoutParams = new LayoutParams(android.view.ViewGroup.LayoutParams.MATCH_PARENT, android.view.ViewGroup.LayoutParams.MATCH_PARENT);
layoutParams.gravity = Gravity.CENTER;
return layoutParams;
}
Thats what i tried so far:
I want the glsurface to be the actucal pixels that i set in OnMeasure,
that's mean the glReadPixel() function will get the same pixels as i set,
but,I want to scale this surface,without changing to actucal pixels.
when i try this:
this.mRenderSurfaceView.setPivotX(0.0));
this.mRenderSurfaceView.setScaleX(2.0));
It just copy the surface to another place in the screen,without any scale.
What i do wrong?
Please,I don't need to change the actual width and height of this surface.i need it as is-but i have to scale it that the user will see it bigger then the real pixel that i set with onMeasure method.
**i encode the surface to video so any change in width/height will change the pixel that i get in glReadpixel-so just scale is needed.
I would like to set
android:layout_height="wrap_content"
to some pixels(int) during my application start. This is a textView and the reason behind this is that i want the height of the textView dynamic based on some inputs from my end which will be computed when the onCreate method is called.
Is this possible? if yes any example would be great.
Forgot to add my textView xml looks like this
<TextView
android:id="#+id/sometext"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:fadingEdge="vertical"
android:background="#drawable/dropshadow"
android:scrollbars="vertical"
android:text="Getting data on your slow n/w..."
android:textColor="#ffffff"
/>
Just edit the layout params of the view.
TextView v = (TextView) findViewById(R.id.sometext);
LayoutParams lp = v.getLayoutParams();
lp.height = 50;
v.setLayoutParams(lp);
The view will be 50px high in this example.
Note that it's generally not recommended to use pixel dimensions for layouts due to the many device specs out there. Rather use dp (density independent pixels) instead. You can calculate pixel dimensions from dp values in the following way (50dp to px here):
float dp = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 50,
getResources().getDisplayMetrics());
I am developing for two different devices and need to force pxs on one device's layoutparameter object through code when specifying width and height. How do I do that?
GridView.LayoutParams glp = new GridView.LayoutParams(GridView.LayoutParams.WRAP_CONTENT, GridView.LayoutParams.WRAP_CONTENT);
glp.width = 80; <- need to ensure this is in px or LDPI interprets it .75 of 80
glp.height = 80;
Normally I would use setFontSize(TypedValue.COMPLEX_UNIT_PX, size) and it works. How about Layouts themselves?
You need the params to be fixed? If you want to make sure adding pixels, create dimen resources e.g: 80px in string.xml then load in LayoutParams
new GridView.LayoutParams(pixels, pixels); They are already pixels.
See the docs
http://developer.android.com/reference/android/view/ViewGroup.LayoutParams.html#ViewGroup.LayoutParams(int, int)
Im using a customized webview as follows
<merge
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:MyWebView="http://schemas.android.com/apk/res/com.mds.android.mireader.webview" android:background="#FFFFFF" android:layout_height="fill_parent" android:layout_width="fill_parent">
<com.mds.android.mireader.webview.MyWebView
xmlns:android="http://schemas.android.com/apk/res/android" android:id="#+id/myweb" **android:layout_width="318px"** android:layout_height="fill_parent" > </com.mds.android.mireader.webview.MyWebView>
im doing some thing strange, for that i need to satisfy following condition
condition: webview width should always divisible by 3
So i can't give fixed value or fill_parent in above xml file.For example if i give fill_parent it works fine for portrait(assume HVGA emulator with portrait width=480 which is divisible by 3)but no success for landscape(bcz landscape width 800 which is not divisible by 3).
Solution should work for both orientation change and device change"
Im struggling with 1 or 2 pixel values difference and it will be solved if i satisfy that condition. TIA ....
HI,
You can get the height and width of the screen using following piece of code:
DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
int height = displaymetrics.heightPixels;
int width = displaymetrics.widthPixels;
Now you have the width you can check whether it is divisible by 3 or not. If yes then well and good, otherwise you can subtract the modulus(width%3) from the original width and set it as the width of your webview.
Add A View Programmatically:
Sample XML:
<LinearLayout (define id:testLayout, width, height here> </LinearLayout>
Java Code:
LinearLayout linLayout = (LinearLayout) findViewById(R.id.testlayout);
// create an object of your webview and set all the properties you want like background color etc.
MyWebView myWebView = new MyWebView();
linLayout.addView(myWebView, new LinearLayout.LayoutParams(<my calculated width>, LayoutParams.FILL_PARENT);
PS: you dont have to add the webview object in your XML file. "addView" function will add your webview inside your linear layout object.
Hope this helps!!
i have a relative layout on which i am displaying a page and some content. when i zoom my page...the layout size is not increasing. i want my layout to increase its size dynamically. how do i set it??
i tried doing it in java code.
contentLayout.getLayoutParams().height = x (some value which is equal to the page size)
contentLayout.requestLayout()
but this is nt working. i have also set the layout params android:layout_width and android:layout_height to wrap-content in the xml file.
Kindly, help me out. thank you
You can do it this way. I also added setting of the margins:
RelativeLayout targetItem = (RelativeLayout) findViewById(R.id.RelativeLayout01);
RelativeLayout.LayoutParams adaptLayout = new RelativeLayout.LayoutParams(mWidth, mHeight);
adaptLayout.setMargins(marginLeft, marginTop, marginRight, marginBottom);
targetItem.setLayoutParams(adaptLayout);
For mWidth and mHeight you also may set dip values to make it adapt to the different screen sizes. Easy way to do that is to define Dimension in your strings.xml and work with them, not with absolute values.
So, if you define relative_width as 120dip, and relative_height as 100 dip then in code you have
RelativeLayout.LayoutParams adaptLayout = new RelativeLayout.LayoutParams(getResources().getDimension(R.dimen.relative_width), getResources().getDimension(R.dimen.relative_height));