1

I am parsing a JSON data from a URL. i am using this example to parse the data i successfully parsed the data, now i want a little bit thing that if the product available i want to keep this in green color but if the product available i want the keep that name in some other color.

my Data for the availability is.

this is my json data for available

       {
          "id":"1234",
          "price": "311.95",
          "seller": "Shopclues.com",
          "availability": "Available",
          "currency": "USD"
        }

this is for not avaliable

        {
          "id":"4321"
          "price": "290.05",
          "seller": "Tradus.com",
          "availability": "Not Available",
          "currency": "USD"
        }

So i want the data like if product is avaliable i want that "id":"1234" should be in green colour other wise it should some other color like black or gray or red.

i tried with normal data but this i am getting forma URL so give me any suggestion for dynamic JSON data which is getting from a url.

4 Answers4

2
JSONObject jObj = new JSONObject(String);
if (jObj.getString("availability").equalsIgnoreCase("Not Available")) {
       //textcolor = red
} else {
       //textcolor = green
}
Kalyaganov Alexey
  • 1,701
  • 1
  • 16
  • 23
  • Thanks you Kalyaganov Alexey sir , but where did i need to diclare the color wether in xml or class file and one more doubht sir if i keep this condition did only ID will change to that color or entire data. i am trying but i seen that no changes made.. please let me know sir.. –  Dec 25 '13 at 05:56
  • You can put the color in xml file and can refer from there also you can set the color code directly in the `setColor` method also in hexadecimal format. @user3124880 – GrIsHu Dec 25 '13 at 06:02
  • sir is there any example please let me know i tried with offline application but i want with online applications..please help sir –  Dec 25 '13 at 06:05
2

If you want to change part of a string, then use Spannable in android. Take a look at this post for an example. Otherwise TextView has a setTextColor method which takes an int. This int is NOT the resource id. It should be used as follows textView1.setTextColor(getResources().getColor(R.color.mycolor)) or textView1.setTextColor(Color.RED)

Community
  • 1
  • 1
Ali
  • 12,354
  • 9
  • 54
  • 83
  • Its ok Ali sir but i want to remove availability from that string because i dont have place to display that so i want to keep that if this id is available i want to keep that id in green if not there i need to show some other color i am parsing the data from a url.. –  Dec 25 '13 at 06:02
  • It sounds like you are displaying the above JSON. In that cause you have 2 options, either read the JSON into a JSONObject and remove the availability key from the JSONObject and print it out again. Or just use `String.indexOf()` to check if the `"availability:"Available"` exists and use `String.replace()` to remove it. Since you use indexOf to check if it existed, you can use that to set the color as well. – Ali Dec 25 '13 at 06:08
2
JSONObject jObj = new JSONObject(String);
if (jObj.getString("availability").equalsIgnoreCase("Not Available")) {
     textView1.setTextColor(Color.RED)
} else {
      textView1.setTextColor(Color.GREEN)
}
Rizwan
  • 1,461
  • 12
  • 26
1

Try this out..

     JSONObject object= new JSONObject(String);
     if (object.getString("availability").equalsIgnoreCase("Available")) {
           textView1.setTextColor(Color.GREEN)
     } else {
           textView1.setTextColor(Color.RED)
     }
Gowtham Raj
  • 2,915
  • 1
  • 24
  • 38