I want to create a screen with a backgroundcolor that keeps changing from red to blue. For some reason it always crashes when i try to instantiate the ValueAnimator. I have no idea what's wrong with my code
Thank you
Animation class
public BackgroundAnimation(Context context){
super(context);
ValueAnimator colorAnim = ObjectAnimator.ofInt(R.anim.animator, "backgroundColor", Color.RED, Color.BLUE);
colorAnim.setDuration(3000);
colorAnim.setEvaluator(new ArgbEvaluator());
colorAnim.setRepeatCount(ValueAnimator.INFINITE);
colorAnim.setRepeatMode(ValueAnimator.REVERSE);
colorAnim.start();
}
animator.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<objectAnimator
android:propertyName="backgroundColor"/>
</set>
Main class
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.menu);
LinearLayout container = (LinearLayout) findViewById(R.id.container);
container.addView(new BackgroundAnimation(this));
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<TextView
android:id="#+id/TextView01"
android:gravity="center"
android:textSize="20sp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<TextView
android:id="#+id/TextView02"
android:gravity="center"
android:textSize="20sp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<ImageView
android:id="#+id/ImageView01"
android:layout_width="fill_parent"
android:layout_height="300sp"/>
</LinearLayout>
You can use the ObjectAnimator to change the background color:
For API >= 21 :
ObjectAnimator colorAnimator = ObjectAnimator.ofArgb(travelersListView.getBackground().mutate(), "tint", mCurrentBackground, mFadeColor);
colorAnimator.setInterpolator(new AccelerateDecelerateInterpolator());
colorAnimator.start();
For backward support from API 16 use this:
ObjectAnimator colorAnimator = ObjectAnimator.ofObject(travelersListView.getBackground().mutate(), "tint", new ArgbEvaluator(), mCurrentBackground, mFadeColor);
colorAnimator.setInterpolator(new AccelerateDecelerateInterpolator());
colorAnimator.start();
There is not id parameter in XML file for LinearLayout.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:id="#+id/container">
#Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.menu);
LinearLayout container = (LinearLayout) findViewById(R.id.container);
changeBackground(container, "#F44336", "#2196F3"); //#F44336 : Red , #2196F3 : Blue
}
public void changeBackground(final View view, String color1, String color2) {
ValueAnimator anim = new ValueAnimator();
anim.setIntValues(Color.parseColor(color1), Color.parseColor(color2));
anim.setEvaluator(new ArgbEvaluator());
anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
#Override
public void onAnimationUpdate(ValueAnimator valueAnimator) {
view.setBackgroundColor((Integer) valueAnimator.getAnimatedValue());
}
});
anim.setDuration(2000);
anim.setRepeatCount(ValueAnimator.INFINITE);
anim.setRepeatMode(ValueAnimator.REVERSE);
anim.start();
}
Try this, Hope this helps
No variant of ObjectAnimator.ofInt() takes resource ID as parameter. Please read this for declaring animations in XML. http://developer.android.com/guide/topics/graphics/prop-animation.html#declaring-xml
Related
I want to have a layout constantly on top of everything and visible all the time, however, once I swipe to the next element in the ViewPager, the FrameLayout is getting behind the ViewPager.
<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:gravity="center_vertical"
tools:context=".MainActivity">
<androidx.viewpager2.widget.ViewPager2
android:id="#+id/videosViewPager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:layout_editor_absoluteX="0dp"
tools:layout_editor_absoluteY="0dp">
</androidx.viewpager2.widget.ViewPager2>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<FrameLayout
android:id="#+id/preview_display_layout"
android:layout_width="200dp"
android:layout_height="200dp"
android:visibility="visible"
tools:ignore="MissingConstraints"
tools:layout_editor_absoluteX="216dp"
tools:layout_editor_absoluteY="0dp">
</FrameLayout>
</RelativeLayout>
</RelativeLayout>
I tried to place a button instead of and on top of the FrameLayout and, guess what, the button remains on top of everything without going behind the ViewPager after each sroll.
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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"
tools:context=".MainActivity">
<androidx.viewpager2.widget.ViewPager2
android:id="#+id/videosViewPager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:layout_editor_absoluteX="0dp"
tools:layout_editor_absoluteY="0dp">
</androidx.viewpager2.widget.ViewPager2>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button
android:layout_width="197dp"
android:layout_height="286dp">
</Button>
<FrameLayout
android:id="#+id/preview_display_layout"
android:layout_width="197dp"
android:layout_height="286dp"
android:visibility="visible"
tools:ignore="MissingConstraints"
tools:layout_editor_absoluteX="216dp"
tools:layout_editor_absoluteY="0dp">
</FrameLayout>
</RelativeLayout>
</FrameLayout>
Here is my code in MainActivity
private ViewPager2 videosViewPager;
private SurfaceView previewDisplayView;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(getContentViewLayoutResId());
previewDisplayView = new SurfaceView(this);
setupPreviewDisplayView();
videosViewPager = findViewById(R.id.videosViewPager);
List<VideoItem> videoItems = new ArrayList<>();
VideoItem life = new VideoItem();
String path = "android.resource://"+getPackageName()+"/"+ R.raw.video;
life.videoURL = Uri.parse(path);
videoItems.add(life);
VideoItem rest = new VideoItem();
String path2 = "android.resource://"+getPackageName()+"/"+ R.raw.video1;
rest.videoURL = Uri.parse(path);
videoItems.add(rest);
videosViewPager.setAdapter(new VideoAdapter(videoItems));
}
private void setupPreviewDisplayView() {
previewDisplayView.setVisibility(View.GONE);
ViewGroup viewGroup = findViewById(R.id.preview_display_layout);
viewGroup.addView(previewDisplayView);
previewDisplayView
.getHolder()
.addCallback(
new SurfaceHolder.Callback() {
#Override
public void surfaceCreated(SurfaceHolder holder) {
processor.getVideoSurfaceOutput().setSurface(holder.getSurface());
Log.d("Surface","Surface Created");
}
#Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
onPreviewDisplaySurfaceChanged(holder, format, width, height);
Log.d("Surface","Surface Changed");
}
#Override
public void surfaceDestroyed(SurfaceHolder holder) {
processor.getVideoSurfaceOutput().setSurface(null);
Log.d("Surface","Surface destroy");
}
});
}
I guess my issue is in adding SurfaceView(previewDisplayView)in ViewGroup.
What can I do to make the previewDisplayView get always on top of everything? By the way, I tried bringToFront(), but after a swipe the previewDisplayLayout is automatically going behind the ViewPager.
Thank you in advance
Have you tried android:elevation property ? I think it is what you're looking for,
Hope will help
I am trying to programatically change the width of an ImageView without resizing the image. The ImageView originally shows the entire image, but when the width of the image is decreased, a portion of the image should be cut off, like this:
However, when my code reduces the width of the ImageView from 350dp to 200dp, the image is automatically resized instead:
How do I ensure that the ImageView does not resize the image when I decrease its width? Below is my code:
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ImageView
android:id="#+id/photo_1_preview"
android:layout_width="350dp"
android:layout_height="350dp"
android:adjustViewBounds="false"
android:background="#drawable/android"/>
</LinearLayout>
MainActivity.java:
package com.example.changingimageviewwidth;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ImageView;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Reduce the width of the image preview box
ImageView imagePreviewBox = findViewById(R.id.photo_1_preview);
imagePreviewBox.getLayoutParams().width = 250;
}
}
Use android:scaleType="matrix" for the ImageView and put the desired image in android:src.
NOTE: In this case, I set the LinearLayout to be 400x400 because the image size is 400x400 px and wrap the ImageView inside of a LinearLayout just for clarity.
main.xml
<?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:background="#333333"
android:gravity="center"
android:orientation="vertical">
<LinearLayout
android:layout_height="400px"
android:layout_width="400px"
android:orientation="vertical"
android:background="#0000FF">
<ImageView
android:layout_height="match_parent"
android:layout_width="match_parent"
android:src="#drawable/android"
android:scaleType="matrix"
android:id="#+id/iv"/>
</LinearLayout>
</LinearLayout>
MainActivity.java
public class MainActivity extends Activity
{
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ImageView iv = findViewById(R.id.iv);
ViewGroup.LayoutParams params = iv.getLayoutParams();
params.width = 200; //200 pixels.
//Not necessary in my test. You may need to call the following
//method if you set the params outside of onCreate(), onCreateView().
//iv.requestLayout();
}
}
Output:
Set the scale type to CENTER_CROP. That will center the image in the view, and crop out anything that doesn't fit.
To get your desired behavior, simply set android:scaleType="fitStart" to your ImageView in xml. It will not change the aspect ratio of the src, but aligns it to the start of the ImageView, just as you mentioned.
The problem you're facing is because the drawable is set as a background of ImageView instead of being set as a source
After you fix that, the right scaleType to choose for your use-case is fitStart which will maintain the original aspect ratio for the bitmap and align it to the top-left of the ImageView
Please use the updated layout file -
<?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" >
<ImageView
android:id="#+id/photo_1_preview"
android:layout_width="350dp"
android:layout_height="350dp"
android:adjustViewBounds="false"
android:scaleType="fitStart"
android:src="#drawable/android"/>
</LinearLayout>
Try with the following code.
//activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/darker_gray"
android:gravity="center"
android:orientation="vertical">
<ImageView
android:id="#+id/image_view"
android:layout_width="300dp"
android:layout_height="300dp"
android:scaleType="fitCenter"
android:src="#drawable/dumy" />
</LinearLayout>
//MainActivity.java
public class MainActivity extends AppCompatActivity {
private ImageView imageView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView = findViewById(R.id.image_view);
imageView.getLayoutParams().width = dpToPixels(150);
imageView.requestLayout();
imageView.invalidate();
}
private int dpToPixels(int size) {
return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, size, getResources().getDisplayMetrics());
}
}
I want to change images in my popupwindow. I am trying to change ImageView image source which is in test.xml while my contentView is activity_main. When I try to just call imageView.setImageResource(android.R.drawable.ic_menu_help); it gives me nullpointerexception I realized i have to use inflate method, but when I use it it doesn't do what I want.I know there is a lot of questions regarding this, but none of them really helped me.
This is activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/relative"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.android.popupwindow.MainActivity">
<Button
android:text="Test"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/button"
android:layout_centerVertical="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
test.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/holo_blue_dark"
android:id="#+id/test1"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/linear1">
<ImageView
android:layout_width="75dp"
android:layout_height="75dp"
android:src="#android:drawable/btn_star_big_on"
android:id="#+id/imageView1"
android:layout_weight="1"
/>
</LinearLayout>
</RelativeLayout>
my main.java:
public class MainActivity extends AppCompatActivity {
private PopupWindow popupWindow;
private RelativeLayout relativeLayout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button test = (Button) findViewById(R.id.button);
relativeLayout = (RelativeLayout) findViewById(R.id.relative);
final View view1 = getLayoutInflater().inflate(R.layout.test, null);
ImageView imageView = (ImageView) view1.findViewById(R.id.imageView1);
imageView.setImageResource(android.R.drawable.ic_menu_help); //Image source is not changed in my popupWindow,
// but it is changed when I call relativeLayout.addView(view1);
test.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//relativeLayout.addView(view1); // <-- this does the trick, but its not what i want
View container = getLayoutInflater().inflate(R.layout.test, null);
popupWindow = new PopupWindow(container, android.app.ActionBar.LayoutParams.MATCH_PARENT, android.app.ActionBar.LayoutParams.WRAP_CONTENT, true);
popupWindow.showAtLocation(relativeLayout, Gravity.BOTTOM, 0, 0);
container.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View view, MotionEvent motionEvent) {
popupWindow.dismiss();
return true;
}
});
}
});
}
}
You're inflating two separate views. Obviously what you do in one will have no impact on the other. You should set the popup view to your originally inflated view:
popupWindow = new PopupWindow(view1,
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.WRAP_CONTENT,
true);
I have android view that I want to fadeIn and Fadeout when users click on a button. The animation itself is being called and the callback is being called but when the animation triggers the view cannot be seen. I think the problem is with the xml layout, I have been stuck on this for hours if someone can help it would be greatly appreciated.
My Main Layout: - Just view in question
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="left|top"
android:background="#color/whiteOverlay"
android:gravity="center_vertical|center_horizontal"
android:id="#+id/uploadOptions"
android:alpha="0"
android:visibility="gone">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:paddingLeft="40dp"
android:paddingRight="40dp">
<Button
style = "#style/basic_button"
android:text="#string/selectFromGallery"
android:id="#+id/gallery_select"
android:layout_marginTop="10dp"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<Button
style = "#style/basic_button"
android:text="#string/selectFromCamera"
android:id="#+id/camera_select"
android:layout_marginTop="10dp"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
</LinearLayout>
FadeIn
<?xml version="1.0" encoding="UTF-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<alpha
android:fromAlpha="0.0"
android:toAlpha="1.0"
android:interpolator="#android:anim/accelerate_interpolator"
android:duration="500" />
</set>
FadeOut
<?xml version="1.0" encoding="UTF-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<alpha
android:fromAlpha="1.0"
android:toAlpha="0.0"
android:interpolator="#android:anim/accelerate_interpolator"
android:duration="500" />
</set>
Activity Java -
public class regPP extends Activity {
private String lang = "";
private Boolean optionsActive = false;
private LinearLayout options = null;
private void fade (final View view, final Boolean Toggle){
Animation anim;
view.setVisibility(View.VISIBLE);
if(Toggle){
//fadeOut
anim = AnimationUtils.loadAnimation(regPP.this, R.anim.fadeout);
Log.d("FADE","FadeOut");
} else {
//fadeIn
anim = AnimationUtils.loadAnimation(regPP.this, R.anim.fadein);
Log.d("FADE","FadeIn");
}
view.startAnimation(anim);
anim.setAnimationListener(new Animation.AnimationListener(){
#Override
public void onAnimationStart(Animation animation) {
}
#Override
public void onAnimationEnd(Animation arg0) {
//Functionality here
if(Toggle){
view.setVisibility(View.GONE);
Log.d("FADE", "FadeOut Callback");
} else {
view.setVisibility(View.VISIBLE);
Log.d("FADE", "FadeIn Callback");
}
}
#Override
public void onAnimationRepeat(Animation animation) {
}
});
}
#Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.lang = getResources().getString(R.string.lang);
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
final Context context = regPP.this;
setContentView(R.layout.reg_pp);
ImageButton uploadTog = (ImageButton) findViewById(R.id.uploadTog);
options = (LinearLayout) findViewById(R.id.uploadOptions);
uploadTog.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
fade(options,false);
optionsActive = true;
}
});
}
#Override
public void onBackPressed() {
if(optionsActive){
fade(options,true);
optionsActive = false;
} else {
super.onBackPressed();
}
}
}
remove this line may help u
android:visibility="gone"
visibility is gone that's why your view is not visible
android:visibility="gone"
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!
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);
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);
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
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
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);
}
});