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);
}
Related
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;
}
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;
}
}
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.
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 ?
I'm trying to change the background row color of a list view based on a parameter and on the line View v = super.getView(position, convertView, parent) its giving me the error "Cannot directly invoke the abstract method getView(int, View, ViewGroup) for the type Adapter". Here is the all of the code.
public class CustomListViewAdapter extends BaseAdapter
{
LayoutInflater inflater;
List<ListViewItem> items;
public CustomListViewAdapter(Activity context, List<ListViewItem> items) {
super();
this.items = items;
this.inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return items.size();
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
View v = super.getView(position, convertView, parent);
ListViewItem item = items.get(position);
if(convertView==null)
v = inflater.inflate(R.layout.my_hospitals, null);
TextView status = (TextView) v.findViewById(R.id.status);
TextView name = (TextView) v.findViewById(R.id.name);
TextView table = (TextView) v.findViewById(R.id.id);
TextView info = (TextView) v.findViewById(R.id.info);
status.setText(item.status);
name.setText(item.name);
table.setText(item.table);
info.setText(item.info);
if (item.status == "green"){
v.setBackgroundColor(Color.GREEN);
}
return v;
}
}
Change getView()
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
View v=convertView;
ListViewItem item = items.get(position);
if(v==null){
v = inflater.inflate(R.layout.my_hospitals, null);
}
TextView status = (TextView) v.findViewById(R.id.status);
TextView name = (TextView) v.findViewById(R.id.name);
TextView table = (TextView) v.findViewById(R.id.id);
TextView info = (TextView) v.findViewById(R.id.info);
status.setText(item.status);
name.setText(item.name);
table.setText(item.table);
info.setText(item.info);
String state=tem.status;
if (state.equals("green"){
v.setBackgroundColor(Color.GREEN);
}
return v;
}
well like the error says you cannot do this
View v = super.getView(position, convertView, parent);
instead you should be doing
View v = convertView;
then check if v is null
if(v == null){
//get the view
}
Maybe i didn't understand well your question but instead of this
item.status == "green"
Use this
item.status.equals("green")