how to change text size of listview
Asked Answered
C

2

10

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

Constructivism answered 8/12, 2013 at 17:18 Comment(0)
G
47

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));
Grueling answered 8/12, 2013 at 17:24 Comment(6)
i use cart.xml instead of android.R.layout.simple_list_item_1, see my codes.Constructivism
I am saying that you need to replace the default textView layout in your setListAdapter with the custom one you made as i wrote in my answer. What is the problem?Grueling
oh, i got it, let me try.Constructivism
sp is scale-independent pixels, while dp or dip is Density-independent pixels. Android spec recommends using sp for text dimensions if you require fixed consistency across devices.Maller
I was tearing my hair out trying to solve my issue! this really helped. thanks!Foolhardy
Thanks. this answer helped me with my projectDoit
F
0

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.

Foreglimpse answered 8/1, 2019 at 12:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.