-3

I want to change the color of ImageView from my Service class. But I don't know how to access ImageView from service class. This is my layout file:

<?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="match_parent"
    android:orientation="vertical">

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scaleType="fitXY"
        android:layout_alignParentStart ="true"
        android:layout_centerVertical = "true"
        android:src="@drawable/batman_r"/>

</RelativeLayout>

I want to change the color of ImageView from my Service class. But I don't know how to access ImageView from service class. I just want to getImageView and get drawable from it and set its Tint to my desirable color. Please he

3 Answers3

0

you shouldn't and in fact you can't change UI from Service, it's not UI thread... you have to make own custom implementation of bounding Service, then send apriopiate message from Service to Activity, which can make visual changes (in UI thread). some tutorial in here

another way is to use LocalBroadcastManager and send broadcast from Service, listen for it in Activity (register/unregister receiver in lifecycle), then change UI. article about in here

snachmsm
  • 17,866
  • 3
  • 32
  • 74
  • ok, thanks for the answer, can you please tell me if the same thing I want to do from Main Activity class, how can I do that. As you can see in my layout I haven't given any id to my ImageView so how can I access it using layout name. – varish ali Sep 11 '19 at 07:05
  • my app has more than 15 layouts so I haven't given any layout ImageView any id. But I want to change the color of them on users preference and one layout only get visible once for a user. layouts are like design which can be changed on the preference of users. – varish ali Sep 11 '19 at 07:10
  • you should give unique ids for these `ImageView`s (and other `View`s which you want to access), then use `findViewById` in `Activity`. thats simplest way to get access to inflated views – snachmsm Sep 11 '19 at 07:13
  • can you please look at my this stack question and help me In it. Sorry for bothering you but I really need help now. – varish ali Sep 18 '19 at 04:48
0

You cannot directly access your layouts/views from a Service. To achieve this, your activity should have a way to communicate with your service and vice-versa. From the service, you will have to trigger your activity's methods/code to update the color/tint of your image view.

There are several ways to setup communication between your service and activities. A simple way to do this would be by using a BroadcastReceiver in your activity, which can be triggered by your Service by creating a broadcast.

Kumar Bibek
  • 9,016
  • 2
  • 39
  • 68
0

For communication between Service and Activity you can use event bus lib, Please check below link event bus

Emad Seliem
  • 608
  • 1
  • 4
  • 5