com.thevelopment.poc.Fragments.FirstFragment.java Source code

Java tutorial

Introduction

Here is the source code for com.thevelopment.poc.Fragments.FirstFragment.java

Source

package com.thevelopment.poc.Fragments;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v4.app.Fragment;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.Spinner;
import android.widget.Toast;

import com.thevelopment.poc.Classes.ScreenBuilder;
import com.thevelopment.poc.R;
import com.thevelopment.poc.SelectRecordActivity;

import org.json.JSONException;
import org.json.JSONObject;

import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Locale;

public class FirstFragment extends Fragment {

    private boolean debugMode = false;

    ScreenBuilder screenBuilder;

    EditText companyEditText;
    EditText prefixEditText;
    EditText licenseEditText;
    Spinner typeSpinner;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_first, container, false);

        screenBuilder = new ScreenBuilder(getActivity());

        renderScreen(view);

        return view;
    }

    // MAIN METHODS
    private void renderScreen(View view) {

        RelativeLayout relativeLayout = (RelativeLayout) view.findViewById(R.id.fragment_first_relative_layout_id);

        DisplayMetrics displaymetrics = new DisplayMetrics();
        getActivity().getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
        int height = displaymetrics.heightPixels;
        int width = displaymetrics.widthPixels;

        LinearLayout innerLinearLayout = createInnerLinearLayout(relativeLayout, width, height);

        companyEditText = createField(innerLinearLayout, "Garagem/Empresa", height);
        prefixEditText = createField(innerLinearLayout, "Prefixo", height);
        licenseEditText = createField(innerLinearLayout, "Placa", height);

        LinearLayout typeAndDateLinearLayout = screenBuilder.createRowLayout(innerLinearLayout);

        typeSpinner = createTypeOfInspectionField(typeAndDateLinearLayout, width);

        FloatingActionButton fab = createFAB(innerLinearLayout);
        setFABListener(fab);
    }

    public static FirstFragment newInstance(String text) {

        FirstFragment f = new FirstFragment();
        Bundle b = new Bundle();
        b.putString("msg", text);

        f.setArguments(b);

        return f;
    }

    // RENDER SCREEN METHODS
    private LinearLayout createInnerLinearLayout(ViewGroup parentView, int width, int height) {
        LinearLayout innerLinearLayout = screenBuilder.createVerticalLinearLayout(parentView);
        RelativeLayout.LayoutParams innerLinearLayoutParams = (RelativeLayout.LayoutParams) innerLinearLayout
                .getLayoutParams();
        innerLinearLayoutParams.setMargins(width / 20, height / 40, width / 20, height / 10);
        innerLinearLayout.setLayoutParams(innerLinearLayoutParams);
        return innerLinearLayout;
    }

    private EditText createField(ViewGroup parentView, String fieldName, int height) {

        screenBuilder.createTextView(parentView, fieldName + ":", 14);

        final EditText editText = screenBuilder.createEditText(parentView, "Inserir " + fieldName + "...");
        LinearLayout.LayoutParams editTextParams = (LinearLayout.LayoutParams) editText.getLayoutParams();
        editTextParams.bottomMargin = height / 50;
        editText.setLayoutParams(editTextParams);
        return editText;
    }

    public Spinner createTypeOfInspectionField(ViewGroup parentView, int width) {
        // CREATE TYPE OF INSPECTION LINEAR LAYOUT
        LinearLayout typeOfInspectionLinearLayout = screenBuilder.createVerticalLinearLayout(parentView,
                2 * width / 5, LinearLayout.LayoutParams.WRAP_CONTENT);
        LinearLayout.LayoutParams typeOfInspectionParams = (LinearLayout.LayoutParams) typeOfInspectionLinearLayout
                .getLayoutParams();
        typeOfInspectionParams.rightMargin = width / 10;
        typeOfInspectionLinearLayout.setLayoutParams(typeOfInspectionParams);

        // CREATE TYPE OF INSPECTION TEXT VIEW
        screenBuilder.createTextView(typeOfInspectionLinearLayout, "Tipo de Inspecao", 14);

        // CREATE TYPE OF INSPECTION SPINNER
        final Spinner typeSpinner = screenBuilder.createSpinner(typeOfInspectionLinearLayout,
                Arrays.asList(getResources().getStringArray(R.array.type_array)));

        return typeSpinner;

    }

    public EditText createDateField(ViewGroup parentView, int width, int height) {
        // CREATE DATE LINEAR LAYOUT
        LinearLayout dateLinearLayout = screenBuilder.createVerticalLinearLayout(parentView, 2 * width / 5,
                LinearLayout.LayoutParams.WRAP_CONTENT);

        // CREATE DATE TEXT VIEW
        screenBuilder.createTextView(dateLinearLayout, "Data:", 14);

        // CREATE DATE EDIT TEXT
        final EditText dateEditText = screenBuilder.createEditText(dateLinearLayout,
                getResources().getString(R.string.edit_text_fragment_1_date));
        LinearLayout.LayoutParams dateEditTextParams = (LinearLayout.LayoutParams) dateEditText.getLayoutParams();
        dateEditTextParams.bottomMargin = height / 50;
        dateEditText.setLayoutParams(dateEditTextParams);

        return dateEditText;
    }

    public FloatingActionButton createFAB(ViewGroup parentView) {
        FloatingActionButton fab = new FloatingActionButton(getActivity());
        fab.setImageResource(R.drawable.ic_go);
        LinearLayout.LayoutParams fabParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
                LinearLayout.LayoutParams.WRAP_CONTENT);
        fabParams.gravity = Gravity.RIGHT;
        fab.setLayoutParams(fabParams);

        parentView.addView(fab);
        return fab;
    }

    public void setFABListener(FloatingActionButton fab) {

        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if ((companyEditText.length() == 0 || prefixEditText.length() == 0 || licenseEditText.length() == 0)
                        && !debugMode) {
                    if (companyEditText.length() == 0)
                        companyEditText.setError("Empresa/Garagem Obrigatrio");
                    if (prefixEditText.length() == 0)
                        prefixEditText.setError("Prefixo Obrigatrio");
                    if (licenseEditText.length() == 0)
                        licenseEditText.setError("Placa Obrigatria");
                } else {
                    Snackbar.make(view, "Aguarde...", Snackbar.LENGTH_LONG).setAction("Action", null).show();
                    JSONObject infoJSON = new JSONObject();

                    Calendar c = Calendar.getInstance();
                    SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy", Locale.US);
                    String date = format.format(c.getTime());

                    try {
                        infoJSON.put("company", companyEditText.getText().toString());
                        infoJSON.put("prefix", prefixEditText.getText().toString());
                        infoJSON.put("licensePlate", licenseEditText.getText().toString());
                        infoJSON.put("inspectionType", typeSpinner.getSelectedItem().toString());
                        infoJSON.put("companyId", "companyTest");
                        infoJSON.put("userId", "userTest");
                        infoJSON.put("vehicleId", "vehicleTest");
                        infoJSON.put("date", date);

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

                    Intent selectRecordIntent = new Intent(getActivity(), SelectRecordActivity.class);
                    selectRecordIntent.putExtra("recordHeader", infoJSON.toString());
                    startActivity(selectRecordIntent);
                }
            }
        });
    }
}