Android Code Tips - Chapter 1
All coding tips for Android:
Bottom Sheet Dialog Theme -
<style name="BaseBottomSheetDialog" parent="@style/Theme.Design.Light.BottomSheetDialog">
<item name="android:windowIsFloating">false</item>
<item name="bottomSheetStyle">@style/BottomSheet</item>
</style>
<style name="BottomSheet" parent="@style/Widget.Design.BottomSheet.Modal">
<item name="android:background">@drawable/bottom_sheet_bg</item>
</style>
<style name="BottomSheetDialog" parent="BaseBottomSheetDialog">
<item name="android:statusBarColor">@android:color/transparent</item>
</style>
Extends BottomSheetDialogFragment
Alert Dialog -
fun showAlertDialog(code: String = "", isError: Boolean){
val dialog = AlertDialog.Builder(context)
when(isError){
true -> {
dialog.setTitle(resources.getString(R.string.label_message))
dialog.setMessage(resources.getString(R.string.label_code_not_valid))
}
false -> {
dialog.setTitle(resources.getString(R.string.label_message))
dialog.setMessage(resources.getString(R.string.label_code_valid) + code)
}
}
dialog.setPositiveButton(resources.getString(R.string.label_cancel)){
_dialog, which ->
_dialog.dismiss()
}
dialog.show()
}
Collapsable Toolbar -
<androidx.coordinatorlayout.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<it.posteitaliane.df_home.uicomponents.ToolbarComponent
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"/>
<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="com.google.android.material.appbar.AppBarLayout$ScrollingViewBehavior"
android:fillViewport="true">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
tools:text="Navigation One Activity"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.core.widget.NestedScrollView>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
Custom Component - View
class AComponent @JvmOverloads constructor(
context: Context,
attrs: AttributeSet? = null,
defStyleAttr: Int = 0
): FrameLayout(context, attrs, defStyleAttr){
private var binding = RowItemActionsLayoutBinding.inflate(LayoutInflater.from(context))
init {
addView(binding.root)
}
fun initComponent(uiMOdel: MyUiModel) {
createChipView(binding.actionsChipGroup, uiMOdel.actions)
}
private fun createChipView(tagGroup: ChipGroup, actions: List<String>) {
actions.forEach { tag ->
val chip = Chip(context)
chip.textSize = 20F
when(tag){
"chat" -> {
chip.text = tag
chip.chipIcon = resources.getDrawable(R.drawable.ic_chat, null)
tagGroup.addView(chip)
chip.setOnClickListener {
}
}
"call" -> {
chip.text = tag
chip.chipIcon = resources.getDrawable(R.drawable.ic_call, null)
tagGroup.addView(chip)
chip.setOnClickListener {
val intent = Intent(Intent.ACTION_DIAL)
intent.data = Uri.parse("tel:3323842384")
context.startActivity(intent)
}
}
"share" -> {
chip.text = tag
chip.chipIcon = resources.getDrawable(R.drawable.ic_share, null)
tagGroup.addView(chip)
chip.setOnClickListener {
val sendIntent: Intent = Intent().apply {
action = Intent.ACTION_SEND
putExtra(Intent.EXTRA_TEXT, "This is my text to send.")
type = "text/plain"
}
val shareIntent = Intent.createChooser(sendIntent, null)
context.startActivity(shareIntent)
}
}
}
}
}
}
EditTextField
<style name="TextInput.MyStyle" parent="Widget.MaterialComponents.TextInputLayout.OutlinedBox">
<item name="android:textColor">@color/black</item>
<item name="android:textColorHint">@color/gray</item>
<item name="hintTextColor">@color/blue</item>
<item name="boxStrokeColor">@color/edittext_color_stroke</item>
<item name="android:textAppearance">@style/regular_font</item>
<item name="android:textSize">@dimen/font_17_sp</item>
</style>
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/textLayout"
style="@style/TextInput.MyStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginEnd="@dimen/margin_16_dp"
android:layout_weight="1"
android:labelFor="@+id/part_tv"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/textInputEt"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true"
android:focusable="true"
android:hint="Enter your Text"
android:inputType="numberDecimal"
android:singleLine="true"
android:textColorHint="@color/gray"
android:textSize="@dimen/font_17_sp" />
</com.google.android.material.textfield.TextInputLayout>
Fragment - As Layout
<fragment
android:id="@+id/my_fragment"
android:name="com.main.MyFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent"
/>
Spinner -
<Spinner
android:id="@+id/spinner_view"
android:layout_width="0dp"
android:layout_height="0dp"
android:background="@null"
android:layout_margin="@dimen/margin_16_dp"
android:padding="@dimen/padding_16_dp"
android:textColor="@color/gray_poste"
android:textSize="@dimen/font_17_sp"
tools:text="Select" />
private fun setView() {
// spinner
val mockArray = arrayOf("Content Type 1", "Content Type 2", "Content Type 3")
val spinnerAdapter =
ArrayAdapter(
activity as MyActivity,
R.layout.spinner_textview_selected_item,
mockArray
)
spinnerAdapter.setDropDownViewResource(R.layout.spinner_textview_dropdown_item)
binding.spinnerView.adapter = spinnerAdapter
binding.spinnerView.onItemSelectedListener =
(object : AdapterView.OnItemSelectedListener, AdapterView.OnItemClickListener {
override fun onNothingSelected(parent: AdapterView<*>?) {
}
override fun onItemSelected(
parent: AdapterView<*>?,
view: View?,
position: Int,
id: Long
) {
binding.tipologiaTv.setText(mockArray.get(position))
}
override fun onItemClick(
parent: AdapterView<*>?,
view: View?,
position: Int,
id: Long
) {
}
})
}
Guideline -
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.75" />
View - Divider
<View
android:id="@+id/dividerOne"
android:layout_width="match_parent"
android:layout_height="8dp"
android:layout_marginTop="@dimen/margin_32_dp"
android:background="@color/gray"
app:layout_constraintTop_toBottomOf="@id/topContainer" />
Include -
<include
android:id="@+id/yourLayout"
layout="@layout/layoutName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="@dimen/margin_16_dp"
android:layout_marginEnd="@dimen/margin_16_dp"
android:layout_marginTop="@dimen/margin_24_dp"
app:layout_constraintTop_toBottomOf="@id/toplayout"/>
Selector -
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="@color/blue" android:state_checked="true" />
<item android:color="@color/white" />
</selector>
Data Binding -
android:text='@{`your text`+ item.doSomething()}'
HtmlText -
android:text='@{item.formatStringFromHtml(item.title +" <b>"+ item.date +"</b>, "+ item.hour + "<b> "+item.time+"</b>, "+"<b>"+item.location+ "</b>" )}'
fun formatStringFromHtml(string: String)=
HtmlCompat.fromHtml(
string,
HtmlCompat.FROM_HTML_MODE_LEGACY
)
Calender - DatePickerDialog
private fun onDateClicked() {
val calendar = Calendar.getInstance()
calendar.timeInMillis = Calendar.getInstance().timeInMillis
val picker = context?.let {
DatePickerDialog(
it, this, calendar.get(Calendar.YEAR),
calendar.get(Calendar.MONTH),
calendar.get(Calendar.DAY_OF_MONTH)
)
}
picker?.datePicker?.minDate = System.currentTimeMillis() - 1000
picker?.show()
}
override fun onDateSet(view: DatePicker?, year: Int, month: Int, dayOfMonth: Int) {
val calender = Calendar.getInstance()
calender.set(Calendar.YEAR, year)
calender.set(Calendar.MONTH, month)
calender.set(Calendar.DAY_OF_MONTH, dayOfMonth)
val selectedDate: String =
DateFormat.getDateInstance(DateFormat.FULL).format(calender.getTime())
binding.ritiroDataTv.setText(selectedDate)
}
As a leading Top Mobile App Development Company in India, Appsinvo company is well known to design and build customized web and mobile app solutions for the clients.
ReplyDeleteMobile App Development Company in Dubai