I've been following this series of Android Development Tutorials and I've encountered some problem trying to tweak one of the projects I've created.
Basically all I want to know is how to apply the WebView correctly in my project - in the easiest and most simple way possible.
What I've done:
enabled permission to the internet on the manifest
created the webview on the main_activity.xml
imported the WebView webkit to my MainActivity Java class
And all I've done after that is simply loading the url on my onCreated method like this:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView v = (WebView) findViewById(R.id.weblol);
v.loadUrl("www.google.com");
}
However nothing seems to happen and the block of the WebView remains blank white
Care to explain to a noob what am I doing wrong ?
You should use http://www.google.com instead of www.google.com.
Only you to this,webview can notice this it is a URL.
Otherwise.you can use local file in webview ,like file:///android_asset/xxx in assets folder.
That's all.
Related
I get a massage says "app is keep stopping" when I run the project on my mobile.
the error at on Create(MainActivity.java:16)
at the line 16 set Content View method
help me
The method setContentView() sets the View that your MainActivity should display.
The View is usually declared in layout/activity_main.xml.
Some error seems to appear when trying to call this method in your application.
A common reason why android applications crash there is calling some methods in the overridden method onCreate(Bundle savedInstanceState) in a wrong order.
Check if your code follows this pattern:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView call
// other things
}
I cannot help you further now, please copy and paste the stacktrace (crash information) into your question. When your application just crashed on an emulator, you can see it in the Run-section (usually red text).
I'm currently working on making an Android app, but have been having some trouble. I want to be able to push a button with the title of the document as it's TextView and then have that document open to be read. I've looked around for guides but everything I've found is either out of date or doesn't explain any of the code shown. Does anyone know how I can put such a thing in my app? At this point, I'm not even sure where to start with the process.
Note, I'm working in Java not Kotlin.
UPDATE: I was directed to a solution using intents. Now there seems to be an issue loading the file. My code is this:
public class atotf_pdf extends literature {
File file = new File("/storage/emulated/0/AToTF Preview.pdf");
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_main);
Uri pdfURI = FileProvider.getUriForFile(atotf_pdf.this, "net.whispwriting.whispwriting.provider", file);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(pdfURI, "application/pdf");
intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
startActivity(intent);
}
Is it something here that's causing the error?
It looks like the file is somehow not being stored in the filesystem when the app is installed.
Just like #ruben said, you could use a WebView for such a task: https://developer.android.com/reference/android/webkit/WebView. For more information about using WebView to display content, you can take a look at this post: How can I display a pdf document into a Webview?
Another potential solution could be using intents, which has also been discussed before: How to open a PDF via Intent from SD card
Hopefully this helps!
I'm basically writing my first hello world in android studio and the java file says that the xml layout file and other resources in the res file don't exist. I took the references to these things from books/tutorials so they should work.
The book said to use
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main.xml);
}
But when that didn't work I changed it to res.layout.activity_main.xml
my project directory looks like this
How do I make it work?
http://i.stack.imgur.com/N71sl.png
//EDIT
http://i.stack.imgur.com/HUgsm.png
You are referencing res not R.
do something like this: setContentView( R.layout.activity_main);
Leave off the ".xml"
update your android studio in stable channel and restart android studio and build the project again .
You need to understand the concept of the R class in android.
The R class is auto generated every time you build your project and cannot be modified.
This class contains a map of all the projects assets. Every time you created or import a resource, set a dimension, a string, create a layout, add or change id etc. the R file is changed in the background.
So if you want to access a resource you simply call it using the R class.
For example:
layout: R.layout.activity_main
string: R.string.hello_world
id: R.id.et_main_helloWorld
And so on.
More info can be found here.
Make sure you also check Providing Resources and Accessing Resources for a better understanding.
Good luck and happy coding.
In the end I started a new project and added in everything piece by piece, I had put a mp3 file in the values directory which was messing up the R file.
i had made html+jquery mobile app using webview , my all code is inside assest folder, i noticed sometime my app hangs and sometime it works fine ,if i am missing some thing
below is android java code
i made html +jquery app , app hangs most of time , and some time it works any way to make it work fine
below problem:
1: some time object in canvas gets duplicated however there is not changes of getting duplicate object on any of canvas this problem occurs only in android browser
2: sometime while dragging object gets stuck any where without being dropped in canvas only in android browser
below is my java code for that
public class MainActivity extends Activity implements OnClickListener {
private static final String URL = "file:///android_asset/index.html";
private WebView mWebView;
#SuppressLint("SetJavaScriptEnabled")
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mWebView = (WebView) findViewById(R.id.webview);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.setWebChromeClient(new WebChromeClient());
mWebView.setWebViewClient(new WebViewClient(){
});
refreshWebView();
}
private void refreshWebView() {
mWebView.loadUrl(URL);
}
#Override
public void onClick(View v) {
refreshWebView();
}
}
and html code that i am using inside assest folde is below link
http://liveweave.com/JckSgC
I faced this bug before 6 months ago. After a lot lot of research on this topic I personally concluded that Its a API bug of android which was fixed in API level 18(KITKAT).
It means, "If the application is working on an android version less than Kitkat, webview starts hanging or works madly"
Reason:
WebView's of android version less than KITKAT doesn't support HTML5 and CSS3.
http://www.mobilexweb.com/blog/android-4-4-kitkat-browser-chrome-webview
Suggestions:
You can use external webviews like "ChromiumWebView"
(Link of ChromiumWebView https://github.com/pwnall/chromeview )
Or you can restrict your application to only runs on android version greater than or equivalent to KITKAT. (I personally doesn't prefer this)
Is there any way to always show zoom controls in webview?
I found this: Always show zoom controls on a MapView
but that's for the mapview.
I want them to always be visible.
super.onCreate(savedInstanceState);
super.loadUrl("file:///android_asset/www/index.html");
WebSettings ws = super.appView.getSettings();
ws.setSupportZoom(true);
ws.setBuiltInZoomControls(true);
setDisplayZoomControls() is only available from API 11 (Android 3). So you can't even consider using it until the vast majority of Android devices are 3 or above - which will not be for some years :(
Not sure if this will work as I never tried, but I checked the reference in developer.android.com
ws.setDisplayZoomControls(true);
If this worked dont forget to best answer this answer.
Also check this out:
http://www.tutorialforandroid.com/2009/02/webview-with-zoomcontrols-in-android.html
good tutorial. (Debunks my theory)
None of the above worked for my. Only when I start draging the webview content the controls show up the first time. So what I did as a "quick fix" is in onStart() of my Fragment that holds the Dialog with the webview I call:
webview.invokeZoomPicker();
Example:
#Override
public void onStart(){
super.onStart();
DialogFragment dialog = this;
//...stuff
if(dialog.getDialog()!= null && dialog.getDialog().getWindow()!=null){
//...more stuff
View v = dialog.getView();
if(v!=null){
//invoke controls on start once, they stay active a few seconds:
WebView webview =v.findViewById(R.id.frag_dlg_WebView);
webview.invokeZoomPicker();
}
}
}
The controls light up only for a few seconds until they disappear again, by then the user in my case should have dragged the webview already.