I am new to android and cannot find a solution online. I have a navigation drawer that leads to the fragment 'RecentNews', which is related to it's XML 'fragment_recent_news'. I have a WebView (webview) setup in the XML, and have set up the fragment so that it should load a page, but it does not. What am I doing incorrectly? I am unsure if the way to properly do this has changed in the past year because nothing online has worked. Thanks
fragment_recent_news(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="com.hardingsoftware.hrcfitness.RecentNews">
<WebView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/webview"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
</RelativeLayout>
RecentNews:
package com.hardingsoftware.hrcfitness;
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;
import android.webkit.WebViewClient;
public class RecentNews extends Fragment {
WebView webView;
public RecentNews() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.fragment_recent_news, container, false);
WebView myWebView = (WebView) rootView.findViewById(R.id.webview);
myWebView.loadUrl("www.hrcfitness.com/recent-news/");
return rootView;
}
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
}
EDIT: I have the page loading now, however I now have another issue, where the webview seems to ignore the relative layout and part of it is lying under the main bar.
Related
I use four tabs in my android application. An image button is in one tab activity and when I click that button I need to go to another activity which is not a tab activity. I tried a lot to do this, but once I click on the tab my app has stopped work. Please if someone could help me to solve this issue. I really appreciate your help.
This is FirstFragment.java file. I use image button in this activity and after click that button need to go to another activity.
package com.example.user.application001.fragmentspkg;
import android.content.Intent;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import com.example.user.application001.Invoice;
import com.example.user.application001.R;
public class FirstFragment extends Fragment {
public FirstFragment(){}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_second, container, false);
ImageButton fabImageButton = (ImageButton) view.findViewById(R.id.fab_image_button);
fabImageButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(getActivity(), Users.class);
startActivity(intent);
}
});
return view;
}
}
This is the XML file
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageButton
android:id="#+id/fab_image_button"
android:layout_width="#dimen/fab_button_diameter"
android:layout_height="#dimen/fab_button_diameter"
android:layout_marginBottom="20dp"
android:layout_marginEnd="36dp"
android:layout_marginRight="36dp"
android:background="#drawable/fab_shape"
android:src="#drawable/plusbtn"
android:tint="#android:color/white"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
</RelativeLayout>
you are using layout of fragment_second in fragment_first change it
use this
View view = inflater.inflate(R.layout.fragment_first, container, false);
instead of this
View view = inflater.inflate(R.layout.fragment_second, container, false);
i know i can make textviews clickable so i can let the user call a number with an intent. but how do i achieve this when inside a fragment. i have multiple fragments and telephone numbers in my string.xml. every fragment has a telephone number, for example the code for one of my fragments:
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class Simmering extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.simmering_layout, container, false);
}
}
and here the xml for this class (fragment):
<FrameLayout 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="at.co.ccc.mondel.Simmering" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="10dp"
android:text="#string/add_sim" />
<TextView
android:id="#+id/call_sim"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="50dp"
android:clickable="true"
android:text="#string/tel_sim" />
</FrameLayout>
please help me, thank you!
I am assuming you are having trouble finding the Text View in that case you can try the following way.
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class Simmering extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.simmering_layout, container, false);
// Here you can access the edit text like this
mTextView = (EditText) rootView.findViewById(R.id.call_sim);
return rootView;
}
}
I'm having problems with a webview inside a fragment. I'm getting a error message saying "java.lang.nullpointerexception" at this line: myWebView.setWebViewClient(new WebViewClient());. I have googled and googled but not found a solution. Any ideas as to what I'm missing?
FragmentB.java
import android.annotation.SuppressLint;
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;
import android.webkit.WebViewClient;
/**
* A simple {#link Fragment} subclass.
*
*/
#SuppressLint("SetJavaScriptEnabled")
public class FragmentB extends Fragment {
public FragmentB() {
// Required empty public constructor
}
private View mContentView;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
mContentView = inflater.inflate(R.layout.fragment_b, container, false);
WebView myWebView = (WebView)mContentView. findViewById(R.id.webView);
myWebView.setWebViewClient(new WebViewClient());
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
myWebView.loadUrl("http://www.nellienova.com");
return mContentView;
}
}
webview.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<WebView
android:id="#+id/webView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
Make sure you are loading the right xml file. I have done this before by mistakenly loading another layout file. You have webview.xml typed above that file but you inflating R.layout.fragment_b
Edit
Each fragment has its own layout file. You can reuse layouts and add multiple layouts to one xml file but through code your fragment will only inflate the file you refer to.
If you want to include other files you have to use include statements to pull those other files in to your xml before you can interact with objects in the other xml files. Click here for more details about include statements. Example below
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width=”match_parent”
android:layout_height=”match_parent”
android:background="#color/app_bg"
android:gravity="center_horizontal">
<include layout="#layout/titlebar"/>
<TextView android:layout_width=”match_parent”
android:layout_height="wrap_content"
android:text="#string/hello"
android:padding="10dp" />
</LinearLayout>
Click here for some good information about fragments. Spend a little time with it and it will save you lots of time troubleshooting problems.
I am hardly trying to create a simple application with a top menu and a changeable view below (by pressing the buttons in the menu fragment we change the view of the fragment below).
So, I have 2 fragments inside the main view but when trying to run the application in the emulator I get an error like:
Cause by android.app (bla bla bla, piece of crap Eclipse doesn't even allow copying the errors):
Trying to instantiate a class com.example.android.topmenu that is not a fragment
So, these are my XML layouts:
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<fragment
android:id="#+id/menuFragment"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:name="com.example.android.topmenu" >
</fragment>
<fragment
android:id="#+id/contentFragment"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:name="com.example.android.bottomcontent" >
</fragment>
</LinearLayout>
topmenu.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<Button
android:id="#+id/Button1"
android:layout_width="wrap_content"
android:layout_height="match_parent" />
</LinearLayout>
bottom_content.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp"
android:orientation="vertical">
<TextView
android:id="#+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#+string/content_text" />
</LinearLayout>
and these are the classes for the main activity and the fragments
main_activity
package com.example.android;
import com.example.android.R;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
public class OLife extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
// The activity is being created
super.onCreate(savedInstanceState);
// Set view
setContentView(R.layout.main);
}
#Override
protected void onDestroy() {
super.onDestroy();
// The activity is about to be destroyed.
super.onDestroy();
// Stop method tracing that the activity started during onCreate()
android.os.Debug.stopMethodTracing();
}
}
topmenu
package com.example.android;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class OLifeMenu extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.topmenu, container, false);
return view;
}
}
bottomcontent
package com.example.android;
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class OLifeMain extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.bottom_content, container, false);
return view;
}
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
}
}
You should be using FragmentActivity instead of Activity that is because you are using support Fragments and multiple Fragments in your activity main
Edit
You can now use the Appcompat support library and extends AppCompatActivity to support toolbar and fragment for lower api.
In my case it turned out, I was doing stuff in onCreate in the wrong order:
setContentView(R.layout.activity_qr_code_scan);
super.onCreate(savedInstanceState);
instead of
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_qr_code_scan);
As you are using fragments in your layout and I suggest you to extend your class from fragment or fragment activity.
I'm trying to get my head around fragments in order to have my apps prepped for ICS.
I have the following files to just get the most basic fragment app you can have. It should have this when launched: One Fragment Layout with a text view "Fragment 1" and next to it another Fragment Layout with "Fragment2".
My package name is com.mwerner.fragments
My files are:
FragmentsActivity.java
ExamplesFragment.java
ExamplesFragment2.java
examples_fragment.xml
examples_fragment2.xml
main.xml
The code for FragmentsActivity.java is:
package com.mwerner.fragments;
import android.app.Activity;
import android.os.Bundle;
public class FragmentsActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
ExamplesFragment.java
package com.mwerner.fragments;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class ExamplesFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.examples_fragment, container, false);
}
}
ExamplesFragment2.java
package com.mwerner.fragments;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class ExamplesFragment2 extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.examples_fragment2, container, false);
}
}
The examples_fragment.xml files just have a linear layout with a textview in it...
Here is the code for the main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<fragment
class="com.mwerner.fragments$ExamplesFragment"
android:id="#+id/list"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="fill_parent"
/>
<fragment
class="com.mwerner.fragments$ExamplesFragment2"
android:id="#+id/viewer"
android:layout_weight="2"
android:layout_width="0dp"
android:layout_height="fill_parent" />
</LinearLayout>
The app crashes on startup with the error
11-07 18:12:12.519: E/AndroidRuntime(696): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mwerner.fragments/com.mwerner.fragments.FragmentsActivity}: android.view.InflateException: Binary XML file line #7: Error inflating class fragment
Can you please tell me what is wrong here? I pretty much copied / pasted the code from the google developers page for fragments.
You defined the path to your fragments incorrectly in your layout xml. Correct the class attributes. Try this:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<fragment
class="com.mwerner.fragments.ExamplesFragment"
android:id="#+id/list"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="fill_parent"
/>
<fragment
class="com.mwerner.fragments.ExamplesFragment2"
android:id="#+id/viewer"
android:layout_weight="2"
android:layout_width="0dp"
android:layout_height="fill_parent" />
</LinearLayout>
Try extending FragmentActivity
public class FragmentsActivity extends FragmentActivity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
When I was writing my first program on Fragments using I incurred same error. Instead of extending "Activity" extend "FragmentActivity" in your launcher activity.