Skip to main content

Posts

Showing posts from October, 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 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