One Layout Class and many fragment layouts in Android Studio


Fragment programming seems to be smart steps towards making worthy applications in Android Studio. In my last experiment, I tried to develop fragments in my app, but I realized that each fragment layout is represented by a Java class which handles the activities. 2017-02-26_001655

How can I use same Java class for different layouts? The first option created an argument constructor for the Java class, but the Fragment sibling Java class doesn’t support thing like that, instead, Android Studio supplies SetArguments and getArguments methods for the purpose.

I have group Main fragment layout carries many buttons for various functions like create,  list, Report fragments respectively. I want to use same Java Class for every fragment including the maingroupFragment.

2017-06-22_084924

The Drawer app code

private void selectItem(int position) {

    mCurrentSelectedPosition = position;
    if (mDrawerListView != null) {
        mDrawerListView.setItemChecked(position, true);
    }
    if (mDrawerLayout != null) {
        mDrawerLayout.closeDrawer(mFragmentContainerView);
    }
    if (mCallbacks != null) {
        mCallbacks.onNavigationDrawerItemSelected(position);
    }
    android.support.v4.app.FragmentManager frg=getFragmentManager();
    switch (position){
        case 0:
            frg.beginTransaction().replace(R.id.FrameContainer, new DashboardFragment()).commit();
            break;
        case 1:
            frg.beginTransaction().replace(R.id.FrameContainer, new LedgerFragment()).commit();
            break;
        case 2:
            frg.beginTransaction().replace(R.id.FrameContainer, new PaymentFragment()).commit();
            break;
        case 3:
            frg.beginTransaction().replace(R.id.FrameContainer, new ReceiptFragment()).commit();
            break;
        case 4:
            frg.beginTransaction().replace(R.id.FrameContainer, new ReportFragment()).commit();
            break;
        case 5:
            Bundle b=new Bundle();
            b.putInt("GroupIndex", R.layout.groupmain_fragment);
            GroupFragment g=new GroupFragment();
            g.setArguments(b);
            frg.beginTransaction().replace(R.id.FrameContainer,g).commit();
            break;
    }
}

The Fragment class

package com.example.developerm.myapplication;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

/**
* Created by developerM on 14-Jun-2017.
*/
public class GroupFragment extends Fragment {



@Override
public void setArguments(Bundle args) {
super.setArguments(args);
}

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// View rootview=inflater.inflate(R.layout.group_registration,container,false);
View rootview=null;
if(getArguments().getInt("GroupIndex")==R.layout.groupmain_fragment){
rootview = inflater.inflate(getArguments().getInt("GroupIndex", 0), container, false);
}
else if(getArguments().getInt("GroupNew")==R.layout.group_registration){

rootview = inflater.inflate(getArguments().getInt("GroupNew", 1), container, false);
}
else if(getArguments().getInt("GroupManage")==R.layout.managegroupsfragment)
{
rootview = inflater.inflate(getArguments().getInt("GroupManage", 2), container, false);
}
else if(getArguments().getInt("GroupAcReports")==R.layout.acreportgroupfragment)
{
rootview = inflater.inflate(getArguments().getInt("GroupAcReports", 3), container, false);
}
else if(getArguments().getInt("GroupList")==R.layout.grouplistfragment)
{
rootview = inflater.inflate(getArguments().getInt("GroupList", 4), container, false);
}

Bundle b=new Bundle();
Button bt;
if(getArguments().getInt("GroupIndex")==R.layout.groupmain_fragment){
bt=(Button) rootview.findViewById(R.id.btn_newGroup);
bt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Bundle b=new Bundle();
b.putInt("GroupNew", R.layout.group_registration);
GroupFragment g=new GroupFragment();
g.setArguments(b);
android.support.v4.app.FragmentManager frg=getFragmentManager();
frg.beginTransaction().replace(R.id.FrameContainer, g).commit();
}
});
bt=(Button) rootview.findViewById(R.id.btn_findEdit);
bt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Bundle b=new Bundle();
b.putInt("GroupManage", R.layout.managegroupsfragment);
GroupFragment g=new GroupFragment();
g.setArguments(b);
android.support.v4.app.FragmentManager frg=getFragmentManager();
frg.beginTransaction().replace(R.id.FrameContainer, g).commit();
}
});
bt=(Button) rootview.findViewById(R.id.btn_acReports);
bt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Bundle b=new Bundle();
b.putInt("GroupAcReports", R.layout.acreportgroupfragment);
GroupFragment g=new GroupFragment();
g.setArguments(b);
android.support.v4.app.FragmentManager frg=getFragmentManager();
frg.beginTransaction().replace(R.id.FrameContainer, g).commit();
}
});
bt=(Button) rootview.findViewById(R.id.btn_groupList);
bt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Bundle b=new Bundle();
b.putInt("GroupList", R.layout.grouplistfragment);
GroupFragment g=new GroupFragment();
g.setArguments(b);
android.support.v4.app.FragmentManager frg=getFragmentManager();
frg.beginTransaction().replace(R.id.FrameContainer, g).commit();
}
});


}

return rootview;
}
}

The above codes will be explained what you have to do. Use the Argument and getArgument in place.

Expect comments and suggestions

1 thought on “One Layout Class and many fragment layouts in Android Studio

  1. Pingback: Hoto start a new activity in Android Studio | DeveloperM – A developer's Manual

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.