Search code examples
androidviewfragmentinvisible

Android - views are invisible when they shouldn't be (old devices)


I am testing and developing apps on a normal phone with Android API level 21. Every view is working they way it should and there are no problems.

However, when switching to an older device with API 16, some specific views of fragments are just not visible. When leaving the fragment and going back to the same fragment, it WILL show.

So for instance, i open the app and the beginning fragment is created. The imagebuttons are loaded and displayed, the text that should appear under them stay hidden. <-- no idea why

After leaving that fragment and going back to it, the text under the buttons DO appear.

This happens with many other fragments and i have no idea HOW this can actually happen, it is inconsistent as well..

This is the specific part i talked about:

<LinearLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:id="@+id/dashboard_shortcut_timetable"
        android:paddingBottom="10dp"
        android:layout_weight="0.3">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            <ImageButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/dashboard_shortcut_1"
                android:src="@drawable/ic_shortcut"
                android:background="@color/bg_white"
                android:layout_centerHorizontal="true"/>
        </RelativeLayout>
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/dashboard_shortcut_timetable"
            android:id="@+id/dashboard_shortcut_textview_1"
            android:gravity="center|bottom"/>

Basic representation of how it looks:

enter image description here

I am just setting the text, basic Android stuff..

TextView dashboardShortCutTextview1 = (TextView) view.findViewById(R.id.dashboard_shortcut_textview_1);
dashboardShortCutTextview1.setText(title1);

So this happens with other views, as well as SOME menu items, that i have to LATER set invisible/visible to actually make it work.

My question is; why does this happen, how to fix it, is it dependent on the device or API level?


Solution

  • For anyone having the same problem, i found a solution to my problem;

    Apparently the multidex had to be enabled, because i had used to many methods/libraries. Too bad the logcat did not give me ANY indication this was happening and that this was causing the problem.

    What is this multidex? Read here

    How to enable the multidex support?

    In build.gradle

    android {
        ...
        defaultConfig {
            ...
            multiDexEnabled true
        }
    }
    
    dependencies {
        ...
        compile 'com.android.support:multidex:1.0.1'
        ...
    }
    

    For backwards compatibility add this in your main application:

    @Override
    protected void attachBaseContext(Context base)
    {
        super.attachBaseContext(base);
        MultiDex.install(this);
    }