Horizontal progressbar with custom color

activity_main.xml

<progressbar android:id="@+id/progressBar"
style="?android:progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_margin="0dp"
android:max="100"
android:padding="0dp"
tools:progress="40"
android:progressDrawable="@drawable/progress_drawable"
app:layout_collapseMode="pin"/>

MainActivity.java

ProgressBar mProgressBar = (ProgressBar) findViewById(R.id.progressBar);

//somewhere when scrollin
mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);

int lastVisiblePosition = mLayoutManager.findLastVisibleItemPosition();
int allItem = mLayoutManager.getItemCount();

if (allItem > 0) {
double v = ((double) lastVisiblePosition / (double) (allItem - 1)) * 100;
if (mProgressBar != null)
mProgressBar.setProgress((int) v);
}
}
});

progress_drawable.xml

< ?xml version="1.0" encoding="utf-8"?>
<layer -list xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="@android:id/background">
<shape>
<solid android:color="@android:color/transparent"></solid>
</shape>
</item>
<item android:id="@android:id/progress">
<clip>
<shape>
<solid android:color="@color/badge_2"></solid>
</shape>
</clip>
</item>
</layer>

Color theming – some words

(Found somewhere in gist)

With the new theming in AppCompat, a lot of assets are tinted automatically for you via theme attributes. That has often led me to wonder “where the hell did this color come from?” You can replace your normal theme with this debug theme to help figure out the source of that color.

<!-- You can change the parent around to whatever you normally use -->
<style name="DebugColors" parent="Theme.AppCompat">

<!-- System colors -->
<item name="android:windowBackground">@color/__debugWindowBackground</item>

<item name="android:colorPressedHighlight">#FF4400</item>
<item name="android:colorLongPressedHighlight">#FF0044</item>
<item name="android:colorFocusedHighlight">#44FF00</item>
<item name="android:colorActivatedHighlight">#00FF44</item>
<item name="android:colorMultiSelectHighlight">#4400FF</item>

<item name="android:statusBarColor">#FFF000</item>
<item name="android:navigationBarColor">#000FFF</item>

<item name="android:colorForeground">#440000</item>
<item name="android:colorForegroundInverse">#004400</item>
<item name="android:colorBackground">#444400</item>
<item name="android:colorBackgroundCacheHint">#440044</item>

<item name="android:textColorPrimary">#FFFF00</item>
<item name="android:textColorSecondary">#FF00FF</item>
<item name="android:textColorTertiary">#00FFFF</item>

<item name="android:textColorPrimaryInverse">#CCCC00</item>
<item name="android:textColorSecondaryInverse">#CC00CC</item>
<item name="android:textColorTertiaryInverse">#00CCCC</item>

<item name="android:textColorPrimaryDisableOnly">#FFCC00</item>
<item name="android:textColorPrimaryInverseDisableOnly">#FF00CC</item>

<item name="android:textColorPrimaryNoDisable">#CCFF00</item>
<item name="android:textColorSecondaryNoDisable">#00FFCC</item>

<item name="android:textColorPrimaryInverseNoDisable">#CC00FF</item>
<item name="android:textColorSecondaryInverseNoDisable">#00CCFF</item>

<item name="android:textColorHint">#FF8800</item>
<item name="android:textColorHintInverse">#FF0088</item>

<item name="android:textColorHighlight">#88FF00</item>
<item name="android:textColorHighlightInverse">#00FF88</item>

<item name="android:textColorLink">#8800FF</item>
<item name="android:textColorLinkInverse">#0088FF</item>

<item name="android:textColorAlertDialogListItem">#444444</item>

<!-- Color palette (via app-compat) -->
<item name="colorPrimary">#FF0000</item>
<item name="colorPrimaryDark">#00FF00</item>
<item name="colorAccent">#0000FF</item>

<item name="colorControlNormal">#CC0000</item>
<item name="colorControlActivated">#00CC00</item>
<item name="colorControlHighlight">#0000CC</item>

<item name="colorButtonNormal">#880000</item>
<item name="colorSwitchThumbNormal">#008800</item>

<!-- Random other things found in app-compat -->
<item name="actionMenuTextColor">#440000</item>
<item name="editTextColor">#004400</item>
<item name="textColorSearchUrl">#000044</item>

</style>

<!-- Also needed, since windowBackground is a reference, not a color -->
<color name="__debugWindowBackground">#888888</color>

Example how to hide and show floating action button while scrolling the list (code not optimized well)

//this example is for recyclerView but it can be used somewhere else
recyclerView.setOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
            }

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);

                if (dy > 0) {
                    hideFab();
                } else if (dy < 0) { showFab(); } } }); //magic happens here private void hideFab() { if (isFabShowing) { isFabShowing = false; if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
                final Point point = new Point();
                getActivity().getWindow().getWindowManager().getDefaultDisplay().getSize(point);
                final float translation = fab.getY() - point.y;
                fab.animate().translationYBy(-translation).start();
            } else {
                Animation animation = AnimationUtils.makeOutAnimation(getActivity(), true);
                animation.setFillAfter(true);

                animation.setAnimationListener(new Animation.AnimationListener() {
                    @Override
                    public void onAnimationStart(Animation animation) {

                    }

                    @Override
                    public void onAnimationEnd(Animation animation) {
                        fab.setClickable(false);
                    }

                    @Override
                    public void onAnimationRepeat(Animation animation) {

                    }
                });

                fab.startAnimation(animation);
            }
        }
    }

    private void showFab() {
        if (!isFabShowing) {
            isFabShowing = true;
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
                fab.animate().translationY(0).start();
            } else {
                Animation animation = AnimationUtils.makeInAnimation(getActivity(), false);
                animation.setFillAfter(true);

                animation.setAnimationListener(new Animation.AnimationListener() {
                    @Override
                    public void onAnimationStart(Animation animation) {

                    }

                    @Override
                    public void onAnimationEnd(Animation animation) {
                        fab.setClickable(true);
                    }

                    @Override
                    public void onAnimationRepeat(Animation animation) {

                    }
                });

                fab.startAnimation(animation);
            }
        }
    }

Example how to take photo and upload it’s thumbnail to your server using okhttp3 (first I convert it to jpeg)

//part when you click on item and start camera
imageView.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        Context context = v.getContext();

        if (takePictureIntent.resolveActivity(context.getPackageManager()) != null) {
            startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
        }
    }
});

//part when you get the thumbnail from camera
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == Activity.RESULT_OK) {
        Bundle extras = data.getExtras();
        Bitmap imageBitmap = (Bitmap) extras.get("data");

        new AsyncTask() {
            @Override
            protected Void doInBackground(Bitmap... bitmaps) {
                OkHttpClient client = new OkHttpClient();

                ByteArrayOutputStream stream = new ByteArrayOutputStream();
                bitmaps[0].compress(Bitmap.CompressFormat.JPEG, 100, stream);
                byte[] byteArray = stream.toByteArray();

                //there are some my custom fields form
                RequestBody requestBody = new MultipartBody.Builder()
                        .setType(MultipartBody.FORM)
                        .addFormDataPart("submit", "")
                        .addFormDataPart("name", "myPhotoName")
                        .addFormDataPart("photo", "tmp_photo_" + System.currentTimeMillis(), RequestBody.create(MediaType.parse("image/jpeg"), byteArray))
                        .build();

                Request request = new Request.Builder()
                        .url("www.example.com/upload_image.php")
                        .post(requestBody)
                        .build();

                Response response = null;
                try {
                    response = client.newCall(request).execute();
                    if (response.isSuccessful()) {
                        Log.d(TAG, "doInBackground: upload success");
                    } else {
                        Log.d(TAG, "doInBackground: upload failed");
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
                return null;
            }
        }.execute(imageBitmap);
    } else {
        //another results
        super.onActivityResult(requestCode, resultCode, data);
    }
}

Create text for empty listView

RelativeLayout lContainerLayout = new RelativeLayout(this);
lContainerLayout.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT, RelativeLayout.LayoutParams.FILL_PARENT));
TextView textView = new TextView(this);
textView.setText("List is empty!");
RelativeLayout.LayoutParams lButtonParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
lButtonParams.addRule(RelativeLayout.CENTER_IN_PARENT);
textView.setLayoutParams(lButtonParams);
lContainerLayout.addView(textView);
addContentView(lContainerLayout, new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT, RelativeLayout.LayoutParams.FILL_PARENT));
listView.setEmptyView(lContainerLayout);

Pretty way for serialization files

Found here: http://stackoverflow.com/a/4118917/619673

Saving (w/o exception handling code):

FileOutputStream fos = context.openFileOutput(fileName, Context.MODE_PRIVATE);
ObjectOutputStream os = new ObjectOutputStream(fos);
os.writeObject(this);
os.close();
fos.close();

Loading (w/o exception handling code):

FileInputStream fis = context.openFileInput(fileName);
ObjectInputStream is = new ObjectInputStream(fis);
SimpleClass simpleClass = (SimpleClass) is.readObject();
is.close();
fis.close();