How to Setcontentview in a Fragment

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

Sample Image

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 with getActivity() in the lines below:

    recyclerView.setLayoutManager(new LinearLayoutManager(this));

    requestQueue = Volley.newRequestQueue(this);

FavoritesFragment

  • Replace this with getActivity() 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:

  1. 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" />
  2. 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();
  3. 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();
  4. In the code snippets above, if getFragmentManager() does not work or if you are using AndroidX, use getSupportFragmentManager() 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 Fragments, 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



Leave a reply



Submit