0

Here is my XML:

<RelativeLayout 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:background="#E0E0E0"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.ccb.lldm.lldmhimnario.Cantos">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:elevation="25dp"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="#303F9F"
        app:popupTheme="@style/AppTheme.PopupOverlay">

        <TextView
            android:layout_width="wrap_content"
            android:textColor="#E0E0E0"
            android:text="Cantos"
            android:id="@+id/toolbarCantos"
            android:textSize="25sp"
            android:layout_height="wrap_content" />

    </android.support.v7.widget.Toolbar>

<ListView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/ListView"
    android:layout_below="@+id/toolbar"
    android:layout_alignParentBottom="true">

</ListView>


</RelativeLayout>

This is for my listview.

Here is my custom listview xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:background="#E0E0E0"
android:layout_height="match_parent">

<TextView
    android:layout_width="wrap_content"
    android:text="Canto: "
    android:background="@drawable/text_ripple"
    android:textColor="#303F9F"
    android:id="@+id/textCanto"
    android:textSize="25sp"
    android:layout_height="45dp" />

   </LinearLayout>

Here is my java files:

public class Cantos extends AppCompatActivity {

ListView lv;
Context context;

ArrayList cantoList;
public static String[] cantos = {"1: Abre Tu Oido", "2: A Cristo Quiero", "3: Acerquese Mi Clamor", "4: A Cristo Yo Alabare",
        "5: Acude Dios", "6: Adelante", "7: A Dios Canto", "8: Adios Para Siempre", "9: Ahora Senor", "10: A Jesucristo Ven",
        "11: Alabad A Dios"};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_cantos);
    initTypeface();
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    getSupportActionBar().setTitle(null);

    context = this;
    lv = (ListView)findViewById(R.id.ListView);
    lv.setAdapter(new CustomAdapter(this, cantos));

}

private void initTypeface() {

    Typeface myTypeface = Typeface.createFromAsset(getAssets(), "fonts/AftaSerifThin-Regular.otf");
    TextView text = (TextView) findViewById(R.id.toolbarCantos);
    text.setTypeface(myTypeface);

}

}

And here is my last Java File:

public class CustomAdapter extends BaseAdapter {

String[] result;
Context context;
private static LayoutInflater inflater = null;


public CustomAdapter(Cantos cantos, String[] cantos1) {

    result = cantos1;
    context = cantos;
    inflater = (LayoutInflater)context.
            getSystemService(Context.LAYOUT_INFLATER_SERVICE);

}

@Override
public int getCount() {
    return result.length;
}

@Override
public Object getItem(int position) {
    return position;
}

@Override
public long getItemId(int position) {
    return position;
}

public class Holder
{
    TextView tv;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    Holder holder = new Holder();
    View rowView;

    rowView = inflater.inflate(R.layout.cantos_list, null);
    holder.tv = (TextView) rowView.findViewById(R.id.textCanto);
    holder.tv.setText(result[position]);


    return rowView;
}
}

I'm new to android and I'm getting a hang of it, but I need help in things like this. Thanks in advance!

Alex
  • 13
  • 1

2 Answers2

0

If you want to custom font for ListView item, you should try to set font to CustomAdaptor.java class in getView(); Look this code :

public View getView(int position, View convertView, ViewGroup parent) {

    Holder holder = new Holder();
    View rowView;

    rowView = inflater.inflate(R.layout.cantos_list, null);
    holder.tv = (TextView) rowView.findViewById(R.id.textCanto);
    holder.tv.setText(result[position]);

    Typeface myTypeface = Typeface.createFromAsset(context.getAssets(), "fonts/AftaSerifThin-Regular.otf");
    holder.tv.setTypeface(myTypeface);

    return rowView;
}
Holi Boom
  • 1,346
  • 1
  • 12
  • 29
  • THANKS! By any chance, would you know how to make certain listviews items send you to a certain activity? Thanks! – Alex Mar 01 '16 at 04:48
  • setOnItemClickListener to ListView `lv.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView> parent, View view, int position, long id) { Toast.makeText(ListActivity.this, "" + position, Toast.LENGTH_SHORT).show(); } });` – Holi Boom Mar 01 '16 at 04:56
  • Sir thank you for your response but I am not looking for this. I want to know how to code so when I press on a random item in my listview, it'll send me to another activity. Would you know how to do this? – Alex Mar 01 '16 at 05:10
  • yes, but you need to create 11 classes activity for those 11 items – Holi Boom Mar 01 '16 at 06:40
0

You can always change the TypeFace in the adapter's getView(), but if you are going to use that font multiple times in different parts of your code you can always extend the TextView, for that you can refer to:

Using custom font in android TextView using xml

In that way, whenever you need to use a different font, you can use that instead of the regular TextView in the layout file.

Community
  • 1
  • 1
lucianohgo
  • 319
  • 1
  • 9
  • THANKS! By any chance, would you know how to make certain listviews items send you to a certain activity? Thanks! – Alex Mar 01 '16 at 04:49
  • Yeah, of course just start a new activity on the onItemClick if you need a different activity for different positions, just check the position and start that one accordingly. Check http://developer.android.com/training/basics/firstapp/starting-activity.html – lucianohgo Mar 02 '16 at 02:42
  • You should also accept the answer if it helped you and/or vote it up. – lucianohgo Mar 02 '16 at 02:43