Move from one Activity to another with a swipe action in Android Studio

[13017 views]




In any Android user interface is displayed via an activity. In Android app development team, we may have to face so many types of situations where we require to switch between one activity (Screen/view) to other. Let us discuss about switching from one point to other via swipe and sending data to the activities.

Here is the code for handling activities. Let us assume Activity class name is SecondScreen.java

Opening New Activity

To open new activity following startActivity () or startActivityForResult () method is used.

Intent i= getIntent (); i.getStringExtra ("key"); String myemail= i. getStringExtra ("email");

Sending parameter to new Activity

To send parameter to the newly created activity putExtra () method is used.

i.putExtra ("Key", "value"); // Example of Sending mail to next screen as // Key= "email" //value= "xyzmail@gmail.com" i.putExtra ("email", "xyzmail@gmail.com");

Now, I will show you the how to use slide library to embed the swipe feature that will convert into an activity. Let us discuss each of them in details. In activity 2nd we can swipe it off the screen and also can able to slide back to keep the activity opened. There is a open button where we can open the slide and there is unlock button also to keep unlock the slide. When swipe off the screen the activity gets closed. For all of the above process, let us clear it by a simple example. As we have already discussed, there are two buttons in the activity named as Lock and Unlock button. When we click any of the button to call lock slide or unlock slide, method that is right now above empty and in our main activity. Here, we have one open activity button which when we clicked open the second activity.

public void openActivity2 (View v) { Intent intent= new Intent (packageContext: this, Activity 2.class); startActivity (intent); }

Now, we have created a second style with opening angle bracket style and we call it and write it in separate file called

resources-

<resources> <! Base application theme --> <style name="AppTheme" parent= "Theme.AppCompat. Light.darkActionBar"> <! -- Customize your theme here. --> <item name= "colorPrimary">@color/colorPrimary</item> <item name= "colorPrimary">@color/colorPrimary</item> <item name= "colorAccent">@color/colorAccent</item> </style> <style name= "AppTheme.SlideActvityTheme" // Add theme dot slider activity theme and change name to set by user <item name= "android: windowIsTranslucent">tr</item> <item name= "android: WindowBackground">@android: color/transparent</item> </style> </resources>

Now, let us discuss unlock file coding-

public class Activity2 extends AppCompatActivity { private SlidrInterface slidr; protected void oneCreate (Bundle savedInstanceState) { super. onCreate (savedInstanceState); setContentView (R. layout. activity_2); slidr= Slidr.attach (activity: this); } public void lockSlide (View v) { slidr. lock (); } }

Receiving parameter on new Activity

To receive parameter on new created activity getStringExtra () method is used.

Intent i= getIntent (); i.getStringExtra ("key"); String myemail= i. getStringExtra ("email");

Open new Activity and follow result

In most of the situations, we can expect some data from newly created activity. In those situations, startActivityForResult () method is used. And once this new activity is closed then we should have to use onActivityResult () method to read the result.

Intent i= new Intent (getApplicationContext (), SecondScreen.class); startActivityForResult (i, 100); //100 is code to identify the result // Function to read the result from newly activity Protected void onActivityResult (int requestCode, int resultCode, Intent data) { Super. OnActivityResult (requestCode, resultCode, data); If (resultCode==100) { Stringmywebsite = data. getExtras (). getExtras (). get (?result?); } }

                 





Comments








Search
Need any Help?


Hot Deals ends in





Earn Money While You Sleep. Join Now to avail Exclusive Bonus




Technical Quiz:



Search Tags