Search code examples
androidbuttondynamicandroid-linearlayout

Is there a way to change background color behind a button (button.setBackgroundResource)


I'm a new Android dev student, and I'm trying to create a dynamic layout witch contains a TextView and a button inside the same row.

but I have a little problem. I set my Button in my Drawables ressources by

 button.setBackgroundResource(R.drawable.ic_comment_black_48px);

and now, I cannot change the backgroundcolor behind it.

I have created a newLinearlayout inside my main LinearLayout and have created a new textView and a new Button. I have put them inside the LinearLayout's child and put it inside the main. That's work but not the background color behind my button.

is there a way to do that?

my xml layout

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="@color/grey"
    android:id="@+id/historyLayout">

</LinearLayout>

my complete activity

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

        mLinearLayout = findViewById(R.id.historyLayout);

        mMoodSaved = new ArrayList(7); // Define the max size of my ArrayList
        loadData();
        for (int i = 1; i <= 7; i++) {
            final TextView textView = new TextView(this);
            mLinearLyt = new LinearLayout(this);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
            mLinearLyt.setOrientation(LinearLayout.HORIZONTAL);
            textView.setHeight(300);
            textView.setWidth(400);
            textView.setBackgroundColor(Color.RED);
            textView.setTextColor(Color.BLACK);
            textView.setTextSize(12);
            textView.setText(String.valueOf(i));
            mLinearLyt.setBackgroundColor(Color.YELLOW);
            mLinearLyt.addView(textView);
            mLinearLyt.setLayoutParams(params);


            ImageButton button = new ImageButton(this);
            LinearLayout.LayoutParams param2 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
            param2.setMargins(20,50,0,0);
            param2.height = 100;
            param2.width = 100;
            button.setLayoutParams(param2);
            button.setBackgroundResource(R.drawable.ic_comment_black_48px);
            button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    soundConfirm();
                    Toast.makeText(getApplicationContext(), textView.getText(), Toast.LENGTH_LONG).show(); //Display Toast Message
                }
            });
            mLinearLyt.addView(button);
            mLinearLayout.addView(mLinearLyt);
        }
    }

Solution

  • Since this an ImageButton, set

    button.setImageResource(R.drawable.ic_comment_black_48px)
    

    instead of setBackgroundResource.