Last active
September 17, 2019 06:39
-
-
Save bapspatil/23430053ced21ad4f02914330b5e06eb to your computer and use it in GitHub Desktop.
CounterView
This file contains hidden or 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="IncDecView"> | |
<attr name="value" format="integer" /> | |
<attr name="type"> | |
<flag name="money" value="0" /> | |
<flag name="non-money" value="1" /> | |
</attr> | |
<attr name="stepSize" format="integer" /> | |
</declare-styleable> | |
</resources> |
This file contains hidden or 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" | |
android:id="@+id/root_view" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:background="@drawable/bg_outline" | |
android:orientation="horizontal"> | |
<ImageButton | |
android:id="@+id/button_dec" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:background="@color/colorPrimary" | |
android:padding="8dp" | |
android:src="@drawable/ic_remove_white_24dp" /> | |
<RelativeLayout | |
android:layout_width="0dp" | |
android:layout_height="wrap_content" | |
android:layout_gravity="center" | |
android:layout_weight="9"> | |
<LinearLayout | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content"> | |
<TextView | |
android:id="@+id/text_type" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_gravity="center" | |
android:layout_marginEnd="16dp" | |
android:layout_marginStart="16dp" | |
android:text="@string/rupee_symbol" | |
android:textColor="@color/grey" | |
android:textSize="20sp" /> | |
<View | |
android:id="@+id/view_type" | |
android:layout_width="1dp" | |
android:layout_height="20dp" | |
android:layout_gravity="center" | |
android:background="@color/grey" /> | |
</LinearLayout> | |
<TextView | |
android:id="@+id/text_value" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_centerInParent="true" | |
android:layout_gravity="center" | |
android:fontFamily="@font/verdana_ab" | |
android:text="@string/_1_00_00_000" | |
android:textAppearance="?android:attr/textAppearanceMedium" | |
android:textColor="@color/grey" | |
android:textSize="14sp" | |
android:textStyle="bold" /> | |
</RelativeLayout> | |
<ImageButton | |
android:id="@+id/button_inc" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:background="@color/colorPrimary" | |
android:padding="8dp" | |
android:src="@drawable/ic_add_white_24dp" /> | |
</LinearLayout> |
This file contains hidden or 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
import android.content.Context | |
import android.util.AttributeSet | |
import android.view.View | |
import android.widget.LinearLayout | |
import kotlinx.android.synthetic.main.inc_dec_view.view.* | |
/** | |
* Created by Bapusaheb Patil. | |
*/ | |
class IncDecView : LinearLayout, View.OnClickListener { | |
private var value: Int = 0 | |
private var stepSize: Int = 0 | |
private var type: Int? = 1 | |
private var listener: IncDecListener? = null | |
constructor(ctx: Context?) : super(ctx) { | |
initView(ctx, null, 0) | |
} | |
constructor(ctx: Context?, attrs: AttributeSet?) : super(ctx, attrs) { | |
initView(ctx, attrs, 0) | |
} | |
constructor(ctx: Context?, attrs: AttributeSet?, defStyle: Int) : super(ctx, attrs, defStyle) { | |
initView(ctx, attrs, defStyle) | |
} | |
private fun initView(ctx: Context?, attrs: AttributeSet?, defStyle: Int) { | |
View.inflate(ctx, R.layout.inc_dec_view, this) | |
this.button_inc.setOnClickListener(this) | |
this.button_dec.setOnClickListener(this) | |
if (attrs != null) { | |
val attributes = context.theme.obtainStyledAttributes(attrs, R.styleable.IncDecView, defStyle, 0) | |
this.value = attributes.getInt(R.styleable.IncDecView_value, 1000000) | |
this.type = attributes.getInt(R.styleable.IncDecView_type, 1) | |
this.stepSize = attributes.getInt(R.styleable.IncDecView_stepSize, 1) | |
attributes.recycle() | |
} | |
updateUI(ctx) | |
} | |
private fun updateUI(ctx: Context?) { | |
this.text_value.text = this.value.toString() | |
when (this.type) { | |
TYPE_MONEY -> { | |
this.text_type.text = ctx?.getString(R.string.rupee_symbol) | |
} | |
else -> { | |
this.text_type.visibility = View.GONE | |
this.view_type.visibility = View.GONE | |
} | |
} | |
} | |
override fun onClick(v: View?) { | |
var textViewValue = this.text_value.text.toString().toInt() | |
when (v?.id) { | |
R.id.button_inc -> { | |
textViewValue += this.stepSize | |
this.value = textViewValue | |
this.text_value.text = textViewValue.toString() | |
this.listener?.onIncClick(this.text_value.text.toString().toInt()) | |
} | |
R.id.button_dec -> { | |
textViewValue -= this.stepSize | |
if (textViewValue < 1) | |
textViewValue = 1 | |
this.value = textViewValue | |
this.text_value.text = textViewValue.toString() | |
this.listener?.onDecClick(this.text_value.text.toString().toInt()) | |
} | |
} | |
} | |
fun setCounterListener(incDecListener: IncDecListener): IncDecView { | |
this.listener = incDecListener | |
return this | |
} | |
interface IncDecListener { | |
fun onIncClick(value: Int) | |
fun onDecClick(value: Int) | |
} | |
companion object { | |
const val TYPE_MONEY = 0 | |
const val TYPE_NON_MONEY = 1 | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment