Created
August 29, 2018 07:46
-
-
Save bmonjoie/b7d9e150adf50e16f6531a1b3543e474 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<resources> | |
<declare-styleable name="ConditionalView"> | |
<attr name="condition" format="boolean"/> | |
<attr name="ifTrue" format="reference"/> | |
<attr name="ifFalse" format="reference"/> | |
</declare-styleable> | |
</resources> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<resources> | |
<bool name="myFeatureCondition">true</bool> | |
</resources> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package be.xzan.test | |
import android.content.Context | |
import android.support.annotation.LayoutRes | |
import android.util.AttributeSet | |
import android.view.LayoutInflater | |
import android.view.View | |
import android.view.ViewGroup | |
class ConditionalView @JvmOverloads constructor( | |
context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0 | |
) : View(context, attrs, defStyleAttr) { | |
private val condition: Boolean | |
@LayoutRes | |
private val ifTrueLayout: Int | |
@LayoutRes | |
private val ifFalseLayout: Int | |
init { | |
val a = context.theme.obtainStyledAttributes(attrs, R.styleable.ConditionalView, 0, 0) | |
try { | |
condition = a.getBoolean(R.styleable.ConditionalView_condition, false) | |
ifTrueLayout = a.getResourceId(R.styleable.ConditionalView_ifTrue, 0) | |
ifFalseLayout = a.getResourceId(R.styleable.ConditionalView_ifFalse, 0) | |
} finally { | |
a.recycle() | |
} | |
} | |
override fun onAttachedToWindow() { | |
super.onAttachedToWindow() | |
with(parent as ViewGroup) { | |
val view = LayoutInflater.from(context).inflate(if (condition) ifTrueLayout else ifFalseLayout, this, false) | |
val index = indexOfChild(this@ConditionalView) | |
removeView(this@ConditionalView) | |
addView(view, index) | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<ImageView xmlns:android="http://schemas.android.com/apk/res/android" | |
android:src="@mipmap/ic_launcher" | |
android:layout_width="48dp" | |
android:layout_height="48dp"/> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
xmlns:app="http://schemas.android.com/apk/res-auto" | |
android:gravity="center" | |
android:orientation="vertical"> | |
<TextView | |
android:text="First View" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" /> | |
<TextView | |
android:text="Second View" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" /> | |
<be.xzan.test.ConditionalView | |
app:condition="@bool/myFeatureCondition" | |
app:ifTrue="@layout/textview" | |
app:ifFalse="@layout/imageview" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" /> | |
<TextView | |
android:text="Fourth View" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" /> | |
<TextView | |
android:text="Fifth View" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" /> | |
</LinearLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<TextView xmlns:android="http://schemas.android.com/apk/res/android" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:text="Some text for third view :)"/> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment