Frame-by-Frame Animated Splash-Screen - java

[Android Studio] I'm trying to get a few frames to display quickly one after another and then change to a different activity automatically on startup.
I'm able to make the splash activity wait the appropriate amount of time before proceeding, but the animation still isn't playing.
I'm using this person's method (https://www.youtube.com/watch?v=lGRWYJlS3d0) to animate the frames and display them to an ImageView - in the example, the animation is controlled by a stop and start button which sets the frames to loop endlessly (or not), however I would just like it to be activated and displayed when the app is launched.
The way I'm currently implementing this method is causing the app to crash once particular lines are read, but I'm probably just doing it wrong so any help will be appreciated. Thanks. :)
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.company.framebyframe">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".SplashActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.HOME" />
<category android:name="android.intent.category.MAIN" />
</intent-filter>
</activity>
</application>
</manifest>
activity_splash.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/splash_layout"
android:background="#drawable/custom_animation">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<ImageView
android:id="#+id/imageView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center" />
</FrameLayout>
</android.support.constraint.ConstraintLayout>
custom_animation.xml under drawables
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/image0" android:duration="250"/>
<item android:drawable="#drawable/image1" android:duration="100"/>
<item android:drawable="#drawable/image2" android:duration="100"/>
<item android:drawable="#drawable/image3" android:duration="100"/>
<item android:drawable="#drawable/image4" android:duration="750"/>
<item android:drawable="#drawable/image3" android:duration="750"/>
<item android:drawable="#drawable/image4" android:duration="750"/>
<item android:drawable="#drawable/image3" android:duration="500"/>
<item android:drawable="#drawable/image0" android:duration="5000"/>
</animation-list>
SplashActivity.java
public class SplashActivity extends AppCompatActivity {
AnimationDrawable anim;
ImageView imageView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
imageView = (ImageView) findViewById(R.id.imageView);
imageView.setBackgroundResource(R.drawable.custom_animation);
anim = (AnimationDrawable) imageView.getBackground();
anim.start();
// // start new activity after 3.5 seconds // //
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
startNextActivity();
}
}, 3500);
}
public void startNextActivity() {
if (isFinishing())
return;
startActivity(new Intent(SplashActivity.this, MainActivity.class));
finish();
}
crashes tend to occur when i start unquoting these lines:
imageView = (ImageView) findViewById(R.id.imageView);
imageView.setBackgroundResource(R.drawable.custom_animation);
PS: if there are any naming mistakes, it might be due to me trying to make the filenames more generic and understandable, but feel free to point out anything you believe might cause problems.

You need to set the layout on the activity class, otherwise the application won't know what layout if should inflate. That's the main reason your application crashes when you try to find the image view, because you haven't set the content layout. Add this line just after super.onCreate and before you try to manipulate any UI element
setContentView(R.layout.activity_splash);
Also, remove the background declaration on the constraint layout, you're already loading the animated drawable to an image view
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/splash_layout">
...
</android.support.constraint.ConstraintLayout>

Related

Android splash screen jump java

I wanted to implement a splash screen but in a correct way - no superficial delay, only showing it while the app is doing some real work. So I googled a lot and found following solution: I apply theme to SplashActivity, procced with the logic and open next activity. The problem is that splash screen jumps a little bit. Tried different options - fullscreen, no action bar for splash, no action bar for splash and following activity, android:windowDrawsSystemBarBackgrounds true\false and several others. If I set delay for 115-150 millisecs then 9 cases out of 10 there is no jump. The question is how to get rid of the jump and keep splash screen lean.
SplashActivity:
public class SplashActivity extends CheckForInternetActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
routeActivity();
}
AndroidManifest:
<activity
android:name="ru.awac.CBoRrates.SplashActivity"
android:screenOrientation="portrait"
tools:ignore="LockedOrientationActivity"
android:theme="#style/AppTheme.SplashScreen">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
styles.xml:
<style name="AppTheme.SplashScreen" parent="Theme.AppCompat.Light.NoActionBar">
<item name="android:windowBackground">#drawable/splash_screen</item>
<item name="android:windowDrawsSystemBarBackgrounds">true</item>
</style>
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
splash_screen:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android"
android:opacity="opaque">
<item android:drawable="#android:color/white"/>
<item>
<bitmap
android:layout_width="match_parent"
android:layout_height="match_parent"
android:adjustViewBounds="true"
android:scaleType="fitCenter"
android:layout_margin="5dip"
android:src="#drawable/awac_presents_transparent_1" />
</item>
</layer-list>
awac_presents_transparent_1 is just a png
Youtube video of the jump

How to make title background transparent on android app

There is an issue in the follow Android App where guidance is greatly appreciated. This application is to support a Renesas RX130 microcontroller and Texas Instrument CC2650 Bluetooth Low Energy hardware demonstration design. In the initial device scanning page the tile has a bright red background as shown below.
The tool bar has a Purple to Red gradient background.
Question: How can the Red Tile background be made transparent?
The application has two activities. Below are excepts from the AndroidManifest.xml file
<activity android:name="capsense.application.rx130_cc2650.MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="capsense.application.rx130_cc2650.DeviceControlActivity"/>
<service android:name="capsense.application.rx130_cc2650.BLE_Service" android:enabled="true" />
Code from activity_main.xml, listitem.xml, toolbar.xml and MainActivity.java generated the first activity. Relevant sections of code is from the files are below.
Excepts from activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<include
android:id="#+id/toolbar"
layout="#layout/toolbar"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
Excepts from toolbar.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/ic_launcher_background_rx130_cc2650"
app:popupTheme="#style/CustomerPopUpMenuStyle"
app:theme="#style/CustomerToolbarStyle"
app:titleTextColor="#android:color/white">
</android.support.v7.widget.Toolbar>
Excepts from MainActivity.java
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(capsense.application.rx130_cc2650.R.layout.activity_main);
mScanning = false;
//Get User Interface Elements
Toolbar toolbar = findViewById(capsense.application.rx130_cc2650.R.id.toolbar);
toolbar.setTitle(R.string.app_label);
setSupportActionBar(toolbar);
The complete project is available on github Android_Mobile_App_RX130_CC2650
References:
Set transparent background of an imageview on Android
Toolbar with Gradient Background set title background transparent
There is a conflict in color from multiple source to background.
Don't use theme unless if needed, as
theme influence all the views and layouts inside the toolbar
style influence only the toolbar and does not interfere with views and layouts inside toolbar
Please try below options, one of which might work for you,
Option 1 (preferred): update CustomerToolbarStyle code as below
<style name="CustomerToolbarStyle" parent="Theme.AppCompat.Light.NoActionBar">
<item name="android:background">#drawable/ic_launcher_background_rx130_cc2650</item>
<item name="android:title">#color/colorforeground</item>
</style>
update toolbar.xml code as below
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:popupTheme="#style/CustomerPopUpMenuStyle"
style= "#style/CustomerToolbarStyle"
app:titleTextColor="#android:color/white"/>
Option 2: set transparent background in CustomerToolbarStyle
<style name="CustomerToolbarStyle" parent="Theme.AppCompat.Light.NoActionBar">
<!-- set background color to transperant -->
<item name="android:background">#00000000</item>
<item name="android:title">#color/colorforeground</item>
</style>
Option 3: remove android:background from the CustomerToolbarStyle
<style name="CustomerToolbarStyle" parent="Theme.AppCompat.Light.NoActionBar">
<item name="android:title">#color/colorforeground</item>
</style>
Option 4: remove app:theme="#style/CustomerToolbarStyle" from toolbar.xml
If above options not working, please check below links
Change Toolbar background color programmatically does not change Toolbar Title Background color
Text background issue on Toolbar

How to create a switching app in android like google translator (dialog over native screen)

I am a beginner to android development and I am trying to make something like this in my application.
I found a great question and an answer related to my issue here Android - How to display a dialog over a native screen?. But, it didn't work as expected. May be I am doing something wrong.
I am attaching the code that I am using to achieve this. Any help would be highly appreciated and if any one can guide me in the right direction that I can follow.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.myapp.MainActivity">
</android.support.constraint.ConstraintLayout>
style.xml
<resources>
<!-- Base application theme. -->
<style name="myTheme" parent="android:style/Theme.Dialog">
<item name="android:windowNoTitle">true</item>
<item name="android:windowContentOverlay">#null</item>
<item name="android:backgroundDimEnabled">true</item>
<item name="android:background">#android:color/transparent</item>
</style>
</resources>
MainActivity.java
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
AlertDialog LDialog = new AlertDialog.Builder(getApplicationContext())
.setTitle("Call Blocked")
.setMessage("Call Blocked, reroute call?")
.setPositiveButton("ok", null).create();
LDialog.show();
}
}
AndroidManifest.xml
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:launchMode="singleInstance" android:excludeFromRecents="true"
android:taskAffinity="" android:theme="#android:style/Theme.Dialog">
...............
...............
</application>
Here, what I am getting now.
Answering the own question because I found the concept that I need to use. It is FAB or Floating Action Button as described here https://developer.android.com/guide/topics/ui/floating-action-button. Also, facebook uses the same concept with the name facebook chat heads.

Displaying Images in Android using Java

I am splitting this off from my other thread here: Display image in popout window after button is clicked -- Android/Java
The old thread got very convoluted and confusing, and a bit off-topic, so I wanted to create another one that is clearer with more information.
I am trying to display an image in Android using an image file path that changes every time the app is run. I know there are ways to declare resources in the XML layout file, but since the picture I'd like to display is taken from the camera, I can't do it that way. I need to be able to display the image without hardcoding it.
The following is the code that I have:
photo_viewer.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/pictureViewer"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<ImageView
android:id="#+id/imageView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:contentDescription="#string/viewImage" />
</RelativeLayout>
Java method to display image:
public void viewPhoto(String file){
ImageView imageView = new ImageView(getApplicationContext());
LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
Bitmap image = BitmapFactory.decodeFile(file);
imageView.setImageBitmap(image);
RelativeLayout rl = (RelativeLayout)findViewById(R.id.pictureViewer);
rl.addView(imageView, lp);
}
However, when I run the above code the application crashes, saying it has stopped working. "file" is a string denoting the directory where the image from the camera is stored, and I know this part is correct. I can use it (using a different method) to change the wallpaper. It's just displaying it that is causing me trouble. I have tried various methods to simply display the image (dialogs, "drawable"s, bitmap factory, etc) and had the same problem with all of them. I feel like it's something simple that I have missed, since I'm new to Android app development. I'm hoping one of you folks might be able to shed some light on this.
EDIT:
I have decided to try another route, but I am still getting a null pointer exception. Here is all my code:
main class, viewPhoto method:
/* View photo */
public void viewPhoto(String file){
Intent imageView = new Intent(this, PhotoViewer.class);
imageView.putExtra("directory", file);
startActivity(imageView);
}
PhotoViewer class:
package com.androidproject;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.RelativeLayout;
public class PhotoViewer extends Activity {
public String file;
ImageView image;
#Override
protected void onCreate(Bundle bundle) {
super.onCreate(bundle);
setContentView(R.layout.photo_viewer);
//get file path of image
Bundle extras = getIntent().getExtras();
file = extras.getString("directory");
Drawable drawable = Drawable.createFromPath(file);
image.setImageDrawable(drawable);
}
}
photo_viewer.xml (created using the Eclipse tool, so it's not just any xml file)
<?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/photo_viewer"
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=".PhotoViewer" >
<ImageView
android:id="#+id/imageView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:contentDescription="#string/viewImage"/>
</RelativeLayout>
Project manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.androidproject"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.SET_WALLPAPER" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".Main"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".PhotoViewer"
android:label="#string/app_name" >
<intent-filter>
<action android:name="com.androidproject.PhotoViewer" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
The app continues to crash and tell me "unfortunately it stopped working". Here's the logcat of the error:
As always, help is appreciated!
The null pointer exception is because
Drawable drawable = Drawable.createFromPath(file);
image.setImageDrawable(drawable);
is referencing an uninitiated variable (image) which is null
My best guess would be that to eliminate said error you'd want to initiate the variable
ImageView image = (ImageView)findViewById(R.id.imageView);

Android TabLayout crashing

I am creating an android app myself (min sdk version 7 or 2.1 and up). And it was going fine until I decided I was going to need a TabLayout. I added a new class, and had it extend TabActivity, however when launching the app in an AVD it crashed and stated this in the log that there was an uncaught exception. And that it could not launch the activity.
ThermostatTabHost.java (note, I did not add package/import to save some space):
public class ThermostatTabHost extends TabActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tabmain);
Resources res = getResources(); // Resource object to get Drawables
TabHost tabHost = getTabHost(); // The activity TabHost
TabHost.TabSpec spec; // Resusable TabSpec for each tab
Intent intent; // Reusable Intent for each tab
// Create an Intent to launch an Activity for the tab (to be reused)
intent = new Intent().setClass(this, ThermostatActivity.class);
// Initialize a TabSpec for each tab and add it to the TabHost
spec = tabHost.newTabSpec("Temperature").setIndicator("Program",
res.getDrawable(R.drawable.arrow_up))
.setContent(intent);
tabHost.addTab(spec);
}
}
The ThermostatActvity class currently only holds a onCreate function which calls super.onCreate(savedInstance) and sets a layout with a textview.
The tabmain.xml file:
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="5dp">
<TabWidget
android:id="#android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="5dp" />
</LinearLayout>
</TabHost>
And finally the manifest file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.pack.thermostat"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="7" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:name=".ThermostatTabHost"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
This example came straight of the android developers website (with a few minor changes), I also looked around for similar issues but I couldn't come up with anything.
Thank you in advance.
Add this "ThermostatActivity" in your manifeast file

Categories