UAS MOBILE PROGRAMING

APLIKASI DAFTAR HARGA SEWA MOBIL 


DESKRIPSI :

Aplikasi android yang saya buat untuk melihat daftar harga sewa mobil perhari dengan jenis mobil dan merek mobil.


  • ActivityLogin
Source Code
package com.rental_apps.android.rental_apps;

import android.app.ProgressDialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.Typeface;
import android.support.design.widget.CoordinatorLayout;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.pixplicity.easyprefs.library.Prefs;
import com.rental_apps.android.rental_apps.SPreferenced.SPref;
import com.rental_apps.android.rental_apps.admin.AdminMain;
import com.rental_apps.android.rental_apps.api.client;
import com.rental_apps.android.rental_apps.model.model_user.DataUser;
import com.rental_apps.android.rental_apps.model.model_user.ResponseLogin;
import com.rental_apps.android.rental_apps.myinterface.InitComponent;
import com.rental_apps.android.rental_apps.user.SplashActivity;
import com.rental_apps.android.rental_apps.user.UserMain;
import com.rental_apps.android.rental_apps.utils.move;
import com.rental_apps.android.rental_apps.utils.validate;

import cn.pedant.SweetAlert.SweetAlertDialog;
import customfonts.MyEditText;
import customfonts.MyTextView;
import es.dmoral.toasty.Toasty;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class ActivityLogin extends AppCompatActivity implements InitComponent, View.OnClickListener {

    //declare componenr
    private MyEditText et_username;
    private MyEditText et_password;
    private MyTextView btn_login;
    private MyTextView txt_register;
    private TextView logofont;
    private CoordinatorLayout coordinatorlayout;

    //declare context
    private Context mContext;

    //declate variable
    private DataUser userData;

    //declare sweet alert
 //   private SweetAlertDialog pDialog;
    private ProgressDialog pDialog;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        mContext=this;
        startInit();
    }

    @Override
    public void startInit() {
        if (Prefs.getInt(SPref.getGroupUser(),0)==1){
            move.moveActivity(mContext,UserMain.class);
            finish();
        }
        if (Prefs.getInt(SPref.getGroupUser(),0)==2){
            move.moveActivity(mContext,UserMain.class);
            finish();
        }
        initToolbar();
        initUI();
        initValue();
        initEvent();
    }

    @Override
    public void initToolbar() {
        getSupportActionBar().hide();
    }

    @Override
    public void initUI() {
        et_username=(MyEditText)findViewById(R.id.et_username);
        et_password=(MyEditText)findViewById(R.id.et_password);
        btn_login=(MyTextView)findViewById(R.id.btn_login);
        txt_register=(MyTextView) findViewById(R.id.txt_register);
        logofont=(TextView)findViewById(R.id.logofont);
        Typeface custom_fonts = Typeface.createFromAsset(getAssets(), "fonts/ArgonPERSONAL-Regular.otf");
        logofont.setTypeface(custom_fonts);
    }

    @Override
    public void initValue() {

    }

    @Override
    public void initEvent() {
        btn_login.setOnClickListener(this);
        txt_register.setOnClickListener(this);
    }


    @Override
    public void onClick(View view) {
        switch (view.getId()){

            case R.id.btn_login:
                if (validate_login())
                    login();
                break;

            case R.id.txt_register:
                move.moveActivity(mContext,ActivityRegister.class);
                break;
        }
    }

    public boolean validate_login(){
        return (!validate.cek(et_username)&&!validate.cek(et_password)) ? true : false;
    }

    public void login(){
        pDialog = new ProgressDialog(this);
      //  pDialog.getProgressHelper().setBarColor(Color.parseColor("#A5DC86"));
        pDialog.setMessage("Loading");
        pDialog.setCancelable(false);
       // pDialog.setIndeterminate(false);
        pDialog.show();

        Call<ResponseLogin> user=client.getApi().auth(et_username.getText().toString(),et_password.getText().toString());
        user.enqueue(new Callback<ResponseLogin>() {
            @Override
            public void onResponse(Call<ResponseLogin> call, Response<ResponseLogin> response) {
                pDialog.hide();
                if (response.isSuccessful()){
                    if (response.body().getStatus()){
                        userData=response.body().getData();
                        Toasty.success(mContext,"login berhasil",Toast.LENGTH_LONG).show();
                        Log.d("data user",userData.toString());
                        setPreference(userData);
                        if (userData.getGroup_user().equals(1))
                            move.moveActivity(mContext,SplashActivity.class);
                        else
                            move.moveActivity(mContext,SplashActivity.class);
                        finish();
                    }else{
                        Toasty.error(mContext,"Username dan password salah",Toast.LENGTH_LONG).show();
                    }
                }else{
                    Toasty.error(mContext,"Username dan password salah",Toast.LENGTH_LONG).show();
                }
            }

            @Override
            public void onFailure(Call<ResponseLogin> call, Throwable t) {
                pDialog.hide();
//                new ProgressDialog(mContext)
//                        .setTitle("Oops...")
//                        .d("Koneksi bermasalah!")
//                        .show();
//                pDialog = new ProgressDialog(ActivityLogin.this);
//                //  pDialog.getProgressHelper().setBarColor(Color.parseColor("#A5DC86"));
//                pDialog.setMessage("Tidak ada koneksi");
//                pDialog.show();
                Toasty.success(mContext,"Koneksi Tidak ada",Toast.LENGTH_LONG).show();

                if (pDialog.isShowing())
                    pDialog.dismiss();
            }
        });
    }

    private void setPreference(DataUser du){
        Prefs.putInt(SPref.getIdUser(),du.getId_user());
        Prefs.putString(SPref.getUSERNAME(),du.getUsername());
        Prefs.putString(SPref.getNAME(),du.getName());
        Prefs.putString(SPref.getEMAIL(),du.getEmail());
        Prefs.putString(SPref.getNoTelp(),du.getNo_telp());
        Prefs.putString(SPref.getJenisKelamin(),du.getJenis_kelamin().toString());
        Prefs.putString(SPref.getPHOTO(),du.getPhoto());
        Prefs.putString(SPref.getLastUpdate(),du.getLast_update().toString());
        Prefs.putString(SPref.getALAMAT(),du.getAlamat());
        Prefs.putInt(SPref.getGroupUser(),du.getGroup_user());
        Prefs.putString(SPref.getPASSWORD(),du.getPassword().toString());
    }
}




  • activity_login.xml 
Layout














XML Text


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/login">
    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#000"
        android:alpha="0.5"
        />

    <ImageView
        android:id="@+id/logo"
        android:layout_width="150dp"
        android:layout_height="150dp"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="50dp"
        android:background="#00A85D5D"
        android:scaleType="centerInside"
        android:src="@drawable/green"
        app:srcCompat="@drawable/blue1"
        tools:srcCompat="@drawable/green" />

    <TextView
        android:id="@+id/logofont"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/logo"
        android:layout_marginTop="70dp"
        android:gravity="center"
        android:text="akbar RentCar"
        android:textColor="#fff"
        android:textSize="30dp" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_alignParentBottom="true">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:layout_marginLeft="30dp"
            android:layout_marginRight="30dp"
            android:background="@drawable/roundwhite">

            <ImageView
                android:layout_weight="1"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:src="@drawable/username"
                android:layout_gravity="center"
                android:paddingLeft="10dp"/>
            <customfonts.MyEditText
                android:id="@+id/et_username"
                android:layout_weight="1"
                android:layout_width="match_parent"
                android:layout_height="50dp"
                android:background="#00000000"
                android:paddingLeft="10dp"
                android:hint="Username"
                android:textColor="#fff"
                android:textColorHint="#fff"
                android:drawablePadding="10dp"
                android:textSize="13dp"
                />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:background="@drawable/roundwhite"
            android:layout_marginLeft="30dp"
            android:layout_marginRight="30dp"
            android:layout_marginTop="10dp">

            <ImageView
                android:id="@+id/pass"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:src="@drawable/password"
                android:layout_weight="1"
                android:layout_gravity="center"
                android:paddingLeft="10dp"
                />

            <customfonts.MyEditText
                android:id="@+id/et_password"
                android:layout_width="match_parent"
                android:layout_height="50dp"
                android:background="#00000000"
                android:paddingLeft="10dp"
                android:hint="Password"
                android:inputType="textPassword"
                android:textColorHint="#fff"
                android:textColor="#fff"
                android:textSize="13dp"
                android:layout_weight="1"
                />
        </LinearLayout>
        <customfonts.MyTextView
            android:id="@+id/btn_login"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:background="@drawable/round"
            android:text="Login"
            android:textStyle="bold"
            android:gravity="center"
            android:textColor="#fff"
            android:layout_above="@+id/bottom"
            android:layout_marginLeft="30dp"
            android:layout_marginRight="30dp"
            android:layout_marginBottom="5dp"
            android:layout_marginTop="10dp"
            />
        <LinearLayout
            android:id="@+id/bottom"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:orientation="horizontal"
            android:padding="16dp">

            <customfonts.MyTextView
                android:id="@+id/txt_register"
                android:layout_weight="1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Create Account"
                android:textColor="#FFF"/>
            <customfonts.MyTextView
                android:layout_weight="1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Need Help ?"
                android:textColor="#FFF"
                android:gravity="end"/>
        </LinearLayout>


    </LinearLayout>
</RelativeLayout>




  • ActivityRegister
Source Code


package com.rental_apps.android.rental_apps;

import android.app.ProgressDialog;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.support.design.widget.CoordinatorLayout;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.Toast;

import com.rental_apps.android.rental_apps.api.client;
import com.rental_apps.android.rental_apps.model.model_user.DataUser;
import com.rental_apps.android.rental_apps.model.model_user.ResponseRegister;
import com.rental_apps.android.rental_apps.myinterface.InitComponent;
import com.rental_apps.android.rental_apps.utils.validate;

import cn.pedant.SweetAlert.SweetAlertDialog;
import es.dmoral.toasty.Toasty;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by Ujang Wahyu on 04/01/2018.
 */
public class ActivityRegister extends AppCompatActivity implements InitComponent, View.OnClickListener{

    //declare component
    private EditText etNama;
    private EditText etNik;
    private EditText etUsername;
    private EditText etNumber;
    private EditText etAlamat;
    private EditText etEmail;
    private EditText etPassword;
    private EditText etConfirmPassword;
    private Character JK;
    private RadioButton rbl;
    private RadioButton rbp;
    private Button btnRegister;
    private CoordinatorLayout coordinatorLayout;

    //declare context
    private Context mContext;

    //declare variable
    private DataUser userData;

    //declare sweet alert
   // private SweetAlertDialog pDialog;
    private ProgressDialog pDialog;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        mContext=this;
        startInit();
    }

    @Override
    public void startInit() {
        initToolbar();
        initUI();
        initValue();
        initEvent();
    }

    @Override
    public void initToolbar() {
        getSupportActionBar().hide();
    }

    @Override
    public void initUI() {
        etNama=(EditText)findViewById(R.id.et_nama);
        etNik=(EditText)findViewById(R.id.et_nik);
        etEmail=(EditText)findViewById(R.id.et_email);
        etNumber=(EditText)findViewById(R.id.et_no_telp);
        etAlamat=(EditText)findViewById(R.id.et_alamat);
        etUsername=(EditText)findViewById(R.id.et_username);
        etPassword=(EditText)findViewById(R.id.et_password);
        etConfirmPassword=(EditText)findViewById(R.id.et_confirm_password);
        rbl=(RadioButton)findViewById(R.id.jkl);
        rbp=(RadioButton)findViewById(R.id.jkp);
        btnRegister=(Button)findViewById(R.id.btn_register);

    }

    @Override
    public void initValue() {

    }

    @Override
    public void initEvent() {
        btnRegister.setOnClickListener(this);
        rbl.setOnClickListener(this);
        rbp.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.btn_register:
                if(validasi())
                    register();
                break;
            case R.id.jkl:
                JK='L';
                rbp.setChecked(false);
                break;
            case R.id.jkp:
                JK='P';
                rbl.setChecked(false);
                break;
        }
    }

    private void register(){
        pDialog = new ProgressDialog(this);
       // pDialog.getProgressHelper().setBarColor(Color.parseColor("#A5DC86"));
        pDialog.setMessage("Loading");
        pDialog.setCancelable(false);
        pDialog.show();

        Call<ResponseRegister> register;
        register = client.getApi().userRegister(etNama.getText().toString(),
                                                etNik.getText().toString(),
                                                etUsername.getText().toString(),
                                                etEmail.getText().toString(),
                                                etNumber.getText().toString(),
                                                JK,
                                                etAlamat.getText().toString(),
                                                etPassword.getText().toString(),
                                                1,2);

        register.enqueue(new Callback<ResponseRegister>() {

            @Override
            public void onResponse(Call<ResponseRegister> call, Response<ResponseRegister> response) {
                pDialog.hide();
                if (response.isSuccessful()){
                    if (response.body().getStatus()) {
                        Toasty.success(mContext,"Berhasil Dibuat", Toast.LENGTH_LONG).show();
                    }else {
                        Toasty.success(mContext,"Gagal dibuat", Toast.LENGTH_LONG).show();
                    }
                }else{
                    Toasty.error(mContext,"Gagal dibuat", Toast.LENGTH_LONG).show();
                }
            }

            @Override
            public void onFailure(Call<ResponseRegister> call, Throwable t) {
                pDialog.hide();
                Toasty.success(mContext,"Koneksi bermasalah", Toast.LENGTH_LONG).show();
            }
        });
    }

    private Boolean validasi(){
        if (!validate.cek(etNama)
                &&!validate.cek(etNik)
                &&!validate.cek(etUsername)
                &&!validate.cek(etEmail)
         &&!validate.cek(etNumber)
         &&!validate.cek(etAlamat)
         &&!validate.cek(etPassword)
         &&!validate.cek(etConfirmPassword)) {
            if (validate.cekPassword(etConfirmPassword,etPassword.getText().toString(),etConfirmPassword.getText().toString())){
                return false;
            }else{
                return true;
            }
        } else{ return false; }
    }
}



  • activity_register.xml
Layout


XML Text


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/login"

    >
    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scrollbars="none">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#000"
        android:alpha="0.5">
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="25dp">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            >

            <ImageView
                android:layout_width="150dp"
                android:layout_height="150dp"
                android:layout_gravity="center_horizontal"
                android:layout_marginBottom="20dp"
                android:background="@drawable/blue2"
                app:srcCompat="@drawable/bg_drawer"
                tools:srcCompat="@drawable/bg_drawer" />

            <EditText
                android:id="@+id/et_nama"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="Nama"
                android:textColor="@color/white"
                android:textColorHint="@color/white"
                android:background="@drawable/roundwhite"
                android:maxLength="16"
                android:padding="15dp"
                android:layout_marginBottom="5dp"
                />
            <EditText
                android:id="@+id/et_nik"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="NIK"
                android:textColor="@color/white"
                android:textColorHint="@color/white"
                android:background="@drawable/roundwhite"
                android:padding="15dp"
                android:inputType="number"
                android:maxLength="16"
                android:layout_marginBottom="5dp"
                />
            <EditText
                android:id="@+id/et_username"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="Username"
                android:textColor="@color/white"
                android:textColorHint="@color/white"
                android:background="@drawable/roundwhite"
                android:padding="15dp"
                android:layout_marginBottom="5dp"
                />
            <EditText
                android:id="@+id/et_no_telp"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="No Telp"
                android:textColor="@color/white"
                android:textColorHint="@color/white"
                android:background="@drawable/roundwhite"
                android:padding="15dp"
                android:inputType="number"
                android:maxLength="12"
                android:layout_marginBottom="5dp"
                />
            <EditText
                android:id="@+id/et_alamat"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="Alamat"
                android:textColor="@color/white"
                android:textColorHint="@color/white"
                android:background="@drawable/roundwhite"
                android:padding="15dp"
                android:layout_marginBottom="5dp"
                />
            <EditText
                android:id="@+id/et_email"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="Email"
                android:textColor="@color/white"
                android:textColorHint="@color/white"
                android:background="@drawable/roundwhite"
                android:padding="15dp"
                android:layout_marginBottom="5dp"
                />
            <EditText
                android:id="@+id/et_password"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="Password"
                android:textColor="@color/white"
                android:textColorHint="@color/white"
                android:background="@drawable/roundwhite"
                android:padding="15dp"
                android:inputType="textPassword"
                android:layout_marginBottom="5dp"
                />
            <EditText
                android:id="@+id/et_confirm_password"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="Confirm Password"
                android:textColor="@color/white"
                android:textColorHint="@color/white"
                android:background="@drawable/roundwhite"
                android:padding="15dp"
                android:inputType="textPassword"
                android:layout_marginBottom="5dp"
                />
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:background="@drawable/roundwhite"
    android:layout_marginBottom="15sp"
    >
    <customfonts.MyTextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="17sp"
        android:text="Jenis Kelamin"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="10dp"
        android:textColor="@color/white"
        />
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        >
        <RadioButton
            android:id="@+id/jkl"
            android:paddingTop="5sp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Laki-laki"
            android:textColor="@color/white"
            android:layout_margin="10dp"
            />
        <RadioButton
            android:id="@+id/jkp"
            android:paddingTop="5sp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Perempuan"
            android:textColor="@color/white"
            android:layout_margin="10dp"
            />
    </LinearLayout>
</LinearLayout>

            <Button
                android:id="@+id/btn_register"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@drawable/round"
                android:text="@string/register"
                android:textColor="@color/white"
                />

        </LinearLayout>
    </RelativeLayout>
    </RelativeLayout>
    </ScrollView>

</RelativeLayout>



  • UserListCar
Source Code 


package com.rental_apps.android.rental_apps.user;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.LayerDrawable;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.mikepenz.itemanimators.SlideLeftAlphaAnimator;
import com.rental_apps.android.rental_apps.R;
import com.rental_apps.android.rental_apps.SPreferenced.SPref;
import com.rental_apps.android.rental_apps.adapter.CarsAdapter;
import com.rental_apps.android.rental_apps.adapter.CarsUserAdapter;
import com.rental_apps.android.rental_apps.adapter.Carts;
import com.rental_apps.android.rental_apps.admin.ActivityCreateMobil;
import com.rental_apps.android.rental_apps.api.client;
import com.rental_apps.android.rental_apps.helper.DrawableCounter;
import com.rental_apps.android.rental_apps.model.model_mobil.DataCars;
import com.rental_apps.android.rental_apps.model.model_mobil.ResponseCars;
import com.rental_apps.android.rental_apps.myinterface.InitComponent;
import com.rental_apps.android.rental_apps.utils.move;

import java.util.ArrayList;
import java.util.List;

import es.dmoral.toasty.Toasty;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by Ujang Wahyu on 04/01/2018.
 */


public class UserListCars extends Fragment implements InitComponent{

    //Declate Toolbar Tittle
    private static final String TEXT_FRAGMENT = "RENTCAR";

    //Declare Component View
    private TextView mTxtTitle;
    private View rootView;
    private RecyclerView recyclerCars;
    //Declate Activity Context
    Context mContext;

    //Declare Object Cars
    ResponseCars dataCars;
    List<DataCars> listCars=new ArrayList<>();

    Menu mnn;

    //Declare Adapter
    private CarsUserAdapter mAdapter;

    public static UserListCars newInstance(String text){
        UserListCars mFragment = new UserListCars();
        Bundle mBundle = new Bundle();
        mBundle.putString(TEXT_FRAGMENT, text);
        mFragment.setArguments(mBundle);
        return mFragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mContext=getActivity();
        // TODO Auto-generated method stub
        rootView = inflater.inflate(R.layout.fragment_admin_cars, container, false);
        startInit();
        return rootView;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onActivityCreated(savedInstanceState);
        setHasOptionsMenu(true);
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        // TODO Auto-generated method stub
        super.onCreateOptionsMenu(menu, inflater);
        inflater.inflate(R.menu.menu_user_icon, menu);
        setCart(menu);
    }

    public void setCart(Menu menu){
        MenuItem menuItem = menu.findItem(R.id.cart);
        LayerDrawable icon = (LayerDrawable) menuItem.getIcon();

        DrawableCounter badge;

        // Reuse drawable if possible
        Drawable reuse = icon.findDrawableByLayerId(R.id.ic_group_count);
        if (reuse != null && reuse instanceof DrawableCounter) {
            badge = (DrawableCounter) reuse;
        } else {
            badge = new DrawableCounter(mContext);
        }

        badge.setCount(""+Carts.getSize(SPref.getCARTS()));
        icon.mutate();
        icon.setDrawableByLayerId(R.id.ic_group_count, badge);
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.refresh:
                getCars();
                return true;
            case R.id.add:
                move.moveActivity(mContext,ActivityCreateMobil.class);
                return true;
            case R.id.cart:
                move.moveActivity(mContext,ActivityListTransaksi.class);
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }

    @Override
    public void startInit() {
        initToolbar();
        initUI();
        initValue();
        initEvent();
    }

    @Override
    public void initToolbar() {
        getActivity().setTitle(getArguments().getString(TEXT_FRAGMENT));
    }

    @Override
    public void initUI() {
        rootView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT ));
        recyclerCars = (RecyclerView)rootView.findViewById(R.id.rCarList);


    }

    @Override
    public void initValue() {
        prepareCars();
        getCars();
    }

    @Override
    public void initEvent() {

    }

    public void getCars(){
        final Call<ResponseCars> cars= client.getApi().dataMobil();
        cars.enqueue(new Callback<ResponseCars>() {
            @Override
            public void onResponse(Call<ResponseCars> call, Response<ResponseCars> response) {
                if (response.isSuccessful()) {
                    dataCars=response.body();
                    if (dataCars.getStatus()) {
                        listCars.clear();
                        listCars.addAll(dataCars.getData());
                        mAdapter.notifyDataSetChanged();
                    } else {
                        Toasty.error(mContext, "gagal", Toast.LENGTH_LONG).show();
                    }
                }else{
                    Toasty.error(mContext,"gagal",Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onFailure(Call<ResponseCars> call, Throwable t) {
                Toasty.error(mContext,t.getMessage(),Toast.LENGTH_LONG).show();
            }
        });
    }

    private void prepareCars(){
        mAdapter = new CarsUserAdapter(listCars);
        recyclerCars.setHasFixedSize(true);
        recyclerCars.setLayoutManager(new LinearLayoutManager(getActivity()));
        recyclerCars.setItemAnimator(new DefaultItemAnimator());
        recyclerCars.setAdapter(mAdapter);
        recyclerCars.setItemAnimator(new SlideLeftAlphaAnimator());
        recyclerCars.getItemAnimator().setAddDuration(500);
        recyclerCars.getItemAnimator().setRemoveDuration(500);
    }

    @Override
    public void onResume(){
        super.onResume();
        getActivity().invalidateOptionsMenu();
    }



}



Layout


XML Text

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="wrap_content"
    >
    <android.support.v7.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10sp">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="16sp">

            <ImageView
                android:id="@+id/imgCar"
                android:layout_width="match_parent"
                android:layout_height="190sp"
                android:background="@color/white"
                android:layout_marginBottom="10dp"/>

            <TextView
                android:layout_below="@+id/imgCar"
                android:id="@+id/carName"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Nama Mobil"
                android:layout_marginRight="50dp"
                android:textColor="@color/colorPrimary"
                android:textSize="14sp"
                android:layout_marginBottom="10sp"
                android:textStyle="bold"
                android:typeface="sans"
                android:layout_alignParentEnd="true" />

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

                <LinearLayout
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:orientation="vertical">

                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:layout_marginBottom="8sp"
                        android:orientation="horizontal">

                        <ImageView
                            android:id="@+id/ic_year"
                            android:layout_width="15sp"
                            android:layout_height="15sp"
                            android:layout_marginRight="10sp"
                            android:src="@drawable/ic_action_event" />

                        <TextView
                            android:id="@+id/year"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:text="2017"
                            android:textColor="@color/colorPrimary"
                            android:textSize="12sp" />
                    </LinearLayout>

                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:layout_marginBottom="8sp"
                        android:orientation="horizontal">

                        <ImageView
                            android:id="@+id/ic_capacity"
                            android:layout_width="15sp"
                            android:layout_height="15sp"
                            android:layout_marginRight="10sp"
                            android:src="@drawable/ic_action_cc_bcc" />

                        <TextView
                            android:id="@+id/capacity"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:text="8"
                            android:textColor="@color/colorPrimary"
                            android:textSize="12sp" />
                    </LinearLayout>

                </LinearLayout>

                <View
                    android:layout_width="0.8dp"
                    android:layout_height="match_parent"
                    android:background="@color/grey_bg"
                    />


                <LinearLayout
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:layout_marginLeft="10dp"
                    android:orientation="vertical">

                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:layout_marginBottom="8sp"
                        android:orientation="horizontal">

                        <ImageView
                            android:id="@+id/ic_color"
                            android:layout_width="15sp"
                            android:layout_height="15sp"
                            android:layout_marginRight="10sp"
                            android:src="@drawable/ic_action_event" />

                        <TextView
                            android:id="@+id/color"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:text="White"
                            android:textColor="@color/colorPrimary"
                            android:textSize="12sp" />
                    </LinearLayout>

                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:layout_marginBottom="8sp"
                        android:orientation="horizontal">

                        <ImageView
                            android:id="@+id/ic_bensin"
                            android:layout_width="15sp"
                            android:layout_height="15sp"
                            android:layout_marginRight="10sp"
                            android:src="@drawable/ic_action_event" />

                        <TextView
                            android:id="@+id/bensin"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:text="Premium"
                            android:textColor="@color/colorPrimary"
                            android:textSize="12sp" />
                    </LinearLayout>
                </LinearLayout>
            </LinearLayout>

            <LinearLayout
                android:layout_marginTop="10sp"
                android:layout_below="@+id/description"
                android:layout_width="match_parent"
                android:orientation="vertical"
                android:layout_height="wrap_content">

                <Button
                    android:id="@+id/status"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:background="@drawable/round"
                    android:text="Tersedia"
                    android:textColor="@color/white"
                    android:textSize="12sp" />

            </LinearLayout>


            <TextView
                android:id="@+id/price"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@color/colorPrimary"
                android:padding="5sp"
                android:text="Rp. 100.000"
                android:textAlignment="center"
                android:textColor="@color/white"
                android:textSize="14sp"
                android:layout_marginRight="5dp"
                android:layout_marginTop="5dp"
                android:layout_alignParentTop="true"
                android:layout_alignParentEnd="true" />
        </RelativeLayout>
    </android.support.v7.widget.CardView>
</LinearLayout>

Komentar