Skip to main content

Posts

Showing posts from 2018

Featured post

Simple RecyclerView example with filter option in Android

Hi Guys, Maybe you all are expert in terms of using RecyclerView in android. This blog is simple example for using filter option with RecyclerView adapter. As for now you will instantiate RecyclerView and set the adapter to RecyclerView as following way. RecyclerView list = (RecyclerView) findViewById(R.id.list); list.setLayoutManager(new LinearLayoutManager(this)); list.setHasFixedSize(true); ArrayList&ltNumber&gt numbers = new ArrayList&lt&gt(); String ONEs[] = {"ZERO", "ONE", "TWO", "THREE", "FOUR", "FIVE", "SIX", "SEVEN", "EIGHT", "NINE", "TEN"}; String TENs[] = {"ZERO", "TEN", "TWENTY", "THIRTY", "FOURTY", "FIFTY", "SIXTY", "SEVENTY", "EIGHTY", "NINETY", "HUNDRED"}; String HUNDREDS[] = {"ZERO", "HUNDRED", "TWO HUND

Simple example of heads-up notification in Android

Beginning with Android 5.0, notifications can briefly appear in a floating window called a heads-up notification. This behavior is normally for important notifications that the user should know about immediately, and it appears only if the device is unlocked. The heads-up notification appears the moment your app issues the notification and it disappears after a moment, but remains visible in the notification drawer as usual. Example conditions that might trigger heads-up notifications include the following: The user's activity is in fullscreen mode (the app uses fullScreenIntent ). The notification has high priority and uses ringtones or vibrations on devices running Android 7.1 (API level 25) and lower. The notification channel has high importance on devices running Android 8.0 (API level 26) and higher. Here is the sample code private fun sendNotification() { var notifyManager: NotificationManager? = null val NOTIFY_ID = 1002 val name = "KotlinApp

Simple example of DraggableTextView in Android | Kotlin

Hello All, By using simple onTouch event we make an DraggableTextView widget. Here is the code sample for you. override fun onTouch(v: View?, event: MotionEvent?): Boolean { val x = event?.rawX?.toInt()!! val y = event.rawY.toInt() when (event.action) { MotionEvent.ACTION_DOWN -> { val layoutParams = layoutParams as ConstraintLayout.LayoutParams xDelta = x - layoutParams.leftMargin yDelta = y - layoutParams.topMargin rect.set(0, 0, width, height) isDragging = true } MotionEvent.ACTION_MOVE -> { val layoutParams = layoutParams as ConstraintLayout.LayoutParams layoutParams.leftMargin = x - xDelta layoutParams.topMargin = y - yDelta setLayoutParams(layoutParams) } MotionEvent.ACTION_UP -> { isDragging = false } } invalidate() return true } Here is the full video tutorial. Interesting

Using shuffle from Collections in Java | Android

From Doc shuffle is used to Randomly permutes the specified list using a default source of randomness. All permutations occur with approximately equal likelihood. Here is the simple example of shuffle from collections. List numbers = new ArrayList<>(); for (int i = 0; i <= 10; i++) { numbers.add(i); } Log.v(TAG, "Original List: " + numbers.toString()); Collections.shuffle(numbers); Log.v(TAG, "Shuffled List: " + numbers.toString()); Output: Original List: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10] Shuffled List: [2, 10, 0, 3, 7, 5, 1, 9, 8, 6, 4] There is also public static void shuffle(List list, Random rnd) which will Randomly permute the specified list using the specified source of randomness. Interesting right? If you are really interested in this example, then please share this post with your friends, also share your feedback as comment here. Thank You

Simple example of using Firebase Cloud Messaging in Android | Kotlin

Firebase Cloud Messaging (FCM) is a cross-platform messaging solution that lets you reliably deliver messages at no cost. You can send notification messages to drive user re-engagement and retention. To write your Firebase Cloud Messaging Android client app, use the FirebaseMessaging API and Android Studio 1.4 or higher with Gradle. FCM clients require devices running Android 4.0 or higher that also have the Google Play Store app installed, or an emulator running Android 4.0 with Google APIs. Note that you are not limited to deploying your Android apps through Google Play Store. Set up Firebase and the FCM SDK * If you haven't already, add Firebase to your Android project . * In Android Studio, add the FCM dependency to your app-level build.gradle file: implementation 'com.google.firebase:firebase-messaging:17.1.0' Create a Service that extends FirebaseMessagingService. Here is the full code of FCMService.kt class FCMService : FirebaseMessagingService() { o

Simple example of using BottomSheetDialog in Android | Kotlin

BottomSheetDialog is simple dialog styled as bottom sheet. BottomSheetDialog implementation is quite simpler and easier than normal Dilaogs. You can just attach view for it and then display it. Here is simple example. This tutorial is developed using kotlin. MainActivity.kt val dialog = BottomSheetDialog(this) val bottomSheet = layoutInflater.inflate(R.layout.bottom_sheet, null) bottomSheet.buttonSubmit.setOnClickListener { dialog.dismiss() } dialog.setContentView(bottomSheet) dialog.show() bottom_sheet.xml &lt?xml version="1.0" encoding="utf-8"?&gt &ltandroid.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:id="@+id/bottomSheet" android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="10dp"&gt &ltTextView

Simple example of using BottomSheetBehavior in Android | Kotlin

BottomSheetBehavior is An interaction behavior plugin for a child view of CoordinatorLayout to make it work as a bottom sheet. Bottom sheet is something like sliding a view from bottom to top, you may already seen this design in Google Play Music or VLC media player app. Here is the simple example of using BottomSheetBehavior in Android, code is developed in Kotlin. MainActivity.kt val bottomSheetBehavior = BottomSheetBehavior.from(bottomSheet) bottomSheetBehavior.setBottomSheetCallback(object : BottomSheetBehavior.BottomSheetCallback() { override fun onStateChanged(bottomSheet: View, newState: Int) { when (newState) { BottomSheetBehavior.STATE_EXPANDED -> { textBottom.text = getString(R.string.slide_down) //textFull.visibility = View.VISIBLE } BottomSheetBehavior.STATE_COLLAPSED -> { textBottom.text = getString(R.string.slide_up) //textFull.visibility = View.GONE

Dynamic Grid Count in GridLayoutManager using RecyclerView | Android | Kotlin

Sometime we need RecyclerView filled with dynamic row count. For example, some row has 3 columns, and some has 2 columns, and some should have only one column like that. Here is the simple example of dynamic grid count using GridLayoutManager in RecyclerView. val layoutManager = GridLayoutManager(this, 6) layoutManager.spanSizeLookup = object : GridLayoutManager.SpanSizeLookup() { override fun getSpanSize(position: Int): Int { when (position) { 0, 1, 2, 6, 7, 8, 12, 13, 14, 18, 19, 20 -> return 2 3, 4, 9, 10, 15, 16, 21, 22, 24, 25 -> return 3 5, 11, 17, 23 -> return 6 else -> return 2 } } } recyclerView.layoutManager = layoutManager Screenshot: Interesting right? If you are really interested in this code, then please share this post with your friends, also share your feedback as comment here. Thank You

Simple example of using firestore in Android | Kotlin

Here is the simple example of maintaining database using Firebase instead of SQLite in Kotlin Programming Language . Step 1. Login into Firebase Console and create your project. Step 2. Navigate into Database and follow the steps given there. Step 3. Create your Android project and add the following classpath into root of the project's build.gradle. classpath 'com.google.gms:google-services:3.1.1' Step 4. And then add the following plugin into your module's build.gradle. apply plugin: 'com.google.gms.google-services' Step 5. And then add the following dependency into your module's build.gradle. implementation 'com.firebaseui:firebase-ui-firestore:3.3.1' That's all you wanted to access the Firebase's Firestore API in your project. Here is the methods to create document(Like table in SQLite), Add data to document, edit data from document, delete data from document. Add data to document using Hashmap. //Create employee usin

Simple Example of Firebase Authentication in Kotlin

Here is the simple example of Authentication process using Firebase in Kotlin Programming Language . Step 1. Login into Firebase Console and create your project. Step 2. Navigate into Authentication and enable your preferred login methods. Step 3. Create your Android project and add the following classpath into root of the project's build.gradle. classpath 'com.google.gms:google-services:3.1.1' Step 4. And then add the following plugin into your module's build.gradle. apply plugin: 'com.google.gms.google-services' Step 5. And then add the following dependency into your module's build.gradle. implementation 'com.firebaseui:firebase-ui-auth:3.3.1' That's you wanted to access the Firebase's Authentication API in your project. Here is the methods to Sign In, Sign Out and delete the account. Sign In private fun signIn() { // Choose authentication providers val providers = Arrays.asList(AuthUI.IdpConfig.GoogleBuilde

Simple example of OCRReader in Android.

Hi Friends, Maybe you all heard/used text scanning using camera feature or extracting text from Image. But this sample made it very easy for you. You can made it in very simple line of code. You can download the source code from OCRSample and import the library as a module into your project. Example usage : MainActivity.java public class MainActivity extends AppCompatActivity { private TextView textView; private final int CAMERA_SCAN_TEXT = 0; private final int LOAD_IMAGE_RESULTS = 1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); textView = findViewById(R.id.textView); } @Override public boolean onCreateOptionsMenu(Menu menu) { MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.menu_main, menu); return super.onCreateOptionsMenu(menu); } @Override public boolean onOptionsItemSele

Simple example of using RadioGroup, RadioButton in Kotlin | Android

Here is the simple example of using RadioGroup, RadioButton in Android Kotlin. &ltRadioGroup xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/radioGroup" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.guna.kotlinapplication.BlankFragment"&gt &ltRadioButton android:id="@+id/radioMale" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/male" /&gt &ltRadioButton android:id="@+id/radioFemale" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/female" /&gt &lt/RadioGroup&gt OnCheckedChangeListener radioGroup.setOnChe

Simple example of using Fragments in Kotlin | Android

Here is the simple example of using Fragments with Activity in Android. MainActivity.kt class MainActivity : AppCompatActivity(), BlankFragment.OnFragmentInteractionListener { override fun onFragmentInteraction(uri: Uri) { TODO("not implemented") //To change body of created functions use File | Settings | File Templates. } override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) setSupportActionBar(toolbar) if (savedInstanceState == null) { supportFragmentManager.beginTransaction(). add(R.id.fragment, BlankFragment.newInstance("Param1", "Param2"), "Second").commit() } fab.setOnClickListener { view -> //Here we will replace the BlanckFragment with SecondFragment. supportFragmentManager.beginTransaction().replace(R.id.fragment, SecondFragment(),