java – Make the layout below button-ThrowExceptions

Exception or error:

In one of my fragment class, it has two buttons (A and B) on top, used to switch to another fragment when clicked using viewPager.

When buttonA is clicked, it should switch to page A and so on.

When I click button A, how can I make the text in A show below the two buttons?

fragment_menu.xml

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

    <Button
        android:id="@+id/buttonA"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="15dp"
        android:layout_marginTop="10dp"
        android:padding="15dp"
        android:text="ButtonA"
        android:textSize="12sp" />


    <Button
        android:id="@+id/buttonB"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_marginLeft="15dp"
        android:layout_marginTop="10dp"
        android:layout_marginRight="15dp"
        android:layout_toRightOf="@+id/buttonB"
        android:backgroundTint="@color/materialGrey600"
        android:padding="15dp"
        android:text="ButtonB"
        android:textSize="12sp" />


    <androidx.viewpager.widget.ViewPager
        android:id="@+id/pager"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />


</RelativeLayout>

MenuFragment

public class MenuFragment extends BaseFragment {


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View v = inflater.inflate(R.layout.fragment_menu, container, false);
        final ViewPager viewPager = (ViewPager) v.findViewById(R.id.pager);
        viewPager.setAdapter(new ViewPagerAdapter(getActivity().getFragmentManager()));

        Button btnA = (Button) v.findViewById(R.id.buttonA);
        Button btnB = (Button) v.findViewById(R.id.buttonB);

        btnA.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                viewPager.setCurrentItem(0, true);
            }
        });

        btnB.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                viewPager.setCurrentItem(1, true);
            }
        });
        return v;
    }
}

ViewPagerAdapter

  public class ViewPagerAdapter extends FragmentPagerAdapter {

        public ViewPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            switch (position)
            {
                case 0:
                    return new A()
                case 1:
                    return new B()

            }
            return null; 
        }



        @Override
        public int getCount() {
            return 2; 
        }
    }

Output

enter image description here

How to solve:

try with below code.hope it is helpful for you 😉
P.S. You just need to set button style as you want 🙂

  <RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <androidx.viewpager.widget.ViewPager
            android:id="@+id/pager"
            android:layout_width="match_parent"
            android:layout_above="@id/llButton"
            android:layout_height="match_parent" />


        <LinearLayout
            android:id="@+id/llButton"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:layout_alignParentBottom="true"
            android:layout_marginBottom="10dp">

            <Button
                android:id="@+id/buttonA"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dp"
                android:padding="15dp"
                android:text="ButtonA"
                android:textSize="12sp" />


            <Button
                android:id="@+id/buttonB"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="15dp"
                android:layout_marginTop="10dp"
                android:layout_marginRight="15dp"
                android:backgroundTint="@color/colorAccent"
                android:padding="15dp"
                android:text="ButtonB"
                android:textSize="12sp" />

        </LinearLayout>


    </RelativeLayout>

###

Since you are using Relative Layout you can add layout_below

 <androidx.viewpager.widget.ViewPager
    android:layout_below="@id/buttonB"
    android:id="@+id/pager"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

###

you are doing it wrong, You cannot align a button to itself. That’s why your buttons are not working properly. In your button B do this:

 android:layout_toRightOf="@+id/buttonA"

instead of this:

 android:layout_toRightOf="@+id/buttonB"

And to your viewPager add "android:layout_below:"

 <androidx.viewpager.widget.ViewPager
        android:id="@+id/pager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/buttonA" />

###

You have to set your layout like this....

[I think you want something like this 2 button fisrt and then viewpager for change the content ][1]


  [1]: https://i.stack.imgur.com/dluXA.png

**Code for main Page **


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:orientation="vertical"
    android:padding="5dp"
    android:layout_height="match_parent">


    <LinearLayout
        android:layout_width="match_parent"
        android:padding="10dp"
        android:layout_marginTop="10dp"
        android:layout_height="match_parent"
        android:orientation="horizontal">


        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/frame_content">
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                android:layout_gravity="center_horizontal">

                <Button
                    android:id="@+id/btn_donner"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_weight="2"
                    android:background="@drawable/rect_outline"
                    android:text="Donor's"
                    android:textAlignment="center"
                    android:textColor="@color/white" />

                <Button
                    android:id="@+id/btn_blood_bank"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="Blood Bank"
                    android:background="@drawable/rect_outline"
                    android:layout_weight="2"
                    android:textAlignment="gravity"
                    android:textColor="@color/white" />
            </LinearLayout>
        </FrameLayout>
    </LinearLayout>
</LinearLayout>


Code For Java File of two Buttons And ViewPager


      //Click Event for donor's and blood bank .....
        View.OnClickListener listener = new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Fragment fragment = null;
                if (view == view.findViewById(R.id.btn_donner))
                {
                    fragment = new TabDonnerFragment();
                }
                else
                {
                    fragment = new TabBloodBankFragment();
                }
                FragmentManager manager = getFragmentManager();
                FragmentTransaction transaction = manager.beginTransaction();
                transaction.replace(R.id.frame_content, fragment);
                transaction.commit();
            }
        };
        btn_donner.setOnClickListener(listener);
        btn_blood_bank.setOnClickListener(listener);

        return root;
    }


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


        Fragment fragment = null;
        fragment = new TabDonnerFragment();
        FragmentManager manager = getFragmentManager();
        FragmentTransaction transaction = manager.beginTransaction();
        transaction.replace(R.id.frame_content, fragment);
        transaction.commit();
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *