twitter url can not open in android webview - android

I m beginner in android.
i want to open twitter url in webview. i have a twitter url(http://twitter.com/..). i try to this code but can not open in webview.
code:
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress)
{
activity.setTitle(" Loading...");
activity.setProgress(progress * 100);
if(progress == 100){
activity.setTitle(R.string.app_name);
//view.canGoBack();
}
}
});
webView.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url){
view.loadUrl(url);
view.canGoBack();
return true;
}
});
//The URL that webview is loading
webView.loadUrl(uri);
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(event.getAction() == KeyEvent.ACTION_DOWN){
switch(keyCode)
{
case KeyEvent.KEYCODE_BACK:
if(webView.canGoBack() == true){
webView.goBack();
}else{
finish();
}
return true;
}
}
return super.onKeyDown(keyCode, event);
}
help me.....

I got solution in my Question
webView.getSettings().setUserAgentString("Mozilla/5.0 (Linux; U; Android 2.0; en-us; Droid Build/ESD20) AppleWebKit/530.17 (KHTML, like Gecko) Version/4.0 Mobile Safari/530.17");

try this:
Uri uri = Uri.parse(Place your URL here);
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);

Hope this Helps:
DisplayWeb.java:
public class DisplayWeb extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.web);
String url = "http://www.twitter.com";
System.out.println("++++URL GOT = "+url);
WebView myWebView = (WebView) findViewById(R.id.webview);
myWebView.getSettings().setJavaScriptEnabled(true);
myWebView.getSettings().setBuiltInZoomControls(true);
myWebView.getSettings().setSupportZoom(true);
myWebView.loadUrl(url);
Button back = (Button) findViewById(R.id.back);
back.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
DisplayDirections.this.finish();
}
});
}
}
web.xml:
<?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">
<WebView
android:id="#+id/webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</RelativeLayout>
Hope this help!

I had the same issue and have been doing some searching to look for possible answers to this problem.
Hacking the Browser Agent might work but it's not really a good idea.
The solution given in this post also worked for me and I think is a better solution: Problems loading mobile.twitter in webview
Hope that helps.

Related

loading different urls via WebView settings

I have a WebView app which loads a url, I want to create a Settings Activity where user can customize settings..
My que is, How to make a RadioButton or Chooser which when clicked will open the Mobile version of Site, Similarly if Desktop Ver. is clicked, webview will load Desktop version...
By default webview loads mobile site, if you want to force load desktop site you can use
String newUA= "Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.4)Gecko/20100101 Firefox/4.0";
mWebView.getSettings().setUserAgentString(newUA);
use radio button to choose this settings. Use isChecked() to check radio button else load normal webview.
You can try code below
if(rbtnMobile.isChecked){
webview.getSettings().setJavaScriptEnabled(true);
webview.getSettings().setUserAgentString("Mozilla/5.0 (iPhone; U; CPU like Mac OS X; en) AppleWebKit/420+ (KHTML, like Gecko) Version/3.0 Mobile/1A543a Safari/419.3");
  webview.loadUrl(url);
}else{
webview.loadUrl(url);
}
Below is WebView code.
public class WebViewLinks extends Activity {
WebView webView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.web_view);
String Url = getIntent().getExtras().getString("Url");
webView = (WebView) findViewById(R.id.webView);
webView.setWebViewClient(new MyWebClient());
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
webView.loadUrl(Url);
}
public class MyWebClient extends WebViewClient {
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
}
You can use below code on button click or radio button selection.
Intent webViewIntent = new Intent(this, WebViewLinks.class);
webViewIntent.putExtra("Url", yourURL);
startActivity(webViewIntent);
see below example.
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
if(checkedId == R.id.mobileview){
Intent webViewIntent = new Intent(this, WebViewLinks.class);
webViewIntent.putExtra("Url", mobileURL);
startActivity(webViewIntent);
}else if(checkedId == R.id.desktopview){
Intent webViewIntent = new Intent(this, WebViewLinks.class);
webViewIntent.putExtra("Url", deskTopURL);
startActivity(webViewIntent);
}
}
});

Android WebView error for high density screens

I have an Android application which uses a WebView to render web page. In css I have set to show everything twice as big for high density screens (like Nexus 7 2013). If I open the web page from the browser, Everything shows as it should. But when I run the app, everything is very small.
Is there any way to determine why the application WebView shows content for lower density screens but browser (Chrome) shows as it should!
Device: Nexus 7 2013
// I used this class and my code is working fime at my side please try this may be it will help you
public class WebViewActivity extends Activity {
private WebView webview;
private static final String TAG = "Main";
private ProgressDialog progressBar;
private TextView header_maintext;
private TextView headeroptiontext;
private RelativeLayout back;
private String url_string="http://www.google.com";
private String header_maintext_string="Your text";
/** Called when the activity is first created. */
#SuppressLint("SetJavaScriptEnabled") #Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.webview_layout);
webview = (WebView)findViewById(R.id.webview01);
header_maintext= (TextView)findViewById(R.id.header_maintext);
header_maintext.setText(header_maintext_string);
headeroptiontext = (TextView)findViewById(R.id.headeroptiontext);
headeroptiontext.setVisibility(View.GONE);
WebSettings settings = webview.getSettings();
settings.setJavaScriptEnabled(true);
webview.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
webview.getSettings().setLoadWithOverviewMode(true);
webview.getSettings().setUseWideViewPort(true);
back = (RelativeLayout) findViewById(R.id.back_layout);
back.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0)
{
// TODO Auto-generated method stub
if(webview.canGoBack() == true)
{
webview.goBack();
}
else
{
finish();
}
}
});
final AlertDialog alertDialog = new AlertDialog.Builder(this).create();
progressBar = ProgressDialog.show(WebViewActivity.this, "My application", "Loading...");
webview.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
public void onPageFinished(WebView view, String url) {
Log.i(TAG, "Finished loading URL: " +url);
if (progressBar.isShowing()) {
progressBar.dismiss();
}
}
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
Toast.makeText(WebViewActivity.this, "Oh no! " + description, Toast.LENGTH_SHORT).show();
alertDialog.setTitle("Error");
alertDialog.setMessage(description);
alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
return;
}
});
alertDialog.show();
}
});
webview.loadUrl(url_string);
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(event.getAction() == KeyEvent.ACTION_DOWN){
switch(keyCode)
{
case KeyEvent.KEYCODE_BACK:
if(webview.canGoBack() == true){
webview.goBack();
}else{
finish();
}
return true;
}
}
return super.onKeyDown(keyCode, event);
}
}
I think this article should help explain what you need to do: https://developers.google.com/chrome/mobile/docs/webview/pixelperfect
I suspect that the difference between Chrome and WebView boils down to the WebSettings that are being applied to the WebView.

1)Difference between setOnClickListener and onclick 2) how to only reload webview w/o reloading others in the same linearlayout

My application has a WebView on top. For showing things, I use Toast. But the Toast box has never shown if WebView appears, while before WebView shows, Toast is visible. I was wondering if Toast was likely covered by WebView. Did anyone have the same problems?
Thanks.
EDITING!!!
Hi, thank you for all your inputs. I found I posted a wrong question for my problems. I found that the real problems are the following. In the code, I use setOnClickListener to register a callback for the button. In debugging, I found that the callback is not called for some reason, so the Toast statement is not called, and it is not covered by webview.
Then, I tried the onclick attributes in xml layout for defining a callback clickGo for the button. This one works when I press the button, and the Toast shows.
Now, my question is what is the difference between setOnClickListener and onlick.
One more question, in my clickGo, I refresh the webview. The webview indeed reloads when button is clicked. But in the meanwhile the spinner is also reloaded, and the selection position is reset to the 0-th. How can I prevent this?
Thank you again!
public class MainActivity extends Activity {
JSONArray jArray;
String result = null;
InputStream is = null;
StringBuilder sb=null;
private Spinner spinner;
private Button btnSubmit;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addListenerOnButton();
addListenerOnSpinnerItemSelection();
new DownloadTask().execute("www.google.com");
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
// Given a string representation of a URL, sets up a connection and gets
// an input stream.
private String downloadUrl(String urlString) throws IOException {
HttpClient httpClient = new DefaultHttpClient();
HttpGet get = new HttpGet(urlString);
HttpResponse response = httpClient.execute(get);
// Build up result
return EntityUtils.toString(response.getEntity());
}
public void addListenerOnSpinnerItemSelection() {
spinner = (Spinner) findViewById(R.id.spinner);
spinner.setOnItemSelectedListener(new CustomOnItemSelectedListener());
}
public void clickGo(View v) {
Toast toast = Toast.makeText(MainActivity.this,
"OnClickListener : " +
"\nSpinner 1 : "+ String.valueOf(spinner.getSelectedItem()),
Toast.LENGTH_SHORT);
toast.setGravity(Gravity.TOP, 0, 0);
toast.show();
new DownloadTask().execute("www.google.com");
}
// get the selected dropdown list value
public void addListenerOnButton() {
spinner = (Spinner) findViewById(R.id.spinner);
btnSubmit = (Button) findViewById(R.id.button);
btnSubmit.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Toast toast = Toast.makeText(MainActivity.this,
"OnClickListener : " +
"\nSpinner 1 : "+ String.valueOf(spinner.getSelectedItem()),
Toast.LENGTH_SHORT);
toast.setGravity(Gravity.TOP, 0, 0);
toast.show();
new DownloadTask().execute("www.google.com");
}
});
}
`
// Implementation of AsyncTask used to download XML feed from stackoverflow.com.
private class DownloadTask extends AsyncTask<String, Void, String> {
#Override
protected String doInBackground(String... urls) {
return downloadUrl(urls[0]);
}
#Override
protected void onPostExecute(String result) {
setContentView(R.layout.activity_main);
// Displays the HTML string in the UI via a WebView
WebView myWebView = (WebView) findViewById(R.id.webview);
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
myWebView.loadUrl(result);
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView android:id="#+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/word" />
<Button android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Go"
android:onClick="clickGo" />
</LinearLayout>
<Spinner android:id="#+id/spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:entries="#array/dictionaries"
android:prompt="#string/dict_prompt" />
<WebView android:id="#+id/webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
I think this question could fix your question.
if this is could be shown, then I think toast should not be covered by WebView.
onPageFinished() never called (webview)!
mWebView.setWebViewClient(new WebViewClient() {
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(mWebView, url);
Toast.makeText(getApplicationContext(), "Done!", Toast.LENGTH_SHORT).show();
}
#Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
Toast.makeText(getApplicationContext(), "Oh no! " + description, Toast.LENGTH_SHORT).show();
}
});
mWebView.loadUrl("http://pabebbe.com/m/register");
For this we need to add a webviewclient in android for the WebView we are adding in the xml.
Then we have to register the webviewclient with the WebView we are creating using this method.
myWebView.setWebViewClient(new MyWebViewClient());
Now,Create a new project and name it WebViewDemo.
In main.xml,
<?xml version="1.0" encoding="utf-8"?>
<WebView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
Now create a file named test.html inside the assets folder and copy this code into it. This is the html file that we are loading into the webview.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML Basic 1.1//EN"
"http://www.w3.org/TR/xhtml-basic/xhtml-basic11.dtd">
<html>
<body>
Google
<input type="button" value="Click Me" onClick="showAndroidToast('Hello Google!')"
/>
<script type="text/javascript">
function showAndroidToast(toast) {
Android.showToast(toast);
}
</script>
</body>
</html>
Now in the java code refer the webview and load the html file
WebView myWebView;
myWebView = (WebView) findViewById(R.id.webview);
myWebView.loadUrl("file:///android_asset/test.html");
Now enable the javascript by calling this function
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
Now we have to add the javascript interface for listening to the javascript functions that we define in the webview html file.
myWebView.addJavascriptInterface(new JavaScriptInterface(this), "Android");
// outside oncreate
public class JavaScriptInterface {
Context mContext;
/** Instantiate the interface and set the context */
JavaScriptInterface(Context c) {
mContext = c;
}
/** Show a toast from the web page */
public void showToast(String toast) {
Toast.makeText(mContext, toast, Toast.LENGTH_SHORT).show();
startActivity(new Intent(WebViewDemo.this, WebViewDemo.class));
}
}
Now create a webview client for listening to the browser activities and doing specific function.
myWebView.setWebViewClient(new MyWebViewClient());
private class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (Uri.parse(url).getHost().equals("www.coderzheaven.com")) {
Toast.makeText(getApplicationContext(), "www.coderzheaven.com",
Toast.LENGTH_SHORT).show();
return false;
}
// Otherwise, the link is not for a page on my site, so launch another Activity
that handles URLs
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
return true;
}
}
Now we listen to the backbutton.
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// Check if the key event was the BACK key and if there's history
if ((keyCode == KeyEvent.KEYCODE_BACK) && myWebView.canGoBack()) {
myWebView.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
Now the project is over . Now run and see the result.
Here is the full java code for this example
package com.coderzheaven.pack;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.KeyEvent;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;
public class WebViewDemo extends Activity {
WebView myWebView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
myWebView = (WebView) findViewById(R.id.webview);
myWebView.loadUrl("file:///android_asset/test.html");
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
myWebView.addJavascriptInterface(new JavaScriptInterface(this), "Android");
// myWebView.setWebViewClient(new WebViewClient());
myWebView.setWebViewClient(new MyWebViewClient());
}
public class JavaScriptInterface {
Context mContext;
/** Instantiate the interface and set the context */
JavaScriptInterface(Context c) {
mContext = c;
}
/** Show a toast from the web page */
public void showToast(String toast) {
Toast.makeText(mContext, toast, Toast.LENGTH_SHORT).show();
startActivity(new Intent(WebViewDemo.this, WebViewDemo.class));
}
}
private class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (Uri.parse(url).getHost().equals("www.google.com")) {
// This is my web site, so do not override; let my WebView load the page
Toast.makeText(getApplicationContext(), "www.google.com",
Toast.LENGTH_SHORT).show();
return false;
}
// Otherwise, the link is not for a page on my site, so launch another Activity
that handles URLs
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
return true;
}
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// Check if the key event was the BACK key and if there's history
if ((keyCode == KeyEvent.KEYCODE_BACK) && myWebView.canGoBack()) {
myWebView.goBack();
return true;
}
// If it wasn't the BACK key or there's no web page history, bubble up to the
default
// system behavior (probably exit the activity)
return super.onKeyDown(keyCode, event);
}
}
no toast are drawn with maximum draw order as they are created during runtime.. make sure where are you creating your toast and if its really called.
EDIT
if your problem was about draw order try to use this code
private void moveViewToFront(View currentView)
{
ViewGroup vg = ((ViewGroup) currentView.getParent());
vg.bringChildToFront(vg.getChildAt(vg.indexOfChild(currentView)));
}
moveViewToFront((LinearLayout) findViewById(R.id.main_layout_2_linear));//pass your ui element you want to bring to the top

how to get click event of edittext inside webview

I am displaying webpages in a webview in my app. I want to enable screen orientation only when user clicks/touch on an edittext inside webview.I have tried below code.
mWebView.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
WebView.HitTestResult hr = ((WebView)v).getHitTestResult();
if(hr!=null){
if(hr.getType()==HitTestResult.EDIT_TEXT_TYPE)
((Activity) context).setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
else
((Activity) context).setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
return false;
}
});
But it is not working in OS versions below 2.3. How can i do this in these versions also.please help me .i am stuck on this..
below code working for edittext click inside webview :
WebView web_view = (WebView) subview
.findViewById(R.id.webview_mytagdetail_text);
web_view.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
Intent in = new Intent(getActivity(), URL_SCREEN.class);
in.putExtra("URL", url);
startActivity(in);
return true;
}
});
web_view.getSettings().setJavaScriptEnabled(true);
web_view.getSettings().setLoadWithOverviewMode(true);
web_view.getSettings().setBuiltInZoomControls(true);
web_view.getSettings().setDisplayZoomControls(false);
web_view.loadDataWithBaseURL("", campaignClass.getCampaignDesc(),
"text/html", "UTF-8", "");

How i can call webview form the other screen

i have 3 diferents screens: main(contains one image), dialog(contains another image) and browser(contains a webView) and one activity source, ok, in the activity source I call the dialog on click image in main layout, then the dialog shows another image and I want when click in the dialog image, the app change main layout for browser layout and then the browser load an specific URI and then close the dialog.
My code dont work becouse i dont know how i need use the webView in separate layout, for example, to call in dialog i need use dialog.findViewById(R.id.webView1) or in main only findViewById(R.id.webView1), but this not work now and the app crash...
My code:
ImageView imgMain = (ImageView)findViewById(R.id.imgMain1);
imgMain.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
final Dialog dialog = new Dialog(mainActivity.this);
dialog.setContentView(R.layout.dialog);
dialog.setTitle("Android");
dialog.setCancelable(true);
dialog.show();
//Boton de cerrar del dialog Android
Button closeDialog = (Button)dialog.findViewById(R.id.closeDialogBT);
closeDialog.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
dialog.cancel();
}
});
//click en imagen juegos del dialog android
ImageView imgDialog = (ImageView)dialog.findViewById(R.id.imgDialog1);
imgDialog.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
final WebView mWebView = (WebView)findViewById(R.id.webView1);
mWebView.loadUrl("http://www.example.com");
mWebView.setWebViewClient(new WebViewClient());
setContentView(R.layout.browser);
dialog.cancel();
}
});
}
});
Thank You!!!!
Why do you want to start WebView of one Activity from other activity.
It will be better to start activity containing WebView from your "imgDialog" with the intent.
and send URL string in that intent's extras.
in next step load webview with the URL string fetched from intent.
Try this. Hope this will help you
public class Example extends Activity {
private WebView mWebView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.translate);
mWebView = (WebView) findViewById(R.id.webview);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.loadUrl("http://www.example.com");
mWebView.setWebViewClient(new HelloWebViewClient());
}
private class HelloWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView webview, String url)
{
webview.loadUrl(url);
return true;
}
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack())
{
mWebView.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
}

Categories

Resources