How to back from the activity without button - java

When I was browsing the slack application I found new feature I liked, which is the return of activity by dragging without pressing the back button as usual.
So is there a specific code to do that? I did a research on how to do that but I couldn't find any explanation for it, so how can I do that.
Here is a short video about the new feature
click here

Thanks to Coding in flow for that. Watch this tutorial for more explanation.
Follow these steps to do the job.
1- We will use a library to do that called Slidr, use the last version.
// swipe the Activity to close
implementation 'com.r0adkll:slidableactivity:2.1.0'
2- Add a new style in your styles.xml called SliderActivityTheme.
This theme will make the background for the activity when swipe it as transparent to show the content of the fragment...etc, "default background white"
<!--Theme for Slider Activity-->
<style name="AppTheme.SliderActivityTheme">
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowBackground">#android:color/transparent</item>
</style>
3- A- Add slide method to your class and called in OnCreate
// Make slider on the Activity
public void Slider () {
Slidr.attach(this);
}
B- Called in OnCreate
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.your_activity_name_here);
// slider the activity
Slider();
}
or just simply add this in your OnCreate directly
// slider the activity
Slidr.attach(this);
4- In your AndroidManifest.xml add the theme that we had to create.
android:theme="#style/AppTheme.SliderActivityTheme"
Make it like that
<activity
android:name=".Activity.ActivityImages.ImagesMorningActivity"
android:launchMode="singleTop"
android:theme="#style/AppTheme.SliderActivityTheme"/>
<activity
That's all, and enjoy with your new feature ;)
Working with Fragment? No problem, check the library page to know how to do that

Related

Android Preset Login Activity - Title/Status bar will not go away. Have tried everything

Before someone marks this as a duplicate, I've spent hours looking through google and StackOverflow for answers to my problem and have not found any.
I've used the Android Studio Login Activity but I can't get the title bar to go away in that one activity (not the whole app). I've read over at least 10 different articles on how to do this, most of them say the same thing and none of it works. In fact, editing my .java class crashes my app. Is there a relation to this and the pre-built code involved in the Login Activity?
I've tried the following blocks of code in different orders in one of my java files:
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN); requestWindowFeature(Window.FEATURE_NO_TITLE);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
I've tried setting the NoActionBar theme in my XML file, as well as adding to the Manifest (which I don't want to do because that blankets the app... I just want the one page (login)'s title bar to go away.
I've tried everything that I could find on here and on google's search. Has anyone else ran into this problem while using the Login Activity in Studio?
Also, I found this answer -
"You should use 'NoActionBar' Style in 'styles.xml'. Than create
toolbar in your layout and add child view to your toolbar, set title
and modify as you like. "
This also does not work. I found that answer here: StackOverflow
Still no luck. I'd appreciate any advice. Thanks for your time!
EDIT: Someone told me to delete the ActionBar Class:
private void setupActionBar() {
ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
actionBar.setDisplayHomeAsUpEnabled(true);
setupActionBar();
} }
Also deleted the call to actionBar. -- This also did not work.
I found this topic on Stack: Android Theme.TitleBar does not work
This was not helpful at all.
I tried this topic too which was also useless: How to hide the title bar for an Activity in XML with existing custom theme
When we create an Activity using Login Template, we see something like this -
public class LoginActivity extends AppCompatActivity implements LoaderCallbacks<Cursor> {....
Now, we can see that our Activity extends AppCompatActivity.
Thus, we need to create a custom style that extends AppCompat Theme, and override it to hide status bar.
First, in styles.xml, create a custom style as follows -
<style name="MyNoActionBarTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="android:windowNoTitle">true</item>
<item name="android:windowActionBar">false</item>
<item name="android:windowFullscreen">true</item>
<item name="android:windowContentOverlay">#null</item>
</style>
Now, in AndroidManifest.xml, declare the "theme" attribute for the LoginActivity as follows -
<activity
android:name=".LoginActivity"
android:theme="#style/MyNoActionBarTheme"
android:label="#string/title_activity_login">
</activity>
As you can see from the image, the status bar is gone and the activity is now full-screen.
Do tell if this does not work for you. I too created a Login Template in Android Studio as the very first step.
If your LoginActivity is derived from AppCompatActivity then use below code to hide action bar.
getSupportActionBar().hide();
And if you are using toolbar as action bar then use this
((AppCompatActivity)getActivity()).getSupportActionBar().hide();
For AppCompat, following solution worked for me:
Add new theme style with no action bar in your styles.xml and set parent="Theme.AppCompat.NoActionBar".
<style name="SplashTheme" parent="Theme.AppCompat.NoActionBar">
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimary</item>
<item name="colorAccent">#color/colorAccent</item>
<item name="android:windowBackground">#color/colorPrimary</item>
</style>
Now implement the same theme style to your splash screen activity in androidManifest.xml
<activity
android:name=".ActivityName"
android:theme="#style/SplashTheme"> // apply splash them here
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
Here is result:

How to customize activities transitions on Lollipop, to work differently on some views?

Background
Lollipop introduces a new way to transition between activities (links here, here, here and here) .
The problem
They said on one of the videos (here) that I can choose exactly how each view will transition, but I can't find how to do it. The only thing I've found is how to set it for all views, except for the "hero" view (which you choose how to transition it to the new activity and back).
For example, let's take the Google Now app, which has this screen:
when you click on the editText, the bottom cards have the "explode" effect, but everything darkens and the imageView behind the editText fade out.
The editText is probably the "hero" view, which transitions between the activities, and because it's on the same location on the screen, this probably doesn't have any visual effect for the user.
What I've tried
I've tried to mimic what Google now did, but as I've written, the imageView also has the "explode" effect, so it goes to the bottom, behind the listView, which is a weird effect (since it gets cropped while animating). I'd prefer it to either animate to a different direction, or just fade out.
Here's a sample code of the transition I'm using:
final Intent intent = new Intent(activity, SearchActivity.class);
ViewCompat.setTransitionName(viewToTransitionFromAndTo, VIEW_TO_TRANSITION_TO);
final ActivityOptionsCompat options = ActivityOptionsCompat.makeSceneTransitionAnimation(activity,
viewToTransitionFromAndTo, VIEW_TO_TRANSITION_TO);
ActivityCompat.startActivityForResult(activity, intent, requestCode, options.toBundle());
in the theme of the activity that starts the other activity, I have this:
<style name="AppTheme.transition" parent="#style/AppTheme">
<!-- transition support -->
<item name="android:windowContentTransitions" tools:targetApi="21">true</item>
<item name="android:windowActivityTransitions" tools:targetApi="21">true</item>
<item name="android:windowEnterTransition" tools:targetApi="21">#transition/explode</item>
<item name="android:windowExitTransition" tools:targetApi="21">#transition/explode</item>
<item name="android:windowSharedElementEnterTransition" tools:targetApi="21">#transition/move_image</item>
<item name="android:windowSharedElementExitTransition" tools:targetApi="21">#transition/move_image</item>
<item name="android:windowAllowReturnTransitionOverlap" tools:targetApi="21">true</item>
<item name="android:windowAllowEnterTransitionOverlap" tools:targetApi="21">false</item>
</style>
and the transition files are:
explode.xml
<?xml version="1.0" encoding="utf-8"?>
<explode />
move_image.xml
<?xml version="1.0" encoding="utf-8"?>
<transitionSet>
<changeBounds />
<changeImageTransform />
</transitionSet>
The question
How can I choose what each view will do upon transition, instead of just saying everything to have the same effect (except for the "hero" view) ?
For example, is it possible to choose "explode" transition for all views except for the "hero" view , and a view that will have a different transition (fade out/in, for example) ?
Please show an example of how to do it. You can use the code I've written above if needed.
Use a TransitionSet and add/exclude certain targets using Transition#addTarget() and Transition#excludeTarget(). For example, let's say you have two views and you want each of them to slide off the screen in different directions. You could create such a transition using the following code:
TransitionSet set = new TransitionSet();
Transition slideUp = new Slide(Gravity.UP);
slideUp.addTarget(view1);
set.addTransition(slideUp);
Transition slideDown = new Slide(Gravity.DOWN);
slideDown.addTarget(view2);
set.addTransition(slideDown);
TransitionSet extends Transition, so the resulting set object can then be used as the window content transition, which will play the two slide transitions simultaneously in parallel.

different color on Action Bar MenuItem in different configurations programmatically

I want to set different colors to text of MenuItem that are in action bar and in different configurations - landscape and portrait.
I figured one way to that was have different colors set in Color and Color-land.I've made a custom theme in styles as follows
<style name="MyAppTheme" parent="#android:style/Theme.DeviceDefault.Light">
<item name="android:actionMenuTextAppearance">#style/MyActionBar.MenuTextStyle</item>
</style>
<style name="MyActionBar.MenuTextStyle"
parent="android:style/TextAppearance.DeviceDefault.Widget.ActionBar.Menu">
<item name="android:textColor" >#color/button_color</item>
</style>
and then set the color black in color resource and white in color-land resource.
This would work as the activity will be created when the configuration changes.
However in my activity when the activity is created first an AlertDialog appears which leads to a REST api call and a ProgressDialog follows while the data is fetched from the server.
The problem lies in here when i would change the configuration the activity will be recreated and i will get the AlertDialog again.
if i set in the manifest <activity android:configChanges="orientation" /> the color of the menu item won't change because the activity is never created again but it will avoid the AlertDialog and would maintain the activity state.
I am looking for a way to change the MenuItem Text color in the onConfigurationChanged event. is there some way to do it?

Actionabar issues coming from full screen

I got a full screen activity that leads to an activity with an action bar. When the scond activity loads, the actionbar is half hidden by the title menu.
Why is this happening and what can I do to prevent it?
EDIT: as seen below, the title bar hides some of the action bar.
1 ) You have to keep same theme for all activity ,put actionbar activity them in application tag in manifest file
2) If you want to use full screen activity then getSupportActionBar().hide(); If you have Simle activity and Actionbar activity then while launching simple Actionbar activity to Simple activity creates problem.
check your activity deceleration in the manifest file. Declarer your activity like this.
<activity
android:name="com.android.MainActivity"
android:theme="#android:style/Theme.Light.NoTitleBar">
</activity>

Show up/back button on android nested PreferenceScreen?

I have a two level PreferenceScreen:
<PreferenceScreen>
general settings
<PreferenceScreen android:key="adv_settings">
more advanced settings
</PreferenceScreen>
</PreferenceScreen>
My problem is that the second screen doesn't show the back/up button on the action bar automatically. How do I make the up button appear on adv_settings?
You can add the arrow by writing a custom ActionBar style to be used with your application theme.
res/values-v11/styles.xml: (or add these to your existing styles.xml)
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="MyTheme" parent="#android:style/Theme.Holo.Light">
<item name="android:actionBarStyle">#style/MyActionBar</item>
</style>
<style name="MyActionBar" parent="#android:style/Widget.Holo.ActionBar">
<item name="android:displayOptions">showHome|homeAsUp|showTitle</item>
</style>
</resources>
Then apply this theme in your AndroidManifest.xml:
<application android:theme="#style/MyTheme">
Note: The obvious way to add this arrow should be to call:
getActionBar().setDisplayHomeAsUpEnabled(true);
once the second screen has loaded, but I think there's an Android bug where getActionBar() always returns the first-tier ActionBar object, as opposed to the one that is currently visible, so setting the arrow dynamically fails.
This may be more work, but you could create two PreferenceAtivity files each with their own PreferenceFragment. Each PreferenceFragment will have its own PreferenceScreen XML (the first level screen, and the second. level screen). From the firsts level screen you launch the second PreferenceActivity with an Intent within the tag. In the second PreferenceActivity you can set the home icon by doing this:
ActionBar bar = getActionBar();
bar.setDisplayHomeAsUpEnabled(true);
and then also had a handler for the home button:
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
if (item.getItemId() == android.R.id.home) {
finish();
}
return false;
}
Assets:
FirstPreferenceActivty
FirstPreferenceFragment
pref_first.xml (layout with PreferenceScreen and Prefernce nodes)
SecondPreferenceActivty
SecondPreferenceFragment
pref_second.xml (layout with PreferenceScreen and Prefernce nodes)
If the button does not come automatically, you could add it manually like done on these links:
Android: How can make custom PreferenceScreen?
http://pastebin.com/334Eip35
There is a example code in second answer of that SO question and the snippet in it is taken probably from the another pastebin location.

Categories

Resources