Commit fb13b554 authored by SusanneFischer's avatar SusanneFischer
Browse files

added view component containing regular app bar content

parent 04109fd0
package de.asta.hochschule.trier.verleih.app.component
import android.content.Context
import android.util.*
import android.view.*
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.content.ContextCompat
import com.google.android.material.textview.MaterialTextView
import de.asta.hochschule.trier.verleih.R
import de.hdodenhof.circleimageview.CircleImageView
class AppBarContent @JvmOverloads constructor(
context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : ConstraintLayout(context, attrs, defStyleAttr) {
var titleTextView: MaterialTextView
var profileImageView: CircleImageView
init {
val styledAttributes = context.obtainStyledAttributes(
attrs,
R.styleable.AppBarContent, 0, 0
)
val title = styledAttributes.getString(R.styleable.AppBarContent_title)
val image =
styledAttributes.getResourceId(R.styleable.AppBarContent_profileImage, R.drawable.chick)
styledAttributes.recycle()
LayoutInflater.from(context).inflate(R.layout.appbar_content, this, true)
titleTextView = (getChildAt(0) as ViewGroup).getChildAt(1) as MaterialTextView
titleTextView.text = title
profileImageView = (getChildAt(0) as ViewGroup).getChildAt(0) as CircleImageView
profileImageView.setImageDrawable(ContextCompat.getDrawable(context, image))
profileImageView.setOnClickListener { Log.d("AppBarContent", "onClick") }
}
fun setTitle(title: String) {
titleTextView.text = title
}
fun setProfileImage(drawableId: Int) {
profileImageView.setImageDrawable(ContextCompat.getDrawable(context, drawableId))
}
fun setProfileImageClick(listener: OnClickListener) {
profileImageView.setOnClickListener(listener)
}
}
\ No newline at end of file
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context=".MainActivity">
tools:context=".app.activity.MainActivity">
<com.google.android.material.appbar.AppBarLayout
android:id="@+id/appbar_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<com.google.android.material.appbar.MaterialToolbar
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:theme="@style/ThemeOverlay.AppCompat.Light">
<de.asta.hochschule.trier.verleih.app.component.AppBarContent
android:layout_width="match_parent"
android:layout_height="match_parent"
app:profileImage="@drawable/chick"
app:title="@string/rental_title" />
</com.google.android.material.appbar.MaterialToolbar>
</com.google.android.material.appbar.AppBarLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_height="0dp"
android:layout_margin="16dp"
android:orientation="vertical">
android:orientation="vertical"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/appbar_layout">
<com.google.android.material.textview.MaterialTextView
android:layout_width="wrap_content"
......@@ -29,7 +57,7 @@
android:text="Button disabled" />
<com.google.android.material.button.MaterialButton
style="@style/MaterialButtonOutlinedStyle"
style="@style/MaterialButton.Outlined.Primary"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button Outlined" />
......@@ -52,7 +80,6 @@
android:valueTo="10" />
<com.google.android.material.textfield.TextInputLayout
style="@style/OutlinedBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
......@@ -63,7 +90,7 @@
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/imageView16"
app:startIconDrawable="@drawable/chip_checked_circle">
app:startIconDrawable="@drawable/ic_android">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
......@@ -76,8 +103,7 @@
style="@style/Chip.Choice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:enabled="false"
android:text="Chip Choice Disabled" />
android:text="Chip Choice" />
<com.google.android.material.chip.Chip
style="@style/Chip.Action"
......@@ -99,4 +125,4 @@
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
\ No newline at end of file
</androidx.constraintlayout.widget.ConstraintLayout>
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize">
<de.hdodenhof.circleimageview.CircleImageView
android:id="@+id/appbar_profile_image"
android:layout_width="45dp"
android:layout_height="45dp"
android:src="@drawable/chick"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<com.google.android.material.textview.MaterialTextView
android:id="@+id/appbar_title"
style="@style/Headline6"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginStart="32dp"
android:gravity="center_vertical"
android:text="@string/app_name"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toEndOf="@id/appbar_profile_image"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="AppBarContent">
<attr name="title" format="string" />
<attr name="profileImage" format="reference" />
</declare-styleable>
</resources>
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment