Skip to content

Commit b2edcc3

Browse files
authored
Merge pull request #3 from pranaypatel512/development
Added support for Top and Bottom drawable click
2 parents 2f0031e + 33534f2 commit b2edcc3

File tree

11 files changed

+214
-41
lines changed

11 files changed

+214
-41
lines changed

CONTRIBUTING.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Contributing
2+
3+
1. Fork it!
4+
2. Checkout the development branch: `git checkout development`
5+
3. Create your feature branch: `git checkout -b my-new-feature`
6+
4. Add your changes to the index: `git add .`
7+
5. Commit your changes: `git commit -m 'Add some feature'`
8+
6. Push to the branch: `git push origin my-new-feature`
9+
7. Submit a pull request against the `development` branch

EditDrawableText.png

-51.8 KB
Binary file not shown.

README.md

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
<p align="center">
2+
<img alt="EditDrawableText" src="https://github.com/MindorksOpenSource/EditDrawableText/blob/master/app/src/main/assets/EditDrawableText.png?raw=true" />
3+
</p>
4+
5+
# EditDrawableText - An EditText which makes your Drawable Clickable
6+
[![Mindorks](https://img.shields.io/badge/mindorks-opensource-blue.svg)](https://mindorks.com/open-source-projects)
7+
[![Mindorks Community](https://img.shields.io/badge/join-community-blue.svg)](https://mindorks.com/join-community)
8+
[![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://opensource.org/licenses/Apache-2.0)
9+
10+
## Preview of EditDrawableText
11+
<img src="https://github.com/MindorksOpenSource/EditDrawableText/blob/master/app/src/main/assets/sample1.jpg?raw=true" height="300em" />&nbsp;<img src="https://github.com/MindorksOpenSource/EditDrawableText/blob/master/app/src/main/assets/sample2.jpg?raw=true" height="300em" />&nbsp;<img src="https://github.com/pranaypatel512/EditDrawableText/blob/development/app/src/main/assets/sample3.png?raw=true" height="300em" />&nbsp;<img src="https://github.com/pranaypatel512/EditDrawableText/blob/development/app/src/main/assets/sample4.png?raw=true" height="300em" />
12+
13+
14+
### Overview of EditDrawableText library
15+
* EditDrawableText can be used to Show/Hide Password
16+
* Left/Right Drawables can be clicked to make custom events like Request OTP etc.
17+
* All type of EditText Properties are possible in EditDrawableText
18+
19+
20+
## Using EditDrawableText Library in your Android application
21+
22+
1. Add it in your root build.gradle at the end of repositories:
23+
24+
```groovy
25+
repositories {
26+
maven { url 'https://jitpack.io' }
27+
}
28+
```
29+
2. Add this in your app's build.gradle
30+
31+
```groovy
32+
implementation 'com.github.MindorksOpenSource:EditDrawableText:1.1.0'
33+
```
34+
3. To use this in XML File, use
35+
36+
```XML
37+
<com.mindorks.editdrawabletext.EditDrawableText
38+
android:id="@+id/drawableEditTextLeft"
39+
android:layout_width="match_parent"
40+
android:layout_height="wrap_content"
41+
android:drawableLeft="@drawable/ic_remove_red_eye_black_24dp"
42+
android:hint="Click the Drawables"
43+
android:inputType="text"
44+
android:textAlignment="center"
45+
/>
46+
```
47+
4. Make the drawable clickable in Activity file,
48+
```kotlin
49+
drawableEditText.setDrawableClickListener(object : OnDrawableClickListener {
50+
override fun onClick(target: DrawablePosition) {
51+
when (target) {
52+
DrawablePosition.RIGHT -> //YOUR_LOGIC
53+
DrawablePosition.LEFT -> //YOUR_LOGIC
54+
DrawablePosition.TOP -> //YOUR_LOGIC
55+
DrawablePosition.BOTTOM -> //YOUR_LOGIC
56+
}
57+
}
58+
})
59+
60+
```
61+
### TODO
62+
* Making the TOP,BOTTOM drawables clickable
63+
* More features related to EditText
64+
65+
## If this library helps you in anyway, show your love :heart: by putting a :star: on this project :v:
66+
67+
[Check out Mindorks awesome open source projects here](https://mindorks.com/open-source-projects)
68+
69+
### License
70+
```
71+
Copyright (C) 2018 MINDORKS NEXTGEN PRIVATE LIMITED
72+
73+
Licensed under the Apache License, Version 2.0 (the "License");
74+
you may not use this file except in compliance with the License.
75+
You may obtain a copy of the License at
76+
77+
http://www.apache.org/licenses/LICENSE-2.0
78+
79+
Unless required by applicable law or agreed to in writing, software
80+
distributed under the License is distributed on an "AS IS" BASIS,
81+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
82+
See the License for the specific language governing permissions and
83+
limitations under the License.
84+
```
85+
86+
### Contributing to EditDrawableText
87+
All pull requests are welcome, make sure to follow the [contribution guidelines](CONTRIBUTING.md) when you submit pull request.

app/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,11 @@ android {
3131

3232
dependencies {
3333
implementation fileTree(include: ['*.jar'], dir: 'libs')
34-
implementation "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
34+
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
3535
implementation 'com.android.support:appcompat-v7:27.1.1'
3636
implementation 'com.android.support.constraint:constraint-layout:1.1.2'
3737
testImplementation 'junit:junit:4.12'
38-
implementation 'com.github.MindorksOpenSource:EditDrawableText:1.1.0'
38+
// implementation 'com.github.MindorksOpenSource:EditDrawableText:1.1.0'
3939
androidTestImplementation 'com.android.support.test:runner:1.0.2'
4040
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
4141
implementation project(':editdrawabletext')

app/src/main/assets/sample3.png

65 KB
Loading

app/src/main/assets/sample4.png

66.6 KB
Loading
Lines changed: 41 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,72 @@
11
package com.mindorks.editdrawabletextsample
22

3+
import android.app.Activity
34
import android.support.v7.app.AppCompatActivity
45
import android.os.Bundle
56
import android.widget.Toast
67
import com.mindorks.editdrawabletext.DrawablePosition
7-
import com.mindorks.editdrawabletext.EditDrawableText
8-
import com.mindorks.editdrawabletext.onDrawableClickListener
8+
import com.mindorks.editdrawabletext.OnDrawableClickListener
9+
import kotlinx.android.synthetic.main.activity_main.*
910

1011
class MainActivity : AppCompatActivity() {
1112

1213
override fun onCreate(savedInstanceState: Bundle?) {
1314
super.onCreate(savedInstanceState)
1415
setContentView(R.layout.activity_main)
15-
val drawable_editText: EditDrawableText = findViewById(R.id.drawable_editText)
16-
val drawable_editText_left: EditDrawableText = findViewById(R.id.drawable_editText_left)
17-
onClickListeners(
18-
drawable_editText,
19-
drawable_editText_left
20-
)
21-
16+
setOnClickListeners()
2217
}
2318

24-
private fun onClickListeners(drawable_editText: EditDrawableText, drawable_editText_left: EditDrawableText) {
25-
drawable_editText.setDrawableClickListener(object : onDrawableClickListener {
19+
private fun setOnClickListeners() {
20+
drawableEditText.setDrawableClickListener(object : OnDrawableClickListener {
2621
override fun onClick(target: DrawablePosition) {
2722
when (target) {
2823
DrawablePosition.RIGHT -> displayToastMessage("Clicked Right Drawable").show()
24+
else -> {
25+
}
2926
}
3027
}
3128

3229
})
33-
drawable_editText_left.setDrawableClickListener(object : onDrawableClickListener {
30+
drawableEditTextLeft.setDrawableClickListener(object : OnDrawableClickListener {
3431
override fun onClick(target: DrawablePosition) {
3532
when (target) {
3633
DrawablePosition.LEFT -> displayToastMessage("Clicked Left Drawable").show()
34+
else -> {
35+
}
3736
}
3837
}
3938

4039

40+
})
41+
drawableEditTextTop.setDrawableClickListener(object : OnDrawableClickListener {
42+
override fun onClick(target: DrawablePosition) {
43+
when (target) {
44+
DrawablePosition.TOP -> displayToastMessage("Clicked TOP Drawable").show()
45+
else -> {
46+
}
47+
}
48+
}
49+
50+
51+
})
52+
drawableEditTextBottom.setDrawableClickListener(object : OnDrawableClickListener {
53+
override fun onClick(target: DrawablePosition) {
54+
when (target) {
55+
DrawablePosition.BOTTOM -> displayToastMessage("Clicked BOTTOM Drawable").show()
56+
else -> {
57+
}
58+
}
59+
}
60+
61+
4162
})
4263
}
43-
44-
fun displayToastMessage(message: String): Toast {
45-
return Toast.makeText(this@MainActivity, message, Toast.LENGTH_SHORT)
46-
}
64+
65+
}
66+
67+
/**
68+
* Extension function to show Toast in activity
69+
*/
70+
fun Activity.displayToastMessage(message: String): Toast {
71+
return Toast.makeText(this, message, Toast.LENGTH_SHORT)
4772
}

app/src/main/res/layout/activity_main.xml

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
tools:context=".MainActivity">
1010

1111
<com.mindorks.editdrawabletext.EditDrawableText
12-
android:id="@+id/drawable_editText"
12+
android:id="@+id/drawableEditText"
1313
android:layout_width="match_parent"
1414
android:layout_height="wrap_content"
1515
android:drawableRight="@drawable/ic_remove_red_eye_black_24dp"
@@ -21,7 +21,7 @@
2121
app:layout_constraintRight_toRightOf="parent"
2222
app:layout_constraintTop_toTopOf="parent" />
2323
<com.mindorks.editdrawabletext.EditDrawableText
24-
android:id="@+id/drawable_editText_left"
24+
android:id="@+id/drawableEditTextLeft"
2525
android:layout_width="match_parent"
2626
android:layout_marginTop="10dp"
2727
android:layout_height="wrap_content"
@@ -33,5 +33,31 @@
3333
app:layout_constraintLeft_toLeftOf="parent"
3434
app:layout_constraintRight_toRightOf="parent"
3535
app:layout_constraintTop_toTopOf="parent" />
36+
<com.mindorks.editdrawabletext.EditDrawableText
37+
android:id="@+id/drawableEditTextTop"
38+
android:layout_width="match_parent"
39+
android:layout_marginTop="10dp"
40+
android:layout_height="wrap_content"
41+
android:drawableTop="@drawable/ic_remove_red_eye_black_24dp"
42+
android:hint="@string/clickTheDrawables"
43+
android:inputType="text"
44+
android:textAlignment="center"
45+
app:layout_constraintBottom_toBottomOf="parent"
46+
app:layout_constraintLeft_toLeftOf="parent"
47+
app:layout_constraintRight_toRightOf="parent"
48+
app:layout_constraintTop_toTopOf="parent" />
49+
<com.mindorks.editdrawabletext.EditDrawableText
50+
android:id="@+id/drawableEditTextBottom"
51+
android:layout_width="match_parent"
52+
android:layout_marginTop="10dp"
53+
android:layout_height="wrap_content"
54+
android:drawableBottom="@drawable/ic_remove_red_eye_black_24dp"
55+
android:hint="@string/clickTheDrawables"
56+
android:inputType="text"
57+
android:textAlignment="center"
58+
app:layout_constraintBottom_toBottomOf="parent"
59+
app:layout_constraintLeft_toLeftOf="parent"
60+
app:layout_constraintRight_toRightOf="parent"
61+
app:layout_constraintTop_toTopOf="parent" />
3662

3763
</LinearLayout>

editdrawabletext/src/main/java/com/mindorks/editdrawabletext/DrawablePosition.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,7 @@ package com.mindorks.editdrawabletext
22

33
enum class DrawablePosition {
44
LEFT,
5-
RIGHT
5+
RIGHT,
6+
TOP,
7+
BOTTOM
68
}

editdrawabletext/src/main/java/com/mindorks/editdrawabletext/EditDrawableText.kt

Lines changed: 43 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,23 @@ import android.graphics.drawable.Drawable
66
import android.util.AttributeSet
77
import android.view.MotionEvent
88
import android.widget.EditText
9+
import kotlin.math.abs
910

1011
class EditDrawableText : EditText {
11-
12+
1213
private var drawableRight: Drawable? = null
1314
private var drawableLeft: Drawable? = null
1415
private var drawableTop: Drawable? = null
1516
private var drawableBottom: Drawable? = null
1617
private var positionX: Int = 0
1718
private var positionY: Int = 0
18-
19-
private var onDrawableClickListener: onDrawableClickListener? = null
20-
19+
20+
private var onDrawableClickListener: OnDrawableClickListener? = null
21+
2122
constructor(context: Context, attrs: AttributeSet) : super(context, attrs)
22-
23+
2324
constructor(context: Context, attrs: AttributeSet, defStyle: Int) : super(context, attrs, defStyle)
24-
25+
2526
override fun setCompoundDrawables(leftDrawable: Drawable?,
2627
topDrawable: Drawable?,
2728
rightDrawable: Drawable?,
@@ -43,34 +44,34 @@ class EditDrawableText : EditText {
4344
// this works for left since container shares 0,0 origin with bounds
4445
if (drawableLeft != null) {
4546
bounds = drawableLeft!!.bounds
46-
47+
4748
var xClickPosition: Int
4849
var yClickPosition: Int
4950
/*
5051
* @return pixels into dp
5152
*/
5253
val extraClickArea = (13 * resources.displayMetrics.density + 0.5).toInt()
53-
54+
5455
xClickPosition = positionX
5556
yClickPosition = positionY
56-
57+
5758
if (!bounds!!.contains(positionX, positionY)) {
5859
/** Gives some extra space for tapping. */
5960
xClickPosition = positionX - extraClickArea
6061
yClickPosition = positionY - extraClickArea
61-
62+
6263
if (xClickPosition <= 0) xClickPosition = positionX
6364
if (yClickPosition <= 0) yClickPosition = positionY
64-
65+
6566
/** Creates square from the smallest value from x or y*/
6667
if (xClickPosition < yClickPosition) yClickPosition = xClickPosition
6768
}
68-
69+
6970
if (bounds.contains(xClickPosition, yClickPosition) && onDrawableClickListener != null) {
7071
onDrawableClickListener!!.onClick(DrawablePosition.LEFT)
7172
event.action = MotionEvent.ACTION_CANCEL
7273
return false
73-
74+
7475
}
7576
}
7677

@@ -104,14 +105,37 @@ class EditDrawableText : EditText {
104105
}
105106
return super.onTouchEvent(event)
106107
}
107-
108+
109+
if (drawableTop != null) {
110+
bounds = drawableTop!!.bounds
111+
val extraClickingArea = 13
112+
if (abs((width - paddingLeft - paddingRight) / 2 + paddingLeft - positionX) <= bounds.width() / 2 + extraClickingArea) {
113+
onDrawableClickListener!!.onClick(DrawablePosition.TOP)
114+
event.action = MotionEvent.ACTION_CANCEL
115+
return false
116+
}
117+
}
118+
119+
if(drawableBottom!=null)
120+
{
121+
bounds = drawableBottom!!.bounds
122+
val extraClickingArea = 13
123+
124+
if (abs((width - paddingLeft - paddingRight) / 2 + paddingLeft - positionX) <= bounds.width() / 2 + extraClickingArea) {
125+
onDrawableClickListener!!.onClick(DrawablePosition.BOTTOM)
126+
event.action = MotionEvent.ACTION_CANCEL
127+
return false
128+
}
129+
}
130+
131+
108132
}
109133
return super.onTouchEvent(event)
110134
}
111-
112-
113-
fun setDrawableClickListener(onDrawableClickListener: onDrawableClickListener) {
114-
this.onDrawableClickListener = onDrawableClickListener
135+
136+
137+
fun setDrawableClickListener(OnDrawableClickListener: OnDrawableClickListener) {
138+
this.onDrawableClickListener = OnDrawableClickListener
115139
}
116-
140+
117141
}

0 commit comments

Comments
 (0)