Change background color for Textview

Asked

Viewed 3,334 times

2

I would like to know how to change a background of a text view, because I’m pulling the data from the site parse.with, but I can’t do it, what I’m trying to do is this:

          public class Pizzarias extends ActionBarActivity {

// Declare Variables
Boolean bColorStatus = true;
TextView status;
ListView listview;
List<ParseObject> ob;
ProgressDialog mProgressDialog;
ListViewAdapterPizzarias adapter;
private List<WorldPopulation> worldpopulationlist = null;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pizzarias);


new RemoteDataTask().execute();
//getSupportActionBar().setDisplayHomeAsUpEnabled(true);

status = (TextView) findViewById(R.id.status);
// status.setBackgroundColor(Color.BLUE);


}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_pizzarias, menu);

//Os metodos abaixo são para mostrar o icone do aplicativo na action bar
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setLogo(R.drawable.ic_launcher);
getSupportActionBar().setDisplayUseLogoEnabled(true);

return true;
}


/*@Override
public boolean onOptionsItemSelected(MenuItem item) {
onBackPressed();
return true;
}*/


//RemoteDataTask AsyncTask
private class RemoteDataTask extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
    super.onPreExecute();
    // Create a progressdialog
    mProgressDialog = new ProgressDialog(Pizzarias.this);
    // Set progressdialog title
    mProgressDialog.setTitle("Carregando Pizzarias");
    // Set progressdialog message
    mProgressDialog.setMessage("Aguarde...");
    mProgressDialog.setIndeterminate(false);
    // Show progressdialog
    mProgressDialog.show();
}

@Override
protected Void doInBackground(Void... params) {
    // Create the array
    worldpopulationlist = new ArrayList<WorldPopulation>();
    try {
        // Locate the class table named "Country" in Parse.com
        ParseQuery<ParseObject> query = new ParseQuery<ParseObject>(
                "GerenciarPizzariasPatos");
        // Locate the column named "ranknum" in Parse.com and order list
        // by ascending,


//here is the implementation to search for the word "closed" and change the background color.
        query.whereStartsWith ( "status" , "fechado" );
        ob = query.find();
        if(query.equals("fechado")){
            status.setTextColor(getResources().getColor(R.color.red));
            bColorStatus = false;
        }
        else {
            status.setTextColor(getResources().getColor(R.color.green));
            bColorStatus = true;
        }

        query.orderByAscending("nome");
        query.equals("status");
        ob = query.find();
        for (ParseObject country : ob) {
            WorldPopulation map = new WorldPopulation();
            map.setNome((String) country.get("nome"));
            map.setEndereco((String) country.get("endereco"));
            map.setTelefone((String) country.get("telefone"));
            map.setStatus((String) country.get("status"));
            worldpopulationlist.add(map);
        }
    } catch (ParseException e) {
        Log.e("Error", e.getMessage());
        e.printStackTrace();
    }
    return null;
}

@Override
protected void onPostExecute(Void result) {
    // Locate the listview in listview_main.xml
    listview = (ListView) findViewById(R.id.listviewpizzarias);
    // Pass the results into ListViewAdapter.java
    adapter = new ListViewAdapterPizzarias(Pizzarias.this,
            worldpopulationlist);
    // Binds the Adapter to the ListView
    listview.setAdapter(adapter);
    // Close the progressdialog
    mProgressDialog.dismiss();
}
}

Could someone help?

  • Have you tried the method setBackgroundResource with the color in a Resource?

  • @Wakim. I haven’t tried, but I’ll see how I can do it. Thank you

  • @Wakim. This part where I compare the query, is it correct? Because I took the data from parse.com, I implemented but I don’t know if it is totally correct.

  • 1

    I can’t help much because I’ve never used Parse, but you can debug to see the value and know if you’re comparing it correctly.

  • @Lucas Moresco. Could you help with my question?

1 answer

1

txt.setBackgroundColor(Color.parseColor("#BABABA")); 

or

txt.setBackgroundColor(Color.RED); // set default RED color as background color
  • Boy, I tried to do that, but I couldn’t. I am using a data from parse.com, then I would like to take this data and if it has the string open the background of the text turns green, closed case turns red. It’s complicated, but I’m trying everything. I edited the question.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.