; paras[X - 1].parentNode.insertBefore(ad1, paras[X]); } if (paras.length > X + 4) { var ad1 = document.createElement('div'); ad1.className = 'ad-auto-insert ad-first'; ad1.innerHTML = ` ; paras[X + 3].parentNode.insertBefore(ad2, paras[X + 4]); } if (isMobile && paras.length > X + 8) { var ad1 = document.createElement('div'); ad1.className = 'ad-auto-insert ad-first'; ad1.innerHTML = ` ; paras[X + 7].parentNode.insertBefore(ad3, paras[X + 8]); } });

Advertisement

BottomSlider like Uber


I really like uber's Bottom Slider a lot and i want to create a view like that.After searching, I found a great library do it easily. Here is the screenshot of the sample.





Step: 1
======

Add below line to build.gradle file under app folder and sync

compile 'com.github.lawloretienne:discreteslider:0.0.9'

Credits to the Author of the Library.
Etienne Lawlor
Github Link 


Step: 2
======
create a layout like below

activity_main.xml
=============


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<FrameLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@color/grey_500"
android:orientation="vertical">

</FrameLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:background="@color/grey_100"
android:orientation="vertical">

<RelativeLayout
android:id="@+id/tick_mark_labels_rl"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="8dp" />

<com.etiennelawlor.discreteslider.library.ui.DiscreteSlider
android:id="@+id/discrete_slider"
android:layout_width="match_parent"
android:layout_height="56dp"
android:layout_marginBottom="4dp"
android:background="@color/grey_100"
android:paddingLeft="8dp"
android:paddingRight="8dp"
app:backdropFillColor="@color/grey_200"
app:backdropStrokeColor="@color/grey_300"
app:backdropStrokeWidth="1dp"
app:horizontalBarThickness="4dp"
app:position="0"
app:progressDrawable="@drawable/transparent_progress_drawable"
app:tickMarkCount="4"

app:tickMarkRadius="8dp" />

</LinearLayout>

</LinearLayout>


Step: 3
======
create a drawable with an image like below. I have created four files like this. please see source code for reference.

ubergo.xml
=========

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

<item>

<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">

<size
android:width="40dp"
android:height="40dp" />
<stroke
android:width="8dp"
android:color="@color/fifty_percent_transparency_primary" />

<solid android:color="@color/colorPrimary" />

<corners android:radius="1dp" />

</shape>
</item>

<item>

<bitmap
android:gravity="center"
android:src="@drawable/ic_directions_car_black_24dp" />
</item>


</layer-list>

Add all icons are added as an array in strings.xml file and we can use in MainActivity class.

<array name="icons">
<item>@drawable/uber_pool</item>
<item>@drawable/uber_moto</item>
<item>@drawable/uber_go</item>
<item>@drawable/uber_suv</item>
</array>


Step: 4
======
Create an Activity class and

MainActivity.java
==============


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package com.pratap.discreteslider;

import android.content.res.Resources;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.Gravity;
import android.view.ViewTreeObserver;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.etiennelawlor.discreteslider.library.ui.DiscreteSlider;
import com.etiennelawlor.discreteslider.library.utilities.DisplayUtility;

public class MainActivity extends AppCompatActivity {


DiscreteSlider discreteSlider;
RelativeLayout tickMarkLabelsRelativeLayout;

TypedArray icons;
String[] tickMarkLabels = {"POOL", "uberMOTO", "uberGo", "uberSUV"};

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tickMarkLabelsRelativeLayout = (RelativeLayout) findViewById(R.id.tick_mark_labels_rl);
discreteSlider = (DiscreteSlider) findViewById(R.id.discrete_slider);

Resources res = getResources();
icons = res.obtainTypedArray(R.array.icons);

// Detect when slider position changes
discreteSlider.setOnDiscreteSliderChangeListener(new DiscreteSlider.OnDiscreteSliderChangeListener() {
@Override
public void onPositionChanged(int position) {
int childCount = tickMarkLabelsRelativeLayout.getChildCount();
for (int i = 0; i < childCount; i++) {
TextView tv = (TextView) tickMarkLabelsRelativeLayout.getChildAt(i);
if (i == position) {
tv.setTextColor(ContextCompat.getColor(MainActivity.this, R.color.colorPrimary));
discreteSlider.setThumb(icons.getDrawable(position));

// show selected item
Toast toast = Toast.makeText(MainActivity.this, tickMarkLabels[i], Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
} else
tv.setTextColor(ContextCompat.getColor(MainActivity.this, R.color.grey_700));


}


}
});

tickMarkLabelsRelativeLayout.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
tickMarkLabelsRelativeLayout.getViewTreeObserver().removeGlobalOnLayoutListener(this);

addTickMarkTextLabels();
}
});


}


private void addTickMarkTextLabels() {
int tickMarkCount = discreteSlider.getTickMarkCount();
float tickMarkRadius = discreteSlider.getTickMarkRadius();
int width = tickMarkLabelsRelativeLayout.getMeasuredWidth();

int discreteSliderBackdropLeftMargin = DisplayUtility.dp2px(this, 32);
int discreteSliderBackdropRightMargin = DisplayUtility.dp2px(this, 32);
float firstTickMarkRadius = tickMarkRadius;
float lastTickMarkRadius = tickMarkRadius;
int interval = (width - (discreteSliderBackdropLeftMargin + discreteSliderBackdropRightMargin) - ((int) (firstTickMarkRadius + lastTickMarkRadius)))
/ (tickMarkCount - 1);

int tickMarkLabelWidth = DisplayUtility.dp2px(this, 40);

for (int i = 0; i < tickMarkCount; i++) {
TextView tv = new TextView(this);

RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);

tv.setText(tickMarkLabels[i]);
tv.setGravity(Gravity.CENTER);


if (i == discreteSlider.getPosition()) {
tv.setTextColor(ContextCompat.getColor(MainActivity.this, R.color.colorPrimary));
discreteSlider.setThumb(icons.getDrawable(i));

} else
tv.setTextColor(ContextCompat.getColor(MainActivity.this, R.color.grey_700));

// tv.setBackgroundColor(getResources().getColor(android.R.color.holo_blue_dark));

int left = discreteSliderBackdropLeftMargin + (int) firstTickMarkRadius + (i * interval) - (tickMarkLabelWidth / 2);

layoutParams.setMargins(left,
0,
0,
0);
tv.setLayoutParams(layoutParams);

tickMarkLabelsRelativeLayout.addView(tv);
}
}

}

Source Code
==========
Link

ScreenShots
==========













Image Compression in Android



Image Compression Library for Android

Handy Library to compress and upload images when the files are too large.
https://github.com/zetbaitsu/Compressor


Step 1:

Add below line to buid.gradle file

compile 'id.zelory:compressor:1.0.4'


Step 2:

MainActivity.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package com.pratap.imagecompression;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import java.io.File;
import java.io.IOException;
import java.text.DecimalFormat;

import id.zelory.compressor.Compressor;
import id.zelory.compressor.FileUtil;


public class MainActivity extends AppCompatActivity {


ImageView img_original, img_compressed;
TextView txt_filePath;
Button btnSelectImage;
private File actualImage;
private File compressedImage;


private static final int PICK_IMAGE_REQUEST = 1;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);


img_original = (ImageView) findViewById(R.id.img_original);
img_compressed = (ImageView) findViewById(R.id.img_compressed);
txt_filePath = (TextView) findViewById(R.id.txt_filePath);
btnSelectImage = (Button) findViewById(R.id.btnSelectImage);


btnSelectImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {


chooseImage(view);


}
});
}


public void chooseImage(View view) {
/*Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("image*//*");
startActivityForResult(intent, PICK_IMAGE_REQUEST);*/

Intent i = new Intent(
Intent.ACTION_PICK,
MediaStore.Images.Media.EXTERNAL_CONTENT_URI);

startActivityForResult(i, PICK_IMAGE_REQUEST);

}


@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK) {
if (data == null) {
showError("Failed to open picture!");
return;
}
try {
actualImage = FileUtil.from(this, data.getData());
// code to compress image and stores it Pictures folder 
compressedImage = new Compressor.Builder(this)
.setMaxWidth(640)
.setMaxHeight(480)
.setQuality(90)
.setCompressFormat(Bitmap.CompressFormat.PNG)
.setDestinationDirectoryPath(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES).getAbsolutePath())
.build()
.compressToFile(actualImage);



txt_filePath.setText(
String.format("Actual Size : %s", getReadableFileSize(actualImage.length()) + "" +
"\nActual FilePath : "+ actualImage.getAbsolutePath() +
String.format("\nCompressed Size : %s", getReadableFileSize(compressedImage.length()))+
"\nCompressed FilePath : "+ compressedImage.getAbsolutePath()
));

img_original.setImageBitmap(BitmapFactory.decodeFile(actualImage.getAbsolutePath()));
img_compressed.setImageBitmap(BitmapFactory.decodeFile(compressedImage.getAbsolutePath()));

} catch (IOException e) {
showError("Failed to read picture data!");
e.printStackTrace();
}
}
}


public String getReadableFileSize(long size) {
if (size <= 0) {
return "0";
}
final String[] units = new String[]{"B", "KB", "MB", "GB", "TB"};
int digitGroups = (int) (Math.log10(size) / Math.log10(1024));
return new DecimalFormat("#,##0.#").format(size / Math.pow(1024, digitGroups)) + " " + units[digitGroups];
}


public void showError(String errorMessage) {
Toast.makeText(this, errorMessage, Toast.LENGTH_SHORT).show();
}
}



activity_main.xml
==============

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent">

<LinearLayout
android:id="@+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">

<ImageView
android:id="@+id/img_original"
android:layout_width="match_parent"
android:layout_height="250dp"
android:layout_weight="1"
android:padding="1dp"
android:scaleType="centerCrop" />

<ImageView
android:id="@+id/img_compressed"
android:layout_width="match_parent"
android:layout_height="250dp"
android:layout_weight="1"
android:padding="1dp"
android:scaleType="centerCrop" />


</LinearLayout>

<TextView
android:id="@+id/txt_filePath"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/linearLayout"
android:layout_centerHorizontal="true"
android:layout_margin="10dp"
android:text="" />

<Button
android:id="@+id/btnSelectImage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:text="Select Image" />


</RelativeLayout>


Code Snippet that do compress the image
==============================
 compressedImage = new Compressor.Builder(this)
.setMaxWidth(640)
.setMaxHeight(480)
.setQuality(90)
.setCompressFormat(Bitmap.CompressFormat.PNG)
.setDestinationDirectoryPath(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES).getAbsolutePath())
.build()
.compressToFile(actualImage);


References:
https://github.com/zetbaitsu/Compressor

Scan BarCode in Android barcodescanner:zxing Library



Step: 1
======



Credits to author of the Library.
https://github.com/dm77/barcodescanner

Add library to your build.gradle file under app folder

compile 'me.dm7.barcodescanner:zxing:1.9'


Step: 2
======

MainActivity.java
==================

package com.pratap.scanbarcode;

import android.Manifest;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

EditText txt_barcodevalue;

Button btn_scan;

final int REQUEST_CAMERA_PERMISSION = 1001;
final int DELAY_TIME = 4000;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

txt_barcodevalue = (EditText) findViewById(R.id.txt_barcodevalue);
btn_scan = (Button) findViewById(R.id.btn_scan);

btn_scan.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
checkCameraPermissions();

}
});
}

public void openBarCodeScanner() {


Intent scanIntent = new Intent(this, BarCodeScannerActivity.class);
startActivityForResult(scanIntent, 1006);

}


public void checkCameraPermissions() {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED) {
openBarCodeScanner();
} else {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA}, REQUEST_CAMERA_PERMISSION);
}
}


@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);

if (requestCode == REQUEST_CAMERA_PERMISSION) {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// permission was granted
openBarCodeScanner();
} else {
// permission denied, boo!
showMessage("Camera Permission Denied!!!");
}
}
}


public void showMessage(String message) {

Toast.makeText(this, message, Toast.LENGTH_SHORT).show();

}


@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);

if (requestCode == 1006) {
if (resultCode == Activity.RESULT_OK && data != null) {

String barcodeNumber = data.getStringExtra("BarCodeNumber");
txt_barcodevalue.setText(barcodeNumber);

}
}


}

}


XML Layout file for Main Activity
=========================

activity_main.xml
=============


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_centerVertical="true"
android:layout_margin="16dp"
android:orientation="horizontal">

<EditText
android:id="@+id/txt_barcodevalue"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:inputType="text" />

<Button
android:id="@+id/btn_scan"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Scan" />

</LinearLayout>


</RelativeLayout>





Step: 3
======

BarCodeScannerActivity.java
=====================
Please add this Activity to your AndroidManifest file.


package com.pratap.scanbarcode;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

import com.google.zxing.Result;

import me.dm7.barcodescanner.zxing.ZXingScannerView;

public class BarCodeScannerActivity extends Activity implements ZXingScannerView.ResultHandler {
private ZXingScannerView mScannerView;

@Override
public void onCreate(Bundle state) {
super.onCreate(state);
mScannerView = new ZXingScannerView(this);
setContentView(mScannerView);
}

@Override
public void onResume() {
super.onResume();
mScannerView.setResultHandler(this);
mScannerView.startCamera();
}

@Override
public void onPause() {
super.onPause();
mScannerView.stopCamera();
}

@Override
public void handleResult(Result rawResult) {
// Do something with the result here
Log.v("Scan Result", rawResult.getText()); // Prints scan results
Log.v("Scan Qr code format", rawResult.getBarcodeFormat().toString()); // Prints the scan format (qrcode, pdf417 etc.)

Toast.makeText(BarCodeScannerActivity.this, rawResult.getText(), Toast.LENGTH_SHORT).show();

// If you would like to resume scanning, call this method below:
// mScannerView.resumeCameraPreview(this);

Intent intentMessage = new Intent();

// put the message in Intent
intentMessage.putExtra("BarCodeNumber", rawResult.getText());
// Set The Result in Intent
setResult(RESULT_OK, intentMessage);
// finish The activity
finish();


}
}



Step: 4
======

AndroidManifest.xml
===================


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.pratap.scanbarcode">


<uses-permission android:name="android.permission.CAMERA" />

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>


<activity
android:name=".BarCodeScannerActivity"
android:screenOrientation="portrait">

</activity>
</application>

</manifest>


Demo
======








References
========
Credits to author of the Library.
https://github.com/dm77/barcodescanner


















UPTET news