1

I'm making football application and it has 4 buttons on the top side of the screen. Every button will open different fragment. Now when I open the app by default it's showing 4 buttons and empty field under them where will be shown content of fragments. What should I do so that one of fragments will be opened by default(as MainActivity but it should stay fragment)? Further, how can I sign(note, mark) the choosen button so that user will see which button is selected now? Thanks, if something is unclear tell me and I'll explain further

Yusuf
  • 145
  • 6
  • 12

2 Answers2

1

You can either add a Fragment statically using the activity's layout XML:

<fragment android:name="com.example.myapp.myfragment"
              android:id="@+id/my_fragment"
              android:layout_width="match_parent"
              android:layout_height="match_parent" />

Or you could add it dynamically when your activity starts up:

Fragment myFragment = new MyFragment();       
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.add(R.id.fragment_container, myFragment).commit();
fractalwrench
  • 4,028
  • 7
  • 33
  • 49
0

You could read this: https://developer.android.com/training/basics/fragments/index.html where you also have a sample.

But basically I see 3 options.

  1. Like the answer by fractalwrench, to add the fragment through your xml layout files.

  2. Also like the answer by fractalwrench, add the fragment dynamically by replacing it in a container view, like a FrameLayout in your xml layout files.

  3. You said you have 4 buttons for 4 different fragments. Then I would use the viewpager to swtich between the fragments. You can find an example here: https://github.com/codepath/android_guides/wiki/ViewPager-with-FragmentPagerAdapter.

Anyway, option 2 is probably good for you as well. :)

just_user
  • 11,769
  • 19
  • 90
  • 135