I would like to put two images which are filled the row with equal widths. I have several rows like that. For wide screens such as tablets, the widths of images should be increased according to screen resolution.
What I want to do is exactly placing two images on a row in equal sizes, (%50 - %50 width like CSS) in all cases.
Is there any layout to perform this ? I've tried it with nested Linear and Relative layouts but cannot succeed.
Try this ...
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content" />
<ImageView
android:id="#+id/imageView2"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
This is just for 1 row. Both ImageViews will always occupy 50% of the row irrespective of device. Depending on how many rows you have, you may choose to use custom listview. Hope this helps
I have done this in my project, I don't really now what you need to achieve but in my project I just resize the image to respect a height equal to my width so the image will always be a perfect round picture...
Inside my activity I have this piece of code:
private RelativeLayout playerSpinner;
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
int margin = (int)(45*getResources().getDisplayMetrics().density);
LayoutParams layoutParams = new LayoutParams(playerSpinner.getWidth() - margin,playerSpinner.getWidth() - margin);
layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE);
playerSpinner.setLayoutParams(layoutParams);
}
Then I just added a ImageView inside my playerSpinner layout with MATC_PARENT or FILL_PARENT in the image properties.
Related
I have a constraint layout which displays like an image view but it contains multiple views with the width set as wrap_content.
The constraint layout is put inside horizontal scroll bar.
My problem is,.. for the small dpi phones, the width of layout becomes big so I have to scroll to see the full layout. But I can fix it using this in the xml of constraint layout by adding
android:scaleX="0.7"
android:scaleY="0.7"
But the problem is, for the high dpi phones, the layout becomes very small and there are blank spaces appear around the layout. So i think I need to adjust the scaleX and scaleY to programmatically so that it will become a variable to adjust the layout scale for different screensizes. How to adjust this scaleX and Y in java code programmatically according to the screen dpi?
example,
for the 420dpi phones, the scales should be 0.8
for the 320dpi phones, the scales should be 0.6 or lower, etc...
but using these values with if statement will not accurate for all phones. I want an accurate variable float value of scale to fit the screen for every dpi displays. How to get this float value of scaleX and scaleY from the screen dpi. Please. (if possible, i don't want to use Horizontal scrollview. I want the scale of constraint layout to fit the width of screen only. And also I can't use different layouts in this case.)
This is my xml for this
<androidx.core.widget.NestedScrollView
android:id="#+id/scrollview"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginBottom="55dp"
android:fillViewport="true"
android:visibility="visible"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/constraintlayoutprintcard"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="5dp"
android:layout_marginStart="5dp"
android:layout_marginTop="5dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<HorizontalScrollView
android:id="#+id/horizontalscrollview"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="#00FFFFFF"
android:fillViewport="true"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/constraintlayoutprint"
android:layout_width="500dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="#color/colorWhite1"
android:scaleX="0.8"
android:scaleY="0.8"
android:visibility="visible"
tools:ignore="ExtraText">
</androidx.constraintlayout.widget.ConstraintLayout>
</HorizontalScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.core.widget.NestedScrollView>
Set width of the image to match_parent with margins that you want and height as you wish. This will fit the width of the image according to screen width
Try creating the layout programmatically. There's an inner class in every layout that deals with setting the parameters.
Try this thread
If for some reason there are no scaleX and scaleY properties in the GridLayout and GridLayout.Params classes, you may have to go through each of the children in the layout and adjust the weight and gravity. It's always possible that the xml version is doing just that behind the scenes.
i am having some issues here regarding grid layout.
Lets say that i have a grid layout that covers the whole screen and which has fixed row and column count (eg. 8/8). Now lets say that i want to insert 2 custom views that only match that column and row that they were inserted into.
image example:
i am sorry if the picture is bad :D Have in mind that column width and height is fixed to the device screen and the views should not spread it, instead they should fill that 1 column. I am sure that it is possible to achieve this but unfortunately i have not found a solution till now. Any help is much appreciated, thanks!!!!
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#0099cc"
tools:context="com.example.luka.gridlayouttest.FullscreenActivity"
android:columnCount="12"
android:rowCount="12"
android:columnOrderPreserved="true"
android:rowOrderPreserved="true">
<TextView
android:text="TextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/textView5"
android:layout_row="1"
android:layout_column="3"/>
<TextView
android:text="TextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/textView6"
android:layout_row="4"
android:layout_column="6" />
</GridLayout>
You're doing it correctly. You're just not complete. The undefined views essentially have a zero size. If you add something like
android:text="row0, col0"
android:layout_row="0"
android:layout_column="0"/>
android:text="row1, col1"
android:layout_row="1"
android:layout_column="1"/>
etc, you will see your TextViews going diagonally down to the right. It seems you have to populate every view in the grid for it to give you the complete grid. For each view, you either have to give it a fixed size (instead of wrap_content) or fill it with something (using android:text="row0, col0").
In a linear layout the nested widgets are arranged horizontaly/vertically and according to their width/height.
My question is in the following:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<View
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#ffff0000"
/>
<View
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#ff00ff00"
/>
</LinearLayout>
</LinearLayout>
Why do I see only the first view? I was expecting since they were wrap_content either both would display or none. But I see in the design preview:
Also if I add android:layout_weight="2" in the first View and android:layout_weight="1" in the second the second view gets 2/3 of the space even if I add weight_sum=3 in the enclosing layout
You see only one view because you didn't give a numeric width to the views.
Because of that the first view gets all the screen and other one is not seen at all - it is at the right, next to the red one.
if you want that both of them will be with same width, just give each one the same weight, that way they will be equal to each other and will fit to the screen height or width, depends on the layout orientation of the container.
From the documentation:
android:weightSum - Defines the maximum weight sum. If unspecified, the sum is computed by
adding the layout_weight of all of the children.
I want to create a screen
with two fixed size areas: one aligned to left, one to the right
and in between them another area which spans all the rest of the area?
If I make the middle view with property fill_parent it will catch all the area to the 3rd child.
what will be the effective property when layout_weight=0.X and layout_width=20dp.?
======
I have a linearLayout with orientation= horizontal.
It has a child with layout_weight=0.X and also layout_width=20dp.
What will be the effective property?
If you have a horizontal LinearLayout with the first child with a fixed width (e.g. layout_width="20dp"), the second child with a non-zero weight (e.g. layout_weight="1") and the third with a fixed width (e.g. layout_width="20dp"), then you should get the first aligned to the left, the third aligned to the right and the third filling the area between them.
It's also possible to do this with a RelativeLayout, but I'll leave that as the above solution should work just fine.
It will be something like this:
<LinearLayout android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<View android:id="#+id/view1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<View android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"/>
<View android:id="#+id/view3"
android:layout_width="20dp"
android:layout_height="wrap_content"
android:layout_weight="0"/>
</LinearLayout>
You should use android:layout_weight.
Can you help me to configure my layout? I don't achieve what I'm looking for:
I've put the imageview background to red for explain what I want:
The image's width is smaller than the layout width. I'd like the image grow proportionally keeping its aspect ratio until reach the aspect at the first image (I modified it manually with photoshop) but when setting width="fill_parent" the result is the second image and setting both width and height to "fill_parent" the result is the third image.
I tried to combine with ScaleType with no success
How can I achieve the first option? Thanks
Use android:adjustViewBounds=true attr in xml for ImageView
This question is solved here by extending the ImageView class.
Android ImageView adjusting parent's height and fitting width
I created a sample. It works for me. Here is the xml layout file.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView android:src="#drawable/hydrangeas"
android:layout_height="wrap_content" android:id="#+id/imageView1"
android:scaleType="centerInside"
android:adjustViewBounds="true"
android:background="#android:color/white"
android:layout_width="wrap_content"/>
</LinearLayout>
It works for fitCenter too. Have a look at the snapshot.
Use the attribute fit_center into your ImageView tag.
<ImageView ... android:scaleType="fitCenter" .. />