I am trying to change the image in an ImageView of a different layout. For this, I am using Layout Inflator, because I don't know any other way. The image gets visible when users click a button and according to the selected button, image gets visible. But this method is not working for me.
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:id="@+id/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/my_image"/>
</RelativeLayout>
And this is the code which I am using:
LayoutInflater inflater = (LayoutInflater)getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflater.inflate(R.layout.mylayout, null);
ImageView iv = (ImageView)v.findViewById(R.id.imageView);
iv.setImageResource(R.drawable.myImage2);
This is my service class:
@Override
public void onCreate() {
super.onCreate();
SharedPreferences sharedPreferences = this.getSharedPreferences("ON", Context.MODE_PRIVATE);
n_hi = sharedPreferences.getInt("N_HI",0);
n_wi = sharedPreferences.getInt("N_WI",0);
LayoutInflater inflater = (LayoutInflater)getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflater.inflate(R.layout.newLayout, null);
ImageView iv = (ImageView)v.findViewById(R.id.imageView);
iv.setImageResource(R.drawable.icon);
changeDesign();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
LocalBroadcastManager.getInstance(this).registerReceiver(broadcastReceiver,new IntentFilter("SizeChange"));
if (isPortrait) {
drawPortrait();
} else {
drawLandscape();
}
return Service.START_STICKY;
}
public void drawPortrait(){
if(Build.VERSION.SDK_INT < Build.VERSION_CODES.O){
if (d) {
overlay = WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY;
}else{
overlay = WindowManager.LayoutParams.TYPE_PRIORITY_PHONE;
}
} else {
overlay = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
}
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.P) {
flag_p = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE |
WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL |
WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE |
WindowManager.LayoutParams.FLAG_LAYOUT_INSET_DECOR |
WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_DEFAULT |
WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES |
WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN;
} else {
flag_p = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE |
WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE |
WindowManager.LayoutParams.FLAG_LAYOUT_INSET_DECOR |
WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN;
}
floatingView = View.inflate(getApplicationContext(),R.layout.newLayout, null);
final WindowManager.LayoutParams params = new WindowManager.LayoutParams(
n_wi,
n_hi,
overlay,
flag_p,
PixelFormat.TRANSLUCENT
);
params.gravity = Gravity.TOP | Gravity.CENTER;
windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
windowManager.addView(floatingView, params);
}
My app is working on a service so I used this code in the service class and also on the main activity but it didn't work. Also, I tried to run it on start instead of using it on button press but still didn't work for me.
I am also not sure is this the right approach to do this any suggestion is also great. I am new in programming. Also, suggest that the source image I am changing still stays the same because the app might get closed but by using service app will still show the image, so what is the best option for that.