Webview is displaying its content below the main layout - android

i have created a webview for my android application , inicially i am tying to take application simpler as possible to understand things . i have created a webview to display the different page in it . i simply created a button which load url into webview which is working perfectly fine ,but the webview is loading the urls below the button . it is not overriding the main layout or not opening the url in newpage. i see the webpage in webview and the button above it . how can get rid of this button. thanks in advance. here is my java and xml code
activity
public class TestinglinkActivity extends Activity {
final Activity activity = this;
WebView webview;
private class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
webview = (WebView) findViewById(R.id.webview);
webview.getSettings().setJavaScriptEnabled(true);
webview.setWebViewClient(new MyWebViewClient());
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);
}
});
Button btn_login = (Button) findViewById(R.id.btn_click_login);
btn_login.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
webview.loadUrl("http://www.google.com.pk");
webview.goBack();
webview.goForward();
}
}
);
}
}
xml
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:isScrollContainer="true"
android:scrollbarAlwaysDrawVerticalTrack="true"
android:scrollbarStyle="outsideInset"
android:scrollbars="vertical"
android:scrollbarSize="10dp"
>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<Button
android:id="#+id/btn_click_login"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:layout_weight="2"
android:text="GO"/>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView
android:id="#+id/date"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="5dp"
android:layout_marginRight="5dp"
android:layout_alignParentLeft="true"
android:gravity="center_vertical"
android:lines="4"
/>
<ImageView android:id="#+id/imageBtn"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:padding="5dp"
android:contentDescription="#string/hello"
android:layout_alignParentLeft="true"
/>
</RelativeLayout>
<TextView
android:id="#+id/date"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="5dp"
android:gravity="center_vertical"
android:lines="4"
/>
<WebView
android:id="#+id/webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
</LinearLayout>
</ScrollView>

It sounds to me like you want to start a new Activity when the button is pressed. The reason your WebView appears below the other layout is because that's exactly what your XML describes. If you want your WebView to appear full screen, you need to move the WebView to a separate Activity which is started when your button is clicked.
You probably will want to pass the URL in the Extra of the Intent used to start the new Activity.

Related

how to align text in android when using html code

i create a imageview and textview in android with the help of html code so my question is how to align text and when i click Read more then it will open new pase so.. here is my full source code.can anyone help me please.thanks in advance
MainActivity
public class MainActivity extends Activity {
private final String htmlText = "<body><h1>Heading Text</h1><p>We understand that different client have different needs and requirements therefore special effort might be required to fulfill these requirements. Our custom software development services are aimed to fulfill......Read More </p>" +
"<blockquote>Example from <a href=\"www.javatechig.com\">" +
"Javatechig.com<a></blockquote></body>";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView htmlTextView = (TextView)findViewById(R.id.html_text);
htmlTextView.setText(Html.fromHtml(htmlText));
}
}
My Layout
<RelativeLayout 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"
tools:context=".MainActivity"
>
<ImageView
android:id="#+id/imageView1"
android:layout_width="35dp"
android:layout_height="35dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:src="#drawable/home" />
<TextView
android:id="#+id/html_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginLeft="5dp"
android:layout_toRightOf="#+id/imageView1"
/>
</RelativeLayout>
To align text below image remove following lines from text view in your layout
android:layout_alignParentTop="true"
android:layout_toRightOf="#+id/imageView1"
And add following line
android:layout_below="#+id/imageView1"
With Webview
Replace your layout with following code
<RelativeLayout 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"
tools:context=".MainActivity" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="35dp"
android:layout_height="35dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:src="#drawable/ic_launcher" />
<WebView
android:id="#+id/webview"
android:layout_below="#id/imageView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</RelativeLayout>
And use following code in your activity
private final String htmlText = "<body><h1>Heading Text</h1><p>We understand that different client have different needs and requirements therefore special effort might be required to fulfill these requirements. Our custom software development services are aimed to fulfill......Read More </p>" +
"<blockquote>Example from <a href=\"http://www.javatechig.com\">" +
"Javatechig.com<a></blockquote></body>";
private WebView m_webview;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
m_webview = (WebView) findViewById(R.id.webview);
m_webview.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
if(url.equalsIgnoreCase("http://www.javatechig.com/")) {
Toast.makeText(MainActivity.this, url, Toast.LENGTH_SHORT).show();
return true;
}
return super.shouldOverrideUrlLoading(view, url);
}
});
m_webview.loadDataWithBaseURL("", htmlText, "text/html", HTTP.UTF_8, null);
}
Try adding android:gravity="center" in your xml for your TextView if you want to align your text at the center of it.
To open your page try : htmlTextView.setMovementMethod(LinkMovementMethod.getInstance());

Web view- web page is not displayed

I have created a web view in android activity
my any code has no error.
but when it run it is trying to open in google chrome.
here is the my code
public class MyCustomListView extends Activity {
private WebView webView;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my_custom_list_view);
webView = (WebView) findViewById(R.id.webView1);
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("http://www.google.com");
}
}
and xml layout
<?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" >
<WebView
android:id="#+id/webView1"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="0.87" />
<Button
android:id="#+id/btn_back"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Back"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
and log cat show this
should not happen no rect based test nodes found
It's because, the url is being redirected. You can use the WebViewClient to achieve this.
webView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
});

Display accented characters in webview

My app does not display accented characters correctly. I looked into this last year but was not able to find a solution. I've been digging around but still have not found a solution. I've supplied a screen shot that shows what happens when the user enters "London Château" in an EditText field which is loaded into a WebView when the button is clicked. The accented character "â" does not translate as expected. It translates to "Ãç", "London ChÃçteau". Would love to get a solution, Thanks in advance.
I have supplied code to demonstrate:
public class MainActivity extends Activity {
private WebView webView;
private EditText edittext_1;
private Button buttonUpdate;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webView = (WebView) findViewById(R.id.webview_summary);
webView.getSettings().setJavaScriptEnabled(true);
edittext_1 = (EditText) findViewById(R.id.editText_1);
buttonUpdate = (Button) findViewById(R.id.button_update);
buttonUpdate.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String s = edittext_1.getText().toString();
webView.loadData(s, "text/html", "utf-8");
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
xml:
<?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" >
<WebView
android:id="#+id/webview_summary"
android:layout_width="fill_parent"
android:layout_height="0px"
android:layout_weight=".85" />
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="0px"
android:layout_weight=".15" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<EditText
android:id="#+id/editText_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginLeft="25dp"
android:layout_marginRight="25dp"
android:hint="text"
android:minWidth="100dp"
android:text="" />
<Button
android:id="#+id/button_update"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="#id/editText_1"
android:padding="10dp" />
</RelativeLayout>
</LinearLayout>
</LinearLayout>
As soon as I posted I found a suggestion that worked. I changed the following line from:
webView.loadData(s, "text/html", "utf-8");
to:
webView.loadDataWithBaseURL(null, s, "text/html", "utf-8", null);
try to get s from Html.fromHtml
String s = Html.fromHtml(edittext_1.getText().toString()).toString();

How to set WebView inside a LinearLayout

I am trying to set webview inside a linearlayout. I have just created an xml Linear layout file, progress bar and a webview inside the linear layout..
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ProgressBar
android:id="#+id/progWeb"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/darker_gray"
android:max="100"
android:visibility="invisible" />
<WebView
android:id="#+id/web"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
and java file it shows progress bar only. Webview is not shown by this code
public class MainActivity extends Activity {
private WebView web;
private ProgressBar progBar;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().requestFeature(Window.FEATURE_PROGRESS);
setContentView(R.layout.activity_main);
web = (WebView) findViewById(R.id.web);
progBar = (ProgressBar) findViewById(R.id.progWeb);
progBar.setVisibility(ProgressBar.INVISIBLE);
String url = "http://www.google.com.pk/";
web.getSettings().setJavaScriptEnabled(true);
web.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress) {
System.out.println(progress);
progBar.setProgress(progress);
if (progress == 100) {
progBar.setVisibility(ProgressBar.INVISIBLE);
progBar.setProgress(0);
} else {
progBar.setVisibility(ProgressBar.VISIBLE);
}
}
});
web.loadUrl(url);
}
}
you need to set the orientation of LinearLayout to vertical, the default is horizontal
Change your layout to
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/layout"
android:layout_width="fill_parent"
android:orientation="vertical"//==============> Here
android:layout_height="fill_parent" >
<ProgressBar
android:id="#+id/progWeb"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/darker_gray"
android:max="100"
android:visibility="invisible" />
<WebView
android:id="#+id/web"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
By default LinearLayout will set to orientation to Horizontal. specify the type of orientation will solve your problem
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<WebView
android:id="#+id/web"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
your Activity
public class YourActivityName extends Activity{
private WebView webView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.getWindow().requestFeature(Window.FEATURE_PROGRESS);
setContentView(R.layout.aboutus);
String url = "http://www.google.com.pk/";
getWindow().setFeatureInt(
Window.FEATURE_PROGRESS, Window.PROGRESS_VISIBILITY_ON);
webView=(WebView)findViewById(R.id.webViewLoad);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setSupportZoom(true);
webView.getSettings().setBuiltInZoomControls(true);
webView.loadUrl(url);
webView.setWebViewClient(new WebViewClient() {
ProgressDialog progressDialog = new ProgressDialog(YourActivity.this);
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
Log.e("I am loading Here ","Start");
progressDialog.setTitle("Loading");
progressDialog.setMessage("Please wait....");
progressDialog.show();
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
Log.e("I am loading Here ","Override");
view.loadUrl(url);
return true;
}
#Override
public void onPageFinished(WebView view, String url) {
progressDialog.dismiss();
}
});
}
}
You better use Webview inside the RelativeLayout than Linear, it should solve the problem.
<ProgressBar
android:id="#+id/progWeb"
style="#style/progressbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/darker_gray"
android:max="100"
android:visibility="invisible" />
<WebView
android:id="#+id/web"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/progWeb"
android:layout_marginTop="28dp" />
Change LinearLayout Orientation android:orientation="vertical" and change the width of Progress Bar android:layout_width="wrap_content"
Also change Progressbar Gravity . android:layout_gravity="center"

Android: How to open the webview in a new screen

I've got four Image Buttons in screen1.xml.
I've defined the webview in screen3.xml.
If i click the ImageButton1 or other buttons, the repective URL (in webview) should load in screen3.xml.
I tried to do this, but it throws forceclose error. But If I try the same this with webview defined in screen1.xml, it works. But the problem is the Image Buttons and the webview appears in the same page and they are overlapped which looks awful!
Please help me out to open the webview in a new screen.
BTW, can addView be used for this purpose?
public class click extends Activity
{
private WebView webView;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.screen1);
webView = (WebView) findViewById(R.id.web_view);
ImageButton i1 = (ImageButton) findViewById(R.id.imagebutton1);
ImageButton i2 = (ImageButton) findViewById(R.id.imagebutton2);
ImageButton i3 = (ImageButton) findViewById(R.id.imagebutton3);
ImageButton i4 = (ImageButton) findViewById(R.id.imagebutton4);
}
private void openBrowser1()
{
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("myurl");
}
public void myClickHandler1(View view)
{
openBrowser1();
}
Screen1.xml
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<WebView
android:id="#+id/web_view"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1.0" />
<ImageButton android:id="#+id/imagebutton1" android:clickable="true" android:onClick="myClickHandler1"
android:layout_width="130dip" android:background="#null" android:layout_height="130dip" android:layout_x="21dip"
android:layout_y="61dip" ></ImageButton>
<ImageButton android:id="#+id/imagebutton2" android:clickable="true" android:onClick="myClickHandler2"
android:layout_width="130dip" android:background="#null" android:layout_height="130dip" android:layout_x="171dip"
android:layout_y="61dip" ></ImageButton>
<ImageButton android:id="#+id/imagebutton3" android:clickable="true" android:onClick="myClickHandler3"
android:layout_width="130dip" android:background="#null" android:layout_height="130dip" android:layout_x="21dip"
android:layout_y="241dip" ></ImageButton>
<ImageButton android:id="#+id/imagebutton4" android:clickable="true" android:onClick="myClickHandler4"
android:layout_width="130dip" android:background="#null" android:layout_height="130dip" android:layout_x="171dip"
android:layout_y="241dip" ></ImageButton>
</AbsoluteLayout>
screen3.xml
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<WebView
android:id="#+id/web_view"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1.0" />
</AbsoluteLayout>
I think you need some fix to your code...
public class click extends Activity {
private WebView webView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.screen1);
webView = (WebView) findViewById(R.id.web_view);
ImageButton i1 = (ImageButton) findViewById(R.id.imagebutton1);
ImageButton i2 = (ImageButton) findViewById(R.id.imagebutton2);
ImageButton i3 = (ImageButton) findViewById(R.id.imagebutton3);
ImageButton i4 = (ImageButton) findViewById(R.id.imagebutton4);
WebSettings webSettings = webView.getSettings();
webSettings.setSavePassword(false);
webSettings.setSaveFormData(false);
webSettings.setJavaScriptEnabled(true);
webSettings.setSupportZoom(false);
webView.loadUrl("http://www.reforma.com");
//Add a listener to ImageButton1
i1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
openBrowser1();
}
});
}
private void openBrowser1()
{
//this intent is used to open other activity wich contains another webView
Intent intent = new Intent(click.this,SecondActivity.class);
startActivity(intent);
}
}
screen1.xml
<WebView
android:id="#+id/web_view"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1" />
<ImageButton android:id="#+id/imagebutton1" android:clickable="true"
android:layout_width="130dip" android:layout_height="130dip" android:layout_x="21dip"
android:layout_y="61dip" ></ImageButton>
<ImageButton android:id="#+id/imagebutton2" android:clickable="true"
android:layout_width="130dip" android:layout_height="130dip" android:layout_x="171dip"
android:layout_y="61dip" ></ImageButton>
<ImageButton android:id="#+id/imagebutton3" android:clickable="true"
android:layout_width="130dip" android:layout_height="130dip" android:layout_x="21dip"
android:layout_y="241dip" ></ImageButton>
<ImageButton android:id="#+id/imagebutton4" android:clickable="true"
android:layout_width="130dip" android:layout_height="130dip" android:layout_x="171dip"
android:layout_y="241dip" ></ImageButton>
</AbsoluteLayout>
Add a new activity wich contains your webview where the webpage should be load!.
public class SecondActivity extends Activity {
private WebView webView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.screen3);
webView = (WebView) findViewById(R.id.web_view);
WebSettings webSettings = webView.getSettings();
webSettings.setSavePassword(false);
webSettings.setSaveFormData(false);
webSettings.setJavaScriptEnabled(true);
webSettings.setSupportZoom(false);
webView.loadUrl("http://www.reforma.com");
}
}
screen3.xml
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<WebView
android:id="#+id/web_view"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1" />
</AbsoluteLayout>
Add to your AndroidManifest.xml your second Activity
<activity android:name=".SecondActivity"
android:label="SecondActivity"/>
and you must have Internet permission to load the web page...
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
any doubts???
Jorgesys
It doesn't look like you're inflating and loading the screen3.xml layout anywhere, so attempts to reference the WebView it contains will probably generate an exception. Sounds like what you really want to do is define another Activity for the webview layout and swap to it when the user pushes one of the buttons. See the 2nd part of the Notepad sample for a description of how to add additional Activities to an application and swap between them:
http://developer.android.com/resources/tutorials/notepad/notepad-ex2.html

Categories

Resources