Skip to content
This repository has been archived by the owner on Nov 14, 2018. It is now read-only.

LayoutInflater extensions #423

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions api/current.txt
Original file line number Diff line number Diff line change
Expand Up @@ -664,3 +664,11 @@ package androidx.core.widget {

}

package androidx.view {

public final class LayoutInflaterKt {
ctor public LayoutInflaterKt();
}

}

61 changes: 61 additions & 0 deletions src/androidTest/java/androidx/view/LayoutInflaterTest.kt
Original file line number Diff line number Diff line change
@@ -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<View>(R.layout.test_activity, null, false)
assertNotNull(view)

try {
layoutInflater.inflate<View>(-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<View>(R.layout.test_activity, layout, false)
assertNotNull(view)
assertEquals(0, layout.childCount)

view = layoutInflater.inflate<View>(R.layout.test_activity, layout, true)
assertNotNull(view)
assertEquals(1, layout.childCount)
}
}
48 changes: 48 additions & 0 deletions src/main/java/androidx/view/LayoutInflater.kt
Original file line number Diff line number Diff line change
@@ -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 <reified T : View> 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 <reified T : View> LayoutInflater.inflate(
parser: XmlPullParser,
root: ViewGroup?,
attachToRoot: Boolean = false
): T = inflate(parser, root, attachToRoot) as T