Error when using Glide - android

The app crashes when I try to load an image with Glide.
I have the following code:
.java:
ImageButton home_ib1;
Glide.with(Home.this)
.load(R.drawable.moviep_24_hours_to_live)
.override(98, 155)
.centerCrop()
.placeholder(R.drawable.moviep_24_hours_to_live)
.into(home_ib1);
.xml:
<ScrollView
<RelativeLayout
<ImageButton
android:id="#+id/home_ib1"
android:layout_width="98dp"
android:layout_height="155dp"
android:layout_marginStart="16dp"
android:layout_marginTop="155dp"
android:background="#android:color/transparent"
android:scaleType="fitCenter"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
And when I try to run app, i got crash..(LogCat: You must pass in a non null View)
(The image's resolution that i have to load is 175x276 PNG)

ImageButton home_ib1;
Looks Like home_ib1 is null
Just add
home_ib1 = findViewById(R.id.home_ib1);

Related

Circularimage library display square image

I'm Using circulcar image view libary available on github. I also add it to build.gradle and sync it. but i am still not getting circular image. if I add SRC insted of background, still not getting result. where i am wrong?
<de.hdodenhof.circleimageview.CircleImageView
android:id="#+id/imageView"
android:layout_width="200dp"
android:layout_height="200dp"
android:background="#drawable/splash_screen"
app:civ_border_width="10dp"
app:civ_border_color="#FF000000"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView" />
remove this line : android:background="#drawable/splash_screen"
and Add this line : android:src="#drawable/splash_screen"
Just change the background line. It will show the circular image.
<de.hdodenhof.circleimageview.CircleImageView
android:id="#+id/imageView"
android:layout_width="200dp"
android:src="#drawable/splash_screen"
android:layout_height="200dp"
app:civ_border_width="10dp"
app:civ_border_color="#FF000000"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView" />
Image was displayed to the background that's why it was showing the square.

Android Glide Button background image

There are any was to use Glide with Button's background image?
I tried with code below, but get error for into(building1Button)
"cannot resolve method 'into(android.widget.Button)'
Can somebody help me how to use glide with button background image?
xml code:
<Button
android:id="#+id/building1Button"
android:layout_width="75dp"
android:layout_height="75dp"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginTop="5dp"
android:background="#drawable/building1pintest"
android:onClick="openbuildings1"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.109"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
mainactivity:
Button building1Button;
....
...
..
....onCreate.....
building1Button = findViewById(R.id.building1Button);
Glide.with(this).load(R.drawable.building1pintest).into(building1Button);

How can I have good quality when I get pictures?

I want to create user profile. I am using Firebase to make authentication and get profile picture but when I get profile picture, quality is not good.
I used Glide to load image
Glide.with(this)
.load(this.getCurrentUser().getPhotoUrl())
.apply(RequestOptions.circleCropTransform())
.into(imageViewProfile);
My Image view:
<ImageView
android:id="#+id/imageView"
android:layout_width="113dp"
android:layout_height="102dp"
android:layout_marginStart="8dp"
android:layout_marginTop="32dp"
android:layout_marginEnd="8dp"
android:adjustViewBounds="true"
android:scaleType="fitCenter"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
How can I fix it? I already try to use Picasso but that didn't work.
Instead of using RequestOptions.circleCropTransform(), try using CircleImageView.
Import this -
compile "de.hdodenhof:circleimageview:2.1.0"
in your layout file
<de.hdodenhof.circleimageview.CircleImageView
android:layout_width="#dimen/_80sdp"
android:layout_height="#dimen/_80sdp"
android:layout_centerHorizontal="true"
android:id="#+id/prof_pic"
app:civ_border_width="#dimen/_2sdp"
app:civ_border_color="#bf0000"
android:elevation="#dimen/_2sdp"
android:layout_marginTop="#dimen/_60sdp"
/>
and to load image
Glide.with(getActivity()).load("url").placeholder(R.drawable.bluehead).into(Prof_Pic);

Using Glide to load ImageView but still have a visual reference in the layout xml

I've been able to greatly improve my app's performance using Glide, but it means I've had to remove the srcCompat line from the ImageView in the layout xml.
However, this makes designing much harder, because now I can't reference anything in my Design tab when arranging my xml.
Does anyone have a solution to this? Working without being able to see the image in the xml is extremely inconvenient.
Example:
Before using Glide:
in my activity_main.xml:
<ImageView
android:id="#+id/image"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:adjustViewBounds="true"
app:layout_constraintEnd_toStartOf="#id/rightNarrowGuideline"
app:layout_constraintStart_toEndOf="#id/leftNarrowGuideline"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#drawable/money_coins" />
<Button
android:id="#+id/example"
style="#style/BigRedButton"
android:layout_width="0dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="50dp"
android:text="EXAMPLE"
app:layout_constraintEnd_toEndOf="#id/rightNarrowGuideline"
app:layout_constraintStart_toStartOf="#id/leftNarrowGuideline"
app:layout_constraintTop_toBottomOf="#+id/image" />
and it looks like this in the Design tab.
After using Glide:
Now I load the image in my MainActivity.java with
ImageView mainImage = findViewById(R.id.image);
Glide.with(this).load(R.drawable.money_coins).into(mainImage);
and I've removed this line:
app:srcCompat="#drawable/money_coins"
from my activity_main.xml, so I'm left with this:
<ImageView
android:id="#+id/image"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:adjustViewBounds="true"
app:layout_constraintEnd_toStartOf="#id/rightNarrowGuideline"
app:layout_constraintStart_toEndOf="#id/leftNarrowGuideline"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="#+id/example"
style="#style/BigRedButton"
android:layout_width="0dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="50dp"
android:text="EXAMPLE"
app:layout_constraintEnd_toEndOf="#id/rightNarrowGuideline"
app:layout_constraintStart_toStartOf="#id/leftNarrowGuideline"
app:layout_constraintTop_toBottomOf="#+id/image" />
And now my Design tab looks like this.
If I understand correctly you want to see your image on preview. Well I guess you could set tools:srcCompat="#drawable/money_coins", which basically sets a "placeholder" image for preview. But by doing this you would need to update drawable on two places.
Dunno exactly if you're also changing your image, but for static image use, directly assigning src in layout should be fine performance wise. Unless your drawable image is like huge. If that is the case you should properly resize it or even better, use vector drawable resource. I'm guessing glide downsamples image for you, hence making it load faster.

Glide sometimes takes the whole Layout

Glide's behavior is unpredectable sometimes, while using the app sometimes the ImageView looks like this:
Just going back (to the parent activity) and come back, without restarting the app, to the same movie result, it appears fixed (supposed appearance) like this:
I don't why I have this behavior, here the xml code for the image view:
<ImageView
android:elevation="4dp"
android:src="#drawable/poster_sample"
android:id="#+id/poster_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginLeft="24dp"
android:layout_marginStart="14dp"
android:scaleType="centerCrop"/>
Glide code:
Glide
.with(getContext())
.load(mMovieValues.get(MovieContract.MovieEntry.COL_POSTER_URL))
.centerCrop()
.into(mPoster);
Where is the problem? and how to fix that?
You should give a fixed height and width to your ImageView and no need to use centerCrop
<ImageView
android:elevation="4dp"
android:src="#drawable/poster_sample"
android:id="#+id/poster_image"
android:layout_width="250dp"
android:layout_height="150dp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginLeft="24dp"
android:layout_marginStart="14dp"/>
and remove CenterCrop from here too
Glide
.with(getContext())
.load(mMovieValues.get(MovieContract.MovieEntry.COL_POSTER_URL))
.into(mPoster);

Categories

Resources