Created
November 26, 2022 14:52
-
-
Save hamurcuabi/45c0440ece4f97fae22e5c9d530ec489 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"?> | |
<LinearLayout 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="match_parent" | |
android:orientation="vertical"> | |
<com.hamurcuabi.myapplication.CustomToolbarView | |
android:id="@+id/toolbar" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
app:ctv_showEndIcon="true" | |
app:ctv_textStyle="Bold" | |
app:ctv_title="Xml test" /> | |
<Button | |
android:id="@+id/btn1" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:layout_marginHorizontal="12dp" | |
android:layout_marginTop="100dp" | |
android:layout_marginBottom="12dp" | |
android:text="Type 1" | |
app:layout_constraintBottom_toBottomOf="parent" | |
app:layout_constraintEnd_toEndOf="parent" | |
app:layout_constraintStart_toStartOf="parent" | |
app:layout_constraintTop_toTopOf="parent" /> | |
<Button | |
android:id="@+id/btn2" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:layout_margin="12dp" | |
android:text="Type 2" | |
app:layout_constraintBottom_toBottomOf="parent" | |
app:layout_constraintEnd_toEndOf="parent" | |
app:layout_constraintStart_toStartOf="parent" | |
app:layout_constraintTop_toTopOf="parent" /> | |
<Button | |
android:id="@+id/btn3" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:layout_margin="12dp" | |
android:text="Type 3" | |
app:layout_constraintBottom_toBottomOf="parent" | |
app:layout_constraintEnd_toEndOf="parent" | |
app:layout_constraintStart_toStartOf="parent" | |
app:layout_constraintTop_toTopOf="parent" /> | |
</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"?> | |
<resources> | |
<declare-styleable name="CustomToolbarView"> | |
<attr name="ctv_textStyle" format="enum"> | |
<enum name="Normal" value="0" /> | |
<enum name="Bold" value="1" /> | |
</attr> | |
<attr name="ctv_title" format="string" /> | |
<attr name="ctv_showEndIcon" format="boolean" /> | |
</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
package com.hamurcuabi.myapplication | |
import android.content.Context | |
import android.graphics.Typeface | |
import android.util.AttributeSet | |
import android.view.LayoutInflater | |
import android.view.View | |
import android.widget.FrameLayout | |
import androidx.core.view.isVisible | |
import com.hamurcuabi.myapplication.databinding.ViewCustomToolbarBinding | |
class CustomToolbarView @JvmOverloads constructor( | |
context: Context, | |
attributeSet: AttributeSet? = null, | |
defStyle: Int = 0 | |
) : FrameLayout(context, attributeSet, defStyle) { | |
private val binding = ViewCustomToolbarBinding | |
.inflate(LayoutInflater.from(context), this, true) | |
init { | |
binding.root.id=View.generateViewId() | |
context.obtainStyledAttributes(attributeSet, R.styleable.CustomToolbarView).apply { | |
val title: String? = getString(R.styleable.CustomToolbarView_ctv_title) | |
val showEndIcon = getBoolean(R.styleable.CustomToolbarView_ctv_showEndIcon, false) | |
val textStyle = getInt(R.styleable.CustomToolbarView_ctv_textStyle, CustomToolbarViewTextStyle.Normal.type) | |
recycle() | |
updateTitle(title) | |
showEndIcon(showEndIcon) | |
updateTitleStyle(textStyle) | |
} | |
} | |
private fun updateTitleStyle(type: Int) { | |
val style = when (CustomToolbarViewTextStyle.fromValue(type)) { | |
CustomToolbarViewTextStyle.Normal -> { | |
Typeface.NORMAL | |
} | |
CustomToolbarViewTextStyle.Bold -> { | |
Typeface.BOLD | |
} | |
} | |
binding.tvTitle.setTypeface(null, style) | |
} | |
fun updateTitleStyle(style: CustomToolbarViewTextStyle) { | |
updateTitleStyle(style.type) | |
} | |
fun updateTitle(title: String?) { | |
binding.tvTitle.text = title | |
} | |
fun showEndIcon(isVisible: Boolean) { | |
binding.imgEnd.isVisible = isVisible | |
} | |
} | |
enum class CustomToolbarViewTextStyle(val type: Int) { | |
Normal(0), | |
Bold(1); | |
companion object { | |
fun fromValue(type: Int): CustomToolbarViewTextStyle { | |
return values().firstOrNull { it.type == type } ?: Normal | |
} | |
} | |
} |
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"?> | |
<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="wrap_content"> | |
<ImageView | |
android:id="@+id/imgStart" | |
android:layout_width="36dp" | |
android:layout_height="36dp" | |
android:padding="8dp" | |
android:src="@drawable/ic_arrow" | |
app:layout_constraintBottom_toBottomOf="parent" | |
app:layout_constraintStart_toStartOf="parent" | |
app:layout_constraintTop_toTopOf="parent" /> | |
<TextView | |
android:id="@+id/tvTitle" | |
android:layout_width="0dp" | |
android:layout_height="wrap_content" | |
android:gravity="center" | |
android:text="dsadsa" | |
android:textColor="@color/black" | |
android:textSize="18sp" | |
app:layout_constraintBottom_toBottomOf="@id/imgStart" | |
app:layout_constraintEnd_toStartOf="@id/imgEnd" | |
app:layout_constraintStart_toEndOf="@id/imgStart" | |
app:layout_constraintTop_toTopOf="@id/imgStart" /> | |
<ImageView | |
android:id="@+id/imgEnd" | |
android:layout_width="36dp" | |
android:layout_height="36dp" | |
android:padding="8dp" | |
android:src="@drawable/ic_add" | |
app:layout_constraintBottom_toBottomOf="@id/imgStart" | |
app:layout_constraintEnd_toEndOf="parent" | |
app:layout_constraintTop_toTopOf="@id/imgStart" /> | |
</androidx.constraintlayout.widget.ConstraintLayout> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment