How to setContentView in a fragment?
You dont call setContentView
in fragments, in fact you need to return a View
from onCreateView
.
Try replacing:
setContentView(new SampleView(this));
With this:
return new SampleView(this);
how to set setContentView in fragment
On a fragment you don't call setContentView
explicitly, you return the view after inflating it, as you are. So instead of calling setContentView
consider adding the view aboutPage
to rootView
or one of its children views.
For example, say your layout R.layout.fragment_navigation
contains a LinearLayout
(or any other ViewGroup
for that matter) with an ID of content
. You would do this, before your return statement:
LinearLayout content = (LinearLayout) rootView.findViewById(R.id.content);
content.addView(aboutPage); //<-- Instead of setContentView(aboutPage)
You'll have to adjust this to your layout, I don't know what's inside it.
Full example
fragment.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"
android:id="@+id/container">
</RelativeLayout>
CustomFragment.java
public class FragmentExample extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
ViewGroup viewGroup = (ViewGroup) inflater.inflate(R.layout.fragment, container, false);
Element versionElement = new Element();
versionElement.setTitle("Version 6.2");
Element adsElement = new Element();
adsElement.setTitle("Advertise with us");
View aboutPage = new AboutPage(getActivity())
.isRTL(false)
.addItem(versionElement)
.addItem(adsElement)
.addGroup("Connect with us")
.addEmail("elmehdi.sakout@gmail.com")
.addFacebook("the.medy")
.addTwitter("medyo80")
.addYoutube("UCdPQtdWIsg7_pi4mrRu46vA")
.addPlayStore("com.ideashower.readitlater.pro")
.addInstagram("medyo80")
.addGitHub("medyo")
.create();
viewGroup.addView(aboutPage);
return viewGroup;
}
}
How to use setContentView in fragment instead of an activity
These are the mistakes you made in your code.
AllAssetsFragment
Move the line below to the bottom of the
onCreateView()
method. It is a return statement, therefore, it has to be the last thing in the function.return inflater.inflate(R.layout.fragment_allassets, container, false);
You can't use
startActivity(...)
to launch a fragment. This method is strictly for launching activities. To learn how to launch a fragment, check this awesome answer out.Replace
this
withgetActivity()
in the lines below:recyclerView.setLayoutManager(new LinearLayoutManager(this));
requestQueue = Volley.newRequestQueue(this);
FavoritesFragment
Replace
this
withgetActivity()
in the line below:rv.setLayoutManager(new LinearLayoutManager(this));
Addendum:
To launch a fragment within an activity (in your case, MainActivity), follow the following steps:
In your activity_main.xml file, create a FrameLayout that would house your fragment, you can give it an id:
container
. If you don't know how to do this, just paste the code below:<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/container" />When you want to launch a fragment from an activity, simply use this code snippet:
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.container, new AllAssetsFragment());
transaction.commit();When you want to launch a fragment from another fragment, simply use this code snippet:
FragmentTransaction transaction = getActivity().getFragmentManager().beginTransaction();
transaction.replace(R.id.container, new AllAssetsFragment());
transaction.commit();In the code snippets above, if
getFragmentManager()
does not work or if you are using AndroidX, usegetSupportFragmentManager()
instead.
Is setContentView() needed to inflate a fragment?
when do I need to use setContentView()?
Any time you have an Activity
that you want to display UI, you must call setContentView()
.
For an Activity
that hosts Fragment
s, you still need to call setContentView()
so that you can have something to put the Fragments inside of (e.g. R.id.settings_container
will be part of the activity's content view).
I followed the code in the MySettingsActivity, but without setContentView() the app crashes with the following error.
It is either an outright mistake in that tutorial, or they simply omitted it for brevity.
Some tutorials used
setContentView()
but some don't.
Again, for Activity
, you would only not use setContentView()
if you didn't want your activity to display anything (maybe it starts up to handle some Intent
but then finishes itself immediately).
For Fragment
, the view is created by overriding onCreateView()
and returning a view, rather than by calling setContentView()
.
Android Fragment isn't accepting setcontentview
You can not call setContentView in Fragment. You should override the onCreatView method of fragment and inflate your layout as follows:
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v= inflater.inflate(R.layout.tab_fragment_2, container, false);
//to get view like textview, button etc use v.findViewById(yourid)
TextView textView = v.findViewById(R.id.textView2);
return v;
}
}.
Related Topics
Performing Action After Fragment Transaction Animation Is Finished
How to Change the Android App Package Name When Assembling with Gradle
Setrotation(90) to Take Picture in Portrait Mode Does Not Work on Samsung Devices
Dialogfragment with Clear Background (Not Dimmed)
Android:Static Variable Null on Low Memory
How Does Bitmap Allocation Work on Oreo, and How to Investigate Their Memory
Android: Appwidget with Custom View Not Working
How to Customize List Preference Radio Button
Materialcomponents Theme Alert Dialog Buttons
Admob Shows Test Ads But Not Real Ads
Calling Startintentsenderforresult from Fragment (Android Billing V3)
Android - Inner Element Must Either Be a Resource Reference or Empty
How to Import Android Studio Project in Eclipse
Android String.Xml Reading HTML Tags Problem
How to Include Data Files with the App's APK
Java.Lang.Securityexception: !@Too Many Alarms (500) Registered from Pid 10790 Uid 10206