XML background image in layout - android

This is ridiculous, no error, I already did the same on some other projects and works fine but it still shows nothing...
Someone knows why my background image is fine in the editor (in the Android Studio editor it shows the background image), but when I run the app on my device, it shows the only white background with just my 2 buttons...
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.ale.mediaspectrumplayer.MainActivity"
android:background="#drawable/background">
<Button
android:id="#+id/ID_MediaPlayer_Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="48dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="24dp"
android:text="#string/main_MediaPlayer"
app:layout_constraintBottom_toTopOf="#+id/ID_SpectrumView_Button"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent" />
<Button
android:id="#+id/ID_SpectrumView_Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="48dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="16dp"
android:text="#string/main_SpectrumView"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.25"
app:layout_constraintStart_toStartOf="parent" />
</android.support.constraint.ConstraintLayout>
Neither with code it works and I dunno why (layout.setBackgroundResource(R.drawable.background), and neither with image view and scale fitXY...that's absurd

Sometimes, if the image you are using to set as background is of high resolution or is heavy, system just avoids loading it to save the memory. In case you want that image in the background of the whole content, you have to set an image view whose height and width matches that of the parent layout. Then use an image loading library eg : Picasso, Glide etc to efficiently load the high resolution image in that ImageView without burdening the system

Have you tried using the android:scaleType attribute? This can change the position of the background image to cover the screen, sometimes if the image is too small it won't show up. Setting scaleType to center or centerCrop might help.
Here's the documentation: https://developer.android.com/reference/android/widget/ImageView.ScaleType.html

try Changing parent layout to RelativeLayout or any other layout.

Related

How to go beyond the edge parent element

I have a circular image and a button icon
Lets say image is size of 20x
And button is size of 8x
I want to put the button icon in a way that 80% of its portion stays on the image view and 20% goes beyond it.
My xml code-
RelativeLyaout<
andoird.cardview.widget.Cardview<
<ImageView/>
/>
<button_icon>
layout_alignRight:ImageView
</button_icon>
ImageViw and button_icon overlaps but not that way I want.
80% portion of the button stays behind the image view that i cant see and the rest 20% of the button that goes beyond the image view is visible
This is for my Android studio app. I things above code is less time consuming.
If you need full code i can provide that too.
you can't exceed parents bounds. but you can introduce one extra layer which will hold both your Views and second one may cover part of first one. some basic example with FrameLayout
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<View
android:layout_width="100dp"
android:layout_height="100dp"
android:background="#097267">
<View
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_margin="40dp"
android:background="#A9C26A">
</FrameLayout>
also be aware that elevation and translationZ XML attributes may change order of drawing (first View will be drawn on top of second and will cover it). e.g. Button have some elevation set by default, also CardView (setCardElevation(float)) - additionally (mainly?) this makes some small shadow under these Views
Try this code... I have added constraint layout and inside it i have added button overlapping the image.
You can change the size according to your requirements !!
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<de.hdodenhof.circleimageview.CircleImageView
android:id="#+id/circleImageView4"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_marginTop="60dp"
android:src="#drawable/blue1"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.057"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<androidx.appcompat.widget.AppCompatButton
android:layout_width="60dp"
android:layout_height="40dp"
android:layout_marginTop="50dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.166"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="#+id/circleImageView4" />
</androidx.constraintlayout.widget.ConstraintLayout>

How to properly scale imported image to fit ImageView?

In my application I am importing and displaying many images. To set img uri I am using Img.setImageUri(R.mipmap.myimage) But the result is making image croped in device determined way for example on my phone i have square app icons and layout looks like this
so it's square shaped and on emulator which has circular icons it looks like this
so it's in shape of circle
What i want is to resize images to fit image views
Images are stored as uri in database so i NEED to get result based on that.
my xml for it
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView
android:layout_height="120dp"
android:id="#+id/foodlay"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:layout_marginRight="10dp"
xmlns:app="http://schemas.android.com/apk/res-auto"
app:cardElevation="10dp"
app:cardCornerRadius="20dp"
android:layout_width="120dp"
xmlns:android="http://schemas.android.com/apk/res/android">
<ImageButton
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerInside"
android:adjustViewBounds="true"
android:id="#+id/imago"
android:background="#null"
/>
</androidx.cardview.widget.CardView>
In code i just use Img.setImageUri(cursor.getString(6)) in adapter
i suggest to you use ShapeableImageView instead of this way, however if you want use your solution replace this
android:scaleType="centerCrop"
property instead of centerInside

How to resize images to fit in card view properly

Hello I'm trying to resize images to the size of the card view but some images fit properly and some of them don't I want that all the images fit into the card view properly
I have used all scale type but the best I found is center crop but still after using it some images doesn't fit properly
here is the XML code
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="1dp">
<com.google.android.material.card.MaterialCardView
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_centerInParent="true"
android:layout_marginStart="1dp"
android:layout_marginTop="11dp"
android:layout_marginEnd="1dp"
app:cardCornerRadius="13dp"
app:cardElevation="1dp"
app:cardMaxElevation="4dp">
<com.google.android.material.imageview.ShapeableImageView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/imageView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:adjustViewBounds="true"
android:contentDescription="#string/todo"
android:scaleType="centerCrop"
app:shapeAppearanceOverlay="#style/RoundedCorner" />
</com.google.android.material.card.MaterialCardView>
</RelativeLayout>
Inside card view use:
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/imageView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:contentDescription="#string/todo"
android:scaleType="centerCrop"
app:shapeAppearanceOverlay="#style/RoundedCorner" />
I dont think there's a built-in way to accomplish this.
Also "fit" suggests your target ImageView and the image you want to display on it, have the same width to height ratio. Otherwise, while you can "fit" it, the image will be squished or stretched in one dimension.
Once you confirm that, you can use one of the many image processing libraries to do this.
Popular ones are
Glide
Picasso
Coil
Both Glide and Picasso have very similar APIs, so if you pick one and later want to change to the other, it is very easy.
Checkout the FitCenter transformation for Glide. I think that's what you're looking for, however remember that if your widt to height ratio doesn't match, the image will look weird.
If you want Picasso instead, check this answer.

How do I make my android app scale properly accross different devices?

I'm quite new to android development and I'm currently making an app for a nine men's morris game and I'm using a custom layout xml file with 1 image view for the game board and then 23 other image views for all the different points on the board, all inside a constraint layout and then I use the tag to include this layout inside my main activity layout.
All the image views look perfectly aligned on a pixel 4 using the AVD but they dont look aligned when i test on older devices, so what do you guys think is the best way to make the image views align perfectly on all screen sizes? this pic shows how the layout looks and below i will post code for the first 4 image views
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/g_items">
<ImageView
android:id="#+id/the_board"
android:layout_width="376dp"
android:layout_height="374dp"
android:paddingEnd="15dp"
android:paddingStart="15dp"
android:scaleType="fitCenter"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#drawable/board" />
<ImageView
android:id="#+id/point_0"
android:onClick="onTileClicked"
android:layout_width="65dp"
android:layout_height="56dp"
android:layout_marginStart="4dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="#+id/the_board"
app:srcCompat="#drawable/empty_spot"/>
<ImageView
android:id="#+id/point_1"
android:onClick="onTileClicked"
android:layout_width="65dp"
android:layout_height="56dp"
android:layout_marginStart="104dp"
app:layout_constraintStart_toEndOf="#id/point_0"
app:layout_constraintTop_toTopOf="#+id/the_board"
app:srcCompat="#drawable/empty_spot" />
<ImageView
android:id="#+id/point_2"
android:onClick="onTileClicked"
android:layout_width="65dp"
android:layout_height="56dp"
android:layout_marginStart="104dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#id/point_1"
app:layout_constraintTop_toTopOf="#+id/the_board"
app:srcCompat="#drawable/empty_spot" />``
You need to use guideline widget with ConstraintLayout to tell your views when to stop when resizing(using app:layout_constraintGuide_percent property).
Google documentation
Set width and height of the ImageViews to 0, this way the layout will use constraints instead of the absolute values.
Also add all constraints to each view (Start, Top, End, Bottom).

set imageview in android-studio

Im trying to add 3 imageview with text, but i have problems with the image.
there coming some emty space under and over the image, so the image is been like a square.
And i have problem to change the other image and text.
It is like a layer around the image.
Like this:
This is my code.
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ImageView
android:id="#+id/imageView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#drawable/tuna" />
Set adjustViewBounds to true and mess around with the scale types.
This is happening because of your image file. It consists of transparent pixels.
you can either remove those using photoshop if you are adding images manually. or if you are getting that image from the server you try below answer to remove it via code. remove transparent pixels from bitmap
try to adjust the width and height of the image file using photo editor (ex. Adobe photoshop)

Categories

Resources