diff --git a/api/current.txt b/api/current.txt index 061793ff..c944d334 100644 --- a/api/current.txt +++ b/api/current.txt @@ -664,3 +664,11 @@ package androidx.core.widget { } +package androidx.view { + + public final class LayoutInflaterKt { + ctor public LayoutInflaterKt(); + } + +} + diff --git a/src/androidTest/java/androidx/view/LayoutInflaterTest.kt b/src/androidTest/java/androidx/view/LayoutInflaterTest.kt new file mode 100644 index 00000000..283fd0fb --- /dev/null +++ b/src/androidTest/java/androidx/view/LayoutInflaterTest.kt @@ -0,0 +1,61 @@ +/* + * Copyright (C) 2018 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package androidx.view + +import android.support.test.InstrumentationRegistry +import android.view.Gravity.LEFT +import android.view.LayoutInflater +import android.view.View +import android.view.ViewGroup.LayoutParams +import android.view.ViewGroup.LayoutParams.MATCH_PARENT +import android.widget.LinearLayout +import android.widget.LinearLayout.VERTICAL +import androidx.core.kotlin.test.R +import org.junit.Assert.assertEquals +import org.junit.Assert.assertNotNull +import org.junit.Assert.fail +import org.junit.Test + +class LayoutInflaterTest { + private var context = InstrumentationRegistry.getTargetContext() + private var layoutInflater = LayoutInflater.from(context) + + @Test fun inflateLayout() { + var view = layoutInflater.inflate(R.layout.test_activity, null, false) + assertNotNull(view) + + try { + layoutInflater.inflate(-1, null, false) + fail("should throw exception") + } catch (e: Exception) { + } + + val layout = LinearLayout(context) + layout.orientation = VERTICAL + layout.setHorizontalGravity(LEFT) + layout.layoutParams = LayoutParams(MATCH_PARENT, MATCH_PARENT) + assertEquals(0, layout.childCount) + + view = layoutInflater.inflate(R.layout.test_activity, layout, false) + assertNotNull(view) + assertEquals(0, layout.childCount) + + view = layoutInflater.inflate(R.layout.test_activity, layout, true) + assertNotNull(view) + assertEquals(1, layout.childCount) + } +} diff --git a/src/main/java/androidx/view/LayoutInflater.kt b/src/main/java/androidx/view/LayoutInflater.kt new file mode 100644 index 00000000..df31e86f --- /dev/null +++ b/src/main/java/androidx/view/LayoutInflater.kt @@ -0,0 +1,48 @@ +/* + * Copyright (C) 2018 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +@file:Suppress("NOTHING_TO_INLINE", "EXTENSION_SHADOWED_BY_MEMBER") + +package androidx.view + +import android.support.annotation.LayoutRes +import android.view.InflateException +import android.view.LayoutInflater +import android.view.View +import android.view.ViewGroup +import org.xmlpull.v1.XmlPullParser + +/** + * Inflate a new [View] hierarchy from the specified XML resource. + * + * @throws [InflateException] if there is an error. + */ +inline fun LayoutInflater.inflate( + @LayoutRes resource: Int, + root: ViewGroup?, + attachToRoot: Boolean = false +): T = inflate(resource, root, attachToRoot) as T + +/** + * Inflate a new [View] hierarchy from the specified XML node. + * + * @throws [InflateException] if there is an error. + */ +inline fun LayoutInflater.inflate( + parser: XmlPullParser, + root: ViewGroup?, + attachToRoot: Boolean = false +): T = inflate(parser, root, attachToRoot) as T