My app background won't fill the screen - android

How do I make the background image fill the screen? I have tried several different suggestions and nothing seems to work.
Below is the code for my activity_main.xml:
<?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.andrewvanpeter.upandaway.MainActivity">
<ImageView
android:id="#+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="#drawable/background_flat_720x1280" />
</android.support.constraint.ConstraintLayout>

First solution :
You can put the layout_width and the layout_height of your ImageView to match_parent :
<ImageView
android:id="#+id/imageView2"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:srcCompat="#drawable/background_flat_720x1280" />
Second solution :
You can set the background directly in the ConstraintLayout (and don't forget to delete the ImageView) :
<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"
android:background="#drawable/background_flat_720x1280"
tools:context="com.andrewvanpeter.upandaway.MainActivity">

You can also to set the scaletype of imageview to fitXY like this :
<ImageView
android:id="#+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="fitXY"
app:srcCompat="#drawable/background_flat_720x1280" />

Related

Failed making scrollable a ConstraintLayout in an Android Fragment

Recently I tried to create a fragment and the LinearLayout is bigger than the screen and I need to use the ScrollView. I've tried different ways reading other questions in Stackoverflow and I couldn't resolve the issue.
This is my XML Code:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
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="wrap_content">
<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=".ui.configuration.ConfigurationFragment">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
tools:ignore="MissingConstraints">
<TextView
android:id="#+id/text_configuration_first_tittle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/configuration_first_title"
android:textSize="20sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
</ScrollView>
Any idea?
Resolved... The scrollview was working.
I only need to add marginBotton with enough height size to considere the Botton Navigation Bar

Android: Make an overlay match content height instead of expanding parent's height

I have the following code:
<?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">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/imageView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:srcCompat="#tools:sample/avatars[1]" />
<!-- This is the element of interest.
View is actually a custom element, but for this example, View has same behavior -->
<View
android:id="#+id/MyView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#A4D38D14" />
</FrameLayout>
</android.support.constraint.ConstraintLayout>
Which results in the following. Orange is the size of the View of interest. No-matter what I try, it seems I cannot get the View to not expand the parent.
What I really want, is for the View to scale to the same height as the Image like this:
How can this be achieved?
Note: I do not know the size of the Image..
Please replace the code and you are good to go.
<?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">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:ignore="MissingConstraints">
<ImageView
android:id="#+id/imageView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:srcCompat="#tools:sample/avatars[1]" />
<View
android:id="#+id/MyView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/imageView"
android:background="#A4D38D14" />
</RelativeLayout>
</android.support.constraint.ConstraintLayout>
The key is to specify android:layout_alignBottom on the View and change the frame layout to relative layout
If I understand your question correctly, you may also try this
<?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">
<ImageView
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:id="#+id/imageView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:srcCompat="#tools:sample/avatars[1]" />
<!-- This is the element of interest.
View is actually a custom element, but for this example, View has same behavior -->
<View
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/imageView"
app:layout_constraintBottom_toBottomOf="parent"
android:id="#+id/MyView"
android:layout_width="match_parent"
android:layout_height="0dp"
android:background="#A4D38D14" />
</android.support.constraint.ConstraintLayout>
I am posting this because as far as I am concerned ConstraintLayout is designed to use a single layout for the entire design,
I also faced a problem like you and figure out that the problem is generated for the <View> item. I fixed the problem by replacing the <View> with <Linearlayout>. Maybe <View> had a special characteristic that's why it is being overflown. My code snippet is -
<FrameLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<androidx.recyclerview.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<!-- This is the view intended to be the overlay-->
<LinearLayout
android:id="#+id/ll_overlay"
android:background="#color/colorAccent"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
</LinearLayout>
</FrameLayout>

How to overlay TextView on JavaCameraView in android

I want to overlay transparent TextView on JavaCameraView in Android like this picture. How can I do this? I tried to put TextView in JavaCameraView but it didn't work.
<org.opencv.android.JavaCameraView
android:id="#+id/activity_java_surface_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />
You should use ConstraintLayout or RelativeLayout. In those the views can be placed atop each other.
Try this:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
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">
<org.opencv.android.JavaCameraView
android:id="#+id/activity_java_surface_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"/>
<TextView
android:id="#+id/text_overlay"
android:layout_width="0dp"
android:layout_height="60dp"
android:text="test text"
android:background="#00000000"
app:layout_constraintBottom_toBottomOf="#id/activity_java_surface_view"
app:layout_constraintEnd_toEndOf="#id/activity_java_surface_view"
app:layout_constraintStart_toStartOf="#id/activity_java_surface_view"
/>

how can i get a image background with white contain space on it as shown below in screenshot

Here you can see that there is a background and above it there is a white layer on which contain are given. screenshot is of a website but i want to give that effect on my app. any help will be of great use.
You can do it like this:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
tools:context=".activity.MainActivity">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="#drawable/yourBackgroudImage"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:bacground="#color/white"
android:layout_margin="20dp">
<!--your code inside white backgroud layout goes here-->
</LinearLayout>
</LinearLayout>
Just use a RelativeLayout which helps you to stack your views above each other,then provide a margin to the top layout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="wrap_content">
<ImageView
android:src="#drawable/backgroundimage"
android:scaleType="centerCrop"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<LinearLayout
android:layout_margin="20dp"
android:background="#ffffff"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!--Add Data here as required-->
</LinearLayout>
</RelativeLayout>

Android image squashed

I have an image with a resolution of 1024x1024 pixels.
But, when I try to use this image in my Android app (background activity) it is squashed.
Can anyone tell me how to avoid this?
Now this issue has been fixed by using following code:
<RelativeLayout 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"
tools:context=".MainActivity">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerInParent="true"
android:scaleType="centerCrop"
android:src="#drawable/image" />
</RelativeLayout>
But this solution can not use for following code:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerInParent="true"
android:scaleType="centerCrop"
android:background="#drawable/frontscreenbg"/>
<ScrollView android:id="#+id/scrollviewParentLoginContainer"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:wheel="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<LinearLayout
android:id="#+id/linearLayoutLoginContainer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical"
android:paddingLeft="24dp"
android:paddingRight="24dp">
Please help me.
Don't set the image as the background of your root layout. Try this:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/relativeLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="#drawable/your_background_image"
android:scaleType="centerCrop"/>
...
/* Your other UI elements */
...
</RelativeLayout>
You can't set scale type in a RelativeLayout
You have to set the Scale Type option of your ImageView to CENTER_CROP (see here: http://developer.android.com/reference/android/widget/ImageView.ScaleType.html)
you can do it programmatically using the setScaleType() method of ImageView or adding it to your xml layout using android:scaleType="centerCrop"
EDIT:
If you want to add the background directly to your layout, you have to create a custom drawable object inside the /res/drawable directory. Try to use something like this:
<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
android:src="#drawable/background_image"
android:tileMode="repeat" />
Just use following code:
<RelativeLayout 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"
tools:context=".MainActivity">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerInParent="true"
android:scaleType="centerCrop"
android:src="#drawable/image" />
</RelativeLayout>
This should resolve your issue.

Categories

Resources