简体   繁体   中英

How to change font color or size of Listview item

I'm looking for solution to my Listview where I would like to change the font color to red and size to any desired size of listview items. I've tried using adaper but I'm countering multiple error.

Here is the xml file

 <?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <android.support.v7.widget.Toolbar android:id="@+id/my_toolbar" android:layout_width="match_parent" app:title="Space Time Alarm" android:layout_height="56dp" android:background="#30e410" android:elevation="4dp" android:theme="@style/ThemeOverlay.AppCompat.ActionBar" app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/> <ListView android:id="@+id/listView_Alarms" android:layout_width="0dp" android:layout_height="0dp" android:layout_marginTop="?android:attr/actionBarSize" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.0" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintVertical_bias="0.0" /> </android.support.constraint.ConstraintLayout> 

and Mainactivity file is

 public class MainActivity extends AppCompatActivity { public static boolean havePermission; private ListView listView_Alarms; private FloatingActionButton button_NewAlarm; private Toolbar toolbar; private DatabaseManager databaseManager; @Override protected void onCreate(Bundle savedInstanceState) { Log.d("SPACESTOREALARM", "CREATING"); super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //setting up the toolbar and support toolbar = (Toolbar) findViewById(R.id.my_toolbar); setSupportActionBar(toolbar); listView_Alarms = (ListView) findViewById(R.id.listView_Alarms); SpaceTimeAlarmManager.getInstance().initializeSpaceTimeAlarmManager(this); databaseManager = DatabaseManager.getInstance(this); databaseManager.initialize(this, listView_Alarms); listView_Alarms.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) { SpaceTimeAlarm alarm = (SpaceTimeAlarm) listView_Alarms.getItemAtPosition(position); createOrEditAlarm(alarm); } }); } } 

Here list view contains list of alarms but due to same color both of font and background..the list cannot be seen. So I need help to change font color and size of listview items of above.

Thanks in advance.

I would recommend to create a custom ListView .

Check this for creating a custom ListView

With a custom ListView you have got a lot of more options to customize it.

You can set an adapter and change the background color and item size like below:

lv = findViewById(R.id.listView);
lv.setAdaptater(adapter);
final ArrayAdapter<String> adapter = new ArrayAdapter<String> (this, android.R.layout.simple_list_item_1, list) {
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            // Get the current item from ListView
            View view = super.getView(position, convertView, parent);

            // Set the text color of TextView (ListView Item)
            TextView tv = view.findViewById(android.R.id.text1);
            // Generate ListView Item using TextView
            tv.setTextColor(Color.parseColor("#efefef"));
            tv.setTextSize(20);
            tv.setAllCaps(true);

                // Set a background color for ListView
                view.setBackgroundColor(Color.parseColor("#00afd8"));

            return view;
        }
    };
    lv.setAdapter(adapter);

You can even alternate background colors.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM