Resources not found exception loading drawable animation into image view - java

I'm trying to animate an image view that I'm using as a background in my activity but the app crashes when loading the activity. All the images and the .xml file are in the drawable-hdpi folder so I'm not sure why it's not finding it. Please let me know if there is a better way to set an animation as an activity background.
Heres my .xml file for the animation:
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/selected" android:oneshot="false">
<item android:drawable="#drawable/bow1" android:duration="60" />
<item android:drawable="#drawable/bow2" android:duration="60" />
<item android:drawable="#drawable/bow3" android:duration="60" />
<item android:drawable="#drawable/bow4" android:duration="60" />
<item android:drawable="#drawable/bow5" android:duration="60" />
<item android:drawable="#drawable/bow6" android:duration="60" />
<item android:drawable="#drawable/bow7" android:duration="60" />
<item android:drawable="#drawable/bow8" android:duration="60" />
<item android:drawable="#drawable/bow9" android:duration="60" />
</animation-list>
Heres the code I'm using to try run it:
public class MainActivity extends Activity {
ImageView bground;
AnimationDrawable bganim;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
setContentView(R.layout.activity_main);
bground = (ImageView)findViewById(R.id.background);
bground.setBackgroundResource(R.drawable.bowanimbg);
bganim = (AnimationDrawable) bground.getBackground();
bganim.start();
}
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.lucidity/com.example.lucidity.MainActivity}: android.content.res.Resources$NotFoundException: File res/drawable-xxhdpi/bowanimbg.xml from drawable resource ID #0x7f02000b

I would put this in a comment, but unfortunately I don't have a high enough rep. So I'll do my best to solve the problem.
You mentioned that all of the images were present in the drawable-hdpi folder. But if you notice the error, it looks like it is searching for the file in the res/drawable-xxhdpi folder. What I would suggest is copying the .xml files from the -hdpi folder into the -xxhdpi folder and seeing if it works!

Fixed it!! Since it wasn't loading the animation at all I decided to make an onWindowFocusedChanged method so that it would load the animation once the activity had loaded. I also set the background of the imageview to my bowanimbg.xml file in the .xml which I initially thought wouldn't be necessary.
public void onWindowFocusChanged(boolean hasFocus) {
bground = (ImageView)findViewById(R.id.background);
bground.setImageResource(R.drawable.bowanimbg);
bganim = (AnimationDrawable) bground.getBackground();
if (hasFocus) {
bganim.start();
}
else {
bganim.stop();
}

Related

How can I make the corners of my bottom sheet dialog rounded? [duplicate]

This question already has answers here:
Round corner for BottomSheetDialogFragment
(28 answers)
Closed last year.
I'm trying to make the top corners of my BottomSheetDialog rounded, but I haven't had any luck with anything online. This is what I would like for it to look like:
No matter what I've tried, I keep getting this:
I've tried the method here and using shapeAppearanceLargeComponent (what I'm using now).
Here is my code:
styles.xml
<style name="AppTheme" parent="Theme.MaterialComponents.Light.DarkActionBar">
<!-- Customize your theme here. -->
...
<item name="shapeAppearanceLargeComponent">#style/CustomShapeAppearanceBottomSheetDialog</item>
</style>
<style name="CustomShapeAppearanceBottomSheetDialog" parent="">
<item name="cornerFamily">rounded</item>
<item name="cornerSizeTopRight">16dp</item>
<item name="cornerSizeTopLeft">16dp</item>
</style>
BottomNavMenuFragment:
public class BottomNavMenuFragment extends BottomSheetDialogFragment {
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_bottom_nav_drawer, container, false);
}
}
And this is how I'm showing the fragment:
BottomNavMenuFragment navFragment = new BottomNavMenuFragment();
navFragment.show(getSupportFragmentManager(), navFragment.getTag());
Nothing I seem to do works. Could someone point me in the right direction?
Follow the below steps to attain top rounded corner bottomsheet:
Step 1: Create drawable rounded_top_corners.xml file inside drawable folder
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#android:color/white"/>
<corners android:topLeftRadius="8dp"
android:topRightRadius="8dp"/>
</shape>
Step 2: Create below styles in styles.xml
<style name="BottomSheetDialogStyle" parent="Theme.Design.Light.BottomSheetDialog">
<item name="bottomSheetStyle">#style/bottomSheetBackground</item>
</style>
<style name="bottomSheetBackground" parent="Widget.Design.BottomSheet.Modal">
<item name="android:background">#drawable/rounded_top_corners</item>
</style>
Step 3: Add style in BottomNavMenuFragment class
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setStyle(STYLE_NORMAL, R.style.BottomSheetDialogStyle);
}
That's it, the style will be applied to bottomsheet.
After messing around with the possible solutions people posted, I figured out that my code was working fine, but the corners of my NavigationView were obscuring the rounded corners of the drawer. After adding some padding, the rounded corners are displaying correctly.
You can create following shape in your drawable:
drawable/rounded_corners.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:shape="rectangle">
<solid android:color="#fff"/>
<corners
android:topLeftRadius="20dp"
android:topRightRadius="20dp"/>
</shape>
Then for the layout for your bottom sheet you can add this drawable as a background property.
android:background="#drawable/rounded_corners"
Create a drawable and set it for the bottomsheet inside the setOnShowListener as below.(where R.drawable.bottom_sheet_bkg is my background xml file).
val dialogBinding = MyBottomSheetDialogBinding.inflate(layoutInflater)
val bottomSheet = BottomSheetDialog(requireContext())
bottomSheet.setContentView(dialogBinding.root)
bottomSheet.setOnShowListener { dia ->
val bottomSheetDialog = dia as BottomSheetDialog
val bottomSheetInternal: FrameLayout =
bottomSheetDialog.findViewById(com.google.android.material.R.id.design_bottom_sheet)!!
bottomSheetInternal.setBackgroundResource(R.drawable.bottom_sheet_bkg)
}
bottomSheet.setCancelable(true)
bottomSheet.show()
XML File:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#color/white" />
<corners
android:topLeftRadius="5dp"
android:topRightRadius="5dp" />
</shape>

How to set up app to run in full screen [duplicate]

This question already has answers here:
Fullscreen Activity in Android?
(40 answers)
Closed 5 years ago.
Hello i have one problem i'm creating app that will repeat landscape video as background but i don't know how to set it in full screen. I'm creating app in Android studio. App will have landscape like this>
Try this .
way 1.set in the manifest
<activity
...
android:theme="#android:style/Theme.NoTitleBar.Fullscreen" />
way 2.set in the java code
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// add this before setContentView methood ,and after super
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FILL_PARENT, WindowManager.LayoutParams.FILL_PARENT);
setContentView(R.layout.activity_main);
}
way 3.set in the manifest and set in the style
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="theme_fullScreen" parent="android:Theme.Black">
<item name="android:windowNoTitle">true</item>
<item name="android:windowFullscreen">true</item>
</style>
</resources>
in the manifest
<activity android:name=".LoginActivity"
android:theme="#style/theme_fullScreen"/>
Note
change the android:theme of the Activity.
set getWindow().setFlags(WindowManager.LayoutParams.FILL_PARENT, WindowManager.LayoutParams.FILL_PARENT);
Here you go..
To play the video, create one folder name "raw" under "res" folder. Copy your video under "raw" folder. Please keep video name in small characters. Once done please do below modifications
style.xml
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
</style>
<style name="AppTheme.FullScreen" parent="AppTheme">
<item name="android:windowFullscreen">true</item>
</style>
Note - No need to do any changes in AndroidManifest file.
MainActivity.java
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);
VideoView view = (VideoView) findViewById(R.id.videoView);
Uri uri = Uri.parse("android.resource://"+getPackageName()+"/"+R.raw.samplevideo);
view.setVideoURI(uri);
view.start();
// To keep video in loop
view.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
mp.setLooping(true);
}
});
}
}
activity_main.xml
`
<VideoView
android:id="#+id/videoView"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:layout_editor_absoluteX="0dp"
tools:layout_editor_absoluteY="0dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:textSize="30dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"></TextView>
</android.support.constraint.ConstraintLayout>
`
Note - One thing keep in mind that videoview should be immediate child of parent layout, others views/layout will come after videoview.
Is this what you want?

How to set a button background and back according to selected state not pressed state

I currently have a sound button,and I would like to change its background every time when it's selected and clicked(I'm developing on a pair of android glasses, so when the button is selected it's not pressed, thus two different states).
I have used the xml file to change button background when selected so far:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/no_music"
android:state_selected="true" />
<item android:drawable="#drawable/no_music"
android:state_focused="true" />
<item android:drawable="#drawable/music" />
</selector>
And together in the onClick method of the button, I set the button background according to the state:
public void musicPlay(View view) {
Button music = (Button) findViewById(R.id.music);
if(isPlaying) {
music.setBackgroundResource(R.drawable.no_music);
MusicManager.release()
}else{
music.setBackgroundResource(R.drawable.music);
MusicManager.start(this);
}
isPlaying = !isPlaying;
}
When I click the button, each time it would change its background. But when I select it, it would only change background once. Is there any method that I can use to make selected state the same as the pressed one?
Thank you very much.
Do this in your code:
public void musicPlay(View view) {
Button music = (Button) findViewById(R.id.music);
if(isPlaying) {
music.setPressed(true);
MusicManager.release()
}else{
music.setPressed(false);
MusicManager.start(this);
}
isPlaying = !isPlaying;
}
Also in your xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/no_music" android:state_selected="true"/>
<item android:drawable="#drawable/no_music" android:state_pressed="true"/>
<item android:drawable="#drawable/music"/>
</selector>
you could make an drawable resource as resource for the button:
<selector xmlns:android="http://schemas.android.com/apk/res/android" android:constantSize="true">
<item android:drawable="#drawable/musicon" android:state_checked="false"/>
<item android:drawable="#drawable/musicoff" android:state_checked="true" />
<item android:drawable="#drawable/default" />
something like this should work. good luck.
Set your button pressed property to true, then verify if your button background.xml file has selected item.

Android AnimationDrawable memory management

I'm creating an android application & it has background animations for each activity.
I used AnimationDrawable setBackgroundResource(anim.xml) to run the animation xml files and works fine.
problem is, when I test on devices,some devices gives me an FATAL EXEPTION telling that
bitmap size exeeds VM budget(out of memory).
so I surfed web and found some solutions like executing animation.setCallbacks(null) before
run the animation,scaling down bitmaps(programmatically) and resizing drawables.
I tried them but few devices which has low heap still gives the same error.
so I want to know am I doing something wrong or missing something?
is their any other solution to this problem?
//animation
private void animate() {
imgView = (ImageView)findViewById(R.id.imageView1);
imgView.setVisibility(ImageView.VISIBLE);
imgView.setBackgroundResource(R.drawable.level_animation);
frameAnimation = (AnimationDrawable) imgView.getBackground();
if (frameAnimation.isRunning()) {
frameAnimation.stop();
}
else {
frameAnimation.stop();
frameAnimation.start();
}
}
xml
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android" android:oneshot="true" >
<item android:drawable="#drawable/top_anim1" android:duration="200" />
<item android:drawable="#drawable/top_anim2" android:duration="200" />
<item android:drawable="#drawable/top_anim3" android:duration="200" />
<item android:drawable="#drawable/top_anim4" android:duration="200" />
<item android:drawable="#drawable/top_anim5" android:duration="200" />
</animation-list>

Android Frame by Frame Animation problem on elements in a CursorAdapter

I am having trouble applying an animation to a View. I am trying to load the animation from inside the constructor of a CursorAdapter, so I can set it later assign it to certain children in the list.
In the constructor I have :
shineAnimation = AnimationUtils.loadAnimation(ctx, R.anim.news_list_item_shine);
the animation is in my res/anim dir
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android" android:oneshot="true">
<item android:drawable="#drawable/shine1" android:duration="200" />
<item android:drawable="#drawable/shine2" android:duration="200" />
<item android:drawable="#drawable/shine3" android:duration="200" />
<item android:drawable="#drawable/shine4" android:duration="200" />
<item android:drawable="#drawable/shine5" android:duration="200" />
</animation-list>
I'm getting an exception :
Unknown animation name: animation-list
Help would be much appreciated
Thanks
S
I don't think you load AnimationDrawables via AnimationUtils. AnimationDrawable is a Drawable more than it is an Animation. Try this sample code from the SDK guide.
ImageView rocketImage = (ImageView) findViewById(R.id.rocket_image);
rocketImage.setBackgroundResource(R.anim.rocket_thrust);
rocketAnimation = (AnimationDrawable) rocketImage.getBackground();

Categories

Resources