Home > database >  Show different layout in Android depending if debug mode
Show different layout in Android depending if debug mode

Time:03-18

I have a project with multiple activities and fragments. I'm trying to add a banner at the top of each activity and fragment to say "DEBUG MODE" when the BuildConfig debug setting is set to true or depending on any other conditions.

I have a few ideas on how to achieve this, these are:

  1. Have 2 seperate layout files where 1 is loaded when the debug setting is true.
  2. Create a fragment which is loaded onto each activity and fragment.

I'm not sure what best way to achieve this is and don't even know where to start

CodePudding user response:

My first reaction is 'why?'. But you could create specific layouts for this. In main/res/layout you have activity.xml describing the normal layout. And in the folder debug/res/layout you have the same activity.xml only with the extra stuff you want in debug builds. Do note that you have to keep these two in sync.

Along the same line you could also introduce a new flavor for what you want, but it has the same problem that the layouts need to be kept in sync.

Maybe if you explain the reason to do this it would help to come up with a better solution.

CodePudding user response:

I would add a watermark TextView in each existing Activity layout. This way you don't have to manage multiple versions of the same layout. You can just hide or show the view after the layout is inflated.

If you have multiple Activities, I would put it in each one. You at least don't have to do anything with your Fragments and their layouts. You could put this in a separate layout file and <include> it each of your Activity layouts to reduce code repetition.

It should have a high elevation so it appears in front of everything. And give it a shadow so it will be visible even if the text color matches the background behind it.

enter image description here

<TextView
    android:id="@ id/debugWatermark"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:alpha=".5"
    android:elevation="100dp"
    android:shadowColor="#000000"
    android:shadowRadius="5"
    android:text="@string/debugBuild"
    android:textColor="#FFFFFF"
    android:visibility="gone"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    tools:ignore="TextContrastCheck"
    tools:visibility="visible" />

Then in your activity (or activities), hide it based on whether it's a debug build:

//in onCreate:
binding.debugWatermark.visibility = 
    if (BuildConfig.DEBUG) View.VISIBLE else View.GONE

CodePudding user response:

I saw your comment that you're doing this to let testers identify an APK is a debug or release.

Approach 1 You can use this flag to show/hide a specific view that you need to show for debug builds only:

val isDebuggable = 0 != applicationInfo.flags and ApplicationInfo.FLAG_DEBUGGABLE

if(isDebuggable)
  thisisADebugApkTextView.visbility = View.VISIBILE

Approach 2 (based on comments below)

You can create a CustomTextView that do all the visibility logic based on debug status

class CustomTextView(context: Context, attrs: AttributeSet? = null) : TextView(context, attrs) {

    var isDebuggable = 0 != context.applicationInfo.flags and ApplicationInfo.FLAG_DEBUGGABLE

    init {
        visibility = if(isDebuggable) View.VISIBLE else View.GONE
    }
}

and use it inside your XML as:

<com.path.to.CustomTextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="This is a debug build"/>
  • Related