Skip to content Skip to sidebar Skip to footer

Adding Elements To Fragment_main.xml Does Noting

I added EditText and Button tags to the fragments_main.xml. But it doesn't show a text field and button when i run the application. However when i add the EditText and Button to ac

Solution 1:

Where do you load the fragment_main.xml in your code?

Typically, an Activity should load a Fragment and the Fragment should load the xml file that you just mentioned.

In the above code, you are only loading the activity_main.xml file in the MainActivity. Therefore, only the content of activity_main.xml is displayed in the view.

UPDATE

To load the fragment_main XML file, you can either:

  • Replace the line of code in MainActivity from setContentView(R.layout.activity_main); to setContentView(R.layout.fragment_main);

  • OR, create a class extending Fragment in the MainActivity and let that class load the fragment_main XML.

To do the latter, you can follow the step by step guide here: Fragments | Adding a UI

In essence, Create a class:

publicstaticclassExampleFragmentextendsFragment {
    @Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragmentreturn inflater.inflate(R.layout.fragment_main, container, false);
    }
}

And load the created Fragment class in your MainActivity (Fragments | Adding a Fragment to an Activity).

To load, you can either declare the fragment inside the activity_main.xml layout

<?xml version="1.0" encoding="utf-8"?><LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:orientation="horizontal"android:layout_width="match_parent"android:layout_height="match_parent"><fragmentandroid:name="com.example.ui.ExampleFragment"android:id="@+id/example_fragment"android:layout_width="match_parent"android:layout_height="match_parent" /></LinearLayout>

Or try the answer mustafa gave.

I hope this helps.

Solution 2:

Can you try?

publicclassMainActivityextendsActivity {

    @OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        FrameLayoutframe=newFrameLayout(this);

        setContentView(frame, newLayoutParams(LayoutParams.MATCH_PARENT,    LayoutParams.MATCH_PARENT));

        if (savedInstanceState == null) {
            FragmentnewFragment=newExampleFragment();
            FragmentTransactionft= getFragmentManager().beginTransaction();
            ft.add(newFragment).commit();
        }
    }

    @OverridepublicbooleanonCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        returntrue;
    }
    }

ExampleFragment.java

publicclassExampleFragmentextendsFragment {
            @Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,
                    Bundle savedInstanceState) {
                View view=inflater.inflate(R.layout.fragment_main.xml, container, false);

return view;
            }
        }

Post a Comment for "Adding Elements To Fragment_main.xml Does Noting"