简体   繁体   中英

How to change layout colour on java by hex?

I want to change layout colour according to users answer. There is an EditText on screen. When user write a hex code in there, background color turns to users code.

EditText hex;
Button ok;

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

    hex = (EditText)findViewById(R.id.hex);
      // User writes like that "#232323"

    ok = (Button)findViewById(R.id.btn);


    ok.setOnClickListener(new OnclickListener){
      //I want to make background that code
    }




}

How can I make that?

EditText hex;
Button ok;
RelativeLayout baselayout;

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

    hex = (EditText)findViewById(R.id.hex);
    ok = (Button)findViewById(R.id.btn);
    baseLayout=(RelativeLayout)findViewById(R.id.layout);

    ok.setOnClickListener(new OnclickListener){
        @Override
        public void onClick(View v)
           {
            String colorString=hex.getText().toString();

           //Validate Color before setting
            try {
                Color filteredColor = Color.parseColor(colorString);
                baseLayout.setBackgroundColor(filteredColor);
                } 
           catch (IllegalArgumentException iae) {
                // This color string is not valid
                }            
           }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM