Skip to main content

Posts

Showing posts from 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 SeekBarPreference in Android

There is no predefined SeekBarPreference in Android. But we can make our own SeekBarPreference very simply. Here is simple example of making and using SeekBarPreference in Android. SeekBarPreference.java public class SeekBarPreference extends Preference implements SeekBar.OnSeekBarChangeListener { private TextView textValue; public SeekBarPreference(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { super(context, attrs, defStyleAttr, defStyleRes); } public SeekBarPreference(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } public SeekBarPreference(Context context, AttributeSet attrs) { super(context, attrs); } public SeekBarPreference(Context context) { super(context); } @Override protected View onCreateView(ViewGroup parent) { super.onCreateView(parent); View view = LayoutInflater.from(getContext()).inflate(R.layou

Simple example of Using JobScheduler in Android

JobScheduler is the Android framework API for scheduling tasks or work. It first became available in Android 5.0 (API level 21), and remains under active development. Notably, Android 7.0 (API level 24) added the ability to trigger jobs based on ContentProvider changes. If your app targets Android 5.0 (API level 21), we recommend that you use the JobScheduler to execute background tasks. Here is the simple example of Scheduling and Cancelling the scheduled job. MainActivity.java public class MainActivity extends AppCompatActivity { int count; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Toolbar toolbar = findViewById(R.id.toolbar); setSupportActionBar(toolbar); } public void addJob(View view) { count++; JobScheduler scheduler = (JobScheduler) getSystemService(Context.JOB_SCHEDULER_SERVICE); ComponentName comp

Simple example of using ViewPager in Kotlin | Android

Though Kotlin has lot of massive features to speedup the development time, here is the simple example of using ViewPager in Android. In Kotlin we don't need to declare and initialize Views. We can simply access the id of Views from xml. Ex: activity_main.xml &lt?xml version="1.0" encoding="utf-8"?&gt &ltandroid.support.design.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" tools:context="com.guna.kotlinapplication.MainActivity"&gt &ltandroid.support.design.widget.AppBarLayout android:id="@+id/appBarLayout" android:layout_width="match_parent" android:layout_height="wrap_content" android:the

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

Simple ConstraintLayout tutorial in Android.

A ConstraintLayout is a ViewGroup which allows you to position and size widgets in a flexible way. There are currently various types of constraints that you can use: Relative positioning Margins Centering positioning Circular positioning Visibility behavior Dimension constraints Chains Virtual Helpers objects In this tutorial we will look about Relative positioning more detailly. Relative positioning Relative positioning is one of the basic building block of creating layouts in ConstraintLayout. Those constraints allow you to position a given widget relative to another one. You can constrain a widget on the horizontal and vertical axis: Horizontal Axis: left, right, start and end sides Vertical Axis: top, bottom sides and text baseline Here is the list of available constraints. layout_constraintLeft_toLeftOf layout_constraintLeft_toRightOf layout_constraintRight_toLeftOf layout_constraintRight_toRightOf layout_constraintTop_toTop

Simple way to show menu items on list item get selected

Here is the most easier way to showing menu items on list items get selected. Your Activity will look like this public class MainActivity extends AppCompatActivity { private RecyclerView myList; private int selectedCount; private ArrayList list; private MyListAdapter adapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); myList = (RecyclerView) findViewById(R.id.myList); myList.setHasFixedSize(true); myList.setLayoutManager(new LinearLayoutManager(this)); list = new ArrayList<>(); for (int i = 0; i < 20; i++) { list.add(new Item("Row " + (i + 1))); } adapter = new MyListAdapter(list, this); myList.setAdapter(adapter); } @Override public boo

Do you know final size of this ArrayList?

I bet, You will never expect this strange answer. I am leaving it to you right now. Here is the code. Integer[] list = {2, 4, 6, 8, 10}; ArrayList integerArrayList = new ArrayList<>(); integerArrayList.addAll(Arrays.asList(list)); for (int i = 0; i < integerArrayList.size(); i++) { if (integerArrayList.get(i) % 2 == 0) { integerArrayList.remove(i); } } Do you know the size of integerArrayList now? If your answer is 0, then its wrong. Shocked right? Here is the explanation. Let's deep dive into the code now. Add a Log inside the if statement. Integer[] list = {2, 4, 6, 8, 10}; ArrayList integerArrayList = new ArrayList<>(); integerArrayList.addAll(Arrays.asList(list)); for (int i = 0; i < integerArrayList.size(); i++) { if (integerArrayList.get(i) % 2 == 0) { Log.v(TAG, "Current list data : " + integerArrayList.toString() + ", integerArrayList.get(" + i + ") : " + integerArrayList.get(i)); i

Bypassing onCreate() on up navigation in Android Activity.

You may provide one back button in Android Activity's Toolbar at Top left corner. Default back button is enough. But in recent times this icon is provided for good design looking purpose only. So simple design looking code shouldn't recreate the previous Activity right? But in Android up navigation from one Activity to previous Activity will recreate the previous Activity. So the previous Activity's onCreate() method will get called. But sometime we don't want to recreate the previous Activity. Just resume is enough. So we may avoid unwanted coding execution which already executed. But did you noticed that bottom default back button will not recreate the Activity. It will just resume the Activity which is in paused state already. So we may use this default back button's code in up navigation also. Then while tapping up navigation icon also our previous Activity will resume instead of recreate. Here is the code sample @Override public boolean

AVC

Privacy Policy Privacy Policy Gunaseelan Arumaikkannu built the AVC app as an Ad Supported app. This SERVICE is provided by Gunaseelan Arumaikkannu at no cost and is intended for use as is. This page is used to inform website visitors regarding my policies with the collection, use, and disclosure of Personal Information if anyone decided to use my Service. If you choose to use my Service, then you agree to the collection and use of information in relation to this policy. The Personal Information that I collect is used for providing and improving the Service. I will not use or share your information with anyone except as described in this Privacy Policy. The terms used in this Privacy Policy have the same meanings as in our Terms and Conditions, which is accessible at AVC unless otherwise defined in this Privacy Policy.

Simple SwitchPreference example in Android

We can add SwitchPreference to PreferenceScreen in xml like below in android R.xml.switch_preference.xml &lt?xml version="1.0" encoding="utf-8"?&gt &ltPreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"&gt &ltSwitchPreference android:key="switch" android:summaryOff="Switch off" android:summaryOn="Switch on" android:title="Switch Preference" /&gt &lt/PreferenceScreen&gt We have to use PreferenceFragment to access this xml like below. SwitchPreference.java import android.content.SharedPreferences; import android.os.Bundle; import android.preference.PreferenceFragment; import android.preference.PreferenceManager; import android.support.annotation.Nullable; import android.widget.Toast; /** * Created by Guna on 05-10-2017. */ public class SwitchPreference extends PreferenceFragment { @Override public void onC