Skip to main content

Posts

Showing posts from November, 2017

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 using RecyclerView in Kotlin | Android

Though Kotlin has lot of massive features to speedup the development time, here is the simple example of using RecyclerView in Android. In Kotlin we don't need to declare and initialize RecyclerView. We can simply access the id of RecyclerView from xml. Ex : &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" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.guna.kotlinapplication.MainActivity"&gt &ltandroid.support.v7.widget.RecyclerView android:id="@+id/recyclerView" app:layout_constraintTop_toTopOf="parent" android:layout_width="match_parent" android:layout_

Simple example of using Spinner in Kotlin | Android

Though Kotlin has lot massive features to speedup the development time, here is the simple way of using Spinner in Android. In Kotlin we don't need to declare and initialize Spinner. We can simply access the id of Spinner from xml. Ex : import android.os.Bundle import android.support.v7.app.AppCompatActivity import android.view.View import android.widget.AdapterView import android.widget.ArrayAdapter import android.widget.Toast import android.widget.Toast.LENGTH_LONG import kotlinx.android.synthetic.main.activity_main.* class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) //String array. val myStrings = arrayOf("One", "Two", "Three", "Four", "Five") //Adapter for spinner mySpinner.adapter = ArrayAdapter(this, android.R.layout.simple_spinner_dropdown_i

Simple example of using CheckBox in Kotlin | Android

Though Kotlin has lot massive features to speedup the development time, here is the simple way of using Button in Android. In Kotlin we don't need to declare and initialize CheckBox. We simply access the id of CheckBox from Kotlin file. Ex : import android.os.Bundle import android.support.v7.app.AppCompatActivity import android.widget.Toast import android.widget.Toast.LENGTH_LONG import kotlinx.android.synthetic.main.activity_main.* class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) myCheckBox.setOnCheckedChangeListener { view, isChecked -> Toast.makeText(this, isChecked.toString(), LENGTH_LONG).show() } } } Did you noticed, we never declare myCheckBox nor initialized, instead we directly accessed it from activity_main. And inside setOnCheckedChangeListener method, we didn't implement `onChecke

Basic data types of KOTLIN and simple examples.

In this section we describe the basic types used in Kotlin: numbers, characters, booleans, arrays, and strings. Numbers Kotlin handles numbers in a way close to Java, but not exactly the same. For example, there are no implicit widening conversions for numbers, and literals are slightly different in some cases. Kotlin provides the following built-in types representing numbers (this is close to Java): Type Bit width Double 64 Float 32 Long 64 Int 32 Short 16 Byte 8 We can declare data types in KOTLIN as following way. var a: Int = 0 //int var, we can reassign at runtime or anywhere in the code. val a:Int =0 //int val, val cannot reassign. var a = 0 //int var val a = 0 //int val var a = 10.10 //Double var a = 10.10f //Float var a = 10.10F //Float var a = 10L //Long //You can use underscores to make number constants more readable: val oneMillion=1_000_000 Every

Using Button in Kotlin | Android

Though Kotlin has lot massive features to speedup the development time, here is the simple way of using Button in Android. import android.os.Bundle import android.support.v7.app.AppCompatActivity import android.widget.Toast import android.widget.Toast.LENGTH_LONG import kotlinx.android.synthetic.main.activity_main.* class MainActivity : AppCompatActivity() { private var count: Int = 0 override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) myButton.setOnClickListener({ count = count.inc() Toast.makeText(this, "Clicked Count " + count, LENGTH_LONG).show() myButton.setText("Click again") }) } } Did you noticed, Here we used colon(:) instead of extends keyword, but here we used parentheses at end of the AppCompatActivity . And we initialized count as Int var, where Int is one of the data type in Kotlin used to

Getting Started on Kotlin

Kotlin is now an official language on Android. Kotlin is expressive, concise, extensible, powerful, and a joy to read and write. It has wonderful safety features in terms of nullability and immutability. If you're interested in using Kotlin, it's easy to get started because it works side by side with Java and C++ on Android. So you can keep your existing code, continue to use the various Android libraries, and incrementally add Kotlin code to your project. Unlike almost any other language, Kotlin is a drop-in replacement you can use bi-directionally—you can call into the Java language from Kotlin, and you can call into Kotlin from the Java language. Of course, IDE support is also crucial, and we have it. Android Studio is built upon IntelliJ IDEA, an IDE built by JetBrains—the same company that created the Kotlin language. The JetBrains team has been working for years to make sure Kotlin works great with IntelliJ IDEA. So we're inheriting all their hard work. Starting wi