L
L
LuVairo2021-02-14 16:17:08
Java
LuVairo, 2021-02-14 16:17:08

How to solve the problem with programmatically setting the background for RelativeLayout?

Hello. I decided to make two alternative themes in my application, like dark and light. To do this,
I try to change the background property of the main RelativeLayout programmatically, but the application keeps crashing or
not responding to clicks at all.
Ps: I use a gradient.xml as a background, and the click is tracked on a textView widget.
Maybe someone had a similar problem or I make a mistake.
Thanks in advance!

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;


public class MainActivity extends Activity {

    TextView mark_text;
    RelativeLayout main_layout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mark_text = (TextView)findViewById(R.id.textView);  // textView нажатие на который обрабатываем
        main_layout = (RelativeLayout)findViewById(R.id.mlayout);  // Relative Layout

        mark_text.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                main_layout.setBackgroundResource(R.drawable.background_gradient_dark);
            }
        });
    }

}

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