Skip to main content

Posts

Showing posts from January, 2020

Fragment newInstance

Since the Android system uses the default no-arg constructor when recreating a fragment, we should not pass values over an overloaded constructor with arguments when we instantiate it. Otherwise we get a warning that says “Avoid non-default constructors in fragments: use a default constructor plus Fragment setArguments(Bundle) instead” or we even get an InstantiationException. The correct way to instantiate a new fragment and passing values to it, is by creating a bundle, setting arguments on this bundle and then passing it to our fragment. Inside the fragment we then retrieve this bundle and get the values out of it. A clean way to do this is by creating a so called “factory method”. In MainActivity.java class  add these lines of codes:- import android.support.v7.app.AppCompatActivity; import android.os.Bundle; public class MainActivity extends AppCompatActivity {     @Override     protected void onCreate(Bundle savedInstanceState) { ...

Fragment-to-fragment-communication-with-interfaces

In this Tutorial we will learn  how to send data between two fragments. Since fragments should be modular, reusable components, the communication happens over an interface that the underlying activity implements. This way we can send data from a fragment to an activity and from there to another fragment either by calling a public method or by instantiating a new fragment and sending the values as arguments to it. In previous tutorials we have seen what is fragments and how will we create a fragment. In MainActivity.java class file add these lines of codes:- import android.support.v7.app.AppCompatActivity; import android.os.Bundle; public class MainActivity extends AppCompatActivity implements FragmentA.FragmentAListener, FragmentB.FragmentBListener {     private FragmentA fragmentA;     private FragmentB fragmentB;     @Override     protected void onCreate(Bundle savedInstanceState) {         ...

Notifications & Notification Channels

Part 1 – Notification Channels Part 2 – Tap Action & Action Buttons Part 3 – Large Icon, BigTextStyle & InboxStyle Part 4 – BigPictureStyle & MediaStyle Part 5 – MessagingStyle & Direct Reply Part 6 – Progress Bar Notification Part 7 – Notification Groups Part 8 – NotificationChannelGroups Part 9 – Notification Settings Part 10 – Delete Notification Channels                                     Part 1 – Notification Channels In part 1 we will start by creating notification channels (Categories) which are necessary since Android Oreo (API level 26) to be able to show any notifications. These channels should be created as soon as we start our app, so we will do it in the onCreate method of a class that extends Application. When we create these channels, we have to pass an ID, a name and an importance level. Additionally we can cus...

Contextual Action Mode in android

In this tutorial we will learn, how to activate the contextual action mode, which is very similar to the floating context menu, but shows the menu options in the action bar instead of a pop up menu. We will activate this contextual action menu on a long click, by setting an onLongClickListener on our View. We will set it’s title and implement an ActionMode.Callback interface, in which we then override the oncreateActionMode, onPrepareActionMode, onActionItemClicked and onDestroyActionMode methods. There we inflate our app bar menu with our own menu xml file and handle click events on the single menu items. Lastly we will learn, how to change the color of our action mode app bar and how to prevent it from pushing our toolbar away, instead of displaying on top of it, by using the windowActionBarOverlay attribute in our styles.xml file. In activity_main.xml file add these lines of codes:- <?xml version="1.0" encoding="utf-8"?> <android.s...

Android App Components Activities, Fragments, and Intents

Activities : ========= Activity is one of the most important component for any android app Activities are the User Interface (UI) screens which user see. It is similar to the main() function in different programming languages. Its is the main entry point for user interaction. You can have multiple activities in your app. All your activities must be declared in the manifest file, with their attributes. Every activity has different functions throughout its life, onCreate(), onStart(), onResume(), onPause(), onStop(), onRestart(), onDestroy(). To create activity we can use two methods either we create class file and xml file and include the class in manifest file. ---- method 1  or we can create activity using empty activity like this.. --- method 2 note:- for using method 1 we must include our class file in manifest file  otherwise it will show error in logcat. okay now let us see the activity life cycle. let us program the life cycle of activity in our pro...