Category Archives: Dynamic Views in Android

Dynamic Views in Android


This tutorial is about creating dynamic views in android based on json data. These json data can be a server response or may be not. For everybodies sake, I have stored my json data in the asset folder itself. But, we can use this tutorial by fetching json data from whereever you want. This may help you to fetch your json data. Ok, now lets get started.
What are dynamic views?
Its the process of using one activity as a container and drawing multiple view in the same activity. These views are not declare in XML. We are generating our widgets dynamically.

Our Scenario:
We will create a Profile Activity. The MainActivity is the ViewProfileActivity. EditProfileActivity creates the Dynamic Views and stores user inputs. We have three snapshots of our project.
Dynamic View Project

ViewProfileActivity-SS(1) is the screenshot of the Profile Page of the user. The Edit Profile button located in the top of the ViewProfileActivity starts the EditProfileActivity represented by EditProfileActivity-SS(2). EditProfileActivity displays the dynamic views. It allows user to provide their inputs. At the end of the layout, there is a save button through which the user can store their inputs and finish the EditProfileActivity. After the user presses the Save button, they are redirected to the ViewProfileActivity which displays all their inputs stored in the dynamic widgets.

ViewProfileActivity just displays the data.
The layout for ViewProfileActivity is as follows:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <RelativeLayout
        android:id="@+id/btnListBar_profile"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@color/repeating_bgcolor"
            android:paddingBottom="4dp"
        android:paddingTop="4dp" >
        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="33dp"
            android:layout_centerInParent="true"
            android:layout_margin="5dp"
            android:background="@color/border_button"
            android:gravity="center"
            android:orientation="horizontal"
            android:padding="1dp"
            android:weightSum="3" >
            <Button
                android:id="@+id/btnEditProfile"
                style="@style/CustomProfileButtonStyle"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@android:color/transparent"
                android:text="Edit Profile"
                android:textColor="@color/text_saved"
                android:textSize="14sp" />
        </LinearLayout>
    </RelativeLayout>
    <TextView
        android:id="@+id/field"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:editable="false"
        android:padding="5dp"
        android:shadowColor="#FFFFFF"
        android:shadowDy="1"
        android:shadowRadius="0.01"
        android:text="Details of user profile"
        android:textColor="#000000"
        android:textSize="12sp"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/detailUserData"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:editable="false"
        android:padding="5dp"
        android:shadowColor="#FFFFFF"
        android:shadowDy="1"
        android:shadowRadius="0.01"
        android:text="Click Edit Profile To Fill This Data"
        android:textColor="#000000"
        android:textSize="12sp"
        android:textStyle="bold" />

</LinearLayout>

The content of ViewProfileActivity is as follows:

public class ViewProfileActivity extends Activity {
    Button editProfile;
    ProfileInfoHolder profileInfoHolder;
    ImageView userProfileImage;
    public static final int EDIT_PROFILE_DATA = 1212;
    public static final int CAMERA_UPLOAD = 1313;
    public static final int MEDIA_UPLOAD = 1888;
    TextView detailUserData;
    protected void onCreate(android.os.Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.profile_page_display);
        initViews();

    };

    public void initViews() {
        profileInfoHolder = new ProfileInfoHolder();
        editProfile = (Button) findViewById(R.id.btnEditProfile);

        editProfile.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                Intent editDataIntent = new Intent(ViewProfileActivity.this,
                        EditProfileActivity.class);
                startActivityForResult(editDataIntent, EDIT_PROFILE_DATA);

            }
        });
        detailUserData = (TextView) findViewById(R.id.detailUserData);

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub
        super.onActivityResult(requestCode, resultCode, data);
        Log.i("Result Code", resultCode + "");
        if (requestCode == EDIT_PROFILE_DATA) {

            String detailInformation = "";
            LinkedHashMap<String, String> userFields = new LinkedHashMap<String, String>();
            userFields = UtilityClass.EDIT_PROFILE_WITH_FIELD_NAME;

            // testing testing
            String fieldValueAndPrivacy = null;// = new
                                                // ArrayList<String>();
            LinkedHashMap<String, ArrayList<String>> editProfileHashValues = new LinkedHashMap<String, ArrayList<String>>();

            Iterator fieldIterator = userFields.entrySet().iterator();
            while (fieldIterator.hasNext()) {
                Map.Entry fieldPairs = (Map.Entry) fieldIterator.next();
                String fieldName = (String) fieldPairs.getKey();
                String fieldRunTimeValue = (String) fieldPairs.getValue();
                detailInformation += fieldName + " => " + fieldRunTimeValue
                        + "\n";

            }
            detailUserData.setText(detailInformation);

        }
    }
}

The main work is done by EditProfileActivity. The functionalities of EditProfileActivity are listed below:
1.It fetches the json data from the asset folder named “jsonresponse.txt” and then parses the json data to the java objects. These java objects are created from helper classes. Helper classes are specifically create to hold the parsed json data. We are using two helper classes: Field and ProfileInfoHolder.There is a demo of Parser class:
The contents of helper class Field are as follows:


public class Field implements Serializable {
    public String editProfileFieldId;
    public String editProfileFieldType = null;
    public String editProfileFieldGroup;
    public int ePFieldIsPublished;
    public int ePFieldIsSearchable;
    public int ePFieldIsVisible;
    public int ePFieldIsRequired;
    public int ePFieldIsRegistered;
    public int ePFieldMinimumChars;
    public int ePFieldMaximumChars;
    public int ePFieldOrdering;
    public String editProfileFieldName = null;
    public String editProfileFieldTips = null;
    public String editProfileFieldCode = null;
    public String editProfileFieldValue = null;
    public String editProfileFieldPrivacyAccess = null;
}

The contents of helper class ProfileInfoHolder are as follows:


public class ProfileInfoHolder {
    public static ArrayList<Field> listFieldType = new ArrayList<Field>();
    public static ArrayList<String> nameOfFieldHeader = new ArrayList<String>();
    public static LinkedHashMap<String, ArrayList<Field>> fieldHashValues = new LinkedHashMap<String, ArrayList<Field>>();
    public String userId;
    public String userName;
    public String userEmail;
}

The parser class parses the json data into java objects in the following way:


public class JsonParser {
    public static LinkedHashMap<String, ArrayList<Field>> parseUserProfileDetails(
            String jsonString) throws JSONException {
        LinkedHashMap<String, ArrayList<Field>> hashValuesOfJsonData = new LinkedHashMap<String, ArrayList<Field>>();
        ArrayList<Field> listFieldArrays = new ArrayList<Field>();
        String fieldHeaderName = "nothing";
        ProfileInfoHolder profileDetails = new ProfileInfoHolder();
        JSONObject profileDetailsObj = new JSONObject(jsonString);
        profileDetails.userId = profileDetailsObj.getString("id");
        Log.i("profile uid", profileDetails.userId);
        profileDetails.userName = profileDetailsObj.getString("name");
        Log.i("profile user name", profileDetails.userName);
        profileDetails.userEmail = profileDetailsObj.getString("email");
        Log.i("Profile email", profileDetails.userEmail);
        JSONObject fieldsObject = profileDetailsObj.getJSONObject("fields");
        JSONArray fieldNamesArray = fieldsObject.names();
        // method to parse fields of a category
        for (int i = 0; i < fieldNamesArray.length(); i++) {

            JSONArray fieldsArray = fieldsObject.getJSONArray(fieldNamesArray
                    .get(i).toString());
            fieldHeaderName = fieldNamesArray.get(i).toString();

            listFieldArrays = parseCategoryField(fieldsArray);
            Field field = new Field();
            field.editProfileFieldGroup = fieldHeaderName;
            hashValuesOfJsonData.put(fieldHeaderName, listFieldArrays);
        }
        return hashValuesOfJsonData;
    }
    public static ArrayList<Field> parseCategoryField(JSONArray fieldArray)
            throws JSONException {
        ArrayList<Field> fieldArrayList = new ArrayList<Field>();
        int arrLength = fieldArray.length();
        for (int i = 0; i < arrLength; i++) {
            Field field = new Field();
            JSONObject fieldObj = fieldArray.getJSONObject(i);
            field.editProfileFieldId = fieldObj.getString("id");
            field.editProfileFieldType = fieldObj.getString("type");
            // for int
            field.ePFieldIsVisible = fieldObj.getInt("visible");
            field.ePFieldIsRequired = fieldObj.getInt("required");
            field.ePFieldIsSearchable = fieldObj.getInt("searchable");
            field.ePFieldIsRegistered = fieldObj.getInt("registration");
            // for int
            field.ePFieldOrdering = fieldObj.getInt("ordering");
            field.ePFieldMinimumChars = fieldObj.getInt("min");
            field.ePFieldMaximumChars = fieldObj.getInt("max");
            // for strings
            field.editProfileFieldName = fieldObj.getString("name");
            field.editProfileFieldTips = fieldObj.getString("tips");
            field.editProfileFieldValue = fieldObj.getString("value");
            field.editProfileFieldPrivacyAccess = fieldObj.getString("access");
             fieldArrayList.add(field);
        }
        return fieldArrayList;
    }

2.It instantiates the DynamicFormCreation class. Dynamic form creation class creates dynamic widgets. While instantiating the class, we pass the profile field objects and ids. We pass ids because we can manually assign ids when we create dynamic widgets. Passing ids is important because it makes it easy to store user inputs from these widgets.The content of DynamicFormCreation class is given below:

public class DynamicFormCreation {
    public int extraFieldId;
    private Context context;
    int intToDpi1, intToDpi2, intToDpi3, intToDpi4, intToDpi5, intToDpi45,
            intToDpi0, intToDpi175, intToDpi125, intToDpi20, intToDpi80;
    public ImageView imageViewToUpload;
    private LinearLayout.LayoutParams layoutParamsBasic;
    public DynamicFormCreation(Context context) {
        this.context = context;
        initDpiData();
    }
    public void initDpiData() {
        intToDpi1 = UtilityClass.intToDpi(1, context);
        intToDpi2 = UtilityClass.intToDpi(2, context);
        intToDpi3 = UtilityClass.intToDpi(3, context);
        intToDpi4 = UtilityClass.intToDpi(4, context);
        intToDpi5 = UtilityClass.intToDpi(5, context);
        }
    public View getRuntimeView(Field formFields, int lastFieldId) {
        View mView = null;
        mView = getTextFields(formFields, lastFieldId);
        return mView;
    }

    public View getTextFields(Field formFields, int parentId) {
        LinearLayout rel = UtilityClass.getLinearLayoutHorizontal(context);
        TextView titleTextView = createTextView(context, formFields, parentId);
        rel.addView(titleTextView);
        parentId++;
        EditText editTextField = createEditText(
                formFields.editProfileFieldTips, context, parentId);
        if (formFields.editProfileFieldType.equals("url")) {
            Log.i("Enters here", "tag is set");
            editTextField.setTag("urlTextfield");
        }
        if (formFields.editProfileFieldType.equals("email")) {
            editTextField.setInputType(InputType.TYPE_CLASS_TEXT
                    | InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS);
        } else if (formFields.editProfileFieldType.equals("url")) {
            editTextField.setInputType(InputType.TYPE_CLASS_TEXT
                    | InputType.TYPE_TEXT_VARIATION_URI);
        }
        parentId++;
        editTextField.setTag("editText");
        if (formFields.editProfileFieldType.equals("label")) {
            editTextField.setTag("labelEditText");
            editTextField.setEnabled(false);
        }
        rel.addView(editTextField);
        if (formFields.ePFieldIsRequired == 1) {
            editTextField.addTextChangedListener(new CustomTextChangedListener(
                    context, editTextField, titleTextView,
                    formFields.editProfileFieldType));
        }
        extraFieldId = parentId++;
        rel.setId(extraFieldId);
        return rel;
    }

    public TextView createTextView(Context context, Field formField,
            int parentId) {
        TextView textView = new TextView(context);
        LinearLayout.LayoutParams layoutParamsBasic = UtilityClass.getLayoutParams();
        layoutParamsBasic.width = intToDpi80;
        textView.setLayoutParams(layoutParamsBasic);
        String text = formField.editProfileFieldName;
        textView.setText(text);
        if (formField.ePFieldIsRequired == 1) {
            textView.setText(text + "*");
        } else {
            textView.setText(text);
        }
        extraFieldId = parentId++;
        textView.setPadding(intToDpi1, intToDpi1, intToDpi1, intToDpi1);
        textView.setShadowLayer(1, 1, 1, Color.WHITE);
        textView.setId(extraFieldId);
        return textView;
    }
    public EditText createEditText(String hint, Context context, int parentId) {
        EditText editText = new EditText(context);
        editText.setTextSize(10);
        editText.setPadding(intToDpi2, intToDpi2, intToDpi2, intToDpi2);
        editText.setWidth(UtilityClass.intToDpi(175, context));
        editText.setGravity(Gravity.CENTER);
        LinearLayout.LayoutParams layoutParamsBasic = UtilityClass.getLayoutParams();
        editText.setLayoutParams(layoutParamsBasic);
        extraFieldId = parentId++;
        editText.setId(extraFieldId);
        if (!hint.equals(null))editText.setHint(hint);
        return editText;
    }

}

3.It instantiates StoreDynamicFormValues class to store the user inputs from the dynamic widgets. We have to pass the container layout where we have created our dynamic views and arraylist where we have stored the profile fields and ids for dynamic views. The content of StoreDynamicFormValues is given below:

public class StoreDynamicFormValues {
    public ArrayList<TextView> inCorrectFieldsList;
    public LinkedHashMap<String, String> postFieldsMap;
    public LinkedHashMap<String, String> displayFieldsMap;
    public LinkedHashMap<String, String> privacyFieldsMap;
    private TextView textTitle;
    private EditText editTextRunTime;
    private Pattern pattern;
    String hypertextSelected;
    private Matcher matcher;

    public void getRegistrationFormFields(LinearLayout editPageLayout,
            HashMap<String, Field> displayedExtraFieldsMap) {
        inCorrectFieldsList = new ArrayList<TextView>();
        privacyFieldsMap = new LinkedHashMap<String, String>();
        postFieldsMap = new LinkedHashMap<String, String>();
        displayFieldsMap = new LinkedHashMap<String, String>();
        getFormFields(editPageLayout,(LinkedHashMap<String, Field>) displayedExtraFieldsMap);
    }
    private void getFormFields(LinearLayout layoutCoreFields,
            LinkedHashMap<String, Field> coreFieldsMap) {
        Field runTimeFormFields = null;
        for (int i = 0; i < layoutCoreFields.getChildCount(); i++) {
            View view = layoutCoreFields.getChildAt(i);
            if (view instanceof LinearLayout) {
                LinearLayout rel = (LinearLayout) view;
                for (int j = 0; j < rel.getChildCount(); j++) {
                    View mView = (View) rel.getChildAt(j);
                    if (mView instanceof LinearLayout) {
                        LinearLayout runTimeUserView = (LinearLayout) mView;
                        textTitle = (TextView) runTimeUserView.getChildAt(0);
                        runTimeFormFields = coreFieldsMap.get(String.valueOf(runTimeUserView.getId()));                 
                        for (int p = 0; p < runTimeUserView.getChildCount(); p++) {
                            View runTimeView = runTimeUserView.getChildAt(p);
                            if (runTimeView instanceof EditText) {
                                editTextRunTime = (EditText) runTimeView;
                                String text = editTextRunTime.getText()
                                        .toString();
                                if ((text.equals("") && editTextRunTime
                                        .isEnabled())) {
                                    textTitle.setTextColor(Color.RED);
                                    inCorrectFieldsList.add(textTitle);
                                }
                                runTimeFormFields.editProfileFieldValue = text;
                            }
                            postFieldsMap.put(runTimeFormFields.editProfileFieldId, runTimeFormFields.editProfileFieldValue);
                            displayFieldsMap.put(runTimeFormFields.editProfileFieldName, runTimeFormFields.editProfileFieldValue);
                        }
                    }
                }
            }
        }
    }
}

Hence, its time to summarise the content of EditProfileActivity. The layout for EditProfileActivity is as follows:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <ScrollView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >

        <LinearLayout
            android:id="@+id/linearLayoutForEdit"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="vertical" />
    </ScrollView>

</LinearLayout>

The complete source code for EditProfileActivity is given below

public class EditProfileActivity extends Activity {
    TextView textView;

    private ActionMode actionMode;

    private String JSON_FILENAME = "jsonresponse.txt";
    DynamicFormCreation runTimeUiLibs;
    LinearLayout editPageLayout;
    LinearLayout.LayoutParams layoutParamsBasic;
    Button saveBtn;
    ArrayList<Field> fieldValuesArray;
    ProfileInfoHolder profileDetails;
    
    int intToDpi10;
    int count = 0;
    private LinkedHashMap<String, Field> fieldMapList;
    private StoreDynamicFormValues storeDynamicFormValues = new StoreDynamicFormValues();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_edit_profile);
        initViews();
        try {
            fetchJsonDataFromAssets();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void initViews() {
        initDpiData();
        profileDetails = new ProfileInfoHolder();
        runTimeUiLibs = new DynamicFormCreation(EditProfileActivity.this);
        editPageLayout = (LinearLayout) findViewById(R.id.linearLayoutForEdit);
        layoutParamsBasic = UtilityClass.getLayoutParams();

    }

    public void initDpiData() {
        intToDpi10 = UtilityClass.intToDpi(10, EditProfileActivity.this);
    }

    public void fetchJsonDataFromAssets() throws IOException {

        String jsonLocation = convertJsonToStringFromAssetFolder(JSON_FILENAME);
        Log.i("Json Data Are", jsonLocation);
        parseJsonFileToJavaObjects(jsonLocation);// this function parses the
                                                    // json data to java objects
    }

    public String convertJsonToStringFromAssetFolder(String fileName)
            throws IOException {
        AssetManager manager = this.getAssets();
        InputStream file = manager.open(fileName);

        byte[] data = new byte[file.available()];
        file.read(data);
        file.close();
        return new String(data);
    }

    public void parseJsonFileToJavaObjects(String json) {

        if (json != null) {
            try {
                ProfileInfoHolder.fieldHashValues = JsonParser
                        .parseUserProfileDetails(json);
            } catch (JSONException e) {
                e.printStackTrace();
            }
            createRegistrationForm(ProfileInfoHolder.fieldHashValues);

        } else {
            Toast.makeText(EditProfileActivity.this, "Null JSON",
                    Toast.LENGTH_SHORT).show();
        }
    }

    private void createRegistrationForm(
            LinkedHashMap<String, ArrayList<Field>> fieldHashValues) {

        if (fieldHashValues.size() > 0) {
            fieldMapList = new LinkedHashMap<String, Field>();
            Iterator it = fieldHashValues.entrySet().iterator();
            fieldValuesArray = new ArrayList<Field>();
            count = count + 1;
            while (it.hasNext()) {
                Map.Entry pairs = (Map.Entry) it.next();

                // create field header and its run time UI
                String fieldHeader = (String) pairs.getKey();
                addFieldHeaderTextView(fieldHeader);

                // iteratively create fields of each field header
                fieldValuesArray = (ArrayList<Field>) pairs.getValue();
                int length = fieldValuesArray.size();
                count = count + 1;

                for (int i = 0; i < length; i++) {

                    Field field = fieldValuesArray.get(i);
                    count = count + 1;
                    int lastCoreFieldId = count++;

                    View view = runTimeUiLibs.getRuntimeView(field,
                            lastCoreFieldId);

                    if (view != null) {
                        LinearLayout fieldAndPrivacyLayout = UtilityClass
                                .getLinearLayoutHorizontal(EditProfileActivity.this);

                        view.setLayoutParams(layoutParamsBasic);

                        fieldAndPrivacyLayout.addView(view);

                        editPageLayout.addView(fieldAndPrivacyLayout);
                    }
                    Log.i("Id Checkin in Edit Profile",
                            String.valueOf(view.getId()));
                    fieldMapList.put(String.valueOf(view.getId()), field);
                }
            }
            it.remove(); // avoids a ConcurrentModificationException
            addSaveButton();
        } else {
            Toast.makeText(this, "Error fetching json file", Toast.LENGTH_LONG)
                    .show();
        }
    }
    public void addFieldHeaderTextView(String fieldHeader) {
        TextView tv = new TextView(EditProfileActivity.this);
        tv.setText(fieldHeader);
        layoutParamsBasic = UtilityClass.getLayoutParamsFillParentWidth();
        tv.setLayoutParams(layoutParamsBasic);
        editPageLayout.addView(tv);
    }
    public void addSaveButton() {
        // add save button at the last of the fields
        saveBtn = UtilityClass.getButton(EditProfileActivity.this, "Save");
        layoutParamsBasic = UtilityClass.getLayoutParamsFillParentWidth();
        layoutParamsBasic.setMargins(intToDpi10, intToDpi10, intToDpi10,
                intToDpi10);
        saveBtn.setLayoutParams(layoutParamsBasic);
        saveBtn.setOnClickListener(saveButtonClickListener);
        editPageLayout.addView(saveBtn);
    }
    private OnClickListener saveButtonClickListener = new OnClickListener() {
        @Override
        public void onClick(View v) {
            // the runtime user inputs and privacy fields are accessed through
            // getRegistration form fields
            storeDynamicFormValues.getRegistrationFormFields(editPageLayout,
                    fieldMapList);
            if (storeDynamicFormValues.inCorrectFieldsList.size() > 0) {
                Toast.makeText(EditProfileActivity.this,
                        "Please Fill Form Correctly", Toast.LENGTH_SHORT)
                        .show();
                storeDynamicFormValues.inCorrectFieldsList.clear();
            } else {
                sendEditProfileDataToServer();
            }
        }
    };
    public void sendEditProfileDataToServer() {
        UtilityClass.EDIT_PROFILE_WITH_FIELD_NAME = storeDynamicFormValues.displayFieldsMap;
        finish();
    }
}

Here, we have also create a UtilityClass that stores the codes which can be reused again and again throughout our project. The content of Utility Class is as folllow:

public class UtilityClass {
    public static LinkedHashMap<String, String> EDIT_PROFILE_RUNTIME_INPUT = new LinkedHashMap<String, String>();
    public static LinkedHashMap<String, String> EDIT_PROFILE_WITH_FIELD_NAME = new LinkedHashMap<String, String>();
    public static Button getButton(Context context, String text) {
        Button button = new Button(context);
        LinearLayout.LayoutParams layoutParamsBasic = getLayoutParams();
        layoutParamsBasic.gravity = Gravity.RIGHT;
        layoutParamsBasic.width = intToDpi(55, context);
        layoutParamsBasic.height = intToDpi(40, context);
        button.setLayoutParams(layoutParamsBasic);
        button.setText(text);
        button.setTextSize(12);
        button.setGravity(Gravity.CENTER);
        button.setShadowLayer(1, 1, 1, Color.WHITE);
        int intToDpi2 = intToDpi(2, context);
        button.setPadding(intToDpi2, intToDpi2, intToDpi2, intToDpi2);
        return button;
    }    
    public static int intToDpi(int integer, Context context) {
        return (int) (integer * context.getResources().getDisplayMetrics().density);
    }
    public static LinearLayout.LayoutParams getLayoutParams() {
        return new LinearLayout.LayoutParams(
                ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);
    }
    public static LinearLayout.LayoutParams getLayoutParamsFillParentWidth() {
        return new LinearLayout.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);
    }    
    public static LinearLayout getLinearLayoutHorizontal(Context context) {
        LinearLayout linearLayout = new LinearLayout(context);
        int intToDpi5 = intToDpi(5, context);
        linearLayout.setPadding(intToDpi5, intToDpi5, intToDpi5, intToDpi5);
        linearLayout.setOrientation(LinearLayout.HORIZONTAL);
        LinearLayout.LayoutParams layoutParamsBasic = getLayoutParams();
        linearLayout.setLayoutParams(layoutParamsBasic);
        return linearLayout;
    }
}

The project is stored in GitHub. Download it and check it out.
Hope you enjoyed reading my blog. See you next time through my blog. Until then, Happy Coding 🙂

Advertisements