2

I am trying to write my first app- a simple application where the user enters a hex color code (EditText), hits enter (Button), and the background color of a ImageView is changed to the user's entered hex code. How I see it, I will have to gettext from the edittext, write the gettext to a file, then edit the file to append 0xAA before the hex code so that it can be entered in ImageView.setBackgroundColor(0xAA"HEXHEX"). Please let me know how I can do this, or if that is even the correct way to do this.

Here is my java so far (on button click, bg color changes to white, clear reverts it to black)

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.Button;
import android.widget.ImageView;
import android.view.View;

public class ChkhexActivity extends Activity {
    private EditText hex;
    private Button chk;
    private Button clear;
    private ImageView view;
    /** Called when the activity is first created. */
    @Override

     public void onCreate(Bundle savedInstanceState)
     {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.main);
          initControls();
     }

     private void initControls()
     {
          hex = (EditText)findViewById(R.id.hex);
          chk = (Button)findViewById(R.id.chk);
          clear = (Button)findViewById(R.id.clear);
          view = (ImageView)findViewById(R.id.view);
          chk.setOnClickListener(new Button.OnClickListener() { public void onClick (View v){ Chk(); }});
          clear.setOnClickListener(new Button.OnClickListener() { public void onClick (View v){ Clear(); }});
     }

     private void Chk()
     {  
          view.setBackgroundColor(0xFFffffff);
     }

     private void Clear()
     {
          hex.setText("");
          view.setBackgroundColor(0xFF000000);
     }
}
ScottJShea
  • 7,041
  • 11
  • 44
  • 67

2 Answers2

1

Very good exercise for a beginner.

Use Color.parseColor(). Don't forget to validate the Input first!

view.setBackgroundColor(Color.parseColor(edt.getText().toString()));
st0le
  • 33,375
  • 8
  • 89
  • 89
  • When I try this and run the app, upon clicking the check button, I get the error: Unfortunately, your app has stopped. I get the same error with Sandhya.M's method as well. If I try to debug, I get a Color.class error asking to attach android.jar to a source. I just updated my sources for 4.0 and 4.0.3 and directed it to the source (/platforms/android-14/), but the error won't go away. If I continue, I also get a ZygoteInit$MethodAndArgsCaller source not found error. – killgriff.android Feb 17 '12 at 10:32
  • @killgriff.android, Of course you have to prepend `0x` to the value ...try typing 0x777777 – st0le Feb 18 '12 at 09:34
  • @stOle, Even with 0x it still stops the app. – killgriff.android Feb 19 '12 at 01:13
  • @killgriff.android, post the stacktrace of tehe exception – st0le Feb 19 '12 at 09:10
  • Here's my Logcat output: http://killiangriffin.info/log.txt The color input was 0xffffff. I've tried just ffffff, "black", etc... Nothing seems to be accepted. – killgriff.android Feb 20 '12 at 07:07
  • @killgriff.android, apparently, the format should be `#RRGGBB` or `#AARRGGBB`. Just checked. – st0le Feb 20 '12 at 11:23
0
every time u want to change the color of imageview based on text entered in edittext.so u cant fix it like this 
  view.setBackgroundColor(0xFFffffff);
 u have to get the text from edittext.some example like this..

public class test extends Activity{
private EditText ed;
private Button btn;
private ImageView iv;
 @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.test);

        ed=(EditText)findViewById(R.id.editText1);
        btn=(Button)findViewById(R.id.button1);
        iv=(ImageView)findViewById(R.id.imageView1);
        btn.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub
                String value=ed.getText().toString();
                iv.setBackgroundColor(Color.parseColor(value));
            }
        });

 }

 }
the edittext text u entered can  be like hex format example like #B0171F,#fafad2,..
user1213202
  • 1,305
  • 11
  • 23