Hello everyone,
I have made a program for my android gsm and i have a question about how i can change a buttons color when clicked thats made in xml.
For example i have this part of xml.:
android:id="@+id/gpiotxt3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="GPIO 0"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#FFCC00" />
In my java part i get a value from my database and want to change the color of the button if the value is 1.
This is the part where , if possible, i want the code to change the color button.:
public void getData1(){
String result = "";
InputStream isr = null;
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("YOUR PHP SCRIPT ADDRESS"); //YOUR PHP SCRIPT ADDRESS
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
isr = entity.getContent();
}
catch(Exception e){
Log.e("log_tag", "Error in http connection "+e.toString());
Toast.makeText(gpiomain.this,"Couldnt connect to database", Toast.LENGTH_LONG).show();
}
//convert response to string
try{
BufferedReader reader = new BufferedReader(new InputStreamReader(isr,"iso-8859-1"),8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
isr.close();
result=sb.toString();
}
catch(Exception e){
Toast.makeText(gpiomain.this,"Error converting result "+e, Toast.LENGTH_LONG).show();
}
//parse json data
try {
String s = "";
JSONArray jArray = new JSONArray(result);
for(int i=0; i<jArray.length();i++){
JSONObject json = jArray.getJSONObject(i);
s = s +
"gpio port : "+json.getString("gpiopoort")+"\n"+
"on=1/off=0 : "+json.getString("aan_uit")+"\n";
if (json.getString("aan_uit").equals("0")) {
Toast.makeText(gpiomain.this,"GPIO 0 is off ", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(gpiomain.this,"GPIO 0 is on", Toast.LENGTH_LONG).show();
}
}
} catch (Exception e) {
Toast.makeText(gpiomain.this," "+e, Toast.LENGTH_LONG).show();
}
}
So the part that desides if the color of the button must be change is.:
if (json.getString("aan_uit").equals("0")) {
Toast.makeText(gpiomain.this,"GPIO 0 is off ", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(gpiomain.this,"GPIO 0 is on", Toast.LENGTH_LONG).show();
}
Could anyone help me please?
Best Regards
Pascal.