I'm trying to implement a splash screen for my webview android app, so that it will display until the page is fully loaded. I was using the code from the accepted answer here but am running into some errors that I'm not sure how to solve. I assume that I'm missing a library of some sort, but I have no idea what.
The errors I'm getting are:
View cannot be resolved to a variable
error: Error: No resource found that matches the given name (at 'src' with value #drawable/vert_loading')
The code where I'm getting the view errors is:
webview = (WebView) findViewById(R.id.webview);
webview.getSettings().setJavaScriptEnabled(true);
webview.setWebViewClient(new WebViewClient() {
#Override
public void onPageFinished(WebView view, String url) {
findViewById(R.id.imageLoading1).setVisibility(View.GONE);
findViewById(R.id.webview).setVisibility(View.VISIBLE);
}
});
While my layout xml file is:
<ImageView
android:id="#+id/imageLoading1"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:visibility="visible"
android:src="#drawable/vert_loading"
/>
My assumption is that I might be missing an import of some sort, but I wouldn't know which one. The ones I have are:
import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.webkit.WebView;
import android.webkit.WebViewClient;
Any help would be greatly appreciated.
Add an drawable folder in your project res from File->New->Folder and put your drawable vert_loading in res->drawable folder. then Clean your project from Project->Clean.
Related
I am working on an application where i am loading a webpage from a an external url in a webview.
Loading the page take so much time to be loaded it take between 30 Seconds and 1 Minute
so please take look here on my code
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;
public class WebActivity extends AppCompatActivity {
private WebView webView;
private ProgressBar progressBar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_web);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
webView = (WebView)findViewById(R.id.webView);
progressBar= (ProgressBar)findViewById(R.id.progressBar2);
String link = getIntent().getExtras().getString("webLink");
String title = getIntent().getExtras().getString("webTitle");
setTitle(title);
webView.setVisibility(View.GONE);
progressBar.setVisibility(View.VISIBLE);
Log.d("WEB", link);
webView.setWebViewClient(new MyBrowser());
webView.getSettings().setJavaScriptEnabled(true);
//webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
webView.getSettings().setLoadWithOverviewMode(true);
webView.getSettings().setUseWideViewPort(true);
webView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
//webView.getSettings().setDomStorageEnabled(true);
//webView.getSettings().setAppCachePath(String.valueOf(getCacheDir()));
//webView.getSettings().setAppCacheEnabled(true);
webView.getSettings().setSupportZoom(true);
webView.getSettings().setBuiltInZoomControls(true);
webView.loadUrl(link);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
// app icon in action bar clicked; goto parent activity.
this.finish();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
public class MyBrowser extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return super.shouldOverrideUrlLoading(view, url);
}
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
webView.setVisibility(View.VISIBLE);
progressBar.setVisibility(View.GONE);
}
}
}
any idea to improve the performance ?
Unofortunately, there won't be much you can do to fix this. However:
Try to load your URL in the browser on your android device. Is it faster ? If not, there's not really much you can do.
There's a couple of things you can try though, and a few things to check. Specifically:
You're setting the visibility to View.GONE (making your webview invisible) while the page is loading, and then making it visible again when the page has loaded. This is probably the problem.
Try without this, and you will probably find that it will be quicker. In my experience, onPageFinished(..) only fires some time after the page is loaded.
Does the page really require JavaScript ? If not, don't enable it.
If it's feaseable in your case, you can use a HTML parser like Jsoup to extract only the desired data from the page, and show that to the user. This will be a lot faster.
If the page uses Ajax to load data dynamically, you can also load the data directly from the endpoints it uses. Open the page in a desktop browser, and open the network tab of developer tools to find out how the page works and loads data.
You can block requests from the WebView with shouldInterceptRequest(..). This may help if the page has things like eg. Facebook share buttons or extra images which you don't need. Blocking these will speed up load times.
If you show us the URL you're using, maybe I can investigate more and tell you axactly how you could speed it up in your case. Let me know if it helps.
I think depends on the amount of data that need to download . Also keep in mind that while you are in debug mode the application is much slower as it has to to trace all information.
Maybe this links can help you to improve performance:
WebView performance
WebView performace 2
While loading URLs in your webview if you put all your static resources like CSS, JS fonts etc in your android app's local assets folter and give relative URL to your resource in page then it will load much faster.
This is because right now your webview is loading all the resource from your or third party server wherever they exists but if they are in assets folder then you can load them locally.
So over all your data and HTML will come from your server and static content will be stored and loaded locally.
You have use loadDataWithBaseURL method of webview insteed of loadUrl
Here is a detailed article which can be helpful
http://www.codewithasp.net/2016/06/speedup-android-webview-app-localise-static-resources.html
I am trying to use AdMob in Android and am following tutorials. I have successfully downloaded, installed and added the Google Play Services library. Nearly everything seems fine.
However Eclipse stubbornly gives a red underline under the "adView.setAdSize" bit. The error message says "The method setAdSize(AdSize) in the type AdView is not applicable for the arguments (AdSize)". I don't see what's wrong, that code is in line with all tutorials and documentation I can find, and why would setAdSize(AdSize) not take in AdSize as an argument? That doesn't make sense to me.
Unfortunately setting the ad size is necessary for the code to run so I can't just remove that bit. Relevant code is below. Thanks to anyone that can help.
...
import com.google.ads.AdSize;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;
public class MainActivity extends ActionBarActivity{
private AdView adView;
#Override
protected void onCreate(Bundle savedInstanceState) {
...
// Create the adView.
adView = new AdView(this);
adView.setAdSize(AdSize.SMART_BANNER);
adView.setAdUnitId("ID REMOVED FOR PRIVACY REASONS");
...
}
...
}
Change:
import com.google.ads.AdSize;
For:
import com.google.android.gms.ads.AdSize;
image
Well i have this problem when the code is correct, but it gives me red line error!
can anybody help me.
Main.java
package com.example.sout;
import android.content.DialogInterface;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import
~
import android.widget.ImageView;
public class Main extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
ImageView iv = (ImageView) findViewById(R.id.img1);
iv.setBackgroundResource (R.anim.animation);
~~~~~~~~~~~~~~~~
iv.setOnClickListener(new OnClickListener());
~~~~~~~~~~~~~~~ ~
}
in (note the ~ characters above):
iv.setBackgroundResource (R.anim.animation);
iv.setOnClickListener(new OnClickListener());
The setBackgroundResource and setOnClickListener are red too.
The error in iv.setBackgroundResource (R.anim.animation); comes because that method takes integer as a parameter .So change it to the following,
iv.setBackgroundResource (R.drawable.image1); //image1 is a drawable which is inside your drawable folder.
and the second error comes because you have not imported the required package and have not overridden the methods required for that. Change it to the following,
iv.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//do your task here
}
});
after changing press ctrl+shift+p to import the required packages.
Sometimes, at least with Eclipse, it gets it into it head that there's an error and it won't let it go. But, with Eclipse, you can at least put the cursor over a marker and it will explain what the problem is (or what it thinks it is). I'd try to hover over the errant lines to see if a popup box appears telling you what it thinks.
If you're sure it's wrong, often I've found that saving the file (or all files) will fix it. Sometimes adding then deleting a space on the errant line will fix it.
However, I notice that you have an incomplete import at the top of your file and this may be preventing the syntax checker from properly analysing the source file.
My advice is to fix that first, then try those other two tricks (save then, if that doesn't work, edit and undo on the errant line). Hopefully that will make it disappear.
Of course, you may want to check, just in case, your R.java file to ensure that member exists in there somewhere. I've been bitten by my own misspellings before.
package com.example.one;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
Button button;
TextView txt1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addListenerOnButton();
}
public void addListenerOnButton(){
button=(Button)findViewById(R.id.button1);//error line of code
txt1=(TextView)findViewById(R.id.txt);//error line of code
button.setOnClickListener(
new View.OnClickListener()
{
public void onClick(View view)
{
txt1.setText("hello");
}
});
}
#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;
}
}
I have marked the error code of lines but unable to resolve
the .xml is
<EditText
android:id="#+id/txt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="26dp"
android:layout_marginTop="26dp"
android:ems="10"
android:inputType="text" >
<requestFocus />
</EditText>
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/txt"
android:layout_below="#+id/txt"
android:layout_marginLeft="18dp"
android:layout_marginTop="110dp"
android:text="Button -MYBUT" />
I just want to add a button and a textview in my app beacuse I'm a beginner Android Developer so unable to resolve my issue thanks in advance
Try to use findViewById after you inflate your menu. You are trying to find an element in a view that has not yet be attached to the activity.
This is just fine, check your imports. In android are 2 types of R present.
- R Generated, by your project builder
- R brought by android sdk
I guess that you imported R from android SDK, not generated by your project. That's why eclipse yells with errors at you.
The other situation is, that, the findViewById() method called OnCreateOptionsMenu() callback is trying to reach view that has not been created yet, and returns null.
EDIT. Problems with R might be also associated with weird, not visible and hard to find mistakes in your xml files. When you make huge mistake in some of your xml files, builder returns error and R will not be generated. But in some cases, builder cant find any of your mistakes, but is not generating new R class - this causes your R to be out of date.
I just want to know that about the explanation of this line
but = (Button) findViewById(R.id.but);
findViewById(Id) returns a View and you have to cast that View to whatever UI element you are using.
In order for findViewById(Id) to return a View, you have to assign an Id to each UI element (if you want to use it in java):
<Button
android:id="#+id/myButtonId"
[...]
/>
Now you can do this:
but = (Button) findViewById(R.id.myButtonId);
You should also use the onCreate() method to initialize your UI elements, and not onCreateOptionsMenu().
Eclipse is a Curse try to not use it guys I Prefer NetBeans I love NetBeans so much Hats off for NetBeans!
The Answer of my own Question:
it was a real time fault of my eclipse (a curse) not my fault. The Solution is to restart the IDE or to press the Save All from File Menu of IDE
Visit: https://stackoverflow.com/a/7453201/2277645
i m new to this platform, please help me to find what is the error...
setContentView(R.layout.main); // this line shows the error.
Code:
package com.example.helloandroid;
import android.R;
import android.app.Activity;
import android.os.Bundle;
public class HelloAndroidActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
Try deleting the line import android.R then clean your project.
Just adding more details on why the error is coming.
As there is an import of android.R so setContentView() is looking for a layout file 'android.R.layout.main' and there is no main.xml in the layout files that come along with SDK. So, using the correct R.java import will work.
clean Project then try To Run because i faced same problem before a month and remember layout/main.xml must be their and it must not contains any error.
you can Also Do this
import android.R;
or
import your.application.packagename.R; Now Clear Project and Run it.
You must have to simply change the
setContentView(R.layout.main);
... to:
setContentView(R.layout.activity_main);
... because Layout contains this .xml file.
I hope your problem will be solved.
First remove import android.R;
After any change on xml fiels you must clean project.
Build > Clean Project
after that every things corrects.