Home > Software engineering >  How to add Imageview on WebView in Final Android App
How to add Imageview on WebView in Final Android App

Time:09-17

As i am going to make Alerting App, I am using my website instead of XML code. But for going to my alert activity for web Activity, I made an intent which works onclick on my small image view.

It's my XML code Below:

activity_web.xml

<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".WebActivity">


    <WebView
        android:id="@ id/webview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"

        android:layout_centerInParent="true">


    </WebView>

    <ImageView
        android:id="@ id/imageView"
        android:layout_width="81dp"
        android:layout_height="75dp"
        android:layout_alignParentEnd="true"
        android:layout_alignParentBottom="true"

        tools:srcCompat="@drawable/bell_icon" />





</RelativeLayout>

But the main Problem is that imageview is not visible when I install's the app in my mobile.

It is also visible in my android Studio Layout.

You Check My Android Studio Layout here

Now tell me what should do for making visible it in final build apk which will work in my mobile.

CodePudding user response:

you are using wrong attribute namespace, I'm supprised that AS preview tool is showing this image, it shouldn't. but your ImageView is there on device, it is clickable (wehn you set so), it just have transparent content (wrongly resolved srcCompat attr, as below). as a test you may add android:background="#789810" tag, ImageView will show up with visible background (still without image)

in your code you have

tools:srcCompat="@drawable/bell_icon"

tools namespace points on "some tools", like used tools:context=".WebActivity". for setting custom attribute of particular View you have to use (custom) resources namespace, these are declared in your XML with xmlns:app="http://schemas.android.com/apk/res-auto" line, thus you should use below line

app:srcCompat="@drawable/bell_icon"

btw. if you are using srcCompat the you should also use AppCompatImageView. it works with usual ImageView only because AS project by default is exchanging all ImageViews to AppCompatImageViews during building project to APK/bundle. this behavior may be changed or disabled, then your ImageView will stay without any image set. fully proper line for setting image is default src attribute of ImageView, so:

android:src="@drawable/bell_icon"

android namespace in here as this attribute belong to framework ("built-in"), not some additional library and View (AppCompatImageView comes from AppCompat or AndroidX library)

CodePudding user response:

you can set it properly by using constraint layout, here is a code maybe it's work!

In this i set an ImageView at the top and webview is set at bottom of ImageView.

<androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:id="@ id/imageView"
        android:layout_width="match_parent"
        android:layout_height="180dp"
        android:background="@color/white"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintStart_toStartOf="parent"/>

    <WebView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:layout_constraintTop_toBottomOf="@ id/imageView"
        app:layout_constraintBottom_toBottomOf="parent"/>

</androidx.constraintlayout.widget.ConstraintLayout>
  • Related