I'm working on an android app and I want to be able to load a webpage in the application. I've browsed around the internet for a while and can't seem to get it just right. I know that my internet connection is established, as I can use my browser. I'm new to android development and learning as I go, so any elaboration would be amazing. Here's the code I'm working with:
public class MainActivity extends Activity {
private class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
Button btnBack;
WebView webview;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webview=(WebView)findViewById(R.id.webview1);
webview.setWebViewClient(new MyWebViewClient());
openURL();
}
/** Opens the URL in a browser */
private void openURL() {
webview.loadUrl("http://www.google.com");
webview.requestFocus();
}
}
and my xml
<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"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world" />
<RelativeLayout android:layout_width="fill_parent"
android:layout_height="fill_parent" android:layout_below="#+id/rlayout1"
android:id="#+id/rlayout2">
<WebView android:id="#+id/webview1" android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1.0" />
</RelativeLayout>
Here's what happens when I run the application:
Wow guys, sorry if this is spammed but I just found a forum explanation that I didn't see before. Android requires internet permissions in an app for it to access the internet. I thought that it would give me a permissions exception, but instead it just doesn't load it. here's what I added to the xml manifest for it to work correctly
<uses-permission android:name="android.permission.INTERNET" />
Again, sorry I feel really stupid for not knowing about this. You'd think the console would print some sort of security/permission exception
Related
this is the xml file for the webview
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.testing.WebViewExample">
<WebView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/webView"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
and this code is for Webview.java
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView mywebview = (WebView) findViewById(R.id.webView);
mywebview.loadUrl("https://www.google.com/");
}
}
But the problem is I am not able to view the google and it show an error as webpage not available
the webpage at https://www.google.com/ could not be loaded because:
net::ERR_CACHE_MISS
You may need to add the INTERNET permission to your manifest file:
<uses-permission android:name="android.permission.INTERNET"/>
Another Reference: loadUrl() in Android Webview fails with net::ERR_CACHE_MISS
I am an android beginner, just learning android static fragment concept, but after running my code getting app crash issue. Here is details:
MainActivity.java
package com.example.lalendrakumar.fragmentdemo;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
activity_main.xml
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:weightSum="2">
<fragment
android:id="#+id/fragment1"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_weight="1" />
<fragment
android:id="#+id/fragment2"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_weight="1" />
So anyone can solve my problem.
I'm assuming you want the two fragments to fill the page. "fill_parent" has been replaced now by "match_parent." However, if you use this, in theory, the two fragments will overlap one over the other.
To split them in the screen, each will have to be adjusted to height zero. This is how each fragment's dimensions should read
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
You don't mentioned which layout you are using, but i assume you are using LinearLayout. LinearLayout take orientation horizontal by default, and you gave height and width to match_parent(fill_parent is deprecated now and replaced by match_parent)
Please find more information about LinearLayout on this link
Replace your code with following
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:weightSum="2">
<fragment
android:id="#+id/fragment1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" />
<fragment
android:id="#+id/fragment2"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" />
try commenting out these lines in fragment class (if they are present )
/* #Override
public void onAttach(Context context) {
if (context instanceof OnFragmentInteractionListener) {
mListener = (OnFragmentInteractionListener) context;
} else {
throw new RuntimeException(context.toString()
+ " must implement OnFragmentInteractionListener");
}
}
*/
I have an image in my view and I want to move it when I tilt my phone. It should look like the IOS wallpaper, when you move your phone. Some Android phones have this too. Is there any library or a good tutorial for this? Or does somebody know how I can do this?
Thanks!
Recently... I found a library called gravity-view
This library moves the image based on the device.
Inside Layout XML File:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="none">
<ImageView
android:id="#+id/bg"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</HorizontalScrollView>
</RelativeLayout>
Inside Activity or Fragment:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
gravityView = GravityView.getInstance(this)
.setImage(bg, R.drawable.landingbg)
.center();
}
#Override
protected void onResume() {
super.onResume();
gravityView.registerListener();
}
#Override
protected void onStop() {
super.onStop();
gravityView.unRegisterListener();
}
I'm fairly new to Android development, and I have a question about how WebViews handle data (in Java).
I'm assuming this would fall under the 'cookie' category. But what I have is two different webViews on different tabs of my application. I would like for one webView (call it webView1) to be logged into one account of a website, while the other (webView2) to be logged into another account of the same website. For example, I would want to log into two separate Gmail accounts within the two webViews at the same time.
The problem I am having is that once I log into an account on webView1, webView2 follows suit and logs me into that account. The same problem happens when I log into webView2, as webView1 logs into that account also naturally.
Is there any way to get around this? I want my two webViews to act completely independently from one another is what it comes down to.
Thanks!
I can definitely answer your question. I am mainly an iOS developer and I am creating an Android app with the exact opposite effect of webView cookies not sharing. I created my webviews in tabs to simulate the behavior I use in iOS, but my webViews are acting completely independant of one another. I want them to share the same login info, but as it stands I must login to each tab individually because they aren't playing nice.
Either way, my problem can definitely help you and I would hope that you share your info with me to create the cookie sharing effect I am searching for...
Here is my main.xml layout:
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TabWidget
android:id="#android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<WebView
android:id="#+id/web_engine"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_gravity="bottom"
android:layout_marginTop="-45dp" />
<WebView
android:id="#+id/messages"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_gravity="bottom"
android:layout_marginTop="-45dp" />
<WebView
android:id="#+id/myprofile"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_gravity="bottom"
android:layout_marginTop="-45dp" />
<WebView
android:id="#+id/rncorner"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_gravity="bottom"
android:layout_marginTop="-45dp" />
</FrameLayout>
</LinearLayout>
</TabHost>
Here is my Activity:
package com.example.tabs;
import android.app.TabActivity;
import android.os.Bundle;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.TabHost;
public class TabsActivity extends TabActivity {
WebView webView;
final String DEFAULT_URL = "http://example.com";
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TabHost mTabHost = getTabHost();
mTabHost.addTab(mTabHost.newTabSpec("Home").setIndicator("Home",getResources().getDrawable(R.drawable.home)).setContent(R.id.web_engine));
mTabHost.addTab(mTabHost.newTabSpec("Messages").setIndicator("Messages",getResources().getDrawable(R.drawable.messages)).setContent(R.id.messages));
mTabHost.addTab(mTabHost.newTabSpec("My Profile").setIndicator("My Profile", getResources().getDrawable(R.drawable.myprofile)).setContent(R.id.myprofile));
mTabHost.addTab(mTabHost.newTabSpec("Map").setIndicator("Map", getResources().getDrawable(R.drawable.rncorner)).setContent(R.id.rncorner));
mTabHost.setCurrentTab(0);
//home
webView = (WebView)findViewById(R.id.web_engine);
webView.setWebViewClient(new MyWebViewClient());
webView.loadUrl(DEFAULT_URL);
//messages
webView = (WebView)findViewById(R.id.messages);
webView.setWebViewClient(new MyWebViewClient());
webView.loadUrl("http://example.com/index.php2");
//my profile
webView = (WebView)findViewById(R.id.myprofile);
webView.setWebViewClient(new MyWebViewClient());
webView.loadUrl("http://example.com/index.php3");
//rncorner
webView = (WebView)findViewById(R.id.rncorner);
webView.setWebViewClient(new MyWebViewClient());
webView.loadUrl("http://example.com/index.php4");
}
public class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// TODO Auto-generated method stub
view.loadUrl(url);
return true;
}
}
}
Is it possible to create a magazine based on Android embedding flash in it?
As your question is very wilde and generic, I can only say "yes that's possible"!
Here is a link to embed your swf file into an Android application.
Load an SWF into a WebView
public class Test3Activity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
String url ="file:///android_asset/qualibus.swf";
WebView wv=(WebView) findViewById(R.id.webView1);
wv.getSettings().setPluginsEnabled(true);
wv.loadUrl(url);
}
}
main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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/webView1"
android:layout_width="match_parent"
android:layout_height="match_parent">
</WebView>
</LinearLayout>
Result: