Activity_main.xml is as follows
<LinearLayout 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" > <Button android:id="@+id/button_one_activity_one" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="First Button" /> <fragment android:name="fragments.FirstFragment" android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/first_fragment" /> <Button android:id="@+id/button_two_activity_one" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Second Button" /> </LinearLayout>
The main activity class is similar to this
package com.example.testfragmentshoneycomb; import android.os.Bundle; import android.app.Activity; import android.view.Menu; public class MainActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } }
first_fragment.xml is as follows
<?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="wrap_content" android:orientation="vertical" android:background="@color/grey" >" <TextView android:id="@+id/text_view_one_fragment" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Text View one" /> <TextView android:id="@+id/text_view_two_fragment" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Text View two" /> <TextView android:id="@+id/text_view_three_fragment" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Text View three" /> </LinearLayout>
The FirstFragment class is similar to this
package fragments; import com.example.testfragmentshoneycomb.R; import android.app.Fragment; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; public class FirstFragment extends Fragment{ @Override public void onCreate(Bundle savedInstanceState) {
Shows only the first button and nothing else on the screen. If I remove the first button from Activity_main.xml, it will display the fragment, but not the second button.
The minimum version of the SDK is 11, and the build goal is Android 4.1.
source share