1

I need to load a random image, according to the outcome and based on that I need to load a certain HTML file. Can I link them through naming?

When I load a drawable house.jpg it has to link dynamically with house.html.

(The image and HTML file have to be separate. So no need to say you can put everything in one HTML file.)

Sebastian Simon
  • 18,263
  • 7
  • 55
  • 75
Marthy_Mc_Fly
  • 93
  • 1
  • 7
  • 1
    for this, you can use Android Assets folder. for example: save your house.html in the assets folder and set click event to ImageView and open your .html file in next Activity which has WebView in order to parse an HTML. – Parth Patel Sep 06 '18 at 06:54
  • This I have and its working. But now I need the WebView to load the right html. For example when my ImageButton loads **garden.jpg** the click event has to open an activity with **garden.html**. When the ImageButton shows **house.jpg** then the WebView needs to open **house.html** – Marthy_Mc_Fly Sep 06 '18 at 07:08
  • That means now you want to implement it dynamically if I'm not wrong. Then you can use ArrayList of image name and pass image name on the click of an image. – Parth Patel Sep 06 '18 at 07:19
  • Yes that's what i'm aiming for. I'll check into it tonight. Could be that I come back on your comment. – Marthy_Mc_Fly Sep 06 '18 at 07:40
  • You can try it yourself. If you would be stuck somewhere! you can contact me. We will check it out. – Parth Patel Sep 06 '18 at 07:54

1 Answers1

0

You can only do such a thing if our image is inside your /assets folder. Also, you must load your html with a baseUrl that's inside your assets folder

You can use WebView.loadUrl() or WebView.loadDataWithBaseURL():

webView.loadUrl("file:///android_asset/file.html");

or

webView.loadDataWithBaseURL("file:///android_asset/", "<img src='file.jpg' />", "text/html", "utf-8", null);

(file.jpg should be inside your assets folder)

To compare which button :

Use getDrawable() method in ImageButton and compare them using .getConstantState().equals()

Sample code:

ImageButton btn = (ImageButton) findViewById(R.id.myImageBtn);
Drawable drawable = btn.getDrawable();
if (drawable.getConstantState().equals(getResources().getDrawable(R.drawable.myDrawable).getConstantState())){
   //Do your work here
}

References:

Get drawable of image button

sajad zohrei
  • 377
  • 1
  • 10
  • 1
    But the html that is loaded should be different when the Image from an image button changes. So it has to listen to the file name. For example when my ImageButton loads **garden.jpg** the click event has to open an activity with **garden.html**. When the ImageButton shows **house.jpg** then the WebView needs to open **house.html** – Marthy_Mc_Fly Sep 06 '18 at 07:23
  • Works thanks. the only problem is that my list of drawables is huge. So I would end up with an insane amount of lines. I tried working with integers but I can't figure that out. – Marthy_Mc_Fly Sep 06 '18 at 15:36