App crashes when using TransitionDrawable - java

I want to transition the color of my background when a button is clicked but my app seems to crash once I press the button. The button runs the TransitionDrawable code. I have set my background to the drawable folder containing the transition.Could anyone help me? Thank you very much!
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void colourChangeButton(View view) {
final TransitionDrawable transition = (TransitionDrawable) view.getBackground();
transition.startTransition(1000);
}
}
Here are my drawable files that define the colours:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<color android:color="#2575fc"></color>
</item>
</selector>
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<color android:color="#ff0844"></color>
</item>
</selector>
Here is my transition drawable file :
<?xml version="1.0" encoding="utf-8"?>
<transition xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/blue_background"></item>
<item android:drawable="#drawable/pink_background"></item>
</transition>

You are almost there!
You need to set the transition drawable as the background of your view.
Let's say your activity's layout xml is called activity_main.xml and your
transition drawable file is called transition.xml.
In the root layout of your activity_main.xml:
<LinearLayout
...
id="#+id/main_layout"
background="#drawable/transition">
<Button
id="#+id/start_transition_button"
... />
...
In MainActivity.java:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ViewGroup layout = (ViewGroup) findViewById(R.id.main_layout);
TransitionDrawable transition = (TransitionDrawable) layout.getBackground();
Button button = (Button) findViewById(R.id.start_transition_button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
transition.startTransition(500);
}
}
}
}
Here is a good post about it:
https://proandroiddev.com/transitiondrawable-small-gems-of-the-android-framework-4dcdd3c83319

Related

Android Studio - Animations between activities

I'm doing a "University Chat App". The first activity of the app is just the logo and the second activity is the sign in. I want to add a small fade from the first activity to the second activity.
This is the code for the fade in:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<alpha android:fromAlpha="0.5"
android:toAlpha="1.0"
android:interpolator="#android:anim/accelerate_interpolator"
android:duration="9000" />
</set>e
This is the code for the fade out:
<?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:fillAfter="true"
android:duration="1000" />
</set>
MainActivity code:
public class MainActivity extends AppCompatActivity {
private static int WELCOME_TIMEOUT = 1500; // Sets time of the transition
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getSupportActionBar().hide(); // Hide Action Bar
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); // Hide the Status Bar
setContentView(R.layout.activity_main);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
Intent welcome = new Intent(MainActivity.this, MainActivity2.class);
startActivity(welcome);
overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
finish();
}
}, WELCOME_TIMEOUT);
}
}
Everything works fine, but the fade out is quite slow and it creates this transparent gray screen before changing to the second activity. I'm assuming the gray screen comes from this line of the fade out android:toAlpha="0.0"
Is there a way to create a better fade between activities, without the transparent gray screen? Also, can different animations be done in between activities?

Android Toolbar Menu Not Displaying on All Activities

I created a Toolbar and a "3-dot menu" that goes on the Toolbar. In my HomeActivity, the menu displays. However, in other activities, the menu doesn't (but the Toolbar does). I'm not sure what's happening here.
Here is the toolbar.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="#color/colorPrimary"
android:elevation="4dp"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="#style/Theme.AppCompat.Light">
</androidx.appcompat.widget.Toolbar>
Here is the settings_menu.xml that should show the 3-dot menu.
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:id="#+id/item2"
android:title="Item 2"
app:showAsAction="never" />
<item android:id="#+id/item3"
android:title="Item 3"
app:showAsAction="never">
<menu>
<item android:id="#+id/subitem1"
android:title="Sub Item 1"/>
<item android:id="#+id/subitem2"
android:title="Sub Item 2"/>
</menu>
</item>
</menu>
Here is the onCreate() method of HomeActivity, where the menu DOES appear.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// deactivate the fullscreen mode used by the splash screen
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
setContentView(R.layout.activity_home);
toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
tvClients = findViewById(R.id.tvClients);
tvClientList = findViewById(R.id.tvClientList);
tvClientName = findViewById(R.id.tvClientName);
buttonSettings = findViewById(R.id.buttonSettings);
buttonLogout = findViewById(R.id.buttonLogout);
buttonCreateNewClient = findViewById(R.id.buttonCreateNewClient);
// Initialize Firebase Auth
mAuth = FirebaseAuth.getInstance();
And here is the onCreate() method of another activity, accessed through a button click in the HomeActivity, where the menu button does NOT appear.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_create_new_client);
toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setTitle("Create New Client");
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
etClientFirstName = findViewById(R.id.etClientFirstName);
etClientLastName = findViewById(R.id.etClientLastName);
etClientAge = findViewById(R.id.etClientAge);
buttonCreateClient = findViewById(R.id.buttonCreateClient);
buttonCreateClient.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
createClient();
}
});
// Initialize Firebase Auth
mAuth = FirebaseAuth.getInstance();
}
you have to override the onCreateOptionsMenu in every activity you need menu to be shown within
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.settings_menu, menu);
// return true so that the menu pop up is opened
return true;
}
check this thread for full details

PNG Sequence Animation in Android Studio

So I currently have this code in my oncreate. I really want to create an animation as soon as the user opens the app using a png sequence.
public void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_home);
view = (ImageView) findViewById(R.id.imageAnimation);
view.setBackgroundResource(R.drawable.animation_list_filling);
frameAnimation = (AnimationDrawable) view.getBackground();
And..
#Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
if (hasFocus) {
frameAnimation.start();
} else {
frameAnimation.stop();
}
}
And I have created an xml file
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="false">
<item
android:duration="100"
android:drawable="#drawable/tugs_00000"/>
<item
android:duration="100"
android:drawable="#drawable/tugs_00001"/>
<item
android:duration="100"
android:drawable="#drawable/tugs_00002"/>
...
<item
android:duration="100"
android:drawable="#drawable/tugs_00043"/>
</animation-list>
But once I run the app, it keeps on crashing. There were no errors in Android Studio present.

Action Button not appearing on Android

I've been trying to get an Action Button working on my Android app, and been failing miserably. I've read so many questions and answers and nothing seems to work. The video link always just appears in the menu which I do not want
Here is the menu Xml.
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:puertogaleradivesites="http://schemas.android.com/apk/res-auto" >
<!-- Search, should appear as action button -->
<item android:id="#+id/youtube"
android:icon="#drawable/ic_play_arrow_black_24dp"
android:title="YouTube"
puertogaleradivesites:showAsAction="ifRoom"/>
<item android:id="#+id/action_settings"
puertogaleradivesites:showAsAction="never"
android:title="Settings"/>
</menu>
And here is the main Java Fragment
public class DivesiteFragment extends Fragment {
private Divesite mDivesite;
private TextView mDepthField;
private TextView mSiteDescription;
private TextView mDistance;
private ProportionalImageView mImageView;
public static final String EXTRA_SITE_ID = "com.blueribbondivers.extraSiteID";
private Context mContext;
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
UUID crimeId = (UUID)getActivity().getIntent()
.getSerializableExtra(EXTRA_SITE_ID);
mDivesite = DiveSites.get(getActivity()).getDiveSite(crimeId);
mContext = getActivity().getApplicationContext();
//getActivity().getActionBar().setTitle(Html.fromHtml("<font color=\"#1c3565\">" + mDivesite.getName() + "</font>"));
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_divesite, parent, false);
Resources resources = mContext.getResources();
//setHasOptionsMenu(true);
mDepthField = (TextView)v.findViewById(R.id.divesite_display_depth);
mDepthField.setText(mDivesite.getMaxDepth());
mSiteDescription = (TextView)v.findViewById(R.id.divesite_display_description);
mSiteDescription.setText(mDivesite.getSiteDescription());
mSiteDescription.setMovementMethod(new ScrollingMovementMethod());
mDistance = (TextView)v.findViewById(R.id.divesite_display_distance);
mDistance.setText(mDivesite.getTravelTime());
mImageView = (ProportionalImageView)v.findViewById(R.id.divesite_display_imageView);
String imageName = mDivesite.getPhoto();
final int resourceID = resources.getIdentifier(imageName,"drawable",mContext.getPackageName());
mImageView.setImageResource(resourceID);
setHasOptionsMenu(true);
return v;
}
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.divesitemenu, menu);
super.onCreateOptionsMenu(menu,inflater);
}
}
I have the full project on Github also https://github.com/Jonnyblue/PuertoGaleraDiveSites
Any help would be greatly appreciated.
For displaying menus like Youtbe, gmail:
You should use yourapp:showAsAction="always"
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:yourapp="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" tools:context=".HomeActivity">
<item
android:id="#+id/action_search"
android:icon="#drawable/ic_search"
yourapp:showAsAction="always"
android:title="#string/search"/>
<item
android:id="#+id/action_refresh"
android:icon="#drawable/ic_refresh"
yourapp:showAsAction="always"
android:title="#string/refresh"/>
</menu>
Hope it will help you.
Your activity extends FragmentActivity. If you'd like a backward compatible action bar that works the same on all API levels (things changed significantly in API 21), you should instead extend AppCompatActivity
If you want to show an ActioBar item a as a button but not as an overflow menu, You need to define an android:orderInCategory for it. So that it doesn't comes inside the overflow menu.
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/action_search"
android:icon="#drawable/ic_magnify"
android:orderInCategory="100"
app:showAsAction="ifRoom|collapseActionView"
android:title="#string/noticiation_count"/>
</menu>
Also as you are using fragments its safe to use this line inside the onCreate() method,
setHasOptionsMenu(true);
Tested and working perfectly with Fragments.

layer-list and ClipDrawable

I have a problem with a simple piece, and I can't understand what is going wrong. What is the problem here? It should be simple.
I'm trying to do a 2-layer layer-list with one layer as a clip. Here is clip_source.xml which I made by instructions:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:drawable="#drawable/img_spare"/>
<item android:id="#+id/clip">
<clip
android:clipOrientation="horizontal"
android:drawable="#drawable/img_filled"
android:gravity="left" />
</item>
</layer-list>
The Activity is simple :
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageView img = (ImageView) findViewById(R.id.imageView1);
LayerDrawable ld= (LayerDrawable) getResources().getDrawable(R.drawable.clip_source);
ClipDrawable cd=(ClipDrawable) ld.findDrawableByLayerId(R.id.clip);
cd.setLevel(4000);
//((ClipDrawable) ld.findDrawableByLayerId(R.id.clip)).setLevel(4000);
img.setImageDrawable(ld);
}
}
cd.setLevel(4000) (or if I try getLevel()) crashes everything with an unknown Exception. Is there a simple way to fix this?

Categories