Skip to content
This repository was archived by the owner on Jul 13, 2020. It is now read-only.

Add start and end rules to RelativeLayout.LayoutParams extensions. #497

Merged
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@
@file:Suppress("NOTHING_TO_INLINE", "unused")
package org.jetbrains.anko

import android.os.Build
import android.support.annotation.IdRes
import android.support.annotation.RequiresApi
import android.view.View
import android.widget.RelativeLayout.*

Expand Down Expand Up @@ -68,6 +70,16 @@ inline fun LayoutParams.leftOf(view: View) {
addRule(LEFT_OF, id)
}

/**
* Place the current View to the start of [view].
*/
@RequiresApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
inline fun LayoutParams.startOf(view: View) {
val id = view.id
if (id == View.NO_ID) throw AnkoException("Id is not set for $view")
addRule(START_OF, id)
}

/**
* Place the current View to the right of [view].
*/
Expand All @@ -77,6 +89,16 @@ inline fun LayoutParams.rightOf(view: View) {
addRule(RIGHT_OF, id)
}

/**
* Place the current View to the end of [view].
*/
@RequiresApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
inline fun LayoutParams.endOf(view: View) {
val id = view.id
if (id == View.NO_ID) throw AnkoException("Id is not set for $view")
addRule(END_OF, id)
}

/**
* Set the current View left attribute the same as for [view].
*/
Expand All @@ -86,6 +108,16 @@ inline fun LayoutParams.sameLeft(view: View) {
addRule(ALIGN_LEFT, id)
}

/**
* Set the current View start attribute the same as for [view].
*/
@RequiresApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
inline fun LayoutParams.sameStart(view: View) {
val id = view.id
if (id == View.NO_ID) throw AnkoException("Id is not set for $view")
addRule(ALIGN_START, id)
}

/**
* Set the current View top attribute the same as for [view].
*/
Expand All @@ -104,6 +136,16 @@ inline fun LayoutParams.sameRight(view: View) {
addRule(ALIGN_RIGHT, id)
}

/**
* Set the current View end attribute the same as for [view].
*/
@RequiresApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
inline fun LayoutParams.sameEnd(view: View) {
val id = view.id
if (id == View.NO_ID) throw AnkoException("Id is not set for $view")
addRule(ALIGN_END, id)
}

/**
* Set the current View bottom attribute the same as for [view].
*/
Expand Down Expand Up @@ -140,16 +182,34 @@ inline fun LayoutParams.bottomOf(@IdRes id: Int) = addRule(BELOW, id)
*/
inline fun LayoutParams.leftOf(@IdRes id: Int) = addRule(LEFT_OF, id)

/**
* Place the current View to the start of the View with a given [id].
*/
@RequiresApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
inline fun LayoutParams.startOf(@IdRes id: Int): Unit = addRule(START_OF, id)

/**
* Place the current View to the left of the View with a given [id].
*/
inline fun LayoutParams.rightOf(@IdRes id: Int) = addRule(RIGHT_OF, id)

/**
* Place the current View to the end of the View with a given [id].
*/
@RequiresApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
inline fun LayoutParams.endOf(@IdRes id: Int): Unit = addRule(END_OF, id)

/**
* Set the current View left attribute the same as for View with a given [id].
*/
inline fun LayoutParams.sameLeft(@IdRes id: Int) = addRule(ALIGN_LEFT, id)

/**
* Set the current View start attribute the same as for View with a given [id].
*/
@RequiresApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
inline fun LayoutParams.sameStart(@IdRes id: Int): Unit = addRule(ALIGN_START, id)

/**
* Set the current View top attribute the same as for View with a given [id].
*/
Expand All @@ -160,6 +220,12 @@ inline fun LayoutParams.sameTop(@IdRes id: Int) = addRule(ALIGN_TOP, id)
*/
inline fun LayoutParams.sameRight(@IdRes id: Int) = addRule(ALIGN_RIGHT, id)

/**
* Set the current View end attribute the same as for View with a given [id].
*/
@RequiresApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
inline fun LayoutParams.sameEnd(@IdRes id: Int): Unit = addRule(ALIGN_END, id)

/**
* Set the current View bottom attribute the same as for View with a given [id].
*/
Expand All @@ -168,12 +234,14 @@ inline fun LayoutParams.sameBottom(@IdRes id: Int) = addRule(ALIGN_BOTTOM, id)
/**
* Align the current View's start edge with another child's start edge.
*/
inline fun LayoutParams.alignStart(@IdRes id: Int) = addRule(18, id) // ALIGN_END
@RequiresApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
inline fun LayoutParams.alignStart(@IdRes id: Int): Unit = addRule(ALIGN_START, id)

/**
* Align the current View's end edge with another child's end edge.
*/
inline fun LayoutParams.alignEnd(@IdRes id: Int) = addRule(19, id) // ALIGN_END
@RequiresApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
inline fun LayoutParams.alignEnd(@IdRes id: Int): Unit = addRule(ALIGN_END, id)

/**
* Align the current View's top edge with its parent's top edge.
Expand Down Expand Up @@ -213,12 +281,14 @@ inline fun LayoutParams.centerInParent() = addRule(CENTER_IN_PARENT)
/**
* Align the current View's start edge with its parent's start edge.
*/
inline fun LayoutParams.alignParentStart() = addRule(20) // ALIGN_PARENT_START
@RequiresApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
inline fun LayoutParams.alignParentStart(): Unit = addRule(ALIGN_PARENT_START)

/**
* Align the current View's end edge with its parent's end edge.
*/
inline fun LayoutParams.alignParentEnd() = addRule(21) // ALIGN_PARENT_END
@RequiresApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
inline fun LayoutParams.alignParentEnd(): Unit = addRule(ALIGN_PARENT_END)

/**
* Positions the baseline of this view on the baseline of the given anchor [view].
Expand Down