listen for when the app goes compltely in the foreground and in the background

in app gradle:

//Lifecycles only (no ViewModel or LiveData) not working at the moment...
implementation "android.arch.lifecycle:extensions:1.1.1"
annotationProcessor "android.arch.lifecycle:compiler:1.1.1"

create a class:

public class AppLifecycleObserver implements LifecycleObserver {

    @OnLifecycleEvent(Lifecycle.Event.ON_START)
    public void onMoveToForeground() {
        Log.d("AppLifecycleObserver", "Returning to foreground…");
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_STOP)
    public void onMoveToBackground() {
        Log.d("AppLifecycleObserver", "Moving to background…");
    }
}

 

initialize the AppLifecycleObserver in your app Application:

public class AppApplication extends Application {

    private AppLifecycleObserver appLifecycleObserver = new AppLifecycleObserver();

    @Override
    public void onCreate() {
        super.onCreate();
        setupLifecycleObserver();
    }

    private void setupLifecycleObserver() {
        ProcessLifecycleOwner
                .get()
                .getLifecycle()
                .addObserver(appLifecycleObserver);
    }
}

 

and finally, declare the AppApplication in the manifest

<application
    android:name=".AppApplication"

source: https://proandroiddev.com/detecting-when-an-android-app-backgrounds-in-2018-4b5a94977d5c

Fix RecyclerView not being smooth while inside a NestedScrollView

use

recyclerView.setNestedScrollingEnabled(false);
keep in mind this will cause the RecyclerView not to use the ViewHolder pattern. Use it when there aren’t a lot of items in it

border only on one side

 

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:left="-2dp" android:bottom="-2dp" android:right="-2dp">
        <shape android:shape="rectangle">
            <stroke android:width="2dp" android:color="@color/borderColor" />
            <solid android:color="@color/backgroundColor" />
        </shape>
    </item>

</layer-list>

 

source

.gitignore for android

Source: http://stackoverflow.com/a/21049338/2810172

# built application files
*.apk
*.ap_

# files for the dex VM
*.dex

# Java class files
*.class

# built native files (uncomment if you build your own)
# *.o
# *.so

# generated files
bin/
gen/

# Ignore gradle files
.gradle/
build/

# Local configuration file (sdk path, etc)
local.properties

# Proguard folder generated by Eclipse
proguard/

# Eclipse Metadata
.metadata/

# Mac OS X clutter
*.DS_Store

# Windows clutter
Thumbs.db

# Intellij IDEA (see https://intellij-support.jetbrains.com/entries/23393067)
.idea/workspace.xml
.idea/tasks.xml
.idea/datasources.xml
.idea/dataSources.ids

Renaming your android studio project

You can do this:

  1. Change the package name manually in the manifest file.
  2. Click on your R.java class and the press F6 (Refactor->Move…). It will allow you to move the class to another package, and all references to that class will be updated.

Source: http://stackoverflow.com/questions/6600329/how-do-i-rename-the-android-package-name

 

Other links that might be useful

http://stackoverflow.com/questions/16804093/android-studio-rename-package

http://stackoverflow.com/questions/18276872/change-project-name-on-android-studio

Previewing widgets in android studio preview mode

Inflating things this way seems to work for the preview:

public FlightOutboundInbound(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    init();
}

private void init() {
    setWeightSum(3);
    setBackgroundResource(R.drawable.dark_blue_border_rect);
    setOrientation(HORIZONTAL);

    LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    inflater.inflate(R.layout.widget_flight_outbound_inbound, this, true);
    ButterKnife.bind(this);
}

But inflating things using the static view does not seem to work.

Source: http://stackoverflow.com/questions/17296552/preview-layout-with-merge-root-tag-in-intellij-idea-android-studio

<merge xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:parentTag="LinearLayout"
    tools:orientation="horizontal">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Some text"
        android:textSize="20sp"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Some other text"/>
</merge>

Recyclerview scrolling messes the UI items

1) If you are using ListView – override the following methods.

    @Override
    public int getViewTypeCount() {

        return getCount();
    }

    @Override
    public int getItemViewType(int position) {

        return position;
    }

2) If you are using RecyclerView – override only getItemViewType method.

    @Override
    public int getItemViewType(int position) {

        return position;
    }

it seems that for the recycler view by just overriding the getItemViewType() solves this issue, no need to manually keep track of the state

 

source: android how to sort an arraylist

Sorting an ArrayList that contains custom objects

Source: http://stackoverflow.com/questions/10853205/android-sort-arraylist-by-properties

You need to implement a Comparator for instance:

public class FishNameComparator implements Comparator<Fish>
{
    public int compare(Fish left, Fish right) {
        return left.name.compareTo(right.name);
    }
}

and then sort it like this:

Collections.sort(fishes, new FishNameComparator());

removing an item from a recyclerview

Source: http://stackoverflow.com/questions/26076965/android-recyclerview-addition-removal-of-items

        @Override
        public void onBindViewHolder(final ContactsSelectedViewHolder holder, final int position) {
            final Contact contact = mContactsSelected.get(position);
            setListeners(holder, position);
        }

        private void setListeners(ContactsSelectedViewHolder holder, final int position) {
            holder.removeIcon.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    int newPosition = holder.getAdapterPosition();
                    mContactsSelected.remove(newPosition);
                    notifyItemRemoved(newPosition);
                    notifyItemRangeChanged(newPosition,mContactsSelected.size());
                }
            });
        }

Changing Toolbar title styling

Source: http://stackoverflow.com/questions/26533510/android-toolbar-center-title-and-custom-font

The ToolBar title is stylable. Any customization you make has to be made in the theme. I’ll give you an example.

Toolbar layout:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
    style="@style/ToolBarStyle.Event"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="?attr/colorPrimary"
    android:minHeight="@dimen/abc_action_bar_default_height_material" />

Styles:

<style name="ToolBarStyle" parent="ToolBarStyle.Base"/>

<style name="ToolBarStyle.Base" parent="">
    <item name="popupTheme">@style/ThemeOverlay.AppCompat.Light</item>
    <item name="theme">@style/ThemeOverlay.AppCompat.Dark.ActionBar</item>
</style>

<style name="ToolBarStyle.Event" parent="ToolBarStyle">
    <item name="titleTextAppearance">@style/TextAppearance.Widget.Event.Toolbar.Title</item>
</style>

<style name="TextAppearance.Widget.Event.Toolbar.Title" parent="TextAppearance.Widget.AppCompat.Toolbar.Title">
    <!--Any text styling can be done here-->
    <item name="android:textStyle">normal</item>
    <item name="android:textSize">@dimen/event_title_text_size</item>
</style>