Barthy
Barthy

Reputation: 3231

Android Fragment/ViewPager - ViewGroups losing background color

I am using a ViewPager with a "Tabbed Activity" generated by Android Studio.

When I run the app, the right layouts are loaded into the right fragments and you can easily swipe between fragments. The layout is correct, with all the background colors set like they should.

Unfortunately, as soon as you swipe into another fragment, all Layouts (ViewGroups) lose their background color, which means that they become transparent. The views inside still look the same.

Example: The EditText at the bottom is inside a LinearLayout that should be white (left picture) but becomes grey (right picture).

Two screenshots: 1) on load and 2) after swiping

normal buggy

The strangest thing is, that if you swipe into a fragment and it loses the backgrounds, everything goes back to normal if you swipe a little to the left or right, without changing the tab.

What could be the problem?

CODE

Adapter

public class PageAdapter extends FragmentPagerAdapter {

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

    @Override
    public Fragment getItem(int position) {
        // getItem is called to instantiate the fragment for the given page.
        // Return a PlaceholderFragment (defined as a static inner class below).
        return PageFragment.newInstance(position + 1);
    }

    @Override
    public int getCount() {
        // Show 4 total pages.
        return 4;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return "POS: " + position;
    }
}

Fragment

public class PageFragment extends Fragment {
    private static final String WHICH = "WHICH";

    /**
     * The fragment argument representing the section number for this
     * fragment.
     */

    public PageFragment() {}

    public static PageFragment newInstance(int which){
        PageFragment page = new PageFragment();
        Bundle args = new Bundle();
        args.putInt(WHICH, which);
        page.setArguments(args);
        return page;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView;
        switch(getArguments().getInt(WHICH)){
            case 1:
                rootView = inflater.inflate(R.layout.activity_one, container, false);
                break;
            case 2:
                rootView = inflater.inflate(R.layout.activity_two, container, false);
                break;
            case 3:
                rootView = inflater.inflate(R.layout.activity_four, container, false);
                break;
            case 4:
                rootView = inflater.inflate(R.layout.activity_five, container, false);
                break;
            default:
                rootView = inflater.inflate(R.layout.activity_defautlt, container, false);
                break;
        }
        return rootView;
    }
}

LinearLayout that has incorrect background:

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/layout_id"
        android:layout_alignParentBottom="true"
        android:orientation="horizontal"
        android:layout_alignParentLeft="true"
        android:background="@color/white"
        android:gravity="center"
        android:weightSum="1">

        <EditText
            android:layout_width="0dp"
            android:layout_weight=".85"
            android:layout_height="match_parent"
            android:textColor="@color/grey_2"
            style="@style/inputDefault"
            android:minLines="1"
            android:maxLines="5"
            android:gravity="start|center"
            android:paddingStart="@dimen/activity_horizontal_margin_medium"
            android:paddingEnd="@dimen/activity_horizontal_margin_medium"
            android:inputType="textMultiLine"
            android:hint="@string/write_message"
            android:id="@+id/chatroom_editText" />

        <ImageButton
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight=".15"
            android:src="@drawable/ic_send_black_24dp"
            style="@style/btnImage"
            android:id="@+id/chatroom_send_btn"
            android:onClick="sendItem"
            android:background="@color/white"
            android:layout_margin="0dp"
            android:padding="0dp"
            />

</LinearLayout>

<style name="inputDefault">
    <item name="android:layout_marginRight">0dp</item>
    <item name="android:layout_marginLeft">0dp</item>
    <item name="android:layout_marginTop">@dimen/activity_vertical_margin_small</item>
    <item name="android:layout_marginBottom">@dimen/activity_vertical_margin_small</item>
    <item name="android:paddingTop">@dimen/activity_vertical_margin_small</item>
    <item name="android:paddingBottom">@dimen/activity_vertical_margin_small</item>
    <item name="android:background">@color/white</item>
    <item name="android:textColor">@color/grey_2</item>
    <item name="android:textColorHint">@color/grey_3</item>
    <item name="android:textStyle">normal</item>
    <item name="android:textCursorDrawable">@null</item>
    <item name="android:stateListAnimator" tools:targetApi="lollipop">@null</item>
</style>

Upvotes: 1

Views: 927

Answers (1)

Amir
Amir

Reputation: 16597

In fact it's not related to your LinearLayout or TabActivity or etc. It's usual behaviour of EditText.

If you want your EditText background be transparent or in your case white it's not necessary to define android:background="@color/white" just put background @null and rest of things will be Ok.

Upvotes: 1

Related Questions