How can i add here a webview? - java

what should I do here working my script, I've tried everything but nothing works.. he give's a error by webview?
package com.dehvb.dehvbapp;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebSettings;
import android.webkit.WebView;
public class ProgrammaFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_programma, container, false);
WebView myWebView = (WebView) findViewById(R.id.webview);
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
myWebView.loadUrl("http://www.example.com");
return rootView;
}
}

public class ProgrammaFragment extends Fragment {
Webview wv;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_programma, container, false);
wv = (WebView) rootView.findViewById(R.id.yourWebviewId);
//Use the thread policy only if your load URL doesn't work on main thread.
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
wv.loadUrl("http://www.example.com");
return rootView;
}
}
P.s. take off import com.dehvb.deheerenveenseboys.R; from the import, it can cause some problems.

If your have already have webview in your layout file than just use directly!
WebView wb = (WebView)rootView.findViewById(R.id.webView);
wb.loadUrl(url);
replace url with your web url like "https://stackoverflow.com/".
Hope this helped!
EDIT:
First update your manifest exactly like this:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.dehvb.dehvbapp"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.dehvb.dehvbapp.MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
and your ProgrammaFragment.class
public class ProgrammaFragment extends Fragment
{
WebView wv;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View rootView = inflater.inflate(R.layout.fragment_programma, container, false);
wv = (WebView) rootView.findViewById(R.id.WebView1);
//Use the thread policy only if your load URL doesn't work on main thread.
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
wv.loadUrl("https://stackoverflow.com/");
return rootView;
}
}
I have tested it, i can post screenshot if you want!

Related

Open specific links in browser, instead of webview by regex

I'm using parts of code form some tutorial to create webview app. Goals is - to open links, that not contain appdev365 in browser, not in webview. I am completly noob at android development, so all that i've searched - didn't match to my(from tutorial) code.
MainActivity.java
package com.example.ynt;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.content.Intent;
public class MainActivity extends AppCompatActivity {
private WebView webView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webView = (WebView) findViewById(R.id.webview);
webView.setWebViewClient(new WebViewClient());
webView.loadUrl("https://yatln.com/appdev365/index.php");
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
}
#Override
public void onBackPressed() {
if (webView.canGoBack()) {
webView.goBack();
} else {
super.onBackPressed();
}
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.ynt">
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="Y&T База талантов"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:usesCleartextTraffic="true"
android:theme="#style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<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"
tools:context="com.example.ynt.MainActivity">
<WebView
android:id="#+id/webview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="visible" />
</RelativeLayout>
SOLUTION (by Varma Lanke):
https://gist.github.com/codedamage/8f98e2ed13d1d1fa05e5d43e67ea3e6c
private WebView htmlWebView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
htmlWebView = (WebView)findViewById(R.id.webview);
htmlWebView.setWebViewClient(new CustomWebViewClient());
WebSettings webSetting = htmlWebView.getSettings();
webSetting.setJavaScriptEnabled(true);
webSetting.setDisplayZoomControls(true);
htmlWebView.loadUrl("https://yatln.com/appdev365/index.php");
}
private class CustomWebViewClient extends WebViewClient{
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if(url.contains("appdev365"))
view.loadUrl(url);
else{
Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(i);
}
// view.loadUrl(url);
return true;
}
}
For you should use shouldOverrideUrlLoading method, once try the below one
private WebView htmlWebView;
String url = "https://yatln.com/appdev365/index.php";
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mainpage);
htmlWebView = (WebView)findViewById(R.id.webView);
if(url.contains("appdev365"))
htmlWebView.setWebViewClient(new CustomWebViewClient());
WebSettings webSetting = htmlWebView.getSettings();
webSetting.setJavaScriptEnabled(true);
webSetting.setDisplayZoomControls(true);
htmlWebView.loadUrl(url);
}
private class CustomWebViewClient extends WebViewClient{
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if(url.equalsIgnoreCase("appdev365"))
view.loadUrl(url);
return true;
}
}
This code opens browser and specified link (ex. google.com):
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com"));
startActivity(browserIntent);

webview chose file android not working

i have android project
using webview but my problem thats html file chose file working good from web browser but when using the android app webview its not working this is my activity_amin code
<WebView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerHorizontal="true"
android:id="#+id/webView"
/>
and this is MainActivity java code
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class MainActivity extends AppCompatActivity {
WebView webview;
#Override
public void onBackPressed() {
if(webview.canGoBack()){
webview.goBack();
}else {
super.onBackPressed();
}
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webview = (WebView) findViewById(R.id.webView);
WebSettings webSettings = webview.getSettings();
webSettings.setJavaScriptEnabled(true);
webview.setWebViewClient(new WebViewClient());
webview.loadUrl("http://softya.com/fitness_time/public/");
}
}
any help please
try this as you Mainactivity.java
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class MainActivity extends AppCompatActivity {
WebView webView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webView = findViewById(R.id.web);
webView.loadUrl("http://www.google.com");
WebSettings webSettings = webView.getSettings();
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebViewClient(new WebViewClient());
}
#Override
public void onBackPressed() {
if(webView.canGoBack()){
webView.goBack();
}
else
{
finish();
}
}
}
add this in your Menifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.aashiq.webview">
<uses-permission android:name="android.permission.INTERNET"></uses- permission>
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
add this in your activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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=".MainActivity">
<WebView
android:id="#+id/web"
android:layout_width="match_parent"
android:layout_height="match_parent">
</WebView>
</RelativeLayout>
You can try setting the webview settings by
webView.getSettings().setAllowFileAccess(true);
Rewritten code
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webview = (WebView) findViewById(R.id.webView);
webview.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setAllowFileAccess(true);
webview.setWebViewClient(new WebViewClient());
webview.loadUrl("http://softya.com/fitness_time/public/");
}
According to your error first you clean your code
then create MyWebViewClient class outside of onCreate method.And check you add webview inside of activity xml or not.

Fragment and FragmentList Android Studio "Unable to start activity ComponentInfo"

I don't know what I'm making wrong, but my App crash when I launch. I want to implement Tab bar list with FragmentActivity.
I really need help :(
The error returned is :
Unable to start activity ComponentInfo
Caused by: java.lang.NullPointerException
at jardelcompany.bundoransurfco.MainActivity.onCreate(MainActivity.java:79)
(line 79 is actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);)
MainActivity.java :
public class MainActivity extends FragmentActivity implements
ActionBar.TabListener {
SectionsPagerAdapter mSectionsPagerAdapter;
ViewPager mViewPager;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ActionBar actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
mSectionsPagerAdapter = new SectionsPagerAdapter(this,
getSupportFragmentManager());
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);
mViewPager
.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
#Override
public void onPageSelected(int position) {
actionBar.setSelectedNavigationItem(position);
}
});
for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) {
actionBar.addTab(actionBar.newTab()
.setText(mSectionsPagerAdapter.getPageTitle(i))
.setTabListener(this));
}
activity_main.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"
tools:context=".MainActivity"
android:id="#+id/mainLayout"
android:background="#FFFFFF"> />
<view
android:layout_width="match_parent"
android:layout_height="match_parent"
class="android.support.v4.view.ViewPager"
android:id="#+id/pager"/>
</RelativeLayout>
I use SectionsPagerAdapter for FragmentPageAdapter
SectionsPagerAdapter.java:
public class SectionsPagerAdapter extends FragmentPagerAdapter {
protected Context mContext;
public SectionsPagerAdapter(Context context, FragmentManager fm) {
super(fm);
mContext = context;
}
#Override
public Fragment getItem(int position) {
switch(position) {
case 0:
return new FirstFragment();
case 1:
return new LoginFragment();
}
return null;
}
#Override
public int getCount() {
return 2;
}
#Override
public CharSequence getPageTitle(int position) {
Locale l = Locale.getDefault();
switch (position) {
case 0:
return mContext.getString(R.string.title_section1).toUpperCase(l);
case 1:
return mContext.getString(R.string.title_section2).toUpperCase(l);
}
return null;
}
}
FirstFragment.java:
public class FirstFragment extends ListFragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.activity_main,
container, false);
return rootView;
}
}
LoginFragment.java :
public class LoginFragment extends ListFragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.activity_login,
container, false);
return rootView;
}
}
Here is my AndroidManifest.xml :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="jardelcompany.bundoransurfco" >
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.USE_CREDENTIALS" /> <!-- To retrieve the account name (email) as part of sign-in: -->
<uses-permission android:name="android.permission.GET_ACCOUNTS" /> <!-- To auto-complete the email text field in the login form with the user's emails -->
<uses-permission android:name="android.permission.READ_PROFILE" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="#string/app_name"
android:screenOrientation="portrait" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".LoginActivity"
android:label="#string/title_activity_login"
android:parentActivityName=".MainActivity"
android:screenOrientation="portrait"
android:windowSoftInputMode="adjustResize|stateVisible" >
</activity>
</application>
</manifest>
And my styles.xml :
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
</style>
</resources>
Remove this code into separate method like:
private void initUI(){
final ActionBar actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
mSectionsPagerAdapter = new SectionsPagerAdapter(this,
getSupportFragmentManager());
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);
mViewPager
.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
#Override
public void onPageSelected(int position) {
actionBar.setSelectedNavigationItem(position);
}
});
for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) {
actionBar.addTab(actionBar.newTab()
.setText(mSectionsPagerAdapter.getPageTitle(i))
.setTabListener(this));
}
}
and call this method from onResume() instead of onCreate(). Also you are using getActionBar() and same time getSupportFragmentManager() maybe you should use getSupportActionBar()? What is the min API level of your project?
Try adding this line of code to your manifest file
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="14" />

Webview app resolution is too big on device

I tried to create an app where it open my web app. I used webview to call the app, but when I run it, the app is kinda zoomed in (not in actual size). How can I change the size of the view? My device resolution is 1280px x 800px. I set up the web with the same resolution.
I tried to add viewport meta-tag on my header.php but it only works in device's browser, not in the app.
Tried this but doesn't work
<meta name="viewport" content="width=800, initial-scale=1, user-scalable=no">">
activity_main.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"
tools:context=".WebView" >
<WebView
android:id="#+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerHorizontal="true"
/>
</RelativeLayout>
MainActivity.java
package com.example.suryaagung;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.os.Bundle;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.app.Activity;
import android.view.Window;
import android.view.WindowManager;
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//No title bar is set for the activity
requestWindowFeature(Window.FEATURE_NO_TITLE);
//Full screen is set for the Window
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);
final WebView SuryaAgung = (WebView)findViewById(R.id.main);
SuryaAgung.setWebViewClient(new WebViewClient());
SuryaAgung.loadUrl("http://localhost:8080/app");
SuryaAgung.getSettings().setJavaScriptEnabled(true);
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.suryaagung"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="21" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-permission android:name="android.permission.INTERNET"/>
</manifest>
UPDATE:
I just tried this code and it would fit the content nicely on both tablet and phone.
public class ActivityWebview extends Activity {
final private String TAG = ActivityWebview.class.getSimpleName();
private WebView wv;
private AQuery aq;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_webview);
aq = new AQuery(this);
wv = aq.id(R.id.wv).getWebView();
wv.setWebViewClient(new WebViewClient());
wv.getSettings().setLoadWithOverviewMode(true);
wv.getSettings().setUseWideViewPort(true);
wv.loadUrl("http://www.cnn.com");
}
}
I used this to fit an image to the device size before, try it.
StringBuilder sb = new StringBuilder();
sb.append("<HEAD>");
sb.append("<meta name=\"viewport\" content=\"width=device-width, height=device-height, initial-scale=1.0, user-scalable=no\" />");
sb.append("</HEAD>");
// add these to webview settings
webview.getSettings().setUseWideViewPort(true);
webview.getSettings().setLoadWithOverviewMode(true);
webview.loadDataWithBaseURL("http://www.example.com", sb.toString(), "text/html", "UTF-8", "");

QuickContactBadge crashes app:

I have searched around but none of the solutions worked for my specific case. If I add
QuickContactBadge contactBadge = (QuickContactBadge) findViewById(R.id.quickContactBadge);
I it crashes. Then I changed it to:
QuickContactBadge contactBadge;
protected void onCreate(Bundle savedInstanceState) {
...
contactBadge = (QuickContactBadge) findViewById(R.id.quickContactBadge);
}
And it doesn't crash. But if I add contactBadge.Anything(); it crashes. No matter what
method it crashes. I.E. contactBadge.assignContactFromEmail("foo#foo.com", true);
Android Activity:
public class MainActivity extends Activity {
QuickContactBadge contactBadge;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getFragmentManager().beginTransaction().add(R.id.container, new MainFragment()).commit();
}
contactBadge = (QuickContactBadge) findViewById(R.id.quickContactBadge);
contactBadge.assignContactFromEmail("pointlightproductions.net#gmail.com", true);
}
public static class MainFragment extends Fragment {
public MainFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
return rootView;
}
}
Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.pointlight.android.kingdomcraft"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="19" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/android:Theme.Holo.NoActionBar" >
<activity
android:name="com.pointlight.android.kingdomcraft.MainActivity"
android:label="#string/app_name"
android:screenOrientation="landscape" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Layout:
<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"
tools:context="com.pointlight.android.kingdomcraft.MainActivity$MainFragment" >
<QuickContactBadge
android:id="#+id/quickContactBadge"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
First method: You're calling findViewById() when the activity is instantiated and its member variables are initialized. The activity won't have a Window yet and it will NPE. Call findViewById() only in onCreate() or later, after setContentView().
Second method: You don't have setContentView() with a view hierarchy that has a view with the given id. null is returned and invoking method on it will NPE.
From the code you later added, it seems the view is in your fragment layout and not the activity layout. It won't be a part of the activity view hierarchy in onCreate(). You should move the code to the fragment's onCreateView() instead:
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
contactBadge = (QuickContactBadge) rootView.findViewById(R.id.quickContactBadge);
contactBadge.assignContactFromEmail("pointlightproductions.net#gmail.com", true);

Categories