Posts

Showing posts from April, 2020

Change Snackbar Position When Bottom App Bar or Bottom Navigation View Is Present

Image
The SnackBar is mainly used to provide feedback to the user and it is shown at the bottom of the screen. This UI element is used alongside Toast or as a replacement in some application.
We typically show snack message as follows.
// Show snack message Snackbar.make(binding.coordinatorLayout, message, LENGTH_SHORT) .show();
This works well as long as the screen does not contain the Bottom App Bar or the Bottom Navigation View. When we have the specified elements the Snack Message overlaps on the Bottom App Bar or ends up behind the Bottom Navigation View.

This behavior is shown in the images below.

To avoid such behavior we need to use setAnchorView method and pass id of a view. The Snack Message is shown on top of the view element with the specified id.
// Show snack message Snackbar.make(binding.coordinatorLayout, message, LENGTH_SHORT) .setAnchorView(binding.fab) .show();

Similarly, when Bottom Navigation View or Floating Action Button id is passed to setAncho…

Material Chip in Android

Image
Chip, a Material Design Component is used as an action like setting an alarm or a choice like selecting a gender or to filter items like dress types or input types like hints.
Similar to RadioGroup, if we want to create a group of chips with single selection behavior we use ChipGroup and set singleSelection variable to true.


<com.google.android.material.chip.ChipGroup android:id="@+id/genderChip" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginStart="@dimen/edit_text_horizontal_margin" android:layout_marginTop="@dimen/edit_text_vertical_margin" android:layout_marginEnd="@dimen/edit_text_horizontal_margin" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/fullNameTextInputLayout" app:singleSelection="true"> <com.google.a…

Create Github Repository

Image
Github is a freemium cloud-hosted version control system where the developers can host unlimited public and private repositories for free.
Follow the below steps to create a new repository on Github.
1. The very first thing to do is go to the link https://github.com/new. It provides the fields to enter in order to create our repository.

2. Enter the Repository name and Description for our project.
3. We can select our project to be a Public project or restrict it to be Private.
4. In addition to that, we can create our project with a default README file by checking the box, gitignore file by selecting the language and a license from the dropdown.
5. Finally, click on the Create repository button.
This creates a Private project that a link https://github.com/gSrikar/Android-Studio-Settings.

Request Result Using startActivityForResult and Listen to It in onActivityResult

Let us say we have two activities (First Activity and Second Activity) in our Android application then startActivityForResult() is used by the First Activity when it expects a result from the Second Activity and onActivityResult() is used to read the returned result from the Second Activity.
In simple terms, we can send information to the top activity in the back stack.
From our First Activity, start the Second Activity with a request code.
// Open the second activity startActivityForResult( Intent(context, SecondActivity::class.java), REQUEST_CODE_FETCH_RESULT )
This opens the Second Activity. When we are done with the Second Activity and want to pass the data back to the First Activity, we call setResult and close our Second Activity.
// Set the result in the Second Activity val intent = Intent().apply { // Intent extras putExtra(BUNDLE_EXTRA_ACTIVITY_FINISHED, true) } activity?.run { // Set the result setResult(RESULT_OK, intent) /…