Payday Loan Payday Loan

Archive for February, 2012

How to cast from View to WebView in Android

I have faced a really idiot problem, and i want to share it with you.
I tried to make a very simple webview activity like below:

 
package com.demoapp;
 
import android.app.Activity;
import android.os.Bundle;
import com.demoapp.R;
import android.webkit.WebView;
public class WebView extends Activity {
 
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.webview);
		Bundle data = getIntent().getExtras();
 
		WebView webview1 = (WebView)findViewById(R.id.webView1);
 
        webview1.getSettings().setJavaScriptEnabled(true); 
        webview1.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
 
        webview1.getSettings().setBuiltInZoomControls(true);
	    webview1.setInitialScale(50);
        webview1.loadUrl(data.getString("webview")); 
 
	}
 
}

This is looking perfect, but the Eclipse dont let you compile this.
You will get error: cannot cast from View to WebView.
You need to observe that the import android.webkit.WebView; cannot be fullfiled.
WHY? Because you have setted the class name same. As you see my class name is WebView.
This generates a conflict, between imported class WebView, and your class called WebView, so just change the name of your class to something like WebView1 and you are done.

||||| 0 I Like It! |||||

Today i will show you how you can handle unexpected changes of screen.
So if you want to know when the user changed the screen orientation, and to handle it, this is the code to manage.
This code must be added in a class that extends Activity.

 
 
    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
      if(newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE){
         //DO THE CODE FOR LANDSCAPE MODE
}
}
||||| 1 I Like It! |||||

If you want to override all this button and make them act as you wish, then you need to add this code in your class that extends Activity.

@Override
public boolean onKeyDown(int keyCode, KeyEvent event)  {
    if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
        // do something on back.
        //instead of KEYCODE_BACK you can choose whatever button you want to w
        return true;
    }
 
    return super.onKeyDown(keyCode, event);
}
||||| 0 I Like It! |||||

Search

Popular

Sponsors