mahsa
mahsa

Reputation: 51

changing font size and text size of listview

In my application I have a list view that has an image-button and a text-view. I want to change font size and font size of my list view by choice of user. My list view extends from ArrayAdapter. here ARE my code:

public class ListAdapter extends ArrayAdapter<String> {
customButtonListener customListner;





public interface customButtonListener {
    public void onButtonClickListner(int position,String value);
}

public void setCustomButtonListner(customButtonListener listener) {
    this.customListner = listener;
}

private Context context;

private ArrayList<String> data = new ArrayList<String>();

public ListAdapter(Context context, ArrayList<String> dataItem) {
    super(context, R.layout.child_listview, dataItem);
    this.data = dataItem;
    this.context = context;
}

@Override
public View getView(final int position, View convertView, ViewGroup parent)     {
    ViewHolder viewHolder;
    if (convertView == null) {
        LayoutInflater inflater = LayoutInflater.from(context);
        convertView = inflater.inflate(R.layout.child_listview, null);

        viewHolder = new ViewHolder();

        viewHolder.text = (TextView) convertView
                .findViewById(R.id.childTextView);

       // textView= (TextView) convertView.findViewById(R.id.childTextView);
      //  viewHolder.text = textView;
        viewHolder.Button = (ImageButton) convertView
                .findViewById(R.id.childButton);
        viewHolder.text.setTextColor(Color.BLUE);
        convertView.setTag(viewHolder);
    } else {
        viewHolder = (ViewHolder) convertView.getTag();
    }
    final String temp = getItem(position);
    viewHolder.text.setText(temp);
    viewHolder.Button.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            if (customListner != null) {
                customListner.onButtonClickListner(position,temp);
            }

        }
    });

    return convertView;
}

public class ViewHolder {
    TextView text;
    ImageButton Button;
}

and my main activity:

    public class MainActivity extends Activity implements
    customButtonListener {

private ListView listView;
public TextView textView;

ListAdapter adapter;
ArrayList<String> dataItems = new ArrayList<String>();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //G.currentActivity = this;
    setContentView(R.layout.activity_main);
    String[] dataArray = getResources().getStringArray(R.array.listdata);
    List<String> dataTemp = Arrays.asList(dataArray);
    dataItems.addAll(dataTemp);
    listView = (ListView) findViewById(R.id.listView);


   // tx.setTypeface(G.defaultFont);
    adapter = new ListAdapter(MainActivity.this, dataItems);
    adapter.setCustomButtonListner(MainActivity.this);
    listView.setAdapter(adapter);


}

@Override
public void onButtonClickListner(int position, String value) {

    Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
    sharingIntent.setType("text/plain");
    String shareBody = dataItems.get(position);
    sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Subject Here");
    sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareBody);
    startActivity(Intent.createChooser(sharingIntent, "Share via"));
}

how i can do this? any suggestion?

Upvotes: 1

Views: 2518

Answers (4)

Soteck
Soteck

Reputation: 51

I suggest you to create a custom view.

Just create a layout with some textViews and ID's (and if you want, some imageviews) and a class.

IE:

On layout folder: layout_custom_list.xml

On src/package foldeR: custom_list_view

public CustomViewAdapter(Context context, String text1, String text2) {
    super(context);
    LayoutInflater inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    inflater.inflate(R.layout.layout_custom_list, this, true);
    tvText1 = (EditText) findViewById(R.id.textView1);
    tvText2 = (EditText) findViewById(R.id.textView2);
    tvText1.setText(text1);
    tvText2.setText(text2);}

And then on the main activity, you must have a layout and run this code:

layout = (RelativeLayout) findViewById(R.id.layoutToAddList);
CustomViewAdapter newView = new CustomViewAdapter(this, "text1", "text2");
layout.addView(newView);

and that's it!

Upvotes: 0

Praveen B. Bhati
Praveen B. Bhati

Reputation: 410

Also note that if the textSize is set in code, calling textView.setTextSize(X) interprets the number (X) as SP. Use setTextSize(TypedValue.COMPLEX_UNIT_DIP, X) to set values in dp.

Upvotes: 1

themoonraker13
themoonraker13

Reputation: 69

You might be having a xml layout file defining the layout of your list view item. You can always modify the text size of the textview you might have used in the list view item layout.

For example,

android:textSize="somedp"

Upvotes: 0

Fahim
Fahim

Reputation: 12358

You can set text size with

textView.setTextSize(16) //change value as per your need

set font using

Typeface custom_font = Typeface.createFromAsset(getAssets(), "fonts/font name.ttf");

textView.setTypeface(custom_font);

Upvotes: 0

Related Questions