T
T
TorentoLowdons2022-03-15 07:01:59
Android
TorentoLowdons, 2022-03-15 07:01:59

Why doesn't the color of the button change when the user creates it?

Good afternoon!
What's wrong with the code? When you click on the buttons, there are no errors and the application does not crash. Initially, I wanted to make the user select the position of the button and its color. Everything is fine with the position, but the color of the button does not change and the application does not crash and there are no errors. Help!

package com.example.createviews;

import static com.example.createviews.R.color.blue;
import static com.example.createviews.R.color.green;
import static com.example.createviews.R.color.red;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.content.res.Resources;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioGroup;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    LinearLayout crZone;
    RadioGroup rGroup;
    RadioGroup rColor;
    Button btnCreate;
    Button btnClear;
    EditText edName;

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

        Resources resources = getResources();
        int red = resources.getColor(R.color.red);
        int blue = resources.getColor(R.color.blue);
        int green = resources.getColor(R.color.green);

        crZone = findViewById(R.id.crZone);
        rGroup = findViewById(R.id.rGroup);
        rColor = findViewById(R.id.rColor);
        btnClear = findViewById(R.id.btnClear);

        btnCreate = findViewById(R.id.btnCreate);
        edName = findViewById(R.id.edName);

        btnCreate.setOnClickListener(this);
        btnClear.setOnClickListener(this);
    }

    @SuppressLint("ResourceAsColor")
    @Override
    public void onClick(View v) {


        switch (v.getId()){
            case R.id.btnCreate:
                LinearLayout.LayoutParams lParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                int btnGravity = Gravity.LEFT;

                switch (rGroup.getCheckedRadioButtonId()){
                    case R.id.rLeft:
                        btnGravity = Gravity.LEFT;
                        break;
                    case R.id.rCentre:
                        btnGravity = Gravity.CENTER_HORIZONTAL;
                        break;
                    case R.id.rRight:
                        btnGravity = Gravity.RIGHT;
                        break;
                }

                lParams.gravity = btnGravity;

                Button btn = new Button(this);
                btn.setText(edName.getText().toString());

                switch (rColor.getCheckedRadioButtonId()){
                    case R.id.rRed:
                        btn.setBackgroundColor(red);
                        break;
                    case R.id.rBlue:
                        btn.setBackgroundColor(blue);
                        break;
                    case R.id.rGreen:
                        btn.setBackgroundColor(green);
                        break;
                    default:
                }

                crZone.addView(btn, lParams);

                Toast.makeText(MainActivity.this, "View created", Toast.LENGTH_SHORT).show();

                break;

            case R.id.btnClear:
                crZone.removeAllViews();
                Toast.makeText(MainActivity.this, "Views deleted", Toast.LENGTH_SHORT).show();

                break;
        }
    }
}

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question