-1

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

Leo
  • 5,017
  • 6
  • 32
  • 55
Nikos
  • 17
  • 1
  • 10
  • use `textStyle` property in your `textView` – suvojit_007 Jun 16 '19 at 04:36
  • Possible duplicate of [how to make a specific text on TextView BOLD](https://stackoverflow.com/questions/14371092/how-to-make-a-specific-text-on-textview-bold) – Zoe Jun 16 '19 at 13:37

2 Answers2

0

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"
Dor
  • 657
  • 1
  • 5
  • 11
  • Thanks for the answer. I use list view because it is scrollable. Also how can i use the method you describe?how will i modify the text before i show the list? – Nikos Jun 16 '19 at 07:19
  • RecyclerView is scrollable as well... it's not a method it's the XML – Dor Jun 16 '19 at 07:49
  • yes but as i read, RecyclerView has not an item click listener. I have a list o bus lines, when user click a line a new list apperas with the routes of line, an then a third list with the stops of route. I am making an "When my bus arrives" app – Nikos Jun 16 '19 at 14:24
  • There are ways to implement click listener on RecyclerView but it's an off topic. do some research and if you will need help we will be happy to help. – Dor Jun 16 '19 at 14:30
0

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.

twothreezarsix
  • 375
  • 3
  • 13