Note that there are some explanatory texts on larger screens.

plurals
  1. POimage view on Customized dialog box
    text
    copied!<p>I have one problem with customized dialog box in android,i have created the customized dialog using javainterface in android ,in my class CustomizeDialog i have one thread do some process in back ground after that i have set one imgae to this customized dialog i set the image in OnPostExecute but i got the following error</p> <pre><code>android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views. </code></pre> <p>i know we can't access the main ui from another thread but we access the main ui in OnPostExecuted in my case that also not working , in my another class(main.class) i will call this class by</p> <pre><code>CustomizeDialog customizeDialog = new CustomizeDialog(mContext); customizeDialog.show(); </code></pre> <p>this my code which create dialog box</p> <pre><code>public class CustomizeDialog extends Dialog implements OnClickListener { Button okButton; ImageView img = null; ImageView img2=null; ImageView img3 = null; ImageView img4= null; Bitmap bm; public CustomizeDialog(Context context) { super(context); img2 = (ImageView) findViewById(R.id.imageView1); img3 = (ImageView) findViewById(R.id.imageView3); img4 = (ImageView) findViewById(R.id.imageView4); WindowManager.LayoutParams params = getWindow().getAttributes(); params.x = 30; params.height = 500; params.width = 500; params.y = 50; this.getWindow().setAttributes(params); setContentView(R.layout.test); img = (ImageView) findViewById(R.id.imageView2); img.setBackgroundResource(R.drawable.sample); AnimationDrawable frameAnimation = (AnimationDrawable) img.getBackground(); //Start the animation (looped playback by default). frameAnimation.start(); new LongOperation1(context).execute(""); } else { Toast toast = Toast.makeText(getContext(), "Erorr", Toast.LENGTH_SHORT); toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0); toast.show(); } } private class LongOperation1 extends AsyncTask&lt;String, String, String&gt; { @Override protected String doInBackground(String... params) { //here i do some process return "Executed"; } @Override protected void onPostExecute(String result) { // here i do some process to get bm img2.setImageBitmap(bm); } @Override protected void onPreExecute() { } @Override protected void onProgressUpdate(String... values) { } } </code></pre> <p>AndroidHTMLActivity class</p> <pre><code> package com.example.hellogap; import com.example.javainterface.R; import android.app.Activity; import android.app.AlertDialog; import android.content.Context; import android.os.Bundle; import android.view.Gravity; import android.webkit.WebView; import android.widget.EditText; import android.widget.ImageView; import android.widget.TextView; import android.widget.Toast; public class AndroidHTMLActivity extends Activity { WebView myBrowser; private static final int TEXT_ID = 0; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); myBrowser = (WebView)findViewById(R.id.mybrowser); myBrowser.addJavascriptInterface(new MyJavaScriptInterface(this), "AndroidFunction"); myBrowser.getSettings().setJavaScriptEnabled(true); myBrowser.loadUrl("file:///android_asset/mypage.html"); } public class MyJavaScriptInterface { Context mContext; MyJavaScriptInterface(Context c) { mContext = c; } public void showToast(String toast){ Toast.makeText(mContext, toast, Toast.LENGTH_SHORT).show(); } public void openAndroidDialog(){ //CustomizeDialog.this.img2.setImageBitmap(bm); CustomizeDialog customizeDialog = new CustomizeDialog(AndroidHTMLActivity.this); customizeDialog.show(); } } } </code></pre> <p>test.xml </p> <pre><code> &lt;AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/widget0" android:layout_width="500dp" android:layout_height="520dp" android:background="@drawable/bg" android:clickable="false" &gt; &lt;ImageView android:id="@+id/imageView3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_x="193dp" android:layout_y="315dp" android:src="@drawable/fb" /&gt; &lt;ImageView android:id="@+id/imageView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_x="51dp" android:layout_y="318dp" android:src="@drawable/fa" /&gt; &lt;ImageView android:id="@+id/imageView4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_x="335dp" android:layout_y="316dp" android:src="@drawable/fc" /&gt; &lt;ImageView android:id="@+id/imageView2" android:layout_width="200dp" android:layout_height="204dp" android:layout_x="298dp" android:layout_y="91dp" /&gt; &lt;/AbsoluteLayout&gt; </code></pre> <p>main.xml</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" &gt; &lt;TextView android:layout_width="fill_parent" android:layout_height="wrap_content" /&gt; &lt;WebView android:id="@+id/mybrowser" android:layout_width="fill_parent" android:layout_height="fill_parent" /&gt; &lt;/LinearLayout&gt; </code></pre> <p>mypage.html</p> <pre><code>&lt;!DOCTYPE html&gt; &lt;html&gt; &lt;head&gt; &lt;meta charset="UTF-8"&gt; &lt;meta name="viewport" content="width=device-width; user-scalable=0;" /&gt; &lt;title&gt;My HTML&lt;/title&gt; &lt;/head&gt; &lt;body&gt; &lt;h1&gt;MyHTML&lt;/h1&gt; &lt;input type="button" value="Say hello" onClick="showAndroidToast('Hello Android!')" /&gt; &lt;script type="text/javascript"&gt; function showAndroidToast(toast) { AndroidFunction.showToast(toast); } &lt;/script&gt; &lt;input type="button" value="Open Dialog" onClick="openAndroidDialog()" /&gt; &lt;script type="text/javascript"&gt; function openAndroidDialog() { AndroidFunction.openAndroidDialog(); } &lt;/script&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>do any have idea about how to do this</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload