1)EmployeeAttendenceFragment.java
package com.guruji.fragments; import android.Manifest;import android.content.Intent;import android.content.pm.PackageManager;import android.databinding.DataBindingUtil;import android.graphics.drawable.GradientDrawable;import android.location.Address;import android.location.Geocoder;import android.os.Build;import android.os.Bundle;import android.os.Handler;import android.support.annotation.NonNull;import android.support.annotation.Nullable;import android.support.design.widget.BottomSheetBehavior;import android.support.v4.app.ActivityCompat;import android.support.v4.app.FragmentManager;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.Toast; import com.bumptech.glide.Glide;import com.bumptech.glide.load.engine.DiskCacheStrategy;import com.google.android.gms.maps.CameraUpdateFactory;import com.google.android.gms.maps.GoogleMap;import com.google.android.gms.maps.OnMapReadyCallback;import com.google.android.gms.maps.SupportMapFragment;import com.google.android.gms.maps.model.BitmapDescriptorFactory;import com.google.android.gms.maps.model.LatLng;import com.google.android.gms.maps.model.LatLngBounds;import com.google.android.gms.maps.model.Marker;import com.google.android.gms.maps.model.MarkerOptions;import com.google.android.gms.maps.model.Polyline;import com.google.android.gms.maps.model.PolylineOptions;import com.guruji.R;import com.guruji.activity.HomeActivity;import com.guruji.activity.NotificationActivity;import com.guruji.api.RestClient;import com.guruji.databinding.FragmentEmployeeAttendenceBinding;import com.guruji.model.LocationData;import com.guruji.model.Locations;import com.guruji.service.SendLocationToserver;import com.guruji.utility.Constants;import com.guruji.utility.CustomDialog;import com.guruji.utility.PrefHelper;import com.guruji.utility.ToastHelper;import com.guruji.utility.Utils; import java.util.Calendar;import java.util.Date;import java.util.HashMap;import java.util.List;import java.util.Locale;import java.util.Map;import java.util.Timer;import java.util.TimerTask; import okhttp3.MediaType;import okhttp3.RequestBody;import retrofit2.Call;import retrofit2.Response; public class EmployeeAttendenceFragment extends BaseFragment implements OnMapReadyCallback, View.OnClickListener, GoogleMap.OnMapLoadedCallback { FragmentEmployeeAttendenceBinding mBinding; private GoogleMap googleMap; double latitude = 0.0, longitude = 0.0; private String address_line = ""; private LatLngBounds.Builder builder; private Marker startMarker, endMarker; public BottomSheetBehavior sheetBehaviorStart; Locations location; private PolylineOptions polylineOptions; private Polyline polyline; Timer timer = new Timer(); String userName = "", userPhoto = ""; private int userId = 0; Handler handler = new Handler(); @Nullable @Override public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { super.onCreateView(inflater, container, savedInstanceState); mBinding = DataBindingUtil.inflate(inflater, R.layout.fragment_employee_attendence, container, false); View view = mBinding.getRoot(); //Set Sliding Menu enable homeActivity = (HomeActivity) getActivity(); homeActivity.setSlidingMenuEnable(true); homeActivity.setSelectedPositionInSlidingMenu(1); if (Utils.validateString(PrefHelper.getString(Constants.FIRST_NAME, "")) && Utils.validateString(PrefHelper.getString(Constants.LAST_NAME, ""))) { userName = PrefHelper.getString(Constants.FIRST_NAME, "") + " " + PrefHelper.getString(Constants.LAST_NAME, ""); } else { userName = PrefHelper.getString(Constants.FIRST_NAME, ""); } userId = PrefHelper.getInt(Constants.USER_ID, 0); userPhoto = PrefHelper.getString(Constants.USER_PHOTO, ""); mBinding.toolbar.toolbarMenu.setOnClickListener(this); mBinding.btmsStart.imgCancel.setOnClickListener(this); mBinding.btmsStart.viewOpenClose.setOnClickListener(this); sheetBehaviorStart = BottomSheetBehavior.from(mBinding.btmsStart.lytStart); sheetBehaviorStart.setState(BottomSheetBehavior.STATE_EXPANDED); mBinding.btmsStart.lytStartShift.setOnClickListener(this); mBinding.btmsStart.txtUserName.setText(userName); location = new Locations(); Glide.with(this) .load(userPhoto) .diskCacheStrategy(DiskCacheStrategy.SOURCE) .fitCenter() .override(80, 80) .error(R.mipmap.icn_default_profile_emp) .into(mBinding.btmsStart.imgProfile); toolbar(); initMap(); setDate(); startTimer(); handler.postDelayed(new Runnable() { @Override public void run() { apiCallGetStatus(); } }, 2000); return view; } private void toolbar() { mBinding.toolbar.toolbarTitle.setText(getActivity().getResources().getString(R.string.title_employee_attendence)); mBinding.toolbar.imgNotification.setVisibility(View.VISIBLE); mBinding.toolbar.imgNotification.setOnClickListener(this); GradientDrawable gradientDrawable = Utils.getInstance().createGradientDrawable("#009425", "#00b800"); mBinding.lytToolbar.setBackground(gradientDrawable); } public void setDate() { Date date = Calendar.getInstance().getTime(); mBinding.btmsStart.edtStartDate.setText(Utils.getFormattedDate(date, "dd MMMM, yyyy") + " | " + Utils.getFormattedDate(date, "hh:mm a").toLowerCase()); } public void startTimer() { if (timer == null) timer = new Timer(); timer.schedule(new TimerTask() { public void run() { setDate(); } }, 0, 1000 * 60 * 1); } private void initMap() { try { new Handler().postDelayed(new Runnable() { @Override public void run() { if (isAdded()) { FragmentManager fm = getChildFragmentManager(); SupportMapFragment mapFragment = SupportMapFragment.newInstance(); fm.beginTransaction().replace(R.id.mapContainer, mapFragment).commit(); mapFragment.getMapAsync(EmployeeAttendenceFragment.this); } } }, 400); } catch (Exception e) { e.printStackTrace(); } } @Override public void onMapReady(GoogleMap googleMap) { this.googleMap = googleMap; googleMap.setOnMapLoadedCallback(this); latitude = PrefHelper.getDouble(Constants.LATITUDE, 0); longitude = PrefHelper.getDouble(Constants.LONGITUDE, 0); // setStartMarkerOnMap(latitude, longitude); updateLocation(); if (ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { return; } googleMap.setMyLocationEnabled(true); googleMap.getUiSettings().setMyLocationButtonEnabled(true); googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(latitude, longitude), 12.0f)); } @Override public void onMapLoaded() { if (googleMap == null || builder == null) return; latitude = PrefHelper.getDouble(Constants.LATITUDE, 0); longitude = PrefHelper.getDouble(Constants.LONGITUDE, 0); googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(latitude, longitude), 12.0f)); } public void getAddressDetail(Double latitude, Double longitude) { if (!Utils.validateString(String.valueOf(latitude)) && !Utils.validateString(String.valueOf(longitude))) return; Geocoder gcd = new Geocoder(getActivity().getApplicationContext(), Locale.ENGLISH); try { List<Address> addresses = gcd.getFromLocation(latitude, longitude, 1); if (addresses.size() == 0) return; address_line = addresses.get(0).getAddressLine(0); mBinding.btmsStart.txtStartAddress.setText(address_line); } catch (Exception e) { e.printStackTrace(); } } private void updateLocation() { latitude = PrefHelper.getDouble(Constants.LATITUDE, 0); longitude = PrefHelper.getDouble(Constants.LONGITUDE, 0); getAddressDetail(latitude, longitude); } private void setStartMarkerOnMap(Double latitude, Double longitude) { try { builder = new LatLngBounds.Builder(); if (startMarker != null) { if (endMarker == null) { MarkerOptions markerOptions = new MarkerOptions(); markerOptions.position(new LatLng(latitude, longitude)) .icon(BitmapDescriptorFactory.fromResource(R.mipmap.icn_end_point)) .anchor(0.5f, 1); endMarker = googleMap.addMarker(markerOptions); } else { endMarker.setPosition(new LatLng(latitude, longitude)); } builder.include(endMarker.getPosition()); location.setEndLat(latitude); location.setEndLng(longitude); } else { if (startMarker == null) { MarkerOptions markerOptions = new MarkerOptions(); markerOptions.position(new LatLng(latitude, longitude)) .icon(BitmapDescriptorFactory.fromResource(R.mipmap.icn_start_point)) .anchor(0.5f, 1); startMarker = googleMap.addMarker(markerOptions); } else { startMarker.setPosition(new LatLng(latitude, longitude)); builder.include(startMarker.getPosition()); } location.setStartLat(latitude); location.setStartLng(longitude); } googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(latitude, longitude), 12.0f));// googleMap.moveCamera(CameraUpdateFactory.newLatLngBounds(builder.build(), 20)); } catch (Exception e) { e.printStackTrace(); } } private void apiCallGetStatus() { if (Utils.getInstance().checkInternetConnection(getActivity())) { mBinding.lytLoader.setVisibility(View.VISIBLE); mBinding.avi.show(); Date date = Calendar.getInstance().getTime(); String startDate = Utils.getFormattedDate(date, "yyyy-MM-dd"); Call<LocationData> objectCall = RestClient.getApiClient().getAttendenceStatus(startDate, userId, 0); homeActivity.restClient.makeApiRequest(getActivity(), objectCall, this, getActivity().getResources().getInteger(R.integer.reqCodeGetStatus), true); } else { CustomDialog.getInstance().showMessageDialog(getActivity(), getActivity().getString(R.string.msgInternetConnectionNotAvailable)); } } private void apiCallStartAt() { if (Utils.getInstance().checkInternetConnection(getActivity())) { latitude = PrefHelper.getDouble(Constants.LATITUDE, 0); longitude = PrefHelper.getDouble(Constants.LONGITUDE, 0); setStartMarkerOnMap(latitude, longitude); Date date = Calendar.getInstance().getTime(); location.setStartAt(date); mBinding.lytLoader.setVisibility(View.VISIBLE); mBinding.avi.show(); Call<LocationData> objectCall = RestClient.getApiClient().startAtUserAttendence(String.valueOf(latitude), String.valueOf(longitude), mBinding.btmsStart.txtStartAddress.getText().toString().trim()); homeActivity.restClient.makeApiRequest(getActivity(), objectCall, this, this.getResources().getInteger(R.integer.reqCodeStartAt), true); } else { CustomDialog.getInstance().showMessageDialog(getActivity(), getActivity().getString(R.string.msgInternetConnectionNotAvailable)); } } private void apiCallEndAt() { updateLocation(); if (Utils.getInstance().checkInternetConnection(getActivity())) { latitude = PrefHelper.getDouble(Constants.LATITUDE, 0.0); longitude = PrefHelper.getDouble(Constants.LONGITUDE, 0.0); setStartMarkerOnMap(latitude, longitude); mBinding.lytLoader.setVisibility(View.VISIBLE); mBinding.avi.show(); Map<String, RequestBody> data = new HashMap<>(); RequestBody endLatitude = RequestBody.create(MediaType.parse("text/plain"), String.valueOf(latitude)); RequestBody endLongitude = RequestBody.create(MediaType.parse("text/plain"), String.valueOf(longitude)); RequestBody address = RequestBody.create(MediaType.parse("text/plain"), address_line); data.put("end_lat", endLatitude); data.put("end_lng", endLongitude); data.put("end_address", address); Call<LocationData> objectCall = RestClient.getApiClient().endAtUserAttendence(location.getId(), data); homeActivity.restClient.makeApiRequest(getActivity(), objectCall, this, this.getResources().getInteger(R.integer.reqCodeEndAt), true); } else { CustomDialog.getInstance().showMessageDialog(getActivity(), getActivity().getString(R.string.msgInternetConnectionNotAvailable)); } } @Override public void onApiResponse(Call<Object> call, Response<Object> response, int reqCode) { if (reqCode == homeActivity.getResources().getInteger(R.integer.reqCodeGetStatus)) { mBinding.lytLoader.setVisibility(View.GONE); mBinding.avi.hide(); if (response != null) { LocationData locationData = (LocationData) response.body(); if (locationData != null) { if (!locationData.isError()) { if (locationData.getData() != null) { location.setId(locationData.getData().getId()); location.setStartAt(locationData.getData().getStartAt()); location.setStartLat(locationData.getData().getStartLat()); location.setStartLng(locationData.getData().getStartLng()); location.setStartAddress(locationData.getData().getStartAddress()); if (locationData.getData().getEndAt() == null) { mBinding.btmsStart.txtStartShift.setText(getActivity().getResources().getString(R.string.end_shift)); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { getActivity().startForegroundService(new Intent(getActivity(), SendLocationToserver.class)); } else { getActivity().startService(new Intent(getActivity(), SendLocationToserver.class)); } } else { mBinding.btmsStart.txtStartShift.setText(getActivity().getResources().getString(R.string.start_shift)); } }else { mBinding.btmsStart.txtStartShift.setText(getActivity().getResources().getString(R.string.start_shift)); } } else { if (Utils.validateString(locationData.getMessage())) { Toast.makeText(getActivity(), locationData.getMessage(), Toast.LENGTH_LONG).show(); } } } } } else if (reqCode == getActivity().getResources().getInteger(R.integer.reqCodeStartAt)) { mBinding.lytLoader.setVisibility(View.GONE); mBinding.avi.hide(); if (response != null) { LocationData locationData = (LocationData) response.body(); if (!locationData.isError()) { location.setId(locationData.getData().getId()); location.setStartAt(locationData.getData().getStartAt()); location.setStartLat(locationData.getData().getStartLat()); location.setStartLng(locationData.getData().getStartLng()); location.setStartAddress(locationData.getData().getStartAddress()); CustomDialog.getInstance().showMessageDialog(getActivity(), locationData.getMessage()); if (mBinding.btmsStart.txtStartShift.getText().toString().equalsIgnoreCase(getActivity().getResources().getString(R.string.start_shift))) { mBinding.btmsStart.txtStartShift.setText(getActivity().getResources().getString(R.string.end_shift)); } if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { getActivity().startForegroundService(new Intent(getActivity(), SendLocationToserver.class)); } else { getActivity().startService(new Intent(getActivity(), SendLocationToserver.class)); } } else { if (Utils.validateString(locationData.getMessage())) { Toast.makeText(getActivity(), locationData.getMessage(), Toast.LENGTH_LONG).show(); } } } } else if (reqCode == getActivity().getResources().getInteger(R.integer.reqCodeEndAt)) { mBinding.lytLoader.setVisibility(View.GONE); mBinding.avi.hide(); if (response != null) { LocationData locationData = (LocationData) response.body(); if (!locationData.isError()) { if (locationData.getData() != null) { setStartMarkerOnMap(locationData.getData().getEndLat(), locationData.getData().getEndLng()); } Intent intent = new Intent(getActivity(), SendLocationToserver.class); getActivity().stopService(intent); mBinding.btmsStart.txtStartShift.setText(getActivity().getResources().getString(R.string.start_shift)); // homeActivity.pushFragments(new HomeFragment(), false, false, true); } } } } @Override public void onApiError(Call<Object> call, Object object, int reqCode) { mBinding.lytLoader.setVisibility(View.GONE); mBinding.avi.hide(); ToastHelper.getInstance().showToast(getActivity(), object.toString()); } public boolean fieldValidation() { boolean flag = true; if (latitude == 0.0 || longitude == 0.0) { flag = false; Toast.makeText(getActivity(), "Please Fetch Current Location", Toast.LENGTH_LONG).show(); } else if (address_line.isEmpty()) { flag = false; Toast.makeText(getActivity(), "Please Fetch Current Location", Toast.LENGTH_LONG).show(); } return flag; } @Override public void onClick(View v) { if (v == mBinding.toolbar.toolbarMenu) { homeActivity.toggleSlidingMenu(); } else if (v == mBinding.toolbar.imgNotification) { Intent mIntent = new Intent(getActivity(), NotificationActivity.class); startActivity(mIntent); } else if (v == mBinding.btmsStart.imgCancel) { sheetBehaviorStart.setState(BottomSheetBehavior.STATE_COLLAPSED); } else if (v == mBinding.btmsStart.viewOpenClose) { if (sheetBehaviorStart.getState() == BottomSheetBehavior.STATE_COLLAPSED) { sheetBehaviorStart.setState(BottomSheetBehavior.STATE_EXPANDED); } else { sheetBehaviorStart.setState(BottomSheetBehavior.STATE_COLLAPSED); } } else if (v == mBinding.btmsStart.lytStartShift) { if (mBinding.btmsStart.txtStartShift.getText().toString().equalsIgnoreCase(getActivity().getResources().getString(R.string.start_shift))) { if (fieldValidation()) { apiCallStartAt(); } } else { if (fieldValidation()) { apiCallEndAt(); } } } } @Override public void onPause() { super.onPause(); try { if (timer != null) timer.cancel(); } catch (Exception e) { } } @Override public void onDestroy() { super.onDestroy(); } }
2)fragment_employee_attendance.xml
<?xml version="1.0" encoding="utf-8"?><layout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto"> <android.support.design.widget.CoordinatorLayout android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/divider_color" android:focusableInTouchMode="true"> <android.support.constraint.ConstraintLayout android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginBottom="@dimen/_36sdp"> <RelativeLayout android:id="@+id/lytLoader" android:layout_width="wrap_content" android:layout_height="wrap_content" android:visibility="gone" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent"> <com.wang.avi.AVLoadingIndicatorView android:id="@+id/avi" style="@style/AVLoadingIndicatorView" android:layout_width="wrap_content" android:layout_height="wrap_content" app:indicatorColor="@color/colorPrimary" app:indicatorName="BallPulseIndicator" /> </RelativeLayout> <LinearLayout android:id="@+id/lyt_toolbar" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <include android:id="@+id/toolbar" layout="@layout/toolbar_layout" android:layout_width="match_parent" android:layout_height="@dimen/_50sdp" /> </LinearLayout> <FrameLayout android:id="@+id/mapContainer" android:layout_width="match_parent" android:layout_height="0dp" android:layout_below="@+id/lyt_toolbar" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintTop_toBottomOf="@+id/lyt_toolbar" /> </android.support.constraint.ConstraintLayout> <include android:id="@+id/btms_start" layout="@layout/bottomsheet_location_start" /> <FrameLayout android:id="@+id/frameLayout" android:layout_width="match_parent" android:layout_height="match_parent" app:behavior_hideable="true" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintTop_toBottomOf="@+id/act_new_location_toolbar" /> </android.support.design.widget.CoordinatorLayout></layout>
3)bottomsheet_location_start.xml
<?xml version="1.0" encoding="utf-8"?><layout xmlns: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.support.constraint.ConstraintLayout android:id="@+id/lyt_start" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/white" android:fitsSystemWindows="true" android:focusableInTouchMode="true" app:behavior_hideable="false" app:behavior_peekHeight="@dimen/_90sdp" app:layout_behavior="android.support.design.widget.BottomSheetBehavior"> <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <RelativeLayout android:id="@+id/bottom_sheet_view_open_close" android:layout_width="match_parent" android:layout_height="wrap_content"> <LinearLayout android:id="@+id/view_open_close" android:layout_width="@dimen/_44sdp" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:padding="@dimen/_10sdp" android:orientation="vertical"> <View android:layout_width="@dimen/_35sdp" android:layout_height="@dimen/_4sdp" android:layout_gravity="center" android:background="@color/gray" /> </LinearLayout> <ImageView android:id="@+id/img_cancel" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentEnd="true" android:layout_below="@+id/view_open_close" android:contentDescription="@string/app_name" android:paddingRight="@dimen/_12sdp" android:paddingTop="@dimen/_5sdp" android:paddingLeft="@dimen/_6sdp" android:src="@mipmap/icn_cancel_dragable" /> </RelativeLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@id/bottom_sheet_view_open_close" android:layout_marginLeft="@dimen/_20sdp" android:layout_marginRight="@dimen/_20sdp" android:orientation="vertical"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <com.guruji.utility.CircularImageView android:id="@+id/img_profile" android:layout_width="@dimen/_40sdp" android:layout_height="@dimen/_40sdp" android:layout_marginEnd="@dimen/_3sdp" android:contentDescription="@string/app_name" android:padding="@dimen/_3sdp" android:src="@mipmap/icn_default_profile_emp" app:shadow="false" app:border="false"/> <TextView android:id="@+id/txt_user_name" fontPath="fonts/Roboto-Bold.ttf" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:gravity="center" android:textColor="@color/gray_dark" android:textSize="@dimen/_15ssp" android:textStyle="bold" /> </LinearLayout> <TextView style="@style/txt_gray_title" android:layout_marginStart="@dimen/_14sdp" android:layout_marginTop="@dimen/_6sdp" android:drawablePadding="@dimen/_10sdp" android:drawableStart="@mipmap/icn_date_time" android:text="@string/date_time" /> <EditText android:id="@+id/edt_start_date" style="@style/edtRegister" android:layout_marginStart="@dimen/_34sdp" android:layout_marginTop="@dimen/_3sdp" android:background="@null" android:focusable="false" android:hint="@string/enter_date_time" android:inputType="datetime" android:textColor="@color/gray_dark" /> <View android:layout_width="match_parent" android:layout_height="@dimen/dp05" android:layout_margin="@dimen/_10sdp" android:background="@color/divider_color" /> <TextView style="@style/txt_gray_title" android:layout_marginStart="@dimen/_14sdp" android:drawablePadding="@dimen/_10sdp" android:drawableStart="@mipmap/icn_location" android:text="@string/current_location" android:textAllCaps="false" /> <EditText android:id="@+id/txtStartAddress" style="@style/edtRegister" android:layout_marginBottom="@dimen/_14sdp" android:layout_marginStart="@dimen/_34sdp" android:layout_marginTop="@dimen/_3sdp" android:background="@null" android:focusable="false" android:inputType="textMultiLine" android:maxLines="3" android:singleLine="false" android:text="" android:textColor="@color/gray_dark" /> <LinearLayout android:id="@+id/lyt_start_shift" style="@style/bottom_button_lyt_style" android:layout_marginBottom="@dimen/_14sdp" android:layout_marginLeft="@dimen/_50sdp" android:layout_marginRight="@dimen/_50sdp" android:background="@drawable/selecter_btn" android:orientation="horizontal"> <TextView android:id="@+id/txt_start_shift" style="@style/bottom_button_text_style" android:text=""> </TextView> </LinearLayout> </LinearLayout> <RelativeLayout android:id="@+id/lytLoader" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:visibility="gone"> <com.wang.avi.AVLoadingIndicatorView android:id="@+id/avi" style="@style/AVLoadingIndicatorView" android:layout_width="wrap_content" android:layout_height="wrap_content" app:indicatorColor="@color/colorPrimary" app:indicatorName="BallPulseIndicator" /> </RelativeLayout> </RelativeLayout> </android.support.constraint.ConstraintLayout></layout>3) BottomNavigationViewHelper.java
==================================
package com.piitch.widgets; import android.annotation.SuppressLint; import android.support.design.internal.BottomNavigationItemView; import android.support.design.internal.BottomNavigationMenuView; import android.support.design.widget.BottomNavigationView; import android.util.Log; import java.lang.reflect.Field; public class BottomNavigationViewHelper { @SuppressLint("RestrictedApi") public static void disableShiftMode(BottomNavigationView view) { BottomNavigationMenuView menuView = (BottomNavigationMenuView) view.getChildAt(0); try { Field shiftingMode = menuView.getClass().getDeclaredField("mShiftingMode"); shiftingMode.setAccessible(true); shiftingMode.setBoolean(menuView, false); shiftingMode.setAccessible(false); if(menuView.getChildCount()<6) { for (int i = 0; i < menuView.getChildCount(); i++) { BottomNavigationItemView item = (BottomNavigationItemView) menuView.getChildAt(i); item.setShiftingMode(false); item.setChecked(item.getItemData().isChecked()); } } } catch (NoSuchFieldException e) { Log.e("BNVHelper", "Unable to get shift mode field", e); } catch (IllegalAccessException e) { Log.e("BNVHelper", "Unable to change value of shift mode", e); } } @SuppressLint("RestrictedApi") public static void removeShiftMode(BottomNavigationView view) { BottomNavigationMenuView menuView = (BottomNavigationMenuView) view.getChildAt(0); try { Field shiftingMode = menuView.getClass().getDeclaredField("mShiftingMode"); shiftingMode.setAccessible(true); shiftingMode.setBoolean(menuView, false); shiftingMode.setAccessible(false); for (int i = 0; i < menuView.getChildCount(); i++) { BottomNavigationItemView item = (BottomNavigationItemView) menuView.getChildAt(i); item.setShiftingMode(false); item.setChecked(item.getItemData().isChecked()); } } catch (NoSuchFieldException e) { Log.e("BottomNav", "Unable to get shift mode field", e); } catch (IllegalAccessException e) { Log.e("BottomNav", "Unable to change value of shift mode", e); } } }
No comments:
Post a Comment