I have a bottom navigation bar that switches between 5 fragments. One of these 5 buttons should open WebView (newsfeed) and display some HTML. The code that calls the newsfeed.xml is this:
public class newsfeedFragment extends Fragment {
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.newsfeed,container,false);
return v;
}
}
Now I followed a youtube tutorial to pull the HTML from a website (code below). In the tutorial, the code is in it's own activity and not called within a fragment. I am not sure how to combine these 2 code snippets to create the webview inside my fragment. Help would be much appreciated!
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class MainActivity extends ActionBarActivity {
private WebView myWebView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myWebView = (WebView)findViewById(R.id.webView);
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
myWebView.loadUrl("http://www.website.com");
myWebView.setWebViewClient(new WebViewClient());
}
#Override
public void onBackPressed() {
if(myWebView.canGoBack()) {
myWebView.goBack();
} else {
super.onBackPressed();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
I assume you want to load the webview into the fragment?
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.newsfeed,container,false)
WebView webview = (WebView) v.findViewById(R.id.webview);
webview.loadUrl("http://www.website.com")
return v;
}
So inflate the Layout and afterwards call findviewbyid with your new layout and then you could do anything you want with your webview and it is loaded in your Fragment. Hopefully the solution for your question.
Take WebView in your newsfeed layout, and do the same thing in fragment.
You can use this code anywhere in the fragment to open the activity:-
startActivity(new Intent(getActivity(),ActivitytoOpen.class));
Related
I am trying to learn android programming by google's android programming course (android studio) in udacity.
The problem is I do not have "PlaceholderFragment" class in my MainActivity.java.
I only have MainActivity class in this file.
Could anyone help me please?
Edit:
my MainActivity.java is:
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
I experienced the same problem when using Udacity course "Developing Android Apps: Android Fundamentals". The Udacity videos are old > 9 months and since android studios is always updating, the newer version of android studio does not provide an inner class for the fragment anymore. Instead android studio generates a separate class containing a fragment. But, no problem.
Go to the tab of Lesson 1: "Using the Sunshine Github Repository" and open the link: Udacity Sunshine repository
Download the branch : "1.01_hello_world" and import the contents into android studios. You will have the "PlaceholderFragment" class within the MainActivity.java and then you can continue the lessons at Udacity
You need to read a good webpage. I recommend reading and studying code from Google's # Fragments.
Code snippets from the webpage:
public static class ExampleFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.example_fragment, container, false);
}
}
ExampleFragment fragment = new ExampleFragment();
fragmentTransaction.add(R.id.fragment_container, fragment);
fragmentTransaction.commit();
Note: ExampleFragment could be your "PlaceholderFragment".
Just delete your code succeeding your package name in the MainActivity.java file and replace it with this one below.
import android.app.Fragment;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.view.ViewGroup;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
return rootView;
}
}
}
I got the same problem, right after I put the "ForecastFragment" class into it's own file.
What solved the problem for me was to go build - clean project. After doing that, it built just fine.
Perhaps there are some old build files that look for placeholderFragment?
I had the same problem, but I tried to figure out how to work with the new structure. I put the code inside MainActivityFragment.java, inside the method onCreateView, since this is the new approach. It worked for me.
Take a look of my final code:
public class MainActivityFragment extends Fragment {
private ArrayAdapter<String> mForecastAdapter;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container);
ArrayList<String> weekForecast = new ArrayList<>();
weekForecast.add("Today - Sunny - 88/63");
weekForecast.add("Tomorrow - Foggy - 70/40");
weekForecast.add("Weds - Cloudy - 72/63");
weekForecast.add("Thurs - Asteroids - 75/65");
weekForecast.add("Fri - Heavy Rain - 65/56");
weekForecast.add("Sat - HELP TRAPPED IN WEATHERSTATION - 60/51");
weekForecast.add("Sun - Sunny - 80/68");
mForecastAdapter = new ArrayAdapter<>(getActivity(),
R.layout.list_item_forecast, R.id.list_item_forecast_textview, weekForecast);
ListView listView = (ListView) rootView.findViewById(R.id.listview_forecast);
listView.setAdapter(mForecastAdapter);
//return inflater.inflate(R.layout.fragment_main, container, false);
return rootView;
}
}
I am trying to make a simple android app that loads a webpage into a WebView.
package com.example.abhi.molinahealthcare;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class Molina_HealthCare extends ActionBarActivity {
private class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
WebView webview;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_molina__health_care);
WebView myWebView = (WebView) findViewById(R.id.webview);
webview.getSettings().setJavaScriptEnabled(true);
webview.setWebViewClient(new MyWebViewClient());
openURL();
}
private void openURL() {
webview.loadUrl("http://www.google.com");
webview.requestFocus();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_molina__health_care, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
ACTIVITY:-
<WebView
android:id="#+id/webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
I was not able to open it. I have the INTERNET permission declared in the AndroidManifest.xml
You are not initializing webview object of WebView which you are using in openURL method. do it by initializing webview instead of myWebView in onCreate:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_molina__health_care);
webview = (WebView) findViewById(R.id.webview);
...
I am trying to hide the ActionBar in my Activity. I have an Activity with a Fragment, which looks like this:
import android.app.Activity;
import android.app.ActionBar;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.os.Build;
public class IntroActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.intro_activity);
if (savedInstanceState == null) {
getFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
ActionBar actionBar = getSupportActionBar();<---- Error here
actionBar.hide();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_intro, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.intro_page_1, container, false);
return rootView;
}
}
}
The error I get is Cannot resolve method getActionBarActivity method. I have tried extending ActionBarActivity, but when I do, I get onCreate errors, and several others.
How can I hide the action bar?
It does not look like you are using the support library? If that is the case you should call getActionBar() not getSupportActionBar()
From your imports, it looks like you're not using support library. So you should use getActionBar() instead of getSupportActionBar().
use this code
ActionBar bar = getActionBar();
bar.hide();
This will solve your problem
Here's my webview drawer app..
It's making me crazy, i can't understand how to insert a "goBack" function like in normal WebView. Here's my code:
package com.my.app;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class WebViewFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Retrieving the currently selected item number
int position = getArguments().getInt("position");
String url = getArguments().getString("url");
// List of rivers
String[] menus = getResources().getStringArray(R.array.menus);
// Creating view corresponding to the fragment
View v = inflater.inflate(R.layout.fragment_layout, container, false);
// Updating the action bar title
getActivity().getActionBar().setTitle(menus[position]);
//Initializing and loading url in webview
WebView webView = (WebView)v.findViewById(R.id.webView);
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl(url);
webView.setWebViewClient(new WebViewClient());
return v;
}
}
I've already checked more than 20+ answers here on Stack, but without results.
Hope someone can explain me a bit how to use the function GoBack and help me with this stressing thing..
Thanks in advance,
Davide
Inside of the your OnCreateView:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
view = inflater.inflate(R.layout.your_layout, container, false);
mWebView = (WebView) view.findViewById(R.id.webView);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.setWebViewClient(new MyWebViewClient());
mWebView.loadUrl(expertsUrl);
return view;
}
private class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
Ok! In your MainActivity you will write it:
#Override
public void onBackPressed() {
if (WebViewFragment.mWebView!=null) {
if (WebViewFragment.mWebView.canGoBack()) {
WebViewFragment.mWebView.goBack();
}
}
}
WebViewFragment= This is your nameFragment that content the Webview
mWebView= The name of your WebView (id)
Now. Inside of your Fragment you have to declare static global variable
public static Webview mWebView;
And that's it! Good luck!
Since you are using a fragment, you have to override your activity onbackpressed() method and then test to see what fragment you are using. If you are using the fragment with the webview in it when the user tries to go back then see if the webview can go back. The code would look something like this.
#override
public void onBackPressed()
{
//get a reference to your webviewfragment before this line
//perhaps find the fragment by tag
if(webviewFragment.isVisible())
{
WebView mWebView = webviewFragment.getWebView();
if(mWebView.canGoBack())
{
mWebView.goBack();
return;
}
}
super.onBackPressed();
}
something like that, not exactly like it but close.
I looked already a lot for a solution for my problem and found similar items - but unfortunately I could not get round with that all to solve my problem. Here is what messes me up:
My small project is with a button and an onClickListener on it.
package com.example.wbbtn;
import android.app.Activity;
import android.app.ActionBar;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
public class MainActivity extends Activity {
Button wb1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
wb1 = (Button) findViewById(R.id.b1);
wb1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
}
#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;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
return rootView;
}
}
}
I put a breakpoint at wb1 = (Button) findViewById(R.id.b1);
When I start the debugging with my tablet already set to the debugging mode and connected to the PC and hit the F6 key I get the message that a source is not found.
I've made a screen shot to make things a bit more clear but I was told to have at least 10 reputations to post a pic. Okay - sorry !
Hello - sorry for this late response. Okay here are the links to the screen shots - hope it works:
a) source not found
b) Break point