Friday, May 18, 2018

Refreshing - Android fragments


Fragments are ways by which you can create multiple views in a single Activity, to avoiding to create multiple activities. You can add / remove fragments while the activity is running. 

You can create a fragment in 2 ways: - static using XML and dynamic using programmatically

XML: 

  • Create 2 fragment classes using the wizard. This will create a class that extends Fragment and create the layout xml accordingly. 
    • headerFragment
    • ArticleFragment. 
  • Now add the 2 fragments in the main activity XML file as below. 
<fragment
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:id="@+id/headline_fragment"
    android:layout_weight="1"
    android:name="io.ctrlspace.fragmentexample.HeadlineFragment"> </fragment>

 <fragment
     android:layout_width="match_parent"
     android:layout_height="0dp"
     android:id="@+id/article_fragment"
     android:layout_weight="1"
     android:name="io.ctrlspace.fragmentexample.ArticleFragment"> </fragment>


Now this is not of much use since we add these in static xml, often we may need to add dynamically fragments based on runtime logic. 


Programmatically: 

  • Create a simple Empty Activity - say MainActivity. 
  • In the main Activity layout file, add a layout to hold the fragments, say a frame layout 
  • Now create a Fragment using Android studio - call it as HomeFragment. This will create a Java class that extends Fragment and a fragment layout file. 
  • Add some content for the fragment in the layout design to identify that its a fragment content, say add a text box with a text inside first fragment. 
  • In the main Activity add the following code. 
    • Get the FragmentManager instance
    • Start the transaction of the fragment manager.
    • Add or replace the fragment using the add / replace method in the fragment transaction, pass the fragment container and the fragment to be added or replaced to this method. 
    • Commit the transaction. 

public static FragmentManager fragmentManager;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    fragmentManager = getSupportFragmentManager();
    if(findViewById(R.id.fragment_container) != null) {
        if (savedInstanceState != null) {
            return;
        }

        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        HomeFragment homeFragment = new HomeFragment();
        fragmentTransaction.add(R.id.fragment_container, homeFragment, null);
        fragmentTransaction.commit();
    }
}

Fragment to Activity communication is handled in a different way - you can view the source code for this here. 


Thursday, May 10, 2018

Google IO 2018 highlights


Google IO 2018 Highlights:
  • Google assistant: 
    • AI: speech conversation from AI to actual person and taking decisions, Booking for a haircut, room, ordering item in Starbucks. 
  • Android P: 
    • Effective battery utilization based on usage pattern of the user by optimizing battery usage on apps that you might use, adaptive brightness. 
    • Understands more about you and what are u going to do next. 
    • Lets you decide how much time you can spend on your app (well there are apps already, now its part of Android P). 
    • Turn your phone down and it goes to vibration.
  • Maps: 
    • Walking navigation. 
    • Augmented reality in showing information on nearby places 
    • VPS: Visual positioning system. 
    • Better way to choose restaurants, maps can show customized preferred restaurant by showing your match score based on your interests. 
  • Speech conversion from morse code for challenged people. 
  • Lens
    • Smart text selection: tap the item in your food menu via the phone camera and see what dish it is. 
    • Style match: Find price and product details based on the picture which your phone camera is pointing to. 
    • Point and view product details demo. 
  • Gmail
    • Smart compose: provide intelligent suggestions while you type your email.
  • Photos:
    • Photos to PDF
    • Coloring old photos. 

Reference: