Android studio dynamic fragments according to date

133 Views Asked by At

I have an android studio project with SQLite Room database. There is a table column that contains the date. I want to add several dynamic fragments according to the date and filter data month-wise, day-wise, year-wise, etc.

Image of expected outcome

How do I add tabs and fragments like this using ViewPager.

1

There are 1 best solutions below

0
JonR85 On

https://developer.android.com/guide/fragments/create

While your activity is running, you can make fragment transactions such as adding, removing, or replacing a fragment. In your FragmentActivity, you can get an instance of the FragmentManager, which can be used to create a FragmentTransaction. Then, you can instantiate your fragment within your activity's onCreate() method using FragmentTransaction.add(), passing in the ViewGroup ID of the container in your layout and the fragment class you want to add and then commit the transaction, as shown in the following example:

public class ExampleActivity extends AppCompatActivity {
    public ExampleActivity() {
        super(R.layout.example_activity);
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction()
                .setReorderingAllowed(true)
                .add(R.id.fragment_container_view, ExampleFragment.class, null)
                .commit();
        }
    }
}

If you need further help. Post your code and explain what isn't working. I'll be glad to help.