Use bold in list view (only for a part of each entry)

I am having a list view with many items. Each item has a number as its first character. I want for every entry of the list the number to be bold. I have a list array which I give to array adapter to make the list view. Values are not stable so I cannot put them in an xml file. Is there any way to achieve my goal? I am making a "when my bus arrives" app

<?xml version="1.0" encoding="utf-8"?>

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"
tools:context=".MainActivity">
<SearchView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/search_view"
    android:queryHint="Αναζήτηση Γραμμής"
    android:inputType="text">

</SearchView>
    <ListView
            android:id="@+id/listview"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

    <ListView
        android:id="@+id/listview2"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:visibility="gone" />

    <ListView
        android:id="@+id/listview3"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:visibility="gone" />

<android.support.v4.widget.SwipeRefreshLayout
    android:id="@+id/swipeLayout"
    android:visibility="gone"
    android:paddingTop="8dp"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
        <ListView
        android:id="@+id/listview4"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:visibility="gone" />
</android.support.v4.widget.SwipeRefreshLayout>

private void initUI() {
    final ArrayList<String> arrayList = new ArrayList<>();
    try {
        AssetManager assetManager = getAssets();
        ObjectInputStream input = new ObjectInputStream(new GZIPInputStream((assetManager.open("names.dat"))));
        Object readObject = input.readObject();
        input.close();

        ArrayList<String> names = (ArrayList<String>) readObject;
        arrayList.addAll(names);
        routes = read_files("routes.dat");
        line_route = read_files("line_route.dat");
        lastcall= new ArrayList<String>();
        editsearch= (SearchView) findViewById(R.id.search_view);
        editsearch.setOnQueryTextListener(this);
        swipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipeLayout);
        swipeRefreshLayout.setOnRefreshListener(this);
        Runnable refresh = new Runnable() {
            public void run() {
                if ((MainActivity.this.afikseis.getVisibility()==View.VISIBLE)&& !(swipeRefreshLayout.isRefreshing())){
                    get_time(lastcall.get(0),lastcall.get(1),lastcall.get(2));
                }
            }
        };
        ScheduledExecutorService executor = Executors.newScheduledThreadPool(1);
        executor.scheduleAtFixedRate(refresh, 10, 30, TimeUnit.SECONDS);

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    }


    arrayAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, arrayList);
    listView.setAdapter(arrayAdapter);
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
            print_routes(arrayList.get(i), line_route, routes);
            print_routes(arrayAdapter.getItem(i).toString(), line_route, routes);
        }
    });
}

Thanks in advance

2 answers

  • answered 2019-06-16 04:57 Dor

    It would be better if you can share some code. I would also suggest replacing listView with RecyclerView which have better optimization, you can read about it here.

    And for your question. you can build your ListItem in a way that splits the numbers from the text and by so give the Number TextView a bold Style. It should look something like this

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    >
    
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Num."
        android:textStyle="bold"
        android:layout_marginRight="5dp"/>
    
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        android:text="Some Text"/>
    

    This line is what make a text bold

    android:textStyle="bold"
    

  • answered 2019-06-16 05:02 twothreezarsix

    You need to use a SpannableString. Here are some useful links:

    how to make a specific text on TextView BOLD

    Explain the meaning of Span flags like SPAN_EXCLUSIVE_EXCLUSIVE

    One possible way is to iterate through your list and changing every string using the methods in the link and then using this updated list in the array adapter.

    You could create a function that does this every time a string is added to your list.The function can apply the bold text to only the first number in the last item of the list view.