user45678
user45678

Reputation: 1514

How to open WebView in Popup Window in AsyncTask

Editing code as per Armaan suggestion : Calling ImageView inside onPostExecute crashing application

Details : If i put close button and call its onClickListener inside onPostExecute it would crash, not sure what could be reason :

public class Async extends Activity {

/** Called when the activity is first created. */

ListView _rssFeedListView;
List<JSONObject> jobs;
List<RssFeedStructure> rssStr;
private BlogAdapter _adapter;
TextView textview;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.listview);
    _rssFeedListView = (ListView) findViewById(R.id.rssfeed_listview);

    textview = (TextView) findViewById(R.id.loading);
    RssFeedTask rssTask = new RssFeedTask();
    rssTask.execute();

}


private class RssFeedTask extends AsyncTask<String, Void, String> {

    String response = "";
    PopupWindow popupWindow;
    ImageView image; 


    @Override
    protected void onPreExecute() {
    }

    @Override
    protected String doInBackground(String... urls) {
        try {
            String feed = "http://someurl";
            XmlHandler rh = new XmlHandler();
            rssStr = rh.getLatestArticles(feed);
        } catch (Exception e) {
        }
        return response;

    }

    @Override
    protected void onPostExecute(String result) {
        if (rssStr != null) {

            _adapter = new BlogAdapter(Async.this, rssStr);
            _rssFeedListView.setAdapter(_adapter);
            textview.setVisibility(View.INVISIBLE);

            _rssFeedListView.setOnItemClickListener(new OnItemClickListener() {

                        @Override
                        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                            LayoutInflater layoutInflater = (LayoutInflater)getBaseContext().getSystemService(LAYOUT_INFLATER_SERVICE);  
                            View layout = layoutInflater.inflate(R.layout.new_popup_layout, null); 

                            popupWindow = new PopupWindow(layout, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
                            WebView web = (WebView)layout.findViewById(R.id.webView1);
                            image = (ImageView) findViewById(R.id.closebutton);

                            web.getSettings().setJavaScriptEnabled(true);
                            web.getSettings().setLoadWithOverviewMode(true);
                            web.getSettings().setDefaultFontSize(10);
                            web.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
                            web.setScrollbarFadingEnabled(true);

                            String text = "<html><body style=\"text-align:justify\"> %s </body></Html>";
                            String summary = rssStr.get(position).getEncodedContent();

                            Log.d("String", summary);
                            web.loadDataWithBaseURL(null, String.format(text, summary), "text/html", "UTF-8",null);
                            popupWindow.showAtLocation(view,Gravity.CENTER, 0, 0);

                            image.setOnClickListener(new OnClickListener() {
                            public void onClick(View v)
                     {
                            popupWindow.dismiss(); 
                     } 
             });

                      }
            });



        }

    }

}

}

Upvotes: 2

Views: 4136

Answers (2)

Armaan Stranger
Armaan Stranger

Reputation: 3130

Try this:

Create Popupwindow Like this

LayoutInflater layoutInflater = (LayoutInflater)getBaseContext().getSystemService(LAYOUT_INFLATER_SERVICE);  
            View layout = layoutInflater.inflate(R.layout.new_popup_layout, null);  
            final PopupWindow popupWindow = new PopupWindow(
                    layout, 
                       LayoutParams.WRAP_CONTENT,  
                             LayoutParams.WRAP_CONTENT);

WebView web = (WebView)layout.findViewbyId(R.id.webView1);
popupWindow.showAtLocation(view,Gravity.CENTER, 0, 0);

and create new_popup_layout layout file with webview like this:

         <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/transparent"
    android:orientation="vertical" >

    <TableRow
        android:id="@+id/tableRow1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="right" >

        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@android:drawable/btn_dialog" />
    </TableRow>

    <WebView
        android:id="@+id/webView1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center"
        android:layout_margin="10dp" />

</LinearLayout>

Hope it Helps!!

Upvotes: 1

Onur A.
Onur A.

Reputation: 3017

You can use custom dialogs, all you need to do is in your custom dialog's layout file put a WebView and then in your code give a source URL to your WebView according to the selected item in your ListView. For how to make a custom dialog take a look at here http://www.mkyong.com/android/android-custom-dialog-example/

Upvotes: 0

Related Questions