if(classModelList.get(myViewHolder.getAdapterPosition()).isCheckBoxStatus()) myViewHolder.classCB.setChecked(true); else myViewHolder.classCB.setChecked(false); myViewHolder.classCB.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton compoundButton, boolean b) { if (b) { classModelList.get(myViewHolder.getAdapterPosition()).setCheckBoxStatus(true); //listAllListeners.onItemCheck(holder.checkBoxTime.getText().toString(), holder.getAdapterPosition()); } else { classModelList.get(myViewHolder.getAdapterPosition()).setCheckBoxStatus(false); //listAllListeners.onItemUncheck(holder.checkBoxTime.getText().toString(), holder.getAdapterPosition()); } } });
Saturday, 6 June 2020
RecyclerView scroll checkbox auto select issue solution
Thursday, 10 October 2019
Shadow effect Elevation
<LinearLayout android:layout_width="match_parent" android:layout_height="200dp" android:layout_gravity="center" android:gravity="center" android:layout_margin="20dp" android:elevation="10dp" android:clickable="true" android:background="@drawable/ripple_effect2" > </LinearLayout> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Elevation Example" android:layout_gravity="center" android:gravity="center" android:elevation="5dp" android:padding="20dp" android:layout_marginTop="20dp" android:layout_marginBottom="20dp" android:background="@drawable/ripple_effect" android:clickable="true" ></TextView>
<?xml version="1.0" encoding="utf-8"?><ripple xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:color="#dddddd" tools:targetApi="lollipop"> <item android:id="@android:id/background"> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <solid android:color="#FFFFFF" /> <stroke android:width="3dp" android:color="#FFFFFF" /> <corners android:bottomLeftRadius="15dp" android:topLeftRadius="15dp" android:bottomRightRadius="15dp" android:topRightRadius="15dp" /> <padding android:bottom="0dp" android:left="0dp" android:right="0dp" android:top="0dp" /> </shape> </item> </ripple>
Wednesday, 27 March 2019
Get Full Address From Latitude and Longitude Android Google Map
Get Full Address From Latitude and Longitude Android Google Map
String zipCode = getZipCode(MapsActivity.this,arg0.latitude,arg0.longitude); Toast.makeText(MapsActivity.this,""+zipCode,Toast.LENGTH_LONG).show();
public String getZipCode(Context c, double lat, double lng){ String fullAdd = null; String locality = null; String zip = null; String country = null; try { Geocoder geocoder = new Geocoder(c,Locale.getDefault()); List<Address> addresses = geocoder.getFromLocation(lat,lng,1); if (addresses.size()>0){ Address address = addresses.get(0); fullAdd = address.getAddressLine(0); // full Addresslocality = address.getLocality(); zip = address.getPostalCode(); country = address.getCountryName(); } }catch (IOException ex){ ex.printStackTrace(); } return zip; }
Monday, 4 February 2019
SMS Permission Run time Android Studio
AndroidManifest.xml
<uses-permission android:name="android.permission.READ_SMS"></uses-permission><uses-permission android:name="android.permission.RECEIVE_SMS"></uses-permission>
MainActivity.java
package com.sjt.smspermissionruntime;import android.Manifest;import android.content.DialogInterface;import android.content.pm.PackageManager;import android.support.v4.app.ActivityCompat;import android.support.v4.content.ContextCompat;import android.support.v7.app.AlertDialog;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.util.Log;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;public class MainActivity extends AppCompatActivity {public static final int REQUEST_ID_MULTIPLE_PERMISSIONS = 1;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);if (checkAndRequestPermissions()) {// carry on the normal flow, as the case of permissions granted.// Toast.makeText(SignUp.this,"Granted",Toast.LENGTH_LONG).show();}}private boolean checkAndRequestPermissions() {int permissionSendMessage = ContextCompat.checkSelfPermission(this,Manifest.permission.READ_SMS);int locationPermission = ContextCompat.checkSelfPermission(this, Manifest.permission.RECEIVE_SMS);List<String> listPermissionsNeeded = new ArrayList<>();if (locationPermission != PackageManager.PERMISSION_GRANTED) {listPermissionsNeeded.add(Manifest.permission.READ_SMS);}if (permissionSendMessage != PackageManager.PERMISSION_GRANTED) {listPermissionsNeeded.add(Manifest.permission.RECEIVE_SMS);}if (!listPermissionsNeeded.isEmpty()) {ActivityCompat.requestPermissions(this,listPermissionsNeeded.toArray(new String[listPermissionsNeeded.size()]),REQUEST_ID_MULTIPLE_PERMISSIONS);return false;}return true;}@Overridepublic void onRequestPermissionsResult(int requestCode,String permissions[], int[] grantResults) {Log.d("Permission", "Permission callback called-------");switch (requestCode) {case REQUEST_ID_MULTIPLE_PERMISSIONS: {Map<String, Integer> perms = new HashMap<>();// Initialize the map with both permissionsperms.put(Manifest.permission.READ_SMS, PackageManager.PERMISSION_GRANTED);perms.put(Manifest.permission.RECEIVE_SMS, PackageManager.PERMISSION_GRANTED);// Fill with actual results from userif (grantResults.length > 0) {for (int i = 0; i < permissions.length; i++)perms.put(permissions[i], grantResults[i]);// Check for both permissionsif (perms.get(Manifest.permission.READ_SMS) == PackageManager.PERMISSION_GRANTED&& perms.get(Manifest.permission.RECEIVE_SMS) == PackageManager.PERMISSION_GRANTED) {Log.d("sms", "READ_SMS & RECEIVE_SMS services permission granted");// process the normal flow//else any one or both the permissions are not granted} else {Log.d("Some", "Some permissions are not granted ask again ");//permission is denied (this is the first time, when "never ask again" is not checked) so ask again explaining the usage of permission// // shouldShowRequestPermissionRationale will return true//show the dialog or snackbar saying its necessary and try again otherwise proceed with setup.if (ActivityCompat.shouldShowRequestPermissionRationale(this,Manifest.permission.READ_SMS) ||ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.RECEIVE_SMS)) {showDialogOK("READ_SMS and RECEIVE_SMS Services Permission required for this app",new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {switch (which) {case DialogInterface.BUTTON_POSITIVE:checkAndRequestPermissions();break;case DialogInterface.BUTTON_NEGATIVE:// proceed with logic by disabling the related features or quit the app.break;}}});}//permission is denied (and never ask again is checked)//shouldShowRequestPermissionRationale will return falseelse {// Toast.makeText(this, "Go to settings and enable permissions", Toast.LENGTH_LONG).show();// //proceed with logic by disabling the related features or quit the app.}}}}}}private void showDialogOK(String message, DialogInterface.OnClickListener okListener) {new AlertDialog.Builder(this).setMessage(message).setPositiveButton("OK", okListener).setNegativeButton("Cancel", okListener).create().show();}}
Friday, 14 July 2017
Sunday, 9 July 2017
How to convert GPS Coordinates ( Latitude and Longitude ) to Address in Android.
Manifest
<uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
activity.gps.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"tools:context="raj.sujeet.com.example.GPSActivity"android:weightSum="1"><Buttonandroid:id="@+id/getLL"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="get Lati/long"tools:layout_editor_absoluteX="39dp"tools:layout_editor_absoluteY="28dp"android:layout_marginTop="10dp"tools:ignore="MissingConstraints"android:layout_weight="0.01" /><EditTextandroid:id="@+id/latitude"android:layout_marginTop="10dp"android:layout_width="match_parent"android:layout_height="wrap_content" /> <EditTextandroid:id="@+id/longitude"android:layout_marginTop="10dp"android:layout_width="match_parent"android:layout_height="wrap_content" /> <Buttonandroid:id="@+id/getLocation"android:layout_marginTop="20dp"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="get location" /> <TextViewandroid:layout_margin="10dp"android:id="@+id/result"android:layout_width="wrap_content"android:layout_height="wrap_content" /> </LinearLayout>GPSActivity.javapublic class GPSActivity extends AppCompatActivity { Button getLL,getlocation; EditText latitudeLL,longitudeLL; String lati,longi; TextView result; Geocoder geocoder; List<Address> addressList; // GPSTracker class GPSTracker gps; Context mContext; double latitude,longitude; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_gps); result = (TextView)findViewById(R.id.result); geocoder = new Geocoder(this, Locale.getDefault()); latitudeLL = (EditText)findViewById(R.id.latitude); longitudeLL = (EditText)findViewById(R.id.longitude); getlocation = (Button)findViewById(R.id.getLocation); mContext = this; getLL = (Button)findViewById(R.id.getLL); getLL.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { if (ContextCompat.checkSelfPermission(mContext, Manifest.permission.ACCESS_FINE_LOCATION)!= PackageManager.PERMISSION_GRANTED&& ActivityCompat.checkSelfPermission(mContext, Manifest.permission.ACCESS_COARSE_LOCATION)!= PackageManager.PERMISSION_GRANTED) { ActivityCompat.requestPermissions(GPSActivity.this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 1); } else { Toast.makeText(mContext, "You need have granted permission",Toast.LENGTH_SHORT).show(); gps = new GPSTracker(mContext, GPSActivity.this); // Check if GPS enabledif (gps.canGetLocation()) { latitude = gps.getLatitude(); longitude = gps.getLongitude(); // \n is for new lineToast.makeText(getApplicationContext(), "Your Location is - \nLat: " + latitude + "\nLong: " + longitude, Toast.LENGTH_LONG).show(); lati = Double.toString(latitude); longi = Double.toString(longitude); latitudeLL.setText(lati); longitudeLL.setText(longi); } else { // Can't get location.// GPS or network is not enabled.// Ask user to enable GPS/network in settings.gps.showSettingsAlert(); } } } }); getlocation.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { try { addressList = geocoder.getFromLocation(latitude,longitude,1); String addressStr = addressList.get(0).getAddressLine(0); String areaStr = addressList.get(0).getLocality(); String cityStr = addressList.get(0).getAdminArea(); String countryStr = addressList.get(0).getCountryName(); String postalcodeStr = addressList.get(0).getPostalCode(); String fullAddress = addressStr+", "+areaStr+", "+cityStr+", "+countryStr+", "+postalcodeStr; result.setText(fullAddress); } catch (IOException e) { e.printStackTrace(); } } }); } @Override public void onRequestPermissionsResult(int requestCode, String[] permissions,int[] grantResults) { super.onRequestPermissionsResult(requestCode, permissions, grantResults); switch (requestCode) { case 1: { // If request is cancelled, the result arrays are empty.if (grantResults.length > 0&& grantResults[0] == PackageManager.PERMISSION_GRANTED) { // permission was granted, yay! Do the // contacts-related task you need to do. gps = new GPSTracker(mContext, GPSActivity.this); // Check if GPS enabledif (gps.canGetLocation()) { double latitude = gps.getLatitude(); double longitude = gps.getLongitude(); // \n is for new lineToast.makeText(getApplicationContext(), "Your Location is - \nLat: " + latitude + "\nLong: " + longitude, Toast.LENGTH_LONG).show(); } else { // Can't get location.// GPS or network is not enabled.// Ask user to enable GPS/network in settings.gps.showSettingsAlert(); } } else { // permission denied, boo! Disable the// functionality that depends on this permission. Toast.makeText(mContext, "You need to grant permission",Toast.LENGTH_SHORT).show(); } return; } } } }GPSTracker.javapackage raj.sujeet.com.example; import android.Manifest; import android.app.Activity; import android.app.Service; import android.content.Context; import android.content.DialogInterface; import android.content.Intent; import android.content.pm.PackageManager; import android.location.Location; import android.location.LocationListener; import android.location.LocationManager; import android.os.Bundle; import android.os.IBinder; import android.provider.Settings; import android.support.v4.app.ActivityCompat; import android.support.v4.content.ContextCompat; import android.support.v7.app.AlertDialog; import android.util.Log; /** * Created by Sujeet Raj on 09-07-2017. */public class GPSTracker extends Service { private Context mContext; // Flag for GPS statusboolean isGPSEnabled = false; // Flag for network statusboolean isNetworkEnabled = false; // Flag for GPS statusboolean canGetLocation = false; Location location;// Locationdouble latitude;// Latitudedouble longitude;// Longitude // The minimum distance to change Updates in metersprivate static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 1000;// 10 meters // The minimum time between updates in millisecondsprivate static final long MIN_TIME_BW_UPDATES = 1000 * 60 * 1;// 1 minute // Declaring a Location Manager protected LocationManager locationManager; Activity activity; public GPSTracker() { } public GPSTracker(Context context, Activity activity) { this.mContext = context; this.activity = activity; getLocation(); } public Location getLocation() { try { locationManager = (LocationManager) mContext.getSystemService(LOCATION_SERVICE); // Getting GPS statusisGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER); // Getting network statusisNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER); if (!isGPSEnabled && !isNetworkEnabled) { // No network provider is enabled} else { this.canGetLocation = true; if (isNetworkEnabled) { int requestPermissionsCode = 50; locationManager.requestLocationUpdates( LocationManager.NETWORK_PROVIDER, MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES, mLocationListener); Log.d("Network", "Network"); if (locationManager != null) { location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER); if (location != null) { latitude = location.getLatitude(); longitude = location.getLongitude(); } } } } // If GPS enabled, get latitude/longitude using GPS Servicesif (isGPSEnabled) { if (location == null) { if (ContextCompat.checkSelfPermission(activity,Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED &&ActivityCompat.checkSelfPermission(activity, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { ActivityCompat.requestPermissions(activity, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 50); } else { locationManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES, mLocationListener); Log.d("GPS Enabled", "GPS Enabled"); if (locationManager != null) { location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); if (location != null) { latitude = location.getLatitude(); longitude = location.getLongitude(); } } } } } } catch (Exception e) { e.printStackTrace(); } return location; } /** * Stop using GPS listener* Calling this function will stop using GPS in your app.*/public void stopUsingGPS() { } private final LocationListener mLocationListener = new LocationListener() { @Override public void onLocationChanged(final Location location) { if (location != null) { latitude = location.getLatitude(); longitude = location.getLongitude(); } } @Override public void onStatusChanged(String provider, int status, Bundle extras) { } @Override public void onProviderEnabled(String provider) { } @Override public void onProviderDisabled(String provider) { } }; /*** Function to get latitude*/public double getLatitude() { if (location != null) { latitude = location.getLatitude(); } // return latitudereturn latitude; } /** * Function to get longitude*/public double getLongitude() { if (location != null) { longitude = location.getLongitude(); } // return longitudereturn longitude; } /** * Function to check GPS/Wi-Fi enabled ** @return boolean */public boolean canGetLocation() { return this.canGetLocation; } /** * Function to show settings alert dialog.* On pressing the Settings button it will launch Settings Options. */public void showSettingsAlert() { AlertDialog.Builder alertDialog = new AlertDialog.Builder(mContext); // Setting Dialog TitlealertDialog.setTitle("GPS is settings"); // Setting Dialog MessagealertDialog.setMessage("GPS is not enabled. Do you want to go to settings menu?"); // On pressing the Settings button.alertDialog.setPositiveButton("Settings", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS); mContext.startActivity(intent); } }); // On pressing the cancel buttonalertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { dialog.cancel(); } }); // Showing Alert MessagealertDialog.show(); } @Override public IBinder onBind(Intent arg0) { return null; } }
Subscribe to:
Posts (Atom)