Sinh Phan
Sinh Phan

Reputation: 1266

OnClickListener for the screen layout webview

I want to get the click event when the user onClickListener by clicking on the screen. I have a layout as follows:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/layout_content">

    <WebView
        android:id="@+id/webview_item"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</RelativeLayout>

I tried to do this by the layout Relative setOnClickListener:

RelativeLayout layoutContent = (RelativeLayout) findViewById(R.id.layout_content);
layoutContent.setOnClickListenner(new OnClickListener() {
    @Override
    public void onClick(View v) {
        ......
    }
});

I can get good click event until webview finished loading, I can not get any more clicklistener. So does anyone have any suggestions for me to be able to get onClicklistener after webview loaded?

Upvotes: 0

Views: 4849

Answers (2)

PREM MOHAN
PREM MOHAN

Reputation: 1

webView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                Intent intent;
                ActivityModel activityModel = new ActivityModel();
                activityModel.setServerId(activityId);
                if (action.equals("discussion")) {
                    intent = new Intent(BannerPopUp.this, DiscussionActivity.class);
                    intent.putExtra("activityModel", activityModel);
                } else {
                    intent = new Intent(BannerPopUp.this, DisplayActivity.class);
                    intent.putExtra(Constants.MODEL_ACTIVITY, activityModel);
                    if (action.equals("join")) {
                        intent.putExtra(Constants.ACTION, "join");
                    }
                }
                if (!isAppRunning) {
                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                }
                startActivity(intent);
                finish();
                return false;`
            }
        });

Upvotes: 0

Prokash Sarkar
Prokash Sarkar

Reputation: 11873

According to this answer,

WebView doesn't seem to send click events to an OnClickListener

For this you need to implement the onTouchListener event.

public class WebViewClicker extends Activity implements OnTouchListener, Handler.Callback {

private static final int CLICK_ON_WEBVIEW = 1;
private static final int CLICK_ON_URL = 2;

private final Handler handler = new Handler(this);

private WebView webView;
private WebViewClient client;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.web_view_clicker);

    webView = (WebView)findViewById(R.id.web);
    webView.setOnTouchListener(this);

    client = new WebViewClient(){ 
        @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { 
            handler.sendEmptyMessage(CLICK_ON_URL);
            return false;
        } 
    }; 

    webView.setWebViewClient(client);
    webView.setVerticalScrollBarEnabled(false);
    webView.loadUrl("http://www.example.com");
}

@Override
public boolean onTouch(View v, MotionEvent event) {
    if (v.getId() == R.id.web && event.getAction() == MotionEvent.ACTION_DOWN){
        handler.sendEmptyMessageDelayed(CLICK_ON_WEBVIEW, 500);
    }
    return false;
}

@Override
public boolean handleMessage(Message msg) {
    if (msg.what == CLICK_ON_URL){
        handler.removeMessages(CLICK_ON_WEBVIEW);
        return true;
    }
    if (msg.what == CLICK_ON_WEBVIEW){
        Toast.makeText(this, "WebView clicked", Toast.LENGTH_SHORT).show();
        return true;
    }
    return false;
}
}

Upvotes: 2

Related Questions