Home » Android » android – how to change text size of listview

android – how to change text size of listview

Posted by: admin June 15, 2020 Leave a comment

Questions:

I am using List Activity to retrieve data from SQLITE. But I can not set the font size of list view. Please help me.

public class CartList extends ListActivity {
    private ArrayList<String> results = new ArrayList<String>();
    public void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        setContentView(com.example.easyshopping.R.layout.cart);
        openAndQueryDatabase();
        displayResultList();
    }
    private void displayResultList() {
        setListAdapter(new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, results));
        getListView().setTextFilterEnabled(true);   }
        private void openAndQueryDatabase() {
        try {
            SQLiteDatabase database = openOrCreateDatabase("ORCL", MODE_PRIVATE, null);
            Cursor c = database.rawQuery("SELECT title,qty,price FROM CART;", null);
                         if (c != null ) {
                int totalPrice=0;
                if  (c.moveToFirst()) {
                    do {
                        String title = c.getString(c.getColumnIndex("title"));
                        int qty = c.getInt(c.getColumnIndex("qty"));
                        int price = c.getInt(c.getColumnIndex("price"));
                        int pricePerTitle=price*qty;
                        results.add("Title: " + title + ",  Quantity: " + qty+",  Price: $"+pricePerTitle);
                        totalPrice=totalPrice+pricePerTitle;
                    }while (c.moveToNext());
                }
               TextView tTotalPrice=(TextView)findViewById(com.example.easyshopping.R.id.txttotalprice);
                String total= Integer.toString(totalPrice);
               tTotalPrice.setText(total);
            }
        } catch (SQLiteException se ) {
            Log.e(getClass().getSimpleName(), "Could not create or Open the database");
        }

    }

cart.xml

LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="match_parent">
    <RelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center" android:layout_gravity="center">
        <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/shoppinglist"

                ></ImageView>

            </RelativeLayout>

    <ListView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:id="@android:id/list"
            ></ListView>

    <RelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center" android:layout_gravity="center" >
       <TextView
               android:layout_width="wrap_content"
               android:layout_height="wrap_content"
               android:text="Total Price is:$ "
               android:textColor="#f3f607"
               android:textSize="10dp"
               android:id="@+id/txttotal"
               ></TextView>
        <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textColor="#f3f607"
                android:textSize="10dp"
                android:layout_toRightOf="@+id/txttotal"
               android:id="@+id/txttotalprice"></TextView>
        <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:text="send your Shopping List."
                android:textSize="12dp"
                android:id="@+id/cartButton"
                android:layout_below="@+id/txttotal"></Button>

            </RelativeLayout>

the font size is very big in list view.
enter image description here

How to&Answers:

Go into layout folder and create a new xml file named mytextview.xml(whatever you want to name it)

    <?xml version="1.0" encoding="utf-8"?>
    <TextView xmlns:android="http://schemas.android.com/apk/res/android" 
     android:id="@android:id/text1"  
            android:paddingTop="2dip" 
            android:paddingBottom="3dip" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content"
        android:textSize="2dp" /> 

and in the code

    setListAdapter(new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_1, results));

change it to

    setListAdapter(new ArrayAdapter<String>(this,
            R.layout.mytextview, results));

Answer:

Do not let the Layout (Relative Layout/constraint layout) remain in the new layout resource file.

That would give an error.
Only have the Textview with your custom settings.