Annie Annie - 11 days ago 10
Android Question

Is there a way to give a default view of xml on click of button which loads fragment in a container in android

I have a frame layout which loads fragment on click of button which is all inside a fragment, but whenever i navigate to that fragment then it shows only those buttons and when i click on them then only the fragment shows up. I knew that all things are working correctly in this but i want to know is there a way to provide a default xml view for that fragment means before the click of any those button, the user is able to view an xml in that frame layout?
P.S. : I am new to fragments so please answer calmly with detail. Thanks in advance.This the fragment having two buttons in yellow and blue on which i want to show a default xml before clicking on these buttons by user

The java code is:

public class AddHearingFragment extends Fragment {
Button b1,b2;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
if (container != null) {
container.removeAllViews();
}
View view = inflater.inflate(R.layout.hearing,
container, false);
b1=(Button) view.findViewById(R.id.button8);
b2=(Button) view.findViewById(R.id.button7);


b1.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Myhearingfrag myf = new Myhearingfrag();

FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.add(R.id.contentFragment, myf);
transaction.commit();
}
});
b2.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Myteamhearingfrag myf = new Myteamhearingfrag();

FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.add(R.id.contentFragment, myf);
transaction.commit();
}
});

return view;
}}


And the xml code:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:layout_marginTop="55dp" xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">

<Button
android:background="#FFBB00"
android:layout_width="180dp"
android:layout_height="wrap_content"
android:text="MY HEARING"
android:id="@+id/button8"
android:layout_gravity="center_horizontal" />
<Button
android:background="#018AC1"
android:layout_width="180dp"
android:layout_height="wrap_content"
android:text="MY TEAM HEARING"
android:id="@+id/button7"
android:layout_gravity="right"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<FrameLayout
android:id="@+id/contentFragment"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:layout_below="@+id/button8" />

</RelativeLayout>

Answer

if you are using viewpager with tabs then use below line in your onCreate() method

 //Creating our pager adapter
        Pager adapter = new Pager(getSupportFragmentManager(), tabLayout.getTabCount());

in Pager use:

public class Pager extends FragmentStatePagerAdapter {

    //integer to count number of tabs
    int tabCount;

    //Constructor to the class 
    public Pager(FragmentManager fm, int tabCount) {
        super(fm);
        //Initializing tab count
        this.tabCount= tabCount;
    }

    //Overriding method getItem
    @Override
    public Fragment getItem(int position) {
        //Returning the current tabs 
        switch (position) {
            case 0:
                Tab1 tab1 = new Tab1(); // first fragment
                return tab1;
            case 1:
                Tab2 tab2 = new Tab2(); // second fragment
                return tab2;
            case 2: 
                Tab3 tab3 = new Tab3(); // third fragment
                return tab3;
            default:
                return null;
        }
    }

    //Overriden method getCount to get the number of tabs 
    @Override
    public int getCount() {
        return tabCount;
    }
}

try this link : https://www.simplifiedcoding.net/android-tablayout-example-using-viewpager-fragments/

And If you are not using viewpager then use this link: http://www.theappguruz.com/blog/easy-way-to-create-tab-layout-in-android-without-viewpager

And according to your code. I have just modified your code. check this

public class AddHearingFragment extends Fragment {
Button b1,b2;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    if (container != null) {
        container.removeAllViews();
    }
    View view = inflater.inflate(R.layout.hearing,
            container, false);
b1=(Button) view.findViewById(R.id.button8);
    b2=(Button) view.findViewById(R.id.button7);

 Myhearingfrag myf = new Myhearingfrag();

            FragmentTransaction transaction = getFragmentManager().beginTransaction();
            transaction.add(R.id.contentFragment, myf);
            transaction.commit();

    b1.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            Myhearingfrag myf = new Myhearingfrag();

            FragmentTransaction transaction = getFragmentManager().beginTransaction();
            transaction.add(R.id.contentFragment, myf);
            transaction.commit();
        }
    });
    b2.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            Myteamhearingfrag myf = new Myteamhearingfrag();

            FragmentTransaction transaction = getFragmentManager().beginTransaction();
            transaction.add(R.id.contentFragment, myf);
            transaction.commit();
        }
    });

    return view;
}}