Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Newcomers » Newcomers » Change color off button on click(How do i change the color of a button when clicked)
Change color off button on click [message #1048125] Wed, 24 April 2013 04:42 Go to next message
pascal Schuffelers is currently offline pascal Schuffelers
Messages: 6
Registered: April 2013
Junior Member
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.
Re: Change color off button on click [message #1048610 is a reply to message #1048125] Wed, 24 April 2013 18:20 Go to previous message
Nitin Dahyabhai is currently offline Nitin Dahyabhai
Messages: 2314
Registered: July 2009
Senior Member
Alas, this isn't an Android Development support forum, it's for questions about using Eclipse and projects hosted at Eclipse, and sometimes even tools built on top of Eclipse. You'll find links to more appropriate locations in the sticky article http://www.eclipse.org/forums/index.php/t/225513/ . Good luck.

---
Nitin Dahyabhai
Eclipse WTP, IBM
Previous Topic:On Load App code
Next Topic:Eclipse RCP
Goto Forum:
  


Current Time: Fri Oct 31 07:54:40 GMT 2014

Powered by FUDForum. Page generated in 0.01498 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software