Skip to content

Commit 45ccb9d

Browse files
author
Nguyễn Anh Tuấn
committed
Add Custom View Example
0 parents  commit 45ccb9d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+833
-0
lines changed

.DS_Store

6 KB
Binary file not shown.

CustomView/.gitignore

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
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

CustomView/.idea/codeStyles/Project.xml

+10
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

CustomView/.idea/codeStyles/codeStyleConfig.xml

+5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

CustomView/.idea/encodings.xml

+4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

CustomView/.idea/gradle.xml

+18
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

CustomView/.idea/misc.xml

+14
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

CustomView/.idea/runConfigurations.xml

+12
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

CustomView/.idea/vcs.xml

+6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

CustomView/app/.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/build

CustomView/app/build.gradle

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
apply plugin: 'com.android.application'
2+
3+
apply plugin: 'kotlin-android'
4+
5+
apply plugin: 'kotlin-android-extensions'
6+
7+
android {
8+
compileSdkVersion 29
9+
buildToolsVersion "29.0.0"
10+
defaultConfig {
11+
applicationId "com.natuan.customview"
12+
minSdkVersion 21
13+
targetSdkVersion 29
14+
versionCode 1
15+
versionName "1.0"
16+
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
17+
}
18+
buildTypes {
19+
release {
20+
minifyEnabled false
21+
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
22+
}
23+
}
24+
}
25+
26+
dependencies {
27+
implementation fileTree(dir: 'libs', include: ['*.jar'])
28+
implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
29+
implementation 'androidx.appcompat:appcompat:1.0.2'
30+
implementation 'androidx.core:core-ktx:1.0.2'
31+
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
32+
testImplementation 'junit:junit:4.12'
33+
androidTestImplementation 'androidx.test:runner:1.2.0'
34+
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
35+
}
36+
37+
androidExtensions {
38+
experimental = true
39+
}

CustomView/app/proguard-rules.pro

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.natuan.customview
2+
3+
import androidx.test.InstrumentationRegistry
4+
import androidx.test.runner.AndroidJUnit4
5+
6+
import org.junit.Test
7+
import org.junit.runner.RunWith
8+
9+
import org.junit.Assert.*
10+
11+
/**
12+
* Instrumented test, which will execute on an Android device.
13+
*
14+
* See [testing documentation](http://d.android.com/tools/testing).
15+
*/
16+
@RunWith(AndroidJUnit4::class)
17+
class ExampleInstrumentedTest {
18+
@Test
19+
fun useAppContext() {
20+
// Context of the app under test.
21+
val appContext = InstrumentationRegistry.getTargetContext()
22+
assertEquals("com.natuan.customview", appContext.packageName)
23+
}
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3+
package="com.natuan.customview">
4+
5+
<application
6+
android:allowBackup="true"
7+
android:icon="@mipmap/ic_launcher"
8+
android:label="@string/app_name"
9+
android:roundIcon="@mipmap/ic_launcher_round"
10+
android:supportsRtl="true"
11+
android:theme="@style/AppTheme">
12+
<activity android:name=".MainActivity"
13+
android:theme="@style/AppTheme.NoActionBar">
14+
<intent-filter>
15+
<action android:name="android.intent.action.MAIN"/>
16+
17+
<category android:name="android.intent.category.LAUNCHER"/>
18+
</intent-filter>
19+
</activity>
20+
</application>
21+
22+
</manifest>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package com.natuan.customview
2+
3+
import android.content.Context
4+
import android.content.res.Resources
5+
import android.util.AttributeSet
6+
import android.view.View
7+
import android.view.ViewGroup
8+
import android.widget.RelativeLayout
9+
import androidx.core.content.ContextCompat
10+
import kotlinx.android.synthetic.main.merge_action_bar_view.view.*
11+
import kotlin.math.roundToInt
12+
13+
class ActionBarView @JvmOverloads constructor(
14+
context: Context,
15+
attrs: AttributeSet? = null,
16+
defStyle: Int = 0,
17+
defStyleRes: Int = 0
18+
) : RelativeLayout(context, attrs, defStyle, defStyleRes) {
19+
20+
interface ActionBarListener {
21+
fun onActionBarItemSelected(view: View)
22+
}
23+
24+
var actionBarListener: ActionBarListener? = null
25+
26+
init {
27+
inflate(context, R.layout.merge_action_bar_view, this)
28+
29+
val attributes = context.obtainStyledAttributes(attrs, R.styleable.ActionBarView)
30+
title_tv.text = attributes.getString(R.styleable.ActionBarView_title)
31+
32+
left_btn.setImageDrawable(attributes.getDrawable(R.styleable.ActionBarView_drawable_left))
33+
right_btn.setImageDrawable(attributes.getDrawable(R.styleable.ActionBarView_drawable_right))
34+
35+
attributes.recycle()
36+
}
37+
38+
override fun onAttachedToWindow() {
39+
super.onAttachedToWindow()
40+
layoutParams = LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, dpToPixel(60))
41+
background = ContextCompat.getDrawable(context, R.drawable.action_bar_bg)
42+
43+
actionBarListener?.let {
44+
left_btn.setOnClickListener { view -> it.onActionBarItemSelected(view) }
45+
right_btn.setOnClickListener { view -> it.onActionBarItemSelected(view) }
46+
}
47+
}
48+
49+
fun dpToPixel(dp: Int): Int {
50+
val metrics = Resources.getSystem().displayMetrics
51+
return (dp * (metrics.densityDpi / 160f)).roundToInt()
52+
}
53+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.natuan.customview
2+
3+
import androidx.appcompat.app.AppCompatActivity
4+
import android.os.Bundle
5+
import android.view.View
6+
import android.widget.Toast
7+
import kotlinx.android.synthetic.main.activity_main.*
8+
9+
class MainActivity : AppCompatActivity(), ActionBarView.ActionBarListener {
10+
11+
override fun onCreate(savedInstanceState: Bundle?) {
12+
super.onCreate(savedInstanceState)
13+
setContentView(R.layout.activity_main)
14+
action_bar.actionBarListener = this
15+
}
16+
17+
override fun onActionBarItemSelected(view: View) {
18+
when (view.id) {
19+
R.id.left_btn -> {
20+
Toast.makeText(this, "Left", Toast.LENGTH_SHORT).show()
21+
}
22+
R.id.right_btn -> {
23+
Toast.makeText(this, "Right", Toast.LENGTH_SHORT).show()
24+
}
25+
}
26+
}
27+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<vector xmlns:android="http://schemas.android.com/apk/res/android"
2+
xmlns:aapt="http://schemas.android.com/aapt"
3+
android:width="108dp"
4+
android:height="108dp"
5+
android:viewportHeight="108"
6+
android:viewportWidth="108">
7+
<path
8+
android:fillType="evenOdd"
9+
android:pathData="M32,64C32,64 38.39,52.99 44.13,50.95C51.37,48.37 70.14,49.57 70.14,49.57L108.26,87.69L108,109.01L75.97,107.97L32,64Z"
10+
android:strokeColor="#00000000"
11+
android:strokeWidth="1">
12+
<aapt:attr name="android:fillColor">
13+
<gradient
14+
android:endX="78.5885"
15+
android:endY="90.9159"
16+
android:startX="48.7653"
17+
android:startY="61.0927"
18+
android:type="linear">
19+
<item
20+
android:color="#44000000"
21+
android:offset="0.0"/>
22+
<item
23+
android:color="#00000000"
24+
android:offset="1.0"/>
25+
</gradient>
26+
</aapt:attr>
27+
</path>
28+
<path
29+
android:fillColor="#FFFFFF"
30+
android:fillType="nonZero"
31+
android:pathData="M66.94,46.02L66.94,46.02C72.44,50.07 76,56.61 76,64L32,64C32,56.61 35.56,50.11 40.98,46.06L36.18,41.19C35.45,40.45 35.45,39.3 36.18,38.56C36.91,37.81 38.05,37.81 38.78,38.56L44.25,44.05C47.18,42.57 50.48,41.71 54,41.71C57.48,41.71 60.78,42.57 63.68,44.05L69.11,38.56C69.84,37.81 70.98,37.81 71.71,38.56C72.44,39.3 72.44,40.45 71.71,41.19L66.94,46.02ZM62.94,56.92C64.08,56.92 65,56.01 65,54.88C65,53.76 64.08,52.85 62.94,52.85C61.8,52.85 60.88,53.76 60.88,54.88C60.88,56.01 61.8,56.92 62.94,56.92ZM45.06,56.92C46.2,56.92 47.13,56.01 47.13,54.88C47.13,53.76 46.2,52.85 45.06,52.85C43.92,52.85 43,53.76 43,54.88C43,56.01 43.92,56.92 45.06,56.92Z"
32+
android:strokeColor="#00000000"
33+
android:strokeWidth="1"/>
34+
</vector>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
3+
<item>
4+
<shape>
5+
<solid android:color="@color/loblolly" />
6+
</shape>
7+
</item>
8+
<item android:bottom="1dp">
9+
<shape>
10+
<solid android:color="@android:color/white" />
11+
</shape>
12+
</item>
13+
</layer-list>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<vector xmlns:android="http://schemas.android.com/apk/res/android"
2+
android:width="25dp"
3+
android:height="18dp"
4+
android:viewportWidth="25"
5+
android:viewportHeight="18">
6+
<path
7+
android:pathData="M8.829,0.818C8.9903,0.674 9.2004,0.5968 9.4166,0.6021C9.6329,0.6074 9.839,0.6948 9.993,0.8466C10.1471,0.9984 10.2376,1.2031 10.2461,1.4192C10.2547,1.6353 10.1806,1.8466 10.039,2.0101L3.906,8.147H24.141C24.253,8.1461 24.3642,8.1673 24.468,8.2094C24.5719,8.2515 24.6664,8.3137 24.7462,8.3923C24.826,8.471 24.8895,8.5647 24.933,8.6679C24.9766,8.7712 24.9993,8.882 25,8.9941C24.9992,9.2216 24.9084,9.4397 24.7475,9.6006C24.5866,9.7615 24.3686,9.8523 24.141,9.8531H3.906L10.038,15.974C10.1952,16.136 10.2831,16.3528 10.2831,16.5785C10.2831,16.8043 10.1952,17.0211 10.038,17.183C9.9591,17.2634 9.8651,17.3272 9.7613,17.3708C9.6575,17.4143 9.5461,17.4368 9.4335,17.4368C9.3209,17.4368 9.2095,17.4143 9.1057,17.3708C9.0019,17.3272 8.9078,17.2634 8.829,17.183L1.245,9.599C1.0869,9.4393 0.9983,9.2237 0.9983,8.9991C0.9983,8.7744 1.0869,8.5588 1.245,8.399L8.829,0.818Z"
8+
android:fillColor="#1E201D"/>
9+
</vector>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<vector xmlns:android="http://schemas.android.com/apk/res/android"
2+
android:width="24dp"
3+
android:height="20dp"
4+
android:viewportWidth="24"
5+
android:viewportHeight="20">
6+
<path
7+
android:pathData="M0.2769,10.9229C0.1894,10.8396 0.1197,10.7394 0.0721,10.6284C0.0245,10.5173 0,10.3978 0,10.2769C0,10.1561 0.0245,10.0366 0.0721,9.9255C0.1197,9.8145 0.1894,9.7143 0.2769,9.6309L1.5689,8.3389C1.6522,8.2514 1.7524,8.1818 1.8635,8.1342C1.9745,8.0866 2.0941,8.062 2.2149,8.062C2.3357,8.062 2.4553,8.0866 2.5663,8.1342C2.6774,8.1818 2.7776,8.2514 2.8609,8.3389L2.9529,8.4309L8.0299,13.8769C8.0716,13.9207 8.1217,13.9555 8.1772,13.9793C8.2327,14.0031 8.2925,14.0154 8.3529,14.0154C8.4133,14.0154 8.4731,14.0031 8.5286,13.9793C8.5841,13.9555 8.6342,13.9207 8.6759,13.8769L21.0459,1.0459H21.1379C21.2212,0.9584 21.3214,0.8888 21.4325,0.8412C21.5435,0.7936 21.6631,0.769 21.7839,0.769C21.9047,0.769 22.0243,0.7936 22.1353,0.8412C22.2464,0.8888 22.3466,0.9584 22.4299,1.0459L23.7219,2.3379C23.8094,2.4213 23.8791,2.5215 23.9267,2.6325C23.9743,2.7436 23.9988,2.8631 23.9988,2.9839C23.9988,3.1048 23.9743,3.2243 23.9267,3.3354C23.8791,3.4464 23.8094,3.5466 23.7219,3.6299L8.9539,18.9539C8.8706,19.0414 8.7704,19.1111 8.6593,19.1587C8.5483,19.2063 8.4287,19.2308 8.3079,19.2308C8.1871,19.2308 8.0675,19.2063 7.9565,19.1587C7.8454,19.1111 7.7452,19.0414 7.6619,18.9539L0.4619,11.1999L0.2769,10.9229Z"
8+
android:fillColor="#0088FF"/>
9+
</vector>

0 commit comments

Comments
 (0)