ListView specific item color - java

I list site addresses with Android listview.I tried for hours, but I couldn't find a solution.
for example
http://site .com
http://site2 .com
http://site3 .com
http://site4 .com
if have http://site3.com in listView i want to change the color of this line.
final ListAdapter adapter = new SimpleAdapter(
MainActivity.this,
resultsdatalist,
R.layout.list_item,
new String[]{"resultid", "result"},
new int[]{R.id.resultid, R.id.result}) {
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = super.getView(position, convertView, parent);
return view;
}
};
lv.setAdapter(adapter);

Try this:
#Override
public View getView (int position, View convertView, ViewGroup parent) {
View view = super.getView(position, convertView, parent);
if(getItem(position).equals("site3.com"))
{
// do something change color
view.setBackgroundColor (Color.RED); // some color
}
else
{
// default state
view.setBackgroundColor (Color.WHITE); // default coloe
}
return view;
}

Related

ArrayAdapter for country flag and image spinner slowing view down

I have the following class:
private class CountryAdapter extends ArrayAdapter<String> {
private LayoutInflater inflater;
CountryAdapter(Context context, int textViewResourceId, ArrayList objects) {
super(context, textViewResourceId, objects);
inflater = getLayoutInflater();
}
#Override
public View getDropDownView(int position, View convertView, #NonNull ViewGroup parent) {
return getCustomDropdown(position, convertView, parent);
}
#NonNull
#Override
public View getView(int position, View convertView, #NonNull ViewGroup parent) {
return getCustomView(position, convertView, parent);
}
View getCustomDropdown(int position, View convertView, ViewGroup parent) {
DropViewHolder dropViewHolder;
if (convertView == null) {
dropViewHolder = new DropViewHolder();
convertView = inflater.inflate(R.layout.country_spinner_row, parent, false);
convertView.setTag(dropViewHolder);
dropViewHolder.label = convertView.findViewById(R.id.spinner_countryNameRow);
dropViewHolder.icon = convertView.findViewById(R.id.spinner_countryFlagRow);
} else {
dropViewHolder = (DropViewHolder) convertView.getTag();
}
dropViewHolder.label.setText(String.format("%s (+%s)", countryNameList.get(position), countryList.optJSONObject(position).optString("phoneCountryCode")));
String flagPath = "flag_" + countryList.optJSONObject(position).optString("countryCode").toLowerCase();
dropViewHolder.icon.setImageResource(getResources().getIdentifier(flagPath, "drawable", getPackageName()));
return convertView;
}
View getCustomView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder;
if (convertView == null) {
viewHolder = new ViewHolder();
convertView = inflater.inflate(R.layout.country_spinner_row_dropdown, parent, false);
convertView.setTag(viewHolder);
viewHolder.icon = convertView.findViewById(R.id.spinner_countryFlagRow);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
String flagPath = "flag_" + countryList.optJSONObject(position).optString("countryCode").toLowerCase(); viewHolder.icon.setImageResource(getResources().getIdentifier(flagPath, "drawable", getPackageName()));
return convertView;
}
private class DropViewHolder {
TextView label;
AdapterImageView icon;
}
private class ViewHolder {
AdapterImageView icon;
}
}
I think it complies with all the standards for an ArrayAdapter, reusing views etc. However when opening the keyboard, the view redraws and there is significant lag on button positioning updating.
Is there anything glaringly obvious I'm missing?
Thanks in advance :)
EDIT: for extra clarity, AdapterImageView is just a class that extends ImageView and overrides requestLayout without calling super to prevent expensive redraws.
EDIT2: By contrast, this piece of code, which is older and in theory worse performing, does not lag the UI.
public class CountryAdapter extends ArrayAdapter<String> {
public CountryAdapter(Context context, int textViewResourceId, ArrayList objects) {
super(context, textViewResourceId, objects);
}
#Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
return getCustomView(position, convertView, parent);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
return getCustomView(position, convertView, parent);
}
public View getCustomView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = getLayoutInflater();
View row = inflater.inflate(R.layout.country_spinner_row, parent, false);
TextView label = (TextView) row.findViewById(R.id.spinner_countryNameRow);
label.setText(countryNameList.get(position).toString());
ImageView icon = (ImageView) row.findViewById(R.id.spinner_countryFlagRow);
String flagPath = "flag_" + countryList.optJSONObject(position).optString("countryCode").toLowerCase();
icon.setImageResource(getResources().getIdentifier(flagPath, "drawable", getPackageName()));
return row;
}
}

How to remove Toolbar goes transparent with a tint

I have made a new method in my app to set certain text and items to colors, as themes. But for some reason when I code it in a OOP way I get the toolbar to be tinted for some odd reason.
With the non OOP method i get:
https://gyazo.com/6084992a654573b85adfe0c01077627e
public void setOffTheme(){
slotsActivity.getToolbar().setTitle("\"Roulette\"");
slotsActivity.getPlaceBetText().setHint("\"Bet\"");
slotsActivity.getInstructions().setText("\"Pick a color\"");
slotsActivity.getStart().setText(" \"Start\" ");
Typeface tf =Typeface.createFromAsset(getResources().getAssets(), "Helvetica Bold.ttf");
slotsActivity.getInstructions().setTypeface(tf);
slotsActivity.getThemeImage().setImageResource(R.drawable.offtemplate);
slotsActivity.getmConstraintLayout().setBackgroundColor(getTheColor( R.color.colorAccent));
slotsActivity.getColorSpinner().setBackgroundColor(getTheColor( R.color.colorOffWhite));
slotsActivity.getInstructions().setTextColor(getTheColor(R.color.colorGucciBlack));
slotsActivity.getPlaceBetText().setTypeface(tf);
slotsActivity.getPlaceBetText().setTextColor(getTheColor(R.color.colorGucciBlack));
slotsActivity.getPlaceBetText().setBackgroundTintList(ColorStateList.valueOf(getResources().getColor(R.color.colorGucciBlack) ));
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, R.layout.my_spinner_style, slotsActivity.getBettingColors()){
//changes color of the drop down and selected
public View getView(int position, View convertView, ViewGroup parent) {
View v = super.getView(position, convertView, parent);
((TextView) v).setTextSize(16);
((TextView) v).setTextColor(getResources().getColorStateList(R.color.colorGucciBlack) );
((TextView) v).setBackgroundColor(Color.TRANSPARENT);
return v;
}
public View getDropDownView(int position, View convertView, ViewGroup parent) {
View v = super.getDropDownView(position, convertView, parent);
((TextView) v).setTextColor(getResources().getColorStateList(R.color.colorGucciBlack) );
v.setBackgroundColor(ContextCompat.getColor(settingsActivity.this, R.color.colorOffWhite));
((TextView) v).setGravity(Gravity.LEFT);
return v;
}
};
slotsActivity.getColorSpinner().setAdapter(dataAdapter);
slotsActivity.getStart().setBackgroundResource(R.drawable.buttonshapeoff);
slotsActivity.getStart().setTextColor(getTheColor(R.color.colorGucciBlack));
slotsActivity.getStart().setTypeface(tf);
slotsActivity.getToolbar().setBackgroundColor(getTheColor(R.color.colorOffWhite));
}
While the OOP code shows: https://gyazo.com/15e2ded1ccdc5a7514c370651c638499
//this is the call to the method
setTheme(R.color.colorAccent, R.color.colorOffWhite, R.color.colorGucciBlack, R.drawable.buttonshapeoff,R.drawable.offtemplate,"\"Roulette\"","\"Bet\"","\"Pick a color\"", " \"Start\" ","Helvetica Bold.ttf");
// this is the method
public void setTheme(final int backgroundColor, final int primaryColor, final int textColor,int buttonShape, int templete,String title,String betName,String pickName,String startName,String typrface){
Typeface tf =Typeface.createFromAsset(getResources().getAssets(), typrface);
slotsActivity.getInstructions().setTypeface(tf);
slotsActivity.getToolbar().setTitle(title);
slotsActivity.getPlaceBetText().setHint(betName);
slotsActivity.getInstructions().setText(pickName);
slotsActivity.getStart().setText(startName);
slotsActivity.getThemeImage().setImageResource(templete);
slotsActivity.getmConstraintLayout().setBackgroundColor(getTheColor( backgroundColor));
slotsActivity.getColorSpinner().setBackgroundColor(getTheColor( primaryColor));
slotsActivity.getInstructions().setTextColor(getTheColor(textColor));
slotsActivity.getPlaceBetText().setTypeface(tf);
slotsActivity.getPlaceBetText().setTextColor(getTheColor(textColor));
slotsActivity.getPlaceBetText().setBackgroundTintList(ColorStateList.valueOf(getResources().getColor(textColor) ));
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, R.layout.my_spinner_style, slotsActivity.getBettingColors()){
//changes color of the drop down and selected
public View getView(int position, View convertView, ViewGroup parent) {
View v = super.getView(position, convertView, parent);
((TextView) v).setTextSize(16);
((TextView) v).setTextColor(getResources().getColorStateList(textColor) );
((TextView) v).setBackgroundColor(Color.TRANSPARENT);
return v;
}
public View getDropDownView(int position, View convertView, ViewGroup parent) {
View v = super.getDropDownView(position, convertView, parent);
((TextView) v).setTextColor(getResources().getColorStateList(textColor) );
v.setBackgroundColor(ContextCompat.getColor(settingsActivity.this, primaryColor));
((TextView) v).setGravity(Gravity.LEFT);
return v;
}
};
slotsActivity.getColorSpinner().setAdapter(dataAdapter);
slotsActivity.getStart().setBackgroundResource(buttonShape);
slotsActivity.getToolbar().setBackgroundColor(primaryColor);
}

View v is unreachable statement

Please help me.
Is this an error or a warning?
How can I solve this?
Error is that the View v is unreachable statement
public View getView(int position, #Nullable View convertView, #NonNull ViewGroup parent) {
return super.getView(position, convertView, parent);
View v;
v = convertView;
LayoutInflater li = LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = li.inflate(R.layout.custom_layout, null);
TextView champ = (TextView)v.findViewById(R.id.textView);
TextView year = (TextView)v.findViewById(R.id.textView2);
ImageView pic = (ImageView)v.findViewById(R.id.imageView);
champ.setText(adaptArray.get(position).getChamp()); year.setText(adaptArray.get(position).getYear()); pic.setImageResource(adaptArray.get(position).getSport());
return v;
}
Anything else is written after the return keyword it's unreachable.
Remove return super.getView(position, convertView, parent); from the first line of your function.
This is a warning, telling you that static analysis of the code shows that some of your code will never be reached, because the method returns before.
The following is how you can resolve the issue and make proper use of the convertView by passing it to super.getView(...):
public View getView(int position, #Nullable View convertView, #NonNull ViewGroup parent) {
View v = super.getView(position, convertView, parent);
if(v == null){
LayoutInflater li = LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = li.inflate(R.layout.custom_layout, null);
}
TextView champ = (TextView)v.findViewById(R.id.textView);
TextView year = (TextView)v.findViewById(R.id.textView2);
ImageView pic = (ImageView)v.findViewById(R.id.imageView);
champ.setText(adaptArray.get(position).getChamp());
year.setText(adaptArray.get(position).getYear());
pic.setImageResource(adaptArray.get(position).getSport());
return v;
}
Remove this
return super.getView(position, convertView, parent);
It's because of this line of code at the Top of the method:
return super.getView(position, convertView, parent);
When you return from a method, the rest of the code inside the method doesn't run.

getView Custom Spinner Android not Called?

I want to create custom spinner and this is CustomSpinnerAdapter :
#Override
public int getCount() {
// TODO Auto-generated method stub
return (data == null) ? 0 : data.size();
}
#Override
public long getItemId (int position) {
return position;
}
#Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
return getCustomView(position, convertView, parent);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
return getCustomView(position, convertView, parent);
}
public View getCustomView(int position, View convertView, ViewGroup parent) {
View row = inflater.inflate(R.layout.spinner_item_ip, parent, false);
TextView ip = (TextView) row.findViewById(R.id.ip);
ip.setText(data.get(position).toString());
return row;
}
and implement in Fragment
List<String> dataIpList = new ArrayList<String>();
dataIpList.add("192.168.1.1");
dataIpList.add("192.168.1.2");
dataIpList.add("192.168.1.3");
dataIpList.add("192.168.1.4");
_spinnerIpAdapter = new spinnerIpAdapter(getActivity(), R.layout.spinner_item_ip, dataIpList);
publicIP.setAdapter(_spinnerIpAdapter);
the result is getDropDownView us called , but getView never called .
so how to fix it ?

Adding a single string from an array list to TextView

My Code: starts with after onCreate Override then the the bottom portion is my ArrayAdapter. Im trying to add individual strings from the array to a TextView. When I run the app it puts all the strings in the array on every TextView it makes. Thanks for taking a look.
MyListAdapter adapter = new MyListAdapter(mMyStringArrayArray());
setListAdapter(adapter);
...
private class MyListAdapter extends ArrayAdapter<String> {
public MyListAdapter(ArrayList<String> strings) {
super(getActivity(), 0, strings);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// If we weren't given a view, inflate one
if (convertView == null) {
convertView = getActivity().getLayoutInflater().inflate(R.layout.my_list, null);
}
LinearLayout mLinearLayout = (LinearLayout)convertView.findViewById(R.id.my_list_linearLayout);
TextView[] strings = new TextView[mMyStringArray.size()];
for (int i = 0; i < mMyStringArray().size(); i++) {
// Create TextView
strings[i] = new TextView(getActivity());
strings[i].setText(mMyStringsArray(i));
strings[i].setId(i + 5);
mLinearLayout.addView(strings[i]);
}
return convertView;
}
}
getView() is called once for each item in your Adapter. So, with your for loop, that is running through each item in mMyStringArray for every item in your Adapter. Try instead to use position to get the element you need. Something like
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// If we weren't given a view, inflate one
if (convertView == null) {
convertView = getActivity().getLayoutInflater().inflate(R.layout.my_list, null);
}
LinearLayout mLinearLayout = (LinearLayout)convertView.findViewById(R.id.my_list_linearLayout);
TextView[] strings = new TextView[mMyStringArray.size()];
// Create TextView
strings[position] = new TextView(getActivity());
strings[position].setText(mMyStringsArray(i));
strings[position].setId(position + 5);
mLinearLayout.addView(strings[position]);
}
return convertView;
}
Something like that should work. However, since you have the layout it would be much easier to just create a TextView inside your LinearLayout of my_list.xml and use that.
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// If we weren't given a view, inflate one
if (convertView == null) {
convertView = getActivity().getLayoutInflater().inflate(R.layout.my_list, null);
}
LinearLayout mLinearLayout = (LinearLayout)convertView.findViewById(R.id.my_list_linearLayout);
TextView tv = (TextView) convertView.findViewById(R.id.someID) //where someID is the id you give the TextView in your layout file
tv.setText(mMyStringsArray(position));
tv.setId(position + 5); // Shouldn't need this now
}
return convertView;
}
I also suggest watching The world of ListView

Categories