├── app ├── .gitignore ├── src │ └── main │ │ ├── res │ │ ├── mipmap-hdpi │ │ │ ├── ic_launcher.webp │ │ │ └── ic_launcher_round.webp │ │ ├── mipmap-mdpi │ │ │ ├── ic_launcher.webp │ │ │ └── ic_launcher_round.webp │ │ ├── mipmap-xhdpi │ │ │ ├── ic_launcher.webp │ │ │ └── ic_launcher_round.webp │ │ ├── mipmap-xxhdpi │ │ │ ├── ic_launcher.webp │ │ │ └── ic_launcher_round.webp │ │ ├── mipmap-xxxhdpi │ │ │ ├── ic_launcher.webp │ │ │ └── ic_launcher_round.webp │ │ ├── mipmap-anydpi-v26 │ │ │ ├── ic_launcher.xml │ │ │ └── ic_launcher_round.xml │ │ ├── values │ │ │ ├── strings.xml │ │ │ ├── colors.xml │ │ │ └── themes.xml │ │ ├── xml │ │ │ ├── backup_rules.xml │ │ │ └── data_extraction_rules.xml │ │ ├── values-night │ │ │ └── themes.xml │ │ ├── drawable-v24 │ │ │ └── ic_launcher_foreground.xml │ │ ├── layout │ │ │ └── activity_main.xml │ │ └── drawable │ │ │ └── ic_launcher_background.xml │ │ ├── AndroidManifest.xml │ │ └── java │ │ └── ir │ │ └── programmerplus │ │ └── groupbox_example │ │ └── MainActivity.kt ├── proguard-rules.pro └── build.gradle ├── groupbox ├── .gitignore ├── consumer-rules.pro ├── src │ └── main │ │ ├── AndroidManifest.xml │ │ ├── res │ │ ├── values │ │ │ └── attrs.xml │ │ └── layout │ │ │ └── layout_group_box.xml │ │ └── java │ │ └── ir │ │ └── programmerplus │ │ └── groupbox │ │ ├── CutoutDrawable.kt │ │ └── GroupBoxLayout.kt ├── proguard-rules.pro └── build.gradle ├── images ├── 01.jpg ├── 02.jpg └── 03.jpg ├── jitpack.yml ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── .gitignore ├── settings.gradle ├── gradle.properties ├── gradlew.bat ├── README.md └── gradlew /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build -------------------------------------------------------------------------------- /groupbox/.gitignore: -------------------------------------------------------------------------------- 1 | /build -------------------------------------------------------------------------------- /groupbox/consumer-rules.pro: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /groupbox/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /images/01.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/homayoonahmadi/GroupBoxLayout/HEAD/images/01.jpg -------------------------------------------------------------------------------- /images/02.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/homayoonahmadi/GroupBoxLayout/HEAD/images/02.jpg -------------------------------------------------------------------------------- /images/03.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/homayoonahmadi/GroupBoxLayout/HEAD/images/03.jpg -------------------------------------------------------------------------------- /jitpack.yml: -------------------------------------------------------------------------------- 1 | jdk: 2 | - openjdk11 3 | before_install: 4 | - ./scripts/prepareJitpackEnvironment.sh -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/homayoonahmadi/GroupBoxLayout/HEAD/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /app/src/main/res/mipmap-hdpi/ic_launcher.webp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/homayoonahmadi/GroupBoxLayout/HEAD/app/src/main/res/mipmap-hdpi/ic_launcher.webp -------------------------------------------------------------------------------- /app/src/main/res/mipmap-mdpi/ic_launcher.webp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/homayoonahmadi/GroupBoxLayout/HEAD/app/src/main/res/mipmap-mdpi/ic_launcher.webp -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xhdpi/ic_launcher.webp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/homayoonahmadi/GroupBoxLayout/HEAD/app/src/main/res/mipmap-xhdpi/ic_launcher.webp -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxhdpi/ic_launcher.webp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/homayoonahmadi/GroupBoxLayout/HEAD/app/src/main/res/mipmap-xxhdpi/ic_launcher.webp -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxxhdpi/ic_launcher.webp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/homayoonahmadi/GroupBoxLayout/HEAD/app/src/main/res/mipmap-xxxhdpi/ic_launcher.webp -------------------------------------------------------------------------------- /app/src/main/res/mipmap-hdpi/ic_launcher_round.webp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/homayoonahmadi/GroupBoxLayout/HEAD/app/src/main/res/mipmap-hdpi/ic_launcher_round.webp -------------------------------------------------------------------------------- /app/src/main/res/mipmap-mdpi/ic_launcher_round.webp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/homayoonahmadi/GroupBoxLayout/HEAD/app/src/main/res/mipmap-mdpi/ic_launcher_round.webp -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xhdpi/ic_launcher_round.webp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/homayoonahmadi/GroupBoxLayout/HEAD/app/src/main/res/mipmap-xhdpi/ic_launcher_round.webp -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxhdpi/ic_launcher_round.webp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/homayoonahmadi/GroupBoxLayout/HEAD/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.webp -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.webp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/homayoonahmadi/GroupBoxLayout/HEAD/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.webp -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Tue Sep 20 09:20:36 IRDT 2022 2 | distributionBase=GRADLE_USER_HOME 3 | distributionUrl=https\://services.gradle.org/distributions/gradle-7.4-bin.zip 4 | distributionPath=wrapper/dists 5 | zipStorePath=wrapper/dists 6 | zipStoreBase=GRADLE_USER_HOME 7 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | .gradle 3 | /local.properties 4 | /.idea/caches 5 | /.idea/libraries 6 | /.idea/modules.xml 7 | /.idea/workspace.xml 8 | /.idea/navEditor.xml 9 | /.idea/assetWizardSettings.xml 10 | .DS_Store 11 | /build 12 | /captures 13 | .externalNativeBuild 14 | .cxx 15 | local.properties 16 | /.idea 17 | -------------------------------------------------------------------------------- /app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /app/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /app/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | GroupBox Example 3 | Login 4 | Username 5 | Password 6 | "Hello World!" 7 | Enter your login data: 8 | -------------------------------------------------------------------------------- /app/src/main/res/values/colors.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | #FFBB86FC 4 | #FF6200EE 5 | #FF3700B3 6 | #FF03DAC5 7 | #FF018786 8 | #FF000000 9 | #FFFFFFFF 10 | -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | pluginManagement { 2 | repositories { 3 | gradlePluginPortal() 4 | google() 5 | mavenCentral() 6 | } 7 | } 8 | dependencyResolutionManagement { 9 | repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS) 10 | repositories { 11 | google() 12 | mavenCentral() 13 | } 14 | } 15 | rootProject.name = "GroupBox Example" 16 | include ':app' 17 | include ':groupbox' 18 | -------------------------------------------------------------------------------- /app/src/main/res/xml/backup_rules.xml: -------------------------------------------------------------------------------- 1 | 8 | 9 | 13 | -------------------------------------------------------------------------------- /groupbox/src/main/res/values/attrs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /app/src/main/res/xml/data_extraction_rules.xml: -------------------------------------------------------------------------------- 1 | 6 | 7 | 8 | 12 | 13 | 19 | -------------------------------------------------------------------------------- /app/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # You can control the set of applied configuration files using the 3 | # proguardFiles setting in build.gradle. 4 | # 5 | # For more details, see 6 | # http://developer.android.com/guide/developing/tools/proguard.html 7 | 8 | # If your project uses WebView with JS, uncomment the following 9 | # and specify the fully qualified class name to the JavaScript interface 10 | # class: 11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 12 | # public *; 13 | #} 14 | 15 | # Uncomment this to preserve the line number information for 16 | # debugging stack traces. 17 | #-keepattributes SourceFile,LineNumberTable 18 | 19 | # If you keep the line number information, uncomment this to 20 | # hide the original source file name. 21 | #-renamesourcefileattribute SourceFile -------------------------------------------------------------------------------- /groupbox/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # You can control the set of applied configuration files using the 3 | # proguardFiles setting in build.gradle. 4 | # 5 | # For more details, see 6 | # http://developer.android.com/guide/developing/tools/proguard.html 7 | 8 | # If your project uses WebView with JS, uncomment the following 9 | # and specify the fully qualified class name to the JavaScript interface 10 | # class: 11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 12 | # public *; 13 | #} 14 | 15 | # Uncomment this to preserve the line number information for 16 | # debugging stack traces. 17 | #-keepattributes SourceFile,LineNumberTable 18 | 19 | # If you keep the line number information, uncomment this to 20 | # hide the original source file name. 21 | #-renamesourcefileattribute SourceFile -------------------------------------------------------------------------------- /app/src/main/res/values-night/themes.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 16 | -------------------------------------------------------------------------------- /app/src/main/res/values/themes.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 20 | 21 | 24 | -------------------------------------------------------------------------------- /groupbox/src/main/res/layout/layout_group_box.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 7 | 8 | 13 | 14 | 28 | 29 | 30 | -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 15 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 27 | 28 | 29 | 30 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | plugins { 2 | id 'com.android.application' 3 | id 'org.jetbrains.kotlin.android' 4 | } 5 | 6 | android { 7 | namespace 'ir.programmerplus.groupbox_example' 8 | compileSdk 33 9 | 10 | defaultConfig { 11 | applicationId "ir.programmerplus.groupbox_example" 12 | minSdk 17 13 | targetSdk 33 14 | versionCode 1 15 | versionName "1.0" 16 | 17 | testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" 18 | } 19 | 20 | buildTypes { 21 | release { 22 | minifyEnabled false 23 | proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' 24 | } 25 | } 26 | compileOptions { 27 | sourceCompatibility JavaVersion.VERSION_1_8 28 | targetCompatibility JavaVersion.VERSION_1_8 29 | } 30 | kotlinOptions { 31 | jvmTarget = '1.8' 32 | } 33 | buildFeatures { 34 | viewBinding true 35 | } 36 | } 37 | 38 | dependencies { 39 | implementation 'androidx.core:core-ktx:1.9.0' 40 | implementation 'androidx.appcompat:appcompat:1.5.1' 41 | implementation 'com.google.android.material:material:1.6.1' 42 | implementation 'androidx.constraintlayout:constraintlayout:2.1.4' 43 | 44 | implementation project(':groupbox') 45 | } -------------------------------------------------------------------------------- /gradle.properties: -------------------------------------------------------------------------------- 1 | # Project-wide Gradle settings. 2 | # IDE (e.g. Android Studio) users: 3 | # Gradle settings configured through the IDE *will override* 4 | # any settings specified in this file. 5 | # For more details on how to configure your build environment visit 6 | # http://www.gradle.org/docs/current/userguide/build_environment.html 7 | # Specifies the JVM arguments used for the daemon process. 8 | # The setting is particularly useful for tweaking memory settings. 9 | org.gradle.jvmargs=-Xmx2048m -Dfile.encoding=UTF-8 10 | # When configured, Gradle will run in incubating parallel mode. 11 | # This option should only be used with decoupled projects. More details, visit 12 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects 13 | # org.gradle.parallel=true 14 | # AndroidX package structure to make it clearer which packages are bundled with the 15 | # Android operating system, and which are packaged with your app's APK 16 | # https://developer.android.com/topic/libraries/support-library/androidx-rn 17 | android.useAndroidX=true 18 | # Kotlin code style for this project: "official" or "obsolete": 19 | kotlin.code.style=official 20 | # Enables namespacing of each library's R class so that its R class includes only the 21 | # resources declared in the library itself and none from the library's dependencies, 22 | # thereby reducing the size of the R class for that library 23 | android.nonTransitiveRClass=true -------------------------------------------------------------------------------- /groupbox/build.gradle: -------------------------------------------------------------------------------- 1 | plugins { 2 | id 'com.android.library' 3 | id 'kotlin-android' 4 | id 'maven-publish' 5 | } 6 | 7 | android { 8 | namespace 'ir.programmerplus.groupbox' 9 | compileSdk 33 10 | 11 | defaultConfig { 12 | minSdk 17 13 | targetSdk 33 14 | 15 | testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" 16 | consumerProguardFiles "consumer-rules.pro" 17 | } 18 | 19 | buildTypes { 20 | release { 21 | minifyEnabled false 22 | proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' 23 | } 24 | } 25 | 26 | compileOptions { 27 | sourceCompatibility JavaVersion.VERSION_1_8 28 | targetCompatibility JavaVersion.VERSION_1_8 29 | } 30 | 31 | kotlinOptions { 32 | jvmTarget = '1.8' 33 | } 34 | 35 | buildFeatures { 36 | viewBinding true 37 | } 38 | } 39 | 40 | dependencies { 41 | implementation "androidx.annotation:annotation:1.5.0" 42 | implementation 'com.google.android.material:material:1.8.0' 43 | } 44 | 45 | afterEvaluate { 46 | publishing { 47 | publications { 48 | release(MavenPublication) { 49 | from components.release 50 | 51 | groupId = 'com.github.homayoonahmadi' 52 | artifactId = 'groupbox' 53 | version = '1.2.0' 54 | } 55 | } 56 | } 57 | } -------------------------------------------------------------------------------- /app/src/main/res/drawable-v24/ic_launcher_foreground.xml: -------------------------------------------------------------------------------- 1 | 7 | 8 | 9 | 15 | 18 | 21 | 22 | 23 | 24 | 30 | -------------------------------------------------------------------------------- /app/src/main/java/ir/programmerplus/groupbox_example/MainActivity.kt: -------------------------------------------------------------------------------- 1 | package ir.programmerplus.groupbox_example 2 | 3 | import android.os.Bundle 4 | import android.widget.RelativeLayout 5 | import android.widget.TextView 6 | import androidx.appcompat.app.AppCompatActivity 7 | import androidx.core.content.ContextCompat 8 | import ir.programmerplus.groupbox.GroupBoxLayout 9 | import ir.programmerplus.groupbox_example.databinding.ActivityMainBinding 10 | 11 | class MainActivity : AppCompatActivity() { 12 | 13 | private lateinit var binding: ActivityMainBinding 14 | 15 | override fun onCreate(savedInstanceState: Bundle?) { 16 | super.onCreate(savedInstanceState) 17 | binding = ActivityMainBinding.inflate(layoutInflater) 18 | setContentView(binding.root) 19 | 20 | // getting some sample color 21 | val tealColor = ContextCompat.getColor(this, R.color.teal_700) 22 | 23 | // creating a groupbox layout 24 | val groupBoxLayout = GroupBoxLayout(this).apply { 25 | setLabelText("Label Text") 26 | setLabelTextSize(15) 27 | setLabelTextColor(tealColor) 28 | setLabelStyleResource(R.style.GroupBoxLabel) 29 | 30 | setStrokeWidth(5) 31 | setStrokeColor(tealColor) 32 | setCornerRadius(15) 33 | } 34 | 35 | // setting layout params and applying margins 36 | groupBoxLayout.layoutParams = RelativeLayout.LayoutParams( 37 | RelativeLayout.LayoutParams.MATCH_PARENT, 38 | RelativeLayout.LayoutParams.WRAP_CONTENT 39 | ).apply { 40 | setMargins(5, 5, 5, 5) 41 | } 42 | 43 | // setting custom padding 44 | groupBoxLayout.setContentPadding(20, 20, 20, 20) 45 | 46 | // creating a simple text view 47 | val textView = TextView(this).apply { text = getString(R.string.hello_world) } 48 | 49 | // adding text view to groupbox layout 50 | groupBoxLayout.addView(textView) 51 | 52 | binding.root.addView(groupBoxLayout) 53 | 54 | // removing some view from groupbox layout 55 | binding.lytGroupBox.removeView(binding.lytGroupBox.findViewById(R.id.txt_description)) 56 | } 57 | } -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 9 | 10 | 23 | 24 | 30 | 31 | 36 | 37 | 40 | 41 | 42 | 47 | 48 | 51 | 52 | 53 | 57 | 58 | 59 | 60 | -------------------------------------------------------------------------------- /gradlew.bat: -------------------------------------------------------------------------------- 1 | @rem 2 | @rem Copyright 2015 the original author or authors. 3 | @rem 4 | @rem Licensed under the Apache License, Version 2.0 (the "License"); 5 | @rem you may not use this file except in compliance with the License. 6 | @rem You may obtain a copy of the License at 7 | @rem 8 | @rem https://www.apache.org/licenses/LICENSE-2.0 9 | @rem 10 | @rem Unless required by applicable law or agreed to in writing, software 11 | @rem distributed under the License is distributed on an "AS IS" BASIS, 12 | @rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | @rem See the License for the specific language governing permissions and 14 | @rem limitations under the License. 15 | @rem 16 | 17 | @if "%DEBUG%" == "" @echo off 18 | @rem ########################################################################## 19 | @rem 20 | @rem Gradle startup script for Windows 21 | @rem 22 | @rem ########################################################################## 23 | 24 | @rem Set local scope for the variables with windows NT shell 25 | if "%OS%"=="Windows_NT" setlocal 26 | 27 | set DIRNAME=%~dp0 28 | if "%DIRNAME%" == "" set DIRNAME=. 29 | set APP_BASE_NAME=%~n0 30 | set APP_HOME=%DIRNAME% 31 | 32 | @rem Resolve any "." and ".." in APP_HOME to make it shorter. 33 | for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi 34 | 35 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 36 | set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m" 37 | 38 | @rem Find java.exe 39 | if defined JAVA_HOME goto findJavaFromJavaHome 40 | 41 | set JAVA_EXE=java.exe 42 | %JAVA_EXE% -version >NUL 2>&1 43 | if "%ERRORLEVEL%" == "0" goto execute 44 | 45 | echo. 46 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 47 | echo. 48 | echo Please set the JAVA_HOME variable in your environment to match the 49 | echo location of your Java installation. 50 | 51 | goto fail 52 | 53 | :findJavaFromJavaHome 54 | set JAVA_HOME=%JAVA_HOME:"=% 55 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 56 | 57 | if exist "%JAVA_EXE%" goto execute 58 | 59 | echo. 60 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 61 | echo. 62 | echo Please set the JAVA_HOME variable in your environment to match the 63 | echo location of your Java installation. 64 | 65 | goto fail 66 | 67 | :execute 68 | @rem Setup the command line 69 | 70 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 71 | 72 | 73 | @rem Execute Gradle 74 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %* 75 | 76 | :end 77 | @rem End local scope for the variables with windows NT shell 78 | if "%ERRORLEVEL%"=="0" goto mainEnd 79 | 80 | :fail 81 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 82 | rem the _cmd.exe /c_ return code! 83 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 84 | exit /b 1 85 | 86 | :mainEnd 87 | if "%OS%"=="Windows_NT" endlocal 88 | 89 | :omega 90 | -------------------------------------------------------------------------------- /groupbox/src/main/java/ir/programmerplus/groupbox/CutoutDrawable.kt: -------------------------------------------------------------------------------- 1 | package ir.programmerplus.groupbox 2 | 3 | import android.graphics.* 4 | import android.os.Build.VERSION 5 | import android.os.Build.VERSION_CODES 6 | import android.view.View 7 | import com.google.android.material.shape.MaterialShapeDrawable 8 | import com.google.android.material.shape.ShapeAppearanceModel 9 | 10 | 11 | /** 12 | * A {@link MaterialShapeDrawable} that can draw a cutout for the label in outline mode. 13 | */ 14 | class CutoutDrawable(shapeAppearanceModel: ShapeAppearanceModel?) : MaterialShapeDrawable(shapeAppearanceModel ?: ShapeAppearanceModel()) { 15 | 16 | private var cutoutPaint: Paint = Paint(Paint.ANTI_ALIAS_FLAG) 17 | private var cutoutBounds: RectF = RectF() 18 | private var savedLayer = 0 19 | 20 | init { 21 | setPaintStyles() 22 | } 23 | 24 | private fun setPaintStyles() { 25 | cutoutPaint.style = Paint.Style.FILL_AND_STROKE 26 | cutoutPaint.color = Color.WHITE 27 | cutoutPaint.xfermode = PorterDuffXfermode(PorterDuff.Mode.DST_OUT) 28 | } 29 | 30 | fun setCutout(left: Float, top: Float, right: Float, bottom: Float) { 31 | // Avoid expensive redraws by only calling invalidateSelf if one of the cutout's dimensions has changed. 32 | if (left != cutoutBounds.left || top != cutoutBounds.top || right != cutoutBounds.right || bottom != cutoutBounds.bottom) { 33 | cutoutBounds[left, top, right] = bottom 34 | invalidateSelf() 35 | } 36 | } 37 | 38 | fun removeCutout() { 39 | // Call setCutout with empty bounds to remove the cutout. 40 | setCutout(0f, 0f, 0f, 0f) 41 | } 42 | 43 | override fun draw(canvas: Canvas) { 44 | preDraw(canvas) 45 | super.draw(canvas) 46 | 47 | // Draw mask for the cutout. 48 | canvas.drawRect(cutoutBounds, cutoutPaint) 49 | postDraw(canvas) 50 | } 51 | 52 | private fun preDraw(canvas: Canvas) { 53 | val callback = callback 54 | 55 | if (useHardwareLayer(callback)) { 56 | val viewCallback = callback as View? 57 | // Make sure we're using a hardware layer. 58 | if (viewCallback!!.layerType != View.LAYER_TYPE_HARDWARE) { 59 | viewCallback.setLayerType(View.LAYER_TYPE_HARDWARE, null) 60 | } 61 | 62 | } else { 63 | // If we're not using a hardware layer, save the canvas layer. 64 | saveCanvasLayer(canvas) 65 | } 66 | } 67 | 68 | @Suppress("DEPRECATION") 69 | private fun saveCanvasLayer(canvas: Canvas) { 70 | savedLayer = if (VERSION.SDK_INT >= VERSION_CODES.LOLLIPOP) { 71 | canvas.saveLayer(0f, 0f, canvas.width.toFloat(), canvas.height.toFloat(), null) 72 | } else { 73 | canvas.saveLayer(0f, 0f, canvas.width.toFloat(), canvas.height.toFloat(), null, Canvas.ALL_SAVE_FLAG) 74 | } 75 | } 76 | 77 | private fun postDraw(canvas: Canvas) { 78 | if (!useHardwareLayer(callback)) { 79 | canvas.restoreToCount(savedLayer) 80 | } 81 | } 82 | 83 | private fun useHardwareLayer(callback: Callback?): Boolean { 84 | return callback is View 85 | } 86 | } -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # GroupBoxLayout 2 | 3 | [![platform](https://img.shields.io/badge/platform-Android-green.svg)](https://www.android.com) 4 | [![API](https://img.shields.io/badge/API-17%2B-brightgreen.svg?style=flat)](https://android-arsenal.com/api?level=17) 5 | [![Jitpack](https://jitpack.io/v/homayoonahmadi/GroupBoxLayout.svg)](https://jitpack.io/#homayoonahmadi/GroupBoxLayout) 6 | 7 | GroupBoxLayout is a container layout that has a title label and draws a rounded border over the parent view. 8 | + supports rtl and ltr directions (using layoutDirection attribute) 9 | + prevents label text overflow (by ellipsize the label text) 10 | + is compatible with background colors or any drawables 11 | + it doesn't use any white or other color backgrounds for hiding border under the label. 12 | 13 | # Screenshots: 14 | 15 | ![Preview 1](https://github.com/homayoonahmadi/GroupBoxLayout/blob/master/images/01.jpg) 16 | ![Preview 2](https://github.com/homayoonahmadi/GroupBoxLayout/blob/master/images/02.jpg) 17 | ![Preview 3](https://github.com/homayoonahmadi/GroupBoxLayout/blob/master/images/03.jpg) 18 | 19 | # How to add dependency: 20 | 21 | Step 1. Add the JitPack repository to your build.gradle file 22 | 23 | ```groovy 24 | allprojects { 25 | repositories { 26 | maven { url 'https://jitpack.io' } 27 | } 28 | } 29 | ``` 30 | 31 | Step 2. Add the dependency 32 | 33 | ```groovy 34 | dependencies { 35 | implementation 'com.github.homayoonahmadi:GroupBoxLayout:1.2.0' 36 | } 37 | ``` 38 | 39 | # How to use 40 | 41 | + **In XML:** 42 | 43 | ```xml 44 | 57 | 58 | 62 | 63 | 64 | ``` 65 | 66 | You can customize label text using styles: 67 | 68 | ```xml 69 | 70 | 74 | ``` 75 | 76 | 77 | + **In Code:** 78 | 79 | You can also add layout programmatically to your view like this: 80 | 81 | ```kotlin 82 | // get sample color 83 | val tealColor = ContextCompat.getColor(this@MainActivity, R.color.teal_700) 84 | 85 | // create groupbox layout 86 | val groupBoxLayout = GroupBoxLayout(this).apply { 87 | setLabelText("label text") 88 | setLabelTextSize(15) 89 | setLabelTextColor(tealColor) 90 | setLabelStyleResource(R.style.GroupBoxLabel) 91 | 92 | setStrokeWidth(5) 93 | setStrokeColor(tealColor) 94 | setCornerRadius(15) 95 | } 96 | 97 | // set margin 98 | groupBoxLayout.layoutParams = RelativeLayout.LayoutParams( 99 | RelativeLayout.LayoutParams.MATCH_PARENT, 100 | RelativeLayout.LayoutParams.WRAP_CONTENT 101 | ).apply { 102 | setMargins(5, 5, 5, 5) 103 | } 104 | 105 | // set padding 106 | groupBoxLayout.setContentPadding(20, 20, 20, 20) 107 | 108 | // create simple text view 109 | val textView = TextView(this).apply { text = "Hello World!" } 110 | 111 | // add text view to groupbox layout 112 | groupBoxLayout.addView(textView) 113 | 114 | binding.root.addView(groupBoxLayout) 115 | ``` 116 | 117 | # Methods 118 | 119 | + **GroupBoxLayout class methods** 120 | 121 | | method | description | 122 | |---------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------| 123 | | setLabelText(text: String?) | This function will set the text of label to be displayed. | 124 | | getLabelText() | Returns the text that label TextView is displaying. | 125 | | setLabelTextColor(@ColorInt color: Int) | This function will set text color of label TextView | 126 | | setLabelTextSize(unit: Int, size: Int) | Set the default label text size to a given unit and value. | 127 | | setLabelStyleResource(@StyleRes resId: Int) | Sets the text style such as color, size, style, hint color, and highlight color from the specified TextAppearance resource. | 128 | | setStrokeColor(@ColorInt color: Int) | Sets the stroke color of border | 129 | | setStrokeWidth(width: Int) | Sets stroke width of border | 130 | | setCornerRadius(radius: Int) | Sets corner radius of border | 131 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/ic_launcher_background.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 10 | 15 | 20 | 25 | 30 | 35 | 40 | 45 | 50 | 55 | 60 | 65 | 70 | 75 | 80 | 85 | 90 | 95 | 100 | 105 | 110 | 115 | 120 | 125 | 130 | 135 | 140 | 145 | 150 | 155 | 160 | 165 | 170 | 171 | -------------------------------------------------------------------------------- /gradlew: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env sh 2 | 3 | # 4 | # Copyright 2015 the original author or authors. 5 | # 6 | # Licensed under the Apache License, Version 2.0 (the "License"); 7 | # you may not use this file except in compliance with the License. 8 | # You may obtain a copy of the License at 9 | # 10 | # https://www.apache.org/licenses/LICENSE-2.0 11 | # 12 | # Unless required by applicable law or agreed to in writing, software 13 | # distributed under the License is distributed on an "AS IS" BASIS, 14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | # See the License for the specific language governing permissions and 16 | # limitations under the License. 17 | # 18 | 19 | ############################################################################## 20 | ## 21 | ## Gradle start up script for UN*X 22 | ## 23 | ############################################################################## 24 | 25 | # Attempt to set APP_HOME 26 | # Resolve links: $0 may be a link 27 | PRG="$0" 28 | # Need this for relative symlinks. 29 | while [ -h "$PRG" ] ; do 30 | ls=`ls -ld "$PRG"` 31 | link=`expr "$ls" : '.*-> \(.*\)$'` 32 | if expr "$link" : '/.*' > /dev/null; then 33 | PRG="$link" 34 | else 35 | PRG=`dirname "$PRG"`"/$link" 36 | fi 37 | done 38 | SAVED="`pwd`" 39 | cd "`dirname \"$PRG\"`/" >/dev/null 40 | APP_HOME="`pwd -P`" 41 | cd "$SAVED" >/dev/null 42 | 43 | APP_NAME="Gradle" 44 | APP_BASE_NAME=`basename "$0"` 45 | 46 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 47 | DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"' 48 | 49 | # Use the maximum available, or set MAX_FD != -1 to use that value. 50 | MAX_FD="maximum" 51 | 52 | warn () { 53 | echo "$*" 54 | } 55 | 56 | die () { 57 | echo 58 | echo "$*" 59 | echo 60 | exit 1 61 | } 62 | 63 | # OS specific support (must be 'true' or 'false'). 64 | cygwin=false 65 | msys=false 66 | darwin=false 67 | nonstop=false 68 | case "`uname`" in 69 | CYGWIN* ) 70 | cygwin=true 71 | ;; 72 | Darwin* ) 73 | darwin=true 74 | ;; 75 | MINGW* ) 76 | msys=true 77 | ;; 78 | NONSTOP* ) 79 | nonstop=true 80 | ;; 81 | esac 82 | 83 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 84 | 85 | 86 | # Determine the Java command to use to start the JVM. 87 | if [ -n "$JAVA_HOME" ] ; then 88 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 89 | # IBM's JDK on AIX uses strange locations for the executables 90 | JAVACMD="$JAVA_HOME/jre/sh/java" 91 | else 92 | JAVACMD="$JAVA_HOME/bin/java" 93 | fi 94 | if [ ! -x "$JAVACMD" ] ; then 95 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 96 | 97 | Please set the JAVA_HOME variable in your environment to match the 98 | location of your Java installation." 99 | fi 100 | else 101 | JAVACMD="java" 102 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 103 | 104 | Please set the JAVA_HOME variable in your environment to match the 105 | location of your Java installation." 106 | fi 107 | 108 | # Increase the maximum file descriptors if we can. 109 | if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then 110 | MAX_FD_LIMIT=`ulimit -H -n` 111 | if [ $? -eq 0 ] ; then 112 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 113 | MAX_FD="$MAX_FD_LIMIT" 114 | fi 115 | ulimit -n $MAX_FD 116 | if [ $? -ne 0 ] ; then 117 | warn "Could not set maximum file descriptor limit: $MAX_FD" 118 | fi 119 | else 120 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 121 | fi 122 | fi 123 | 124 | # For Darwin, add options to specify how the application appears in the dock 125 | if $darwin; then 126 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 127 | fi 128 | 129 | # For Cygwin or MSYS, switch paths to Windows format before running java 130 | if [ "$cygwin" = "true" -o "$msys" = "true" ] ; then 131 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 132 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 133 | 134 | JAVACMD=`cygpath --unix "$JAVACMD"` 135 | 136 | # We build the pattern for arguments to be converted via cygpath 137 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` 138 | SEP="" 139 | for dir in $ROOTDIRSRAW ; do 140 | ROOTDIRS="$ROOTDIRS$SEP$dir" 141 | SEP="|" 142 | done 143 | OURCYGPATTERN="(^($ROOTDIRS))" 144 | # Add a user-defined pattern to the cygpath arguments 145 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then 146 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" 147 | fi 148 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 149 | i=0 150 | for arg in "$@" ; do 151 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` 152 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option 153 | 154 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition 155 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` 156 | else 157 | eval `echo args$i`="\"$arg\"" 158 | fi 159 | i=`expr $i + 1` 160 | done 161 | case $i in 162 | 0) set -- ;; 163 | 1) set -- "$args0" ;; 164 | 2) set -- "$args0" "$args1" ;; 165 | 3) set -- "$args0" "$args1" "$args2" ;; 166 | 4) set -- "$args0" "$args1" "$args2" "$args3" ;; 167 | 5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; 168 | 6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; 169 | 7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; 170 | 8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; 171 | 9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; 172 | esac 173 | fi 174 | 175 | # Escape application args 176 | save () { 177 | for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done 178 | echo " " 179 | } 180 | APP_ARGS=`save "$@"` 181 | 182 | # Collect all arguments for the java command, following the shell quoting and substitution rules 183 | eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS" 184 | 185 | exec "$JAVACMD" "$@" 186 | -------------------------------------------------------------------------------- /groupbox/src/main/java/ir/programmerplus/groupbox/GroupBoxLayout.kt: -------------------------------------------------------------------------------- 1 | @file:Suppress("MemberVisibilityCanBePrivate", "unused") 2 | 3 | package ir.programmerplus.groupbox 4 | 5 | import android.content.Context 6 | import android.content.res.ColorStateList 7 | import android.graphics.Color 8 | import android.util.AttributeSet 9 | import android.util.TypedValue 10 | import android.view.LayoutInflater 11 | import android.view.View 12 | import android.view.ViewGroup 13 | import android.widget.RelativeLayout 14 | import androidx.annotation.ColorInt 15 | import androidx.annotation.StyleRes 16 | import com.google.android.material.shape.RoundedCornerTreatment 17 | import com.google.android.material.shape.ShapeAppearanceModel 18 | import ir.programmerplus.groupbox.databinding.LayoutGroupBoxBinding 19 | 20 | 21 | /** 22 | * The GroupBoxLayout is a container layout view that has a title label and draws a rounded border 23 | * over the view 24 | */ 25 | class GroupBoxLayout : RelativeLayout { 26 | 27 | companion object { 28 | private const val DEFAULT_TEXT = "" 29 | private const val DEFAULT_TEXT_SIZE = 14 30 | private const val DEFAULT_COLOR = Color.BLACK 31 | private const val DEFAULT_CORNER_RADIUS = 5f 32 | private const val DEFAULT_STROKE_WIDTH = 1.5f 33 | } 34 | 35 | private lateinit var drawable: CutoutDrawable 36 | private lateinit var binding: LayoutGroupBoxBinding 37 | 38 | private var leftPadding: Int = 0 39 | private var rightPadding: Int = 0 40 | private var topPadding: Int = 0 41 | private var bottomPadding: Int = 0 42 | 43 | constructor(context: Context) : super(context) { 44 | initView(context) 45 | } 46 | 47 | constructor(context: Context, attrs: AttributeSet) : super(context, attrs) { 48 | initView(context) 49 | applyAttributes(context, attrs, 0) 50 | } 51 | 52 | constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int) : super(context, attrs, defStyleAttr) { 53 | initView(context) 54 | applyAttributes(context, attrs, defStyleAttr) 55 | } 56 | 57 | /** 58 | * This function will inflate related xml layout and initializes the view's 59 | * default settings and etc. 60 | */ 61 | private fun initView(context: Context) { 62 | binding = LayoutGroupBoxBinding.inflate(LayoutInflater.from(context), this) 63 | 64 | leftPadding = binding.root.paddingLeft 65 | topPadding = binding.root.paddingTop 66 | rightPadding = binding.root.paddingRight 67 | bottomPadding = binding.root.paddingBottom 68 | 69 | initBorderBackground() 70 | 71 | setLabelText(DEFAULT_TEXT) 72 | setLabelTextColor(DEFAULT_COLOR) 73 | setLabelTextSize(TypedValue.COMPLEX_UNIT_SP, DEFAULT_TEXT_SIZE) 74 | 75 | // Update the cutout part of the drawable 76 | binding.label.addOnLayoutChangeListener { _, left, top, right, bottom, _, _, _, _ -> 77 | 78 | if (binding.label.text.isNotEmpty()) { 79 | val realLeft: Int = left - binding.content.left 80 | val realTop: Int = top - binding.content.top 81 | val realRight: Int = right - binding.content.left 82 | val realBottom: Int = bottom - binding.content.top 83 | 84 | drawable.setCutout(realLeft.toFloat(), realTop.toFloat(), realRight.toFloat(), realBottom.toFloat()) 85 | 86 | } else { 87 | drawable.removeCutout() 88 | } 89 | } 90 | } 91 | 92 | /** 93 | * This function will get attributes and apply them on view 94 | * 95 | * @param context context 96 | * @param attrs attributes set 97 | * @param defStyleAttr default style 98 | */ 99 | private fun applyAttributes(context: Context, attrs: AttributeSet?, defStyleAttr: Int) { 100 | val attributes = context.obtainStyledAttributes(attrs, R.styleable.GroupBoxLayout, defStyleAttr, 0) 101 | 102 | val styleResId = attributes.getResourceId(R.styleable.GroupBoxLayout_labelStyle, -1) 103 | if (styleResId != -1) setLabelStyleResource(styleResId) 104 | 105 | val labelText = attributes.getString(R.styleable.GroupBoxLayout_labelText) 106 | setLabelText(labelText) 107 | 108 | val labelTextColor = attributes.getColor(R.styleable.GroupBoxLayout_labelTextColor, DEFAULT_COLOR) 109 | setLabelTextColor(labelTextColor) 110 | 111 | val labelSize = attributes.getDimensionPixelSize(R.styleable.GroupBoxLayout_labelTextSize, 0) 112 | if (labelSize > 0) setLabelTextSize(TypedValue.COMPLEX_UNIT_PX, labelSize) 113 | 114 | val strokeColor = attributes.getColor(R.styleable.GroupBoxLayout_borderColor, DEFAULT_COLOR) 115 | setStrokeColor(strokeColor) 116 | 117 | val strokeWidth = 118 | attributes.getDimensionPixelSize(R.styleable.GroupBoxLayout_borderStrokeWidth, DEFAULT_STROKE_WIDTH.dp()) 119 | setStrokeWidth(strokeWidth) 120 | 121 | val cornerRadius = 122 | attributes.getDimensionPixelSize(R.styleable.GroupBoxLayout_borderCornerRadius, DEFAULT_CORNER_RADIUS.dp()) 123 | setCornerRadius(cornerRadius) 124 | 125 | attributes.recycle() 126 | } 127 | 128 | /** 129 | * This overridden method will add all views to content layout to make sure that layout will perform 130 | * as a container layout 131 | */ 132 | override fun addView(child: View?, index: Int, params: ViewGroup.LayoutParams?) { 133 | if (this::binding.isInitialized) binding.content.addView(child, index, params) 134 | else super.addView(child, index, params) 135 | } 136 | 137 | /** 138 | * This overridden method will remove views from content layout to make sure that layout will perform 139 | * as a container layout 140 | */ 141 | override fun removeView(view: View?) { 142 | if (this::binding.isInitialized) binding.content.removeView(view) 143 | else super.removeView(view) 144 | } 145 | 146 | 147 | /** 148 | * This method will add set padding to content layout 149 | */ 150 | fun setContentPadding(left: Int, top: Int, right: Int, bottom: Int) { 151 | leftPadding = left 152 | topPadding = top 153 | rightPadding = right 154 | bottomPadding = bottom 155 | } 156 | 157 | /** 158 | * Here we recalculate top margin to make label center vertically 159 | * and we set parent root margin to the content layout 160 | */ 161 | override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) { 162 | super.onMeasure(widthMeasureSpec, heightMeasureSpec) 163 | 164 | recalculateTopMargin() 165 | 166 | binding.root.setPadding(0, 0, 0, 0) 167 | 168 | binding.content.setPadding( 169 | leftPadding, 170 | topPadding, 171 | rightPadding, 172 | bottomPadding 173 | ) 174 | } 175 | 176 | /** 177 | * This function will set border background 178 | */ 179 | private fun initBorderBackground() { 180 | // configuration of the shape for the outline 181 | val shape = ShapeAppearanceModel.Builder() 182 | .setAllCorners(RoundedCornerTreatment()) 183 | .setAllCornerSizes(DEFAULT_CORNER_RADIUS) 184 | .build() 185 | 186 | // configuration of the CutOutDrawable 187 | drawable = CutoutDrawable(shape).apply { 188 | strokeWidth = DEFAULT_STROKE_WIDTH.dp().toFloat() 189 | strokeColor = ColorStateList.valueOf(DEFAULT_COLOR) 190 | fillColor = ColorStateList.valueOf(Color.TRANSPARENT) 191 | } 192 | 193 | binding.content.background = drawable 194 | } 195 | 196 | /** 197 | * Set top margin to half of label height 198 | */ 199 | private fun recalculateTopMargin() { 200 | val marginTop: Int = binding.label.measuredHeight / 2 201 | val layoutParams: LayoutParams 202 | 203 | if (binding.content.layoutParams is LayoutParams) { 204 | layoutParams = binding.content.layoutParams as LayoutParams 205 | layoutParams.setMargins(0, marginTop, 0, 0) 206 | 207 | } else { 208 | layoutParams = LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT) 209 | layoutParams.setMargins(layoutParams.leftMargin, marginTop, layoutParams.rightMargin, layoutParams.bottomMargin) 210 | } 211 | 212 | binding.content.layoutParams = layoutParams 213 | } 214 | 215 | /** 216 | * This function will set the text of label to be displayed. 217 | * 218 | * @param text text string to be set 219 | */ 220 | fun setLabelText(text: String?) { 221 | binding.label.text = text 222 | binding.label.visibility = VISIBLE 223 | } 224 | 225 | /** 226 | * Return the text that label TextView is displaying. 227 | * 228 | * @return label text 229 | */ 230 | @Suppress("unused") 231 | fun getLabelText(): CharSequence? { 232 | return binding.label.text 233 | } 234 | 235 | /** 236 | * This function will set text color of label TextView 237 | * 238 | * @param color color to be set 239 | */ 240 | fun setLabelTextColor(@ColorInt color: Int) { 241 | binding.label.setTextColor(color) 242 | } 243 | 244 | /** 245 | * Set the default label text size to a given unit and value. 246 | * 247 | * @param unit The desired dimension unit. 248 | * @param size The desired size in the given units. 249 | */ 250 | fun setLabelTextSize(unit: Int, size: Int) { 251 | binding.label.setTextSize(unit, size.toFloat()) 252 | } 253 | 254 | /** 255 | * Set the default label text size to a given value. 256 | * 257 | * @param size The desired size in SP unit. 258 | */ 259 | fun setLabelTextSize(size: Int) { 260 | binding.label.setTextSize(TypedValue.COMPLEX_UNIT_SP, size.toFloat()) 261 | } 262 | 263 | /** 264 | * Sets the text style such as color, size, style, hint color, and highlight color 265 | * from the specified TextAppearance resource. 266 | * 267 | * @param resId style resource id 268 | */ 269 | fun setLabelStyleResource(@StyleRes resId: Int) { 270 | @Suppress("DEPRECATION") 271 | binding.label.setTextAppearance(context, resId) 272 | } 273 | 274 | /** 275 | * Sets the stroke color of border 276 | * 277 | * @param color color 278 | */ 279 | fun setStrokeColor(@ColorInt color: Int) { 280 | drawable.strokeColor = ColorStateList.valueOf(color) 281 | } 282 | 283 | /** 284 | * Sets stroke width of border 285 | * 286 | * @param width stroke width 287 | */ 288 | fun setStrokeWidth(width: Int) { 289 | drawable.strokeWidth = width.toFloat() 290 | } 291 | 292 | /** 293 | * Sets corner radius of border 294 | * 295 | * @param radius corner radius 296 | */ 297 | fun setCornerRadius(radius: Int) { 298 | val appearanceModel = drawable.shapeAppearanceModel.toBuilder().setAllCornerSizes(radius.toFloat()).build() 299 | drawable.shapeAppearanceModel = appearanceModel 300 | } 301 | 302 | /** 303 | * This function will convert value to dp size 304 | * 305 | * @return value in dp 306 | */ 307 | private fun Float.dp(): Int { 308 | return (this * resources.displayMetrics.density).toInt() 309 | } 310 | 311 | } --------------------------------------------------------------------------------