Saturday, October 15, 2016

android - ListView item align right

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/coordinator_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#cbd2c7"
    >
    <ListView
        android:id="@+id/list_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="15dp"
        android:background="#92a389"
        />
</android.support.design.widget.CoordinatorLayout>
MainActivity.java

package com.cfsuman.me.androidcodesnippets;

import android.app.Activity;
import android.content.Context;
import android.support.design.widget.CoordinatorLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

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


public class MainActivity extends AppCompatActivity {
    private Context mContext;
    private Activity mActivity;

    private CoordinatorLayout mCLayout;
    private ListView mListView;
    private ArrayAdapter mAdapter;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Get the application context
        mContext = getApplicationContext();
        mActivity = MainActivity.this;

        // Get the widget reference from XML layout
        mCLayout = (CoordinatorLayout) findViewById(R.id.coordinator_layout);
        mListView = (ListView) findViewById(R.id.list_view);

        // Initialize a new list with values
        List<String> flowers = new ArrayList<>();
        flowers.add("Aquilegia");
        flowers.add("Astilbe");
        flowers.add("Astrantia");
        flowers.add("Aubreita deltoidea");
        flowers.add("Bee Balm Flower");

        // Initialize an array adapter
        mAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,flowers){
            @Override
            public View getView(int position, View convertView, ViewGroup parent){
                // Cast the current view as a TextView
                TextView tv = (TextView) super.getView(position,convertView,parent);

                /*
                    setGravity
                        void setGravity (int gravity)

                        Sets the horizontal alignment of the text and the vertical gravity that
                        will be used when there is extra space in the TextView beyond what
                        is required for the text itself.
                */
                /*
                    Gravity
                        END
                            Push object to x-axis position at the end of its container, not changing its size.

                        RIGHT
                            Push object to the right of its container, not changing its size.

                        CENTER_VERTICAL
                            Place object in the vertical center of its container, not changing its size.
                */
                // Set the item text gravity to right/end and vertical center
                tv.setGravity(Gravity.RIGHT|Gravity.END|Gravity.CENTER_VERTICAL);

                // Return the view
                return tv;
            }
        };

        // Data bind the list view with array adapter items
        mListView.setAdapter(mAdapter);
    }
}