Saturday, 14 October 2017

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));
        integerArrayList.remove(i);
    }
}

Here is the Logcat output for this Log
Current list data : [2, 4, 6, 8, 10], integerArrayList.get(0) : 2
Current list data : [4, 6, 8, 10], integerArrayList.get(1) : 6
Current list data : [4, 8, 10], integerArrayList.get(2) : 10
So, now you may know.
In our for loop, at our first iteration we have 5 digits in our list, i is 0, size is 5, 0 < 5 condition passed, so we are removing 0th position, which is 2.
At our second iteration we have 4 digits in our list, i is 1, size is 4, 1 < 4 condition passed, and we are removing 1st position, which is 6.
At our third iteration we have 3 digits in our list, i is 2, size is 3, 2 < 3 condition passed, and we are removing 2nd position, which is 10.
At our fourth iteration we have 2 digits remaining in our list, i is 3, size is 2, 3 < 2 condition failed, so finally we have 2 digits our list, they are 4 and 8, still they are dividable by 2.

Interesting right?

If you are really interested in this strange code, then share your solution as comment. How will you remove all of the numbers in the given list which is dividable by 2 or n.



Thank You



Wednesday, 11 October 2017

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 onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()){
            case android.R.id.home:
                //To resume previous activity on up navigation this simple code is enough.
                //By this we can simply resume previous activity.
                onBackPressed();
                break;
        }
        return true;
    }
Detailed video tutorial



I hope this post is useful to you. kindly share your feedback as comment here.



Thank You



Saturday, 7 October 2017

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.

Information Collection and Use

For a better experience, while using our Service, I may require you to provide us with certain personally identifiable information. The information that I request is retained on your device and is not collected by me in any way

The app does use third party services that may collect information used to identify you.

Link to privacy policy of third party service providers used by the app

Log Data

I want to inform you that whenever you use my Service, in a case of an error in the app I collect data and information (through third party products) on your phone called Log Data. This Log Data may include information such as your device Internet Protocol (“IP”) address, device name, operating system version, the configuration of the app when utilizing my Service, the time and date of your use of the Service, and other statistics.

Cookies

Cookies are files with small amount of data that is commonly used an anonymous unique identifier. These are sent to your browser from the website that you visit and are stored on your device internal memory.

This Service does not use these “cookies” explicitly. However, the app may use third party code and libraries that use “cookies” to collection information and to improve their services. You have the option to either accept or refuse these cookies and know when a cookie is being sent to your device. If you choose to refuse our cookies, you may not be able to use some portions of this Service.

Service Providers

I may employ third-party companies and individuals due to the following reasons:

  • To facilitate our Service;
  • To provide the Service on our behalf;
  • To perform Service-related services; or
  • To assist us in analyzing how our Service is used.

I want to inform users of this Service that these third parties have access to your Personal Information. The reason is to perform the tasks assigned to them on our behalf. However, they are obligated not to disclose or use the information for any other purpose.

Security

I value your trust in providing us your Personal Information, thus we are striving to use commercially acceptable means of protecting it. But remember that no method of transmission over the internet, or method of electronic storage is 100% secure and reliable, and I cannot guarantee its absolute security.

Links to Other Sites

This Service may contain links to other sites. If you click on a third-party link, you will be directed to that site. Note that these external sites are not operated by me. Therefore, I strongly advise you to review the Privacy Policy of these websites. I have no control over and assume no responsibility for the content, privacy policies, or practices of any third-party sites or services.

Children’s Privacy

These Services do not address anyone under the age of 13. I do not knowingly collect personally identifiable information from children under 13. In the case I discover that a child under 13 has provided me with personal information, I immediately delete this from our servers. If you are a parent or guardian and you are aware that your child has provided us with personal information, please contact me so that I will be able to do necessary actions.

Changes to This Privacy Policy

I may update our Privacy Policy from time to time. Thus, you are advised to review this page periodically for any changes. I will notify you of any changes by posting the new Privacy Policy on this page. These changes are effective immediately after they are posted on this page.

Contact Us

If you have any questions or suggestions about my Privacy Policy, do not hesitate to contact me.

This privacy policy page was created at privacypolicytemplate.net and modified/generated by App Privacy Policy Generator

Thursday, 5 October 2017

Simple SwitchPreference example in Android

We can add SwitchPreference to PreferenceScreen in xml like below in android
R.xml.switch_preference.xml


    


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(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.switch_preference);
    }

    @Override
    public void onResume() {
        super.onResume();
        //You can change preference summary programmatically like following.
        android.preference.SwitchPreference preference = (android.preference.SwitchPreference) findPreference("switch");
        preference.setSummaryOff("Switch off state updated from code");
        preference.setSummaryOn("Switch on state updated from code");

        //You can read preference value anywhere in the app like following. 
        SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getActivity());
        boolean isChecked = sharedPreferences.getBoolean("switch", false);
        Toast.makeText(getActivity(), "isChecked : " + isChecked, Toast.LENGTH_LONG).show();
    }
}


Detailed video tutorial



I hope this post is useful to you. kindly share your feedback as comment here.



Thank You



Wednesday, 16 November 2016

Dynamically change ListView to GridView and Vice Versa using RecyclerView

We can change the RecyclerView from type of ListView to GridView in single line of code.

Video output of sample project

code is:

private void setLayoutManager() {
    if (mColumnCount <= 1) {
        mColumnCount = 1;
        recyclerView.setLayoutManager(new LinearLayoutManager(context));
    } else {
        recyclerView.setLayoutManager(new GridLayoutManager(context, mColumnCount));
    }
}

Video output of sample project:




I hope this post is useful to you. kindly share your feedback as comment here.


Simple RecyclerView example in Android

Simple RecyclerView example with filter option in Android

Simple recyclerview example with checkbox in Android

Source code on GitHub



Thank You



Friday, 6 May 2016

Simple recyclerview example with checkbox in Android

Good morning to all, Those who all tried RecyclerView adapter with CheckBox, you all may find the issue on dynamic data change in RecyclerView. This will help you to resolve the issue in very simple way.

Video output of this project

So how to achieve this task effectively?

Simple, I hope you all tried with list objects only. Surely this object will have the boolean variable to know whether this object is selected or not. So now while we are select one object with the help of recyclerview and scroll down and up recyclerview adapter, the checked checkbox's position may change, this reflects on our object also, So how to avaid this issue.

simple, In our recyclerview adapter's onBindViewHolder method, first set the checkedOnChangeListener of checkbox to null, and then set whether this object is selected or not to checkbox, and then add the checkedOnChangeListener to the checkbox. That's all. Issue solved.

onBindViewHolder
will be look like
    @Override
    public void onBindViewHolder(final ViewHolder holder, int position) {

        //in some cases, it will prevent unwanted situations
        holder.checkbox.setOnCheckedChangeListener(null);

        //if true, your checkbox will be selected, else unselected
        holder.checkbox.setChecked(numbers.get(position).isSelected());

        holder.checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                numbers.get(holder.getAdapterPosition()).setSelected(isChecked);
            }
        });
    }
here
numbers
is list of objects.

I hope this post is useful to you. kindly share your feedback as comment here.


Simple RecyclerView example in Android

Simple RecyclerView example with filter option in Android

Dynamically change ListView to GridView and Vice Versa using RecyclerView

Source code on GitHub



Thank You



Monday, 2 May 2016

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<Number> numbers = new ArrayList<>();
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 HUNDRED", "THREE HUNDRED", "FOUR HUNDRED", "FIVE HUNDRED", "SIX HUNDRED", "SEVEN HUNDRED", "EIGHT HUNDRED", "NINE HUNDRED", "THOUSAND"};
for (int i = 0; i <= 10; i++) {
    Number number = new Number();
    number.setONEs(i+"");
    number.setTENs(i * 10+"");
    number.setHUNDREDs(i * 100+"");
    number.setTextONEs(ONEs[i]);
    number.setTextTENs(TENs[i]);
    number.setTextHUNDREDs(HUNDREDS[i]);
    this.numbers.add(number);
}
adapter = new NumbersAdapter(this.numbers);
list.setAdapter(adapter);
And also you will create your options menu and inflate SearchView as following way.
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        final MenuItem item = menu.findItem(R.id.action_search);
        final SearchView searchView = (SearchView) MenuItemCompat.getActionView(item);
        searchView.setOnQueryTextListener(this);
        return true;
    }
So by above code your activity/fragment will implement SearchView.OnQueryTextListener. So you will have onQueryTextChange method from this listener. Just change this method like following to start filtering with this adapter.
    @Override
    public boolean onQueryTextChange(String newText) {
        final List<Number> filteredModelList = filter(numbers, newText);
        Log.v("App", newText + ", " + numbers.size() + ", " + filteredModelList.size());
        adapter.animateTo(filteredModelList);
        list.scrollToPosition(0);
        return true;
    }
The filter method from above code will return the filtered list as following.
    private List<Number> filter(List<Number> numbers, String query) {
        query = query.toLowerCase();

        ArrayList<Number> filteredCompanyList = new ArrayList<>();
        for (Number client : numbers) {
            final String textOne = client.getTextONEs().toLowerCase();
            final String textTen = client.getTextTENs().toLowerCase();
            final String textHundred = client.getTextHUNDREDs().toLowerCase();
            final String TENs = client.getTENs();
            final String ONEs = client.getONEs();
            final String HUNDREDs = client.getHUNDREDs();
            if (textOne.contains(query) || TENs.contains(query) || ONEs.contains(query) || HUNDREDs.contains(query) || textTen.contains(query) || textHundred.contains(query)) {
                filteredCompanyList.add(client);
            }
        }
        return filteredCompanyList;
    }
And your adapter will have the following related methods to perform this filter with animation.
    public void animateTo(List<Number> models) {
        applyAndAnimateRemovals(models);
        applyAndAnimateAdditions(models);
        applyAndAnimateMovedItems(models);
    }

    private void applyAndAnimateRemovals(List<Number> newModels) {
        for (int i = numbers.size() - 1; i >= 0; i--) {
            final Number model = numbers.get(i);
            if (!newModels.contains(model)) {
                removeItem(i);
            }
        }
    }

    private void applyAndAnimateAdditions(List<Number> newModels) {
        for (int i = 0, count = newModels.size(); i < count; i++) {
            final Number model = newModels.get(i);
            if (!numbers.contains(model)) {
                addItem(i, model);
            }
        }
    }

    private void applyAndAnimateMovedItems(List<Number> newModels) {
        for (int toPosition = newModels.size() - 1; toPosition >= 0; toPosition--) {
            final Number model = newModels.get(toPosition);
            final int fromPosition = numbers.indexOf(model);
            if (fromPosition >= 0 && fromPosition != toPosition) {
                moveItem(fromPosition, toPosition);
            }
        }
    }

    public Number removeItem(int position) {
        final Number model = numbers.remove(position);
        notifyItemRemoved(position);
        return model;
    }

    public void addItem(int position, Number model) {
        numbers.add(position, model);
        notifyItemInserted(position);
    }

    public void moveItem(int fromPosition, int toPosition) {
        final Number model = numbers.remove(fromPosition);
        numbers.add(toPosition, model);
        notifyItemMoved(fromPosition, toPosition);
    }


I hope this post is useful to you. kindly share your feedback as comment here.


Simple RecyclerView example in Android

Simple recyclerview example with checkbox in Android

Dynamically change ListView to GridView and Vice Versa using RecyclerView

Source code on GitHub



Thank You