Max
Max

Reputation: 1010

WebView in ListItem -> Item not clickable | Android

I already read it's a bad idea to use WebView's within a ListView, but I have to because I have formulae delivered in JavaScript.

Now I'm facing the problem that the WebView 'catches' the clicks, although it's set to notFocusable (nor xml either programmatically changes anything). As a result the ListItems are almost not clickable, since the WebView fills nearly the whole ListItem...

How do I get ListItems with WebView's clickable?

Upvotes: 2

Views: 1732

Answers (3)

Chi Minh Trinh
Chi Minh Trinh

Reputation: 296

In my case, I have to set

android:focusable="false" android:focusableInTouchMode="false"

in the WebView, then everything work fine. Hope it help.

Upvotes: 1

MFQ
MFQ

Reputation: 878

i solved it with this :

webView.setFocusable(false);

webView is taking the focuse from the listView

Upvotes: 0

dangalg
dangalg

Reputation: 6486

This webpage has a very good answer: http://jaydomagala.blogspot.co.il/2010/12/handling-onclick-in-listview-with.html

to sum up the code: Set up the webview to suit your needs

<linearlayout android:background="#ffffff" android:layout_height="wrap_content" android:layout_width="fill_parent" android:padding="4dip" xmlns:android="http://schemas.android.com/apk/res/android">

 <webview android:id="@+id/book_display" android:layout_height="fill_parent" android:layout_marginright="10dip" android:layout_width="75dip">  

<linearlayout android:background="#ffffff" android:layout_height="wrap_content" android:layout_width="fill_parent" android:orientation="vertical">
    <textview android:id="@+id/toptext" android:layout_height="wrap_content" android:layout_width="fill_parent" android:textcolor="#000000">
     <textview android:id="@+id/middletext" android:layout_height="wrap_content" android:layout_width="fill_parent" android:textcolor="#000000">
     <textview android:id="@+id/bottomtext" android:layout_height="wrap_content" android:layout_width="fill_parent" android:textcolor="#000000"> 
    <textview android:id="@+id/moreinfo_ext" android:layout_gravity="right" android:layout_height="wrap_content" android:layout_width="fill_parent" android:text="@string/more_info_text" android:textcolor="#000000">           
</textview></textview></textview></textview></linearlayout>

This is what a normal ListView click looks like:

bookListView.setOnItemClickListener(new OnItemClickListener() {    
  public void onItemClick(AdapterView parent, View view, 
    int position, long id) {

   Intent myIntent = new Intent(view.getContext(), BookDetailApp.class);

   //save serialized book object to pass to BookDetailApp
   Bundle bBooks = new Bundle();    
   currentBook = aBooks.get(position);
   bBooks.putSerializable("currentBook", currentBook);
   myIntent.putExtras(bBooks);

   startActivityForResult(myIntent, 0);        
  }  
 }); 

And the solution: Handling the Webview ontouch:

// Get a handle to our current WebView
    WebView wv = (WebView) row.findViewById(R.id.book_display);

    // Load it up with the properly formatted URL
    wv.loadUrl(url);

    // Set up its onTouchListener
    // This is essentially the same as an onClickListener
    // It just listens for more different types of input
    wv.setOnTouchListener(new View.OnTouchListener() 
    {
     // The definition of your actual onClick-type method
              public boolean onTouch(View v, MotionEvent event) 
              {
               // Switching on what type of touch it was
                  switch (event.getAction()) 
                  {
                   // ACTION_UP and ACTION_DOWN together make up a click
                   // We're handling both to make sure we grab it
                      case MotionEvent.ACTION_DOWN:
                      case MotionEvent.ACTION_UP:
                      {
                       // All of this is just like the previous Intent handler
// Do you own stuff here

                       Intent myIntent = new Intent(v.getContext(), BookDetailApp.class);

               //save serialized book object to pass to BookDetailApp
               Bundle bBooks = new Bundle();    
               currentBook = aBooks.get(bookListView.getPositionForView((View) v.getParent()));
               bBooks.putSerializable("currentBook", currentBook);
               myIntent.putExtras(bBooks);

               startActivityForResult(myIntent, 0);

               // The code will pause here until you exit the new Activity
               // It will then go back to what it was doing
               // In our case, waiting for more input
                          break;
                      }
                  }

                  // Returning false means that we won't be handling any other input
                  // Any un-handled gestures are tossed out
                  return false;
              }
          });

Upvotes: 2

Related Questions