How to adapt Cells of TableLayout to fit the screen? - android

I'm learning android development and I'm doing a minesweeper. So I use a tableLayout to display the board. But how can I do to adapt the width of the cells to fit the screen ?
Currently my cells are fixed so the grid expand beyond the side of the screen...
How can I take the entire screen width for my tableLayout ?
Thanks.

since you are creating your cells dynamically,
you can calculate the height and width of the screen using:
Display display = ((WindowManager) getSystemService(WINDOW_SERVICE)).getDefaultDisplay();
int width = display.getWidth();
int height = display.getHeight();
and then set your cell size accordingly.

This is done through using simple properties found inside of your layout file xml file. Specifically you want to set the
android:layout_width="fill_parent"
This will tell the layout to consume the ENTIRE width of its parent.

Related

Android - How to fit each TableRow to the screen height

I'm looking to create a table with 3 cells (TableRow) each cell should have the dimensions of the size of the full screen (width and height).
Should I use something other than a TableLayout to get the same effect?
Thanks a lot.
Here is an example of the expected result
The standard way of doing this will be by using a RecyclerView along with a Vertical Layout Manager, each and every one of your element will have width and height as match parent.

Dynamic height of ImageView in scrollView

I am trying to add 3-4 imageviews in a scroll view. Imageviews have dynamic heights. Height of first view have 70% of the screen and second and third have 30% height. I am using constraint layout and guidelines but the height is getting set according to the height of the scroll view but not according to the screen height. Is there a way to do this with constraint layout.
Have you tried doing it programmatically?
First, get the Screenheight for your device: How to determine the screen width in terms of dp or dip at runtime in Android?
Secondly, get your Imagview and set its height according to xx% of the determined screen height.
BR

First child of LinearLayout inside ScrollView with 1/3 space of device height

I have a ScrollView with a LinearLayout with 3 elements inside. I would like that the first element has a height of 1/3 the height of the device height and the other 2 with wrap_content, is this possible to do in xml or how would you do this? Using weight alone it does not seem possible because the LinearLayout inside the ScrollView could be longer than the device's height.
According to Android docs, android:layout_weight:
Indicates how much of the extra space in the LinearLayout is allocated to the view associated with these LayoutParams. Specify 0 if the view should not be stretched. Otherwise the extra pixels will be pro-rated among all views whose weight is greater than 0.
This implies that the weight is not dependent on the screen height, but rather only on its parent view, so you can't achieve that from xml. To achieve this I would compute the height of the screen and then resize the view:
Display screen = getWindowManager().getDefaultDisplay();
Point size = new Point();
screen.getSize(size);
int screenHeight = size.y;
myView.setLayoutParams(new LayoutParams(myView.getWidth(), screenHeight / 3));
You can give the first linear layout weight=0.333 with height=0 and the other two layout wrap_content

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

Categories

Resources