0

I'm trying to change the color of my background of view when I click on a button but the app won't open with the code I have right now I don't know what the problem is..

Code:

package on.click.button;

import android.support.v7.app.ActionBarActivity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;

public class MainActivity extends ActionBarActivity {

        private ImageButton imagebutton;
        private ImageButton imagebuttonRED;
        private ImageButton imagebuttonBLUE;
        private ImageButton imagebuttonYELW;
        private ImageButton imagebuttonGRN;
        private View layout;


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


            imagebuttonRED = (ImageButton) findViewById(R.id.imageButton1);
            imagebuttonBLUE = (ImageButton) findViewById(R.id.imageButton3);
            imagebuttonYELW = (ImageButton) findViewById(R.id.imageButton2);
            imagebuttonGRN = (ImageButton) findViewById(R.id.imageButton4);

            layout = (View) findViewById(R.layout.activity_main);

            imagebutton.setOnClickListener(new OnClickListener() {

                public void onClick(View v) {
                    setContentView(R.layout.activity_main);
                }
            });

            imagebuttonRED.setOnClickListener(new OnClickListener() {

                public void onClick(View v) {
                        setContentView(layout);
                    layout.setBackgroundColor(Color.GREEN);
                }
            });

            imagebuttonBLUE.setOnClickListener(new OnClickListener() {

                public void onClick(View v) {
                        setContentView(layout);
                    layout.setBackgroundColor(Color.RED);
                }
            });
            imagebuttonYELW.setOnClickListener(new OnClickListener() {

                public void onClick(View v) {
                        setContentView(layout);
                    layout.setBackgroundColor(Color.YELLOW);
                }
            });
            imagebuttonGRN.setOnClickListener(new OnClickListener() {

                public void onClick(View v) {
                        setContentView(layout);
                    layout.setBackgroundColor(Color.BLUE);
                }
            });

}
}

main_activity.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="nl.aventus.delaatstestap.MainActivity" >

    <ImageButton
        android:id="@+id/imageButton3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/imageButton2"
        android:layout_marginLeft="14dp"
        android:layout_toRightOf="@+id/imageButton2"
        android:src="@drawable/blauw" />

    <ImageButton
        android:id="@+id/imageButton1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:src="@drawable/rood" />

    <ImageButton
        android:id="@+id/imageButton4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/imageButton2"
        android:layout_below="@+id/imageButton2"
        android:src="@drawable/flash" />

    <ImageButton
        android:id="@+id/imageButton2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/imageButton1"
        android:layout_centerHorizontal="true"
        android:src="@drawable/groen" />

    <View
        android:id="@+id/view1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/imageButton4"
        android:layout_centerHorizontal="true"
        android:background="#FF0000" />

    <ImageButton
        android:id="@+id/imageButton5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/view1"
        android:layout_alignRight="@+id/imageButton1"
        android:layout_marginRight="19dp"
        android:src="@drawable/min" />

    <ImageButton
        android:id="@+id/imageButton6"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/view1"
        android:layout_alignLeft="@+id/imageButton3"
        android:layout_marginLeft="18dp"
        android:src="@drawable/plus" />

</RelativeLayout>

Please help!

Pratik Butani
  • 60,504
  • 58
  • 273
  • 437
Tim Jake
  • 57
  • 2
  • 8
  • set id to the your parent layout and access the id to change the background instead of assinging the layout – micky Nov 10 '14 at 10:41

5 Answers5

2

Define backcolor.xml in drawable.

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" 
     android:drawable="@drawable/clicked" />
<item  android:state_focused="false" 
  android:drawable="@drawable/normal" />
</selector>

normal.xml in drawable

<?xml version="1.0" encoding="UTF-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android"> 
<solid android:color="#FFFFFF"/>    
</shape>

Clicked.xml in drawable

 <?xml version="1.0" encoding="UTF-8"?> 
 <shape xmlns:android="http://schemas.android.com/apk/res/android"> 
 <solid android:color="#FF1A47"/>      
 </shape>

Set your background in your layout

android:background="@drawable/background"

And Onclicklistner

 ll.setBackgroundColor(Color.RED);
Ankit Kumar
  • 3,663
  • 2
  • 26
  • 38
0

You need to do two changes. Follow steps to do so.

Step 1 : Add id to your RelativeLayout of main activity

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/rootLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="nl.aventus.delaatstestap.MainActivity" >

Step 2 : Use that id to change background color.

relLayout = (View) findViewById(R.layout.rootLayout);
//to change color
relLayout.setBackgroundColor(Color.GREEN);
Chintan Rathod
  • 25,864
  • 13
  • 83
  • 93
0

The problem seems to here in this line

 layout = (View) findViewById(R.layout.activity_main);

you are trying to findViewById an xml file. So all you have to do is give an id to your parent in activity_main and then find that view by this line

RelativeLayout layout = (RelativeLayout) findViewById(R.layout.yourParentId);// since the parent is RelativeLayout

and then change its background color

Mukesh Rana
  • 4,051
  • 3
  • 27
  • 39
0

First of all assign id to your RelativeLayout in XML,

android:id="@+id/rlyt"

Then make correction in line 31 in java file, that is

            layout = (View)findViewById(R.layout.activity_main); 

to

            layout = (View)findViewById(R.id.rlyt);

and don't use setContentView in OnClick

Harsh Dattani
  • 2,109
  • 1
  • 17
  • 27
-1

Put an Id in your RelativeLayout like this

                <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                  xmlns:tools="http://schemas.android.com/tools"
                  android:id="@+id/viewId"
                  android:layout_width="match_parent"
                  android:layout_height="match_parent"
                  android:paddingBottom="@dimen/activity_vertical_margin"
                  android:paddingLeft="@dimen/activity_horizontal_margin"
                  android:paddingRight="@dimen/activity_horizontal_margin"
                  android:paddingTop="@dimen/activity_vertical_margin"
                  tools:context="nl.aventus.delaatstestap.MainActivity" >

And in your Activity do this :

                  RelativeLayout  layout = (RelativeLayout) findViewById(R.id.viewId);
                  ImageButton imagebutton = (ImageButton)findViewById(R.id......);   

                  imagebutton.setOnClickListener(new OnClickListener() {

                  public void onClick(View v) {
                         setContentView(R.layout.activity_main);
            }
        });
Angbeny Finch
  • 179
  • 1
  • 1
  • 9