How to set WebView inside a LinearLayout - android

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"

Related

Unable to show Progress bar and text view at a time

Hi Guys I am trying to show Progress bar and Text view at a time but Text View only displays once and disappears. Below is my Java File:
public class MainActivity extends Activity {
public boolean show_splash;
ProgressBar progressBar;
#Override
public void onCreate(Bundle savedInstanceState) {
show_splash = true;
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView myWebView = (WebView) findViewById(R.id.webview);
progressBar = (ProgressBar) findViewById(R.id.progressBar);
//enable Javascript
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
webSettings.setAllowContentAccess(true);
webSettings.setAppCacheEnabled(true);
webSettings.setBuiltInZoomControls(true);
webSettings.setDomStorageEnabled(true);
webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
webSettings.setSupportMultipleWindows(true);
webSettings.setSupportZoom(true);
webSettings.setLoadsImagesAutomatically(true);
//loads the WebView completely zoomed out
webSettings.setLoadWithOverviewMode(true);
webSettings.setUseWideViewPort(true);
myWebView.setWebViewClient(new MyWebViewClient());
myWebView.setDownloadListener(new DownloadListener() {
public void onDownloadStart(String url, String userAgent,
String contentDisposition, String mimetype,
long contentLength) {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(url));
startActivity(intent);
}
});
//load the home page URL
myWebView.loadUrl("http://mvc.myorange.ca/");
}
public void Visible(){
WebView webview = (WebView) findViewById(R.id.webview);
//ImageView logo = (ImageView) findViewById(R.id.imageView1);
TextView Text =(TextView) findViewById(R.id.textView1);
if (show_splash){
webview.setVisibility(View.INVISIBLE);
//logo.setVisibility(View.VISIBLE);
Text.setVisibility(View.VISIBLE);
progressBar.setVisibility(View.VISIBLE);
show_splash=false;
}
else {
webview.setVisibility(View.VISIBLE);
progressBar.setVisibility(View.VISIBLE);
Text.setVisibility(View.INVISIBLE);
//logo.setVisibility(View.INVISIBLE);
}
}
public void Invisible(){
WebView webview = (WebView) findViewById(R.id.webview);
//ImageView logo = (ImageView) findViewById(R.id.imageView1);
webview.setVisibility(View.VISIBLE);
//logo.setVisibility(View.INVISIBLE);
progressBar.setVisibility(View.GONE);
}
public class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (Uri.parse(url).getHost().equals("mvc.myorange.ca")) {
return false;
}
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
return true;
}
#Override
public void onPageStarted(WebView view, String url, Bitmap facIcon) {
Visible();
}
#Override
public void onPageFinished(WebView view, String url) {
Invisible();
}
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
WebView myWebView = (WebView) findViewById(R.id.webview);
if ((keyCode == KeyEvent.KEYCODE_BACK) && myWebView.canGoBack()) {
myWebView.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
}
and following is my activity xml file:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingLeft="0dp"
android:paddingRight="0dp"
android:paddingTop="0dp"
android:paddingBottom="0dp"
android:gravity="center"
tools:context=".MainActivity">
<WebView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingLeft="0dp"
android:paddingRight="0dp"
android:paddingTop="0dp"
android:paddingBottom="0dp"/>
<ProgressBar
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_gravity="center"
android:id="#+id/progressBar"
android:layout_marginBottom="30dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true" />
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/text"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#FFFFFF"
android:textSize="65dp"
android:background="#0582FF"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:gravity="center_vertical|center_horizontal" />
</RelativeLayout>
Now the problem is that the text view appears only once and it disappears. I would like it to be displayed first time along with the progress bar which comes after the textview. can anyone point out where i am wrong please ??
Make use ProgressDialog instead of ProgressBar, if you want to load the text with dialog
Do one thing.. try to remove this line
Text.setVisibility(View.INVISIBLE);
and tell me whether the textview stays there or still gets disappeared
A couple of comments for getting a better code.
-The name of variables should be in lowercase. TextView text.
-findViewById() is an expensive call, you should do it in the onCreate method and save all the views as variable to save calls, like you did with ProgressBar but with all of them.
Only a couple of good practices.
I found the error, the problem is that you put the TextView to extend from top to bottom and from start to the end, then it only shows itself. Remove those statements!
Change the layout to this one and see the differences:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingLeft="0dp"
android:paddingRight="0dp"
android:paddingTop="0dp"
android:paddingBottom="0dp"
android:gravity="center"
tools:context=".MainActivity">
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Texto cualquiera"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#FFFFFF"
android:textSize="65dp"
android:background="#0582FF"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:gravity="center_vertical|center_horizontal" />
<ProgressBar
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_below="#+id/textView1"
android:layout_gravity="center"
android:id="#+id/progressBar"
android:layout_marginBottom="30dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true" />
<WebView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/webview"
android:layout_below="#+id/progressBar"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingLeft="0dp"
android:paddingRight="0dp"
android:paddingTop="0dp"
android:paddingBottom="0dp"/>
</RelativeLayout>

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;
}
});

WebView in my Application

i have an xml layout file like this:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" android:background="#262626">
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="EXIT" />
<ScrollView
android:id="#+id/sv2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginTop="10dp" android:background="#ffffff">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<WebView
android:id="#+id/add_webView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:scrollbarStyle="insideOverlay"
android:text="#string/desc" />
</RelativeLayout>
</ScrollView>
</LinearLayout>
i would like to load my Url in my app,and keep the button i have created above of my webView.
Unfortunately, i m getting a webView in a new window on my device browser.
This is how i call the webView:
WebView add_webView = (WebView) dialog
.findViewById(R.id.add_webView);
add_webView.loadUrl(MYLINK);
How can i get my webView into my app and not in the browser window?
Thanks!!:)
As i would like the webView in a dialog,the solution was to use the setWebClient()
AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setTitle("Title here");
WebView wv = new WebView(this);
wv.loadUrl("http:\\www.google.com");
wv.setWebViewClient(new WebViewClient()
{
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
view.loadUrl(url);
return true;
}
});
alert.setView(wv);
alert.setNegativeButton("Close", new DialogInterface.OnClickListener()
{
#Override
public void onClick(DialogInterface dialog, int id)
{
}
});
alert.show();

Webview is displaying its content below the main layout

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.

NPE on setting visibility

Some users of out application are complaining about a nullpointerexception, which seems to occur only to them (HTC Desire, newest Android, 2.3?) but is not reproducable on other devices. I'm a bit puzzled what the source of that may be.
Any ideas?
java.lang.NullPointerException
android.webkit.WebView.navHandledKey(WebView.java:9290)
android.webkit.WebView.requestFocus(WebView.java:7847)
android.view.ViewGroup.onRequestFocusInDescendants(ViewGroup.java:1131)
android.view.ViewGroup.requestFocus(ViewGroup.java:1087)
android.view.ViewGroup.onRequestFocusInDescendants(ViewGroup.java:1131)
android.view.ViewGroup.requestFocus(ViewGroup.java:1087)
android.view.View.requestFocus(View.java:3718)
android.view.View.requestFocus(View.java:3696)
android.view.ViewRoot.focusableViewAvailable(ViewRoot.java:1785)
android.view.ViewGroup.focusableViewAvailable(ViewGroup.java:470)
android.view.ViewGroup.focusableViewAvailable(ViewGroup.java:470)
android.view.ViewGroup.focusableViewAvailable(ViewGroup.java:470)
android.view.ViewGroup.focusableViewAvailable(ViewGroup.java:470)
android.view.View.setFlags(View.java:4680)
android.view.View.setVisibility(View.java:3163)
at com.ichi2.anki.Reviewer.fillFlashcard(Reviewer.java:2282)
the code looks like that:
private FrameLayout mCardContainer;
...
mCardContainer = (FrameLayout) findViewById(R.id.flashcard_frame);
...
mCardFrame = (FrameLayout) findViewById(R.id.flashcard);
mCardFrame.removeAllViews();
mCard = createWebView();
mCardFrame.addView(mCard);
...
mCardContainer.setVisibility(View.VISIBLE); --> 2282
...
...
private WebView createWebView() {
WebView webView = new MyWebView(this);
webView.setWillNotCacheDrawing(true);
webView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
if (mZoomEnabled) {
webView.getSettings().setBuiltInZoomControls(true);
}
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebChromeClient(new AnkiDroidWebChromeClient());
webView.addJavascriptInterface(new JavaScriptInterface(), "interface");
if (Integer.parseInt(android.os.Build.VERSION.SDK) > 7) {
webView.setFocusableInTouchMode(false);
}
mScaleInPercent = webView.getScale();
return webView;
}
...
class MyWebView extends WebView {
public MyWebView(Context context) {
super(context);
}
#Override
public boolean onCheckIsTextEditor() {
return true;
}
}
the according xml-file looks like that:
...
<FrameLayout android:id="#+id/flashcard_frame"
android:layout_margin="3dip"
android:layout_below="#+id/top_bar"
android:layout_above="#+id/answer_nextTime_layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<FrameLayout android:id="#+id/flashcard"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<FrameLayout android:id="#+id/touch_layer"
android:layout_marginTop="20dip"
android:longClickable="true"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
</LinearLayout>
<FrameLayout android:id="#+id/whiteboard"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
<ImageView android:id="#+id/lookup_button"
android:padding="5dip"
android:layout_gravity="right"
android:src="#drawable/ic_lookup"
android:visibility="gone"
android:clickable="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<FrameLayout android:id="#+id/flashcard_border"
android:visibility="gone"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
</FrameLayout>
...
Are the lines:
mCardContainer = (FrameLayout) findViewById(R.id.flashcard_frame);
mCardContainer.setVisibility(View.VISIBLE); --> 2282
After a call to setContentView(R.layout.your_proper_layout_resource); ?

Categories

Resources