me.ziccard.secureit.fragment.EmptyFragment.java Source code

Java tutorial

Introduction

Here is the source code for me.ziccard.secureit.fragment.EmptyFragment.java

Source

/*
 * Copyright (c) 2013-2015 Marco Ziccardi, Luca Bonato
 * Licensed under the MIT license.
 */

package me.ziccard.secureit.fragment;

import me.ziccard.secureit.R;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.LinearLayout.LayoutParams;

public final class EmptyFragment extends Fragment {

    private static final String CONTENT = "fragment_content";

    public static EmptyFragment newInstance(String content) {
        EmptyFragment fragment = new EmptyFragment();

        fragment.mContent = "Service " + content + " disabled";
        return fragment;
    }

    private String mContent = "???";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        if ((savedInstanceState != null) && savedInstanceState.containsKey(CONTENT)) {
            mContent = savedInstanceState.getString(CONTENT);
        }

        TextView text = new TextView(getActivity());
        text.setGravity(Gravity.CENTER);
        text.setText(mContent);
        text.setTextSize(20 * getResources().getDisplayMetrics().density);

        RelativeLayout layout = new RelativeLayout(getActivity());
        layout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
        layout.setGravity(Gravity.CENTER);

        RelativeLayout alert = new RelativeLayout(getActivity());
        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT,
                LayoutParams.WRAP_CONTENT);
        params.leftMargin = 20;
        params.rightMargin = 20;
        params.addRule(RelativeLayout.CENTER_HORIZONTAL);
        params.addRule(RelativeLayout.CENTER_IN_PARENT);
        alert.setLayoutParams(params);
        alert.setBackgroundResource(R.drawable.red_back);
        alert.setPadding(30, 0, 30, 0);
        alert.addView(text);
        layout.addView(alert);

        return layout;
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putString("CONTENT", mContent);
    }
}