Posts

Showing posts from October, 2017

Simple ConstraintLayout tutorial in Android.

Image
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 positioningMarginsCentering positioningCircular positioningVisibility behaviorDimension constraintsChainsVirtual 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 sidesVertical Axis: top, bottom sides and text baseline Here is the list of available constraints. layout_constraintLeft_toLeftOflayout_constraintLeft_toRightOflayout_constraintRight_toLeftOflayout_constraintRight_toRightOflayout_constraintTop_toTopOflayout_constraintTop_toBottomOflayout_constraintBottom_toTopOfl…

Simple way to show menu items on list item get selected

Image
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?

Image
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)); integ…

Bypassing onCreate() on up navigation in Android Activity.

Image
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 onOption…

AVC

Privacy PolicyPrivacy 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

Image
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 onCreate(@N…