Unable to show Progress bar and text view at a time - android

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>

Related

How can I create a text link on a webView to manually

I have a simple webView object and I want create a text link on the webView to manually. I do not want to have the text link from a external server. I want create a text link to manually on my webView. Is this possible?
In XML file:
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click"
android:layout_margin="10dp"
android:layout_gravity="center"
android:textSize="20sp"/>
<WebView
android:id="#+id/webView"
android:layout_width="fill_parent"
android:layout_height="match_parent"/>
In Class onCreate() :
mTextView = (TextView)findViewById(R.id.textView);
mWebView = (WebView) findViewById(R.id.webView);
mTextView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.loadUrl("http://www.google.com");
}
});
You should add a TextView with the text, the text itself doesn't matter, but you have to use an url while coding.
Set onClick and clickable to the Textview:
<TextView
android:id="#+id/txtView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
android:onClick="onTextViewClick"
android:text="Click this text"
/>
And handle it:
TextView mTxtView = (TextView) findViewById(R.id.txtView);
mWebView = (WebView) findViewById(R.id.webView1);
String url = "https://www.google.com"
public void onTextViewClick(View v) {
if(v.getId() == R.id.txtView){
mWebView.loadUrl(url);
}

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

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"

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.

Categories

Resources