Control native component measurement and layout in React Native - android

In React-Native, Can we increase height/width of a view such that it can layout outside of parents bound ?
I want to make a view fullscreen that is several layers down a
hierarchy e.g. an item of a listview..
For Android. especially..
I have tried setting layout params but doesn't work, layout sizes can only be modified from JS.
In React-Native Repo's source code, I have seen stuff like
LayoutShadowNode that can help you provide custom measure function.
But I applied it to my custom native component and measure method
never gets called.
Here is a link to a similar description of issue : https://github.com/brentvatne/react-native-video/issues/170
Also tried steps mentioned in
https://github.com/facebook/react-native/pull/5148
but does not seem to work.

Not exactly sure but you can try something like this and see if it works.
int width = 200; // arbitrary width and height values
int height = 200;
DisplayMetrics displayMetrics = new DisplayMetrics();
Objects.requireNonNull(reactContext.getCurrentActivity()).getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
int screenHeight = displayMetrics.heightPixels;
int screenWidth = displayMetrics.widthPixels;
view.measure(width, height); //the view/layout you want to resize
view.layout(-screenWidth, -screenHeight, width, height); // giving first 2 parameters negative value is the key here

Related

Support Multiple screen sizes

I want to make my application support all screen size in my last published build i use wrap content and match parent for handle it but now i want to generate all layouts that respond for each screen size.
I know how to make this by creating folders for each size and put layouts in it but I ask for there is any plugins or feature that make this operation dynamically without make it by myself, I know i will edits in layout to handle many cases but i want to make it simple by generate layout and customize what I want. Thanks for help.
For supporting multiply devices, there are two ways, one from view side and other is programmatically. If you want to do all operations in xml, you can do it with RelativeLayout easily. It has some parameters, like android:layout_alignParentBottom, android:layout_alignLeft or etc, with these parameters, you can customise your views for multiple screens.
Other way is programmatically way, which you can do your operations in java side. For this way, first of all, you shoul get screen sizes:
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int SCREEN_WIDTH = size.x;
int SCREEN_HEIGHT = size.y;
Before getSize was introduced (in API level 13), you could use the getWidth and getHeight methods that are now deprecated:
Display display = getWindowManager().getDefaultDisplay();
int SCREEN_WIDTH = display.getWidth(); // deprecated
int SCREEN_HEIGHT = display.getHeight(); // deprecated
And then, get display metrics and density:
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
float density = metrics.density;
After getting these parameters, locate your views where you want. For setting view , you will need such these simple lines:
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
layoutParams.width = (int) (40 * density);
layoutParams.height = (int) (40 * density);
imageView.setLayoutParams(layoutParams);

Get Android notification panel width

I'm creating some custom remoteViews, In order to draw custom views and layouts to a bitmap as wide as the notification panel I need its exact width.
As every vendor changes the width of the notification panel and in most cases it will changes based on orientation, It's difficult to find a regular pattern on that.
Is there any way to get its width?
The pink part is pure remoteViews and the bottom part is an imageview which I draw my custom view to it, since I don't have the parent width, all the child views get crumble together.
The easiest way is to use LinearLayout with android:orientation="horizontal" and android:layout_width="match_parent", and to put android:layout_weight="1" and android:layout_width="0dp" for every view in that layout. That means that every view in that layout will have same width. More about it you can read here.
UPDATE: In here it says that notification tray width is 478dp.
So, try with that. Maybe some manufactures have different size, but this is good number to start with :).
You can try this to get the width and calculate your custom layout
DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
int height = displaymetrics.heightPixels;
int width = displaymetrics.widthPixels;
Use the width to calculate your custom layout

Layout stretching

Is there a layout, that would allow me to make it in absolute values, but when it would be on larger / smaller screen, it would stretch and adjusted those values, to fit onto the screen but preserve the same look?
Relative layout still keens on exact values.
For example, I have a button and I want it to have it the width of 1/3 of the screen of every device.
Setting manualy in code the width of an element to (for example) screenWidth/3 works. Yet I don't think it's clean. But this technique works.
Find device dimensions at runtime and set width of button at runtime.
Display mDisplay = getWindowManager().getDefaultDisplay();
int deviceWidth = mDisplay.getWidth();
int deviceHeight = mDisplay.getHeight();
button.getLayoutParams().width = deviceWidth / 3;
Give the values in dpi of your layout and view and they will adjust themselves on any screen

Getting width of a Custom View

I need to get the width of a custom view I'm creating by extending LinearLayout. My initial stab was to do
int width = this.getLayoutParams().width
However, this would only return -1.
Next, I attempted overriding onMeasure(int, int)
#Override
protected void onMeasure(int width, int height) {
super.onMeasure(width, height);
this.width = MeasureSpec.getSize(width);
}
This worked to get the width but it was being executed after I actually needed the width (I guess the view wasn't built yet?)
I specifically need it for Google Maps with this method:`map.moveCamera(CameraUpdateFactory.newLatLngBounds(bounds, width, height, MAP_PADDING));
Any suggestions on getting the width?
Unless you explicitly set your view's width/height to static value (like x-dp or match_parent), you cannot know the size of your view until onMeasure loop is completed. Also android can call onMeasure multiple times to find suitable size, so it's not the best place to find exact size. You may have to find a way to delay your map.moveCamera to onLayout function.
You can do it as follows:
// Name the activity's root view - your custom linear layout
View view = findViewById(R.id.customLinearLayout)
int width = view.getRootView().getWidth(); // this returns the actual width of the view
This has always worked for me

Android SurfaceView width/height

I've made a couple of apps for android, but none used graphics that much. I'm now trying to make an actual game. I have some bitmaps that are on my canvas. My problem is I'm not sure where to initialize their positions. and in OnTouch events I want to make sure the bitmaps don't stray outside the screen.
For example, initializing one bitmap:
canvas.drawBitmap(glider.getGraphic(), 30, 20, null);
20 and 30 were arbitrary. But I want to be able to write something like screen.getWidth() so I know exactly where the bitmap is relative to the border of the screen. I can't find this in the developers reference.
If you're just looking to get the screen dimensions, take a look at the DisplayMetrics class. You can get an instance and the measurements like so:
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
int screenHeight = metrics.heightPixels;
int screenWidth = metrics.widthPixels;
Keep in mind this is the absolute screen size. If you're doing a fullscreen game, this will work fine, but if not, you have to keep in mind the size of the notification bar, and possibly the title bar. It might be better in that instance to just get the height of the SurfaceView itself and use it for your calculations.

Categories

Resources