Monil Andharia Monil Andharia - 1 month ago 16
Android Question

How to set Typeface on ListView which has an ArrayList placed through ArrayAdapter

I want to set the typeface to the listview in the following code. All the related files are attached.

SearchClient.java

Toolbar mToolbar;
EditText search;
ListView listView;
ArrayAdapter<String> adapter;
ArrayList<HashMap<String, String>> productList;
Typeface typeface;
DatabaseHelper databaseHelper;
//Clients List
List<String> client_name = new ArrayList<>();

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search_client);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

mToolbar = (Toolbar) findViewById(R.id.toolbar_search_client);
setSupportActionBar(mToolbar);

typeface = Typeface.createFromAsset(getAssets(),"fonts/ProductSans-Regular.ttf");
init();

search.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
search.setBackgroundTintList(ColorStateList.valueOf(Color.parseColor("#ffffff")));
}
});

databaseHelper = new DatabaseHelper(this);

Cursor res = databaseHelper.TABLE_CLIENTS_view();
while (res.moveToNext()) {
client_name.add(res.getString(1));
}

adapter = new ArrayAdapter<String>(this, R.layout.search_list_item, R.id.search_client_name, client_name);
listView.setAdapter(adapter);

search.addTextChangedListener(new TextWatcher() {

@Override
public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
// When user changed the Text
SearchClient.this.adapter.getFilter().filter(cs);
}

@Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
// TODO Auto-generated method stub

}

@Override
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub
}
});

}

public void init(){
search = (EditText) findViewById(R.id.search_client);
search.setTypeface(typeface);
listView = (ListView) findViewById(R.id.listView_search_client);
}


activity_search_client.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:focusableInTouchMode="true"
android:focusable="true"
android:id="@+id/rel_lay_client_search"
tools:context="com.example.monilandharia.invoice.SearchClient">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<LinearLayout
android:id="@+id/container_toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">

<include
android:id="@+id/toolbar_search_client"
layout="@layout/toolbar_search" />
</LinearLayout>

<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:id="@+id/listView_search_client" />


</LinearLayout>

</RelativeLayout>


search_list_item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<TextView android:id="@+id/search_client_name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="10dip"
android:textSize="16dip"
android:textStyle="bold"/>

</LinearLayout>


And thanks a lot in advance for help!!

Answer

You can create a custom textview like below and set the desired typeface in the custom textview class as shown below.

package rightfit.dooodleblue.com.rightfit.view;

    public class CustomTextView extends TextView {

        public CustomTextView(Context context, AttributeSet attrs) {
            super(context, attrs);
        }

        private void init(Context context, AttributeSet attrs) {
            Typeface typeface = Typeface.createFromAsset(context.getAssets(), "fonts/ProductSans-Regular.ttf");
            this.setTypeface(typeface);
        }
    }

and in your xml (search_list_item.xml) file kindly do the following change.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <com.example.CustomTextView android:id="@+id/search_client_name"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:padding="10dip"
        android:textSize="16dip"
        android:textStyle="bold"/>

</LinearLayout>

Now the type face would be applied to the textview.