shubhamcool shubhamcool - 6 months ago 41
Android Question

How to change and set text color in my listview item by java code

when i am running project then text color of item in list view is going white what is java code for setting color to black . I am pasting my adapter code here :-

public View getView(int position, View view, ViewGroup viewGroup) {
ViewHolder viewHolder = null;
if(view == null){
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.city_item_fragment,null);
viewHolder = new ViewHolder();
///inflator declaration
viewHolder.textViewCityName = (TextView) view.findViewById(R.id.txtViewCityName);
viewHolder.stime = (TextView) view.findViewById(R.id.stime);
viewHolder.endtime = (TextView) view.findViewById(R.id.endtime);
viewHolder.branchof = (TextView) view.findViewById(R.id.branchof);
viewHolder.sectionof = (TextView) view.findViewById(R.id.sectionof);
viewHolder.yearof = (TextView) view.findViewById(R.id.yearof);








view.setTag(viewHolder);
}else{
viewHolder = (ViewHolder) view.getTag();
}
City city = listData.get(position);
/// String cityName = city.getName();
String sttime = city.getStime();
String yearoff = city.getyearof();
String endtime = city.getEndtime();
String branchoff = city.getBranchof();
String sectionoff = city.getSectionof();



/// viewHolder.textViewCityName.setText(cityName);
viewHolder.stime.setText(sttime);
viewHolder.endtime.setText(endtime);
viewHolder.yearof.setText(yearoff);
viewHolder.branchof.setText(branchoff);
viewHolder.sectionof.setText(sectionoff);


return view;
}


and i am pasting my fragment code also

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
String get,msg = null;
Bundle bundle = this.getArguments();
if (bundle != null) {
msg=bundle.getString("msg");
}

Log.d("getting bundle", String.valueOf(msg));

Toast.makeText(getActivity().getApplicationContext(),""+msg,Toast.LENGTH_LONG).show();
// ArrayList<City> cityList = handler.getAllCity();
// mAdapter = new CityAdapter(getActivity().getApplicationContext(),cityList);

}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_item_daylect1, container, false);
list = (AbsListView) view.findViewById(R.id.list);
((AdapterView<ListAdapter>) list).setAdapter(mAdapter);


Log.d("list view2", String.valueOf(list));
View v=getActivity().getCurrentFocus();
Log.d("list view", String.valueOf(v));

handler = new DBHandler(getActivity().getApplicationContext());
try {
handler.open();
} catch (SQLException e) {
e.printStackTrace();
}
int num= handler.getCityCount("monday");
Log.d("count", String.valueOf(num));
NetworkUtils utils = new NetworkUtils(getActivity().getApplicationContext());
if(handler.getCityCount("monday") == 0 && utils.isConnectingToInternet())
{
NetAsync(v);
}
else
{
ArrayList<City> cityList = handler.getAllCity();
adapter = new CityAdapter(getActivity().getApplicationContext(),cityList);
list.setAdapter(adapter);
}

contactList = new ArrayList<HashMap<String, String>>();

personList = new ArrayList<HashMap<String,String>>();

// Set OnItemClickListener so we can be notified on item clicks


return view;
}


Now want that setting of color in listitem should be black .

Answer

There are two solution :- Change in your java code or your adapter code same as previously:-

 @Override
    public View getView(int position, View view, ViewGroup viewGroup) {
        ViewHolder viewHolder = null;
        if(view == null){
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = inflater.inflate(R.layout.city_item_fragment,null);
            viewHolder = new ViewHolder();

            viewHolder.textViewCityName = (TextView) view.findViewById(R.id.txtViewCityName);
            viewHolder.stime = (TextView) view.findViewById(R.id.stime);
            viewHolder.endtime = (TextView) view.findViewById(R.id.endtime);
            viewHolder.branchof = (TextView) view.findViewById(R.id.branchof);
            viewHolder.sectionof = (TextView) view.findViewById(R.id.sectionof);
            viewHolder.yearof = (TextView) view.findViewById(R.id.yearof);

            ///tv.setTextColor(Color.RED);
viewHolder.textViewCityName.setTextColor(Color.BLACK);
            viewHolder.stime.setTextColor(Color.BLACK);
            viewHolder.endtime.setTextColor(Color.BLACK);
            viewHolder.branchof.setTextColor(Color.BLACK);
            viewHolder.sectionof.setTextColor(Color.BLACK);
            viewHolder.yearof.setTextColor(Color.BLACK);

            view.setTag(viewHolder);
        }else{
            viewHolder = (ViewHolder) view.getTag();
        }
        City city = listData.get(position);


        return view;
    }
}

or you can use xml file edits also by editing in your xml of one item also as you are using textview then u can use like these :

  <TextView
        android:id="@+id/yourid"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:textColor="#000000"
        android:text="Title"
        android:textSize="20sp"
        android:textStyle="bold" />

It is just for one item you have 4 to 5 item in one row then you can do like these