Friday, 24 July 2015

Lesson 5- Fragments

This took me a while to understand.

But here's the break down.

Before Honeycomb the usual navigation was from one activity to the next.

With fragments, it was possible for several user components to exist within a single activity.

Each fragment involves a single XML file with a java class.


Create an XML file with 2 relativelayout components.

Now create 2 XML files with a design/background of your choice and create 2 java class files coresponding to them.

public class Fragmentone extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    View v=inflater.inflate(R.layout.fragmentonelayout,container,false);
    return v;
}
}

This is what your java class will look like.

You'll need onCreateView and you'll have to return the view with the inflated XML file.


Now in your main activity, you'll have to access the class.

FragmentManager fm=getFragmentManager();
                FragmentTransaction ft=fm.beginTransaction();
                Fragmentone f1=new Fragmentone();
                ft.add(R.id.fr1_id, f1);
                ft.commit();


Use a fragment manager, and assign it to a fragmenttransaction after using begintransaction()
Then create a object of your fragment class, to invoke the inflater.

Then add the realtivelayout id, with your class object.

Finally commit.

Tuesday, 21 July 2015

Lesson 4- Life Cycle

1) Intent filter is inside the initial activity you want to launch.
2)When you open the app it goes

onCreate
onStart
onResume

3)An activity is in pause as long as you can partially see it. Like Google Play's "Do you wish to install popup. The activity behind it, is currently in pause()

4)Once it is fully gone, it goes into onstop()

5) Why use onpause?

  • Stop animations or other ongoing actions that could consume CPU.
  • Commit unsaved changes, but only if users expect such changes to be permanently saved when they leave (such as a draft email).
  • Release system resources, such as broadcast receivers, handles to sensors (like GPS), or any resources that may affect battery life while your activity is paused and the user does not need them.
6)Call super class when using onPause()

Why?

The classes that make up the Android SDK can be incredibly complex. For instance, both activities and fragments must perform a number of operations in order to function properly (i.e. managing life cycle, optimizing memory usage, drawing the layout to the screen, etc.). Requiring the client to make a call to the base class (often at the beginning of the method) ensures that these operations are still performed, while still providing a reasonable level of abstraction for the developer.


7)Onpause is good when you're sure the users want options like autosave. Such as drafting an email. In that case, it'll be a temporary save and it'll be fast. But operations like writing to a database should be implemented on OnStop. Otherwise you'll see the activity lag.

8)Use onResume to initalize components that were paused in OnPause();

9)OnStop is called when your app isn't visible anymore.

Eg. Using the multitasking button to switch from one app to the next

Eg. Switching from one activity to the next . When you hit the back button, the original activity will go into onrestart()

10) Onrestart() jumps into onstart() so you can perform tasks that are essential when the app starts for the first time or starting again, like checking if your gps is enabled.

11)If you  rotate your screen, your current activity is killed and restarted from onCreate() (since it has to draw the layout all over again.)

In that case EditText text entered wouldn't be lost, but saved variable values may be lost.

So you can save variables before clearing the activity by using onsavedinstance() and accessing it with a bundle.

Add data to the bundle with .putint(String name,int value)

Call the superclass afterwards.

12)To access the saved data, use onrestorestate() after the super.onCreate(savedinstance), just be sure to check if the saved instance is null.

You can also implement it as a separate function just like onsavedinstance(), but in this case, it'll be called after the onStart() method and not right after onCreate() 

Saturday, 18 July 2015

Lesson 3- Other devices

1) A better reason why all your strings are stored in a resource called Strings.xml. It's easier to support multiple languages!

All you have to do is create a separate folder for the language you want to use, and define a strings.xml in it.

Eg. values-fr folder, would mean for France.

So inside it define your strings such as "hello world" as "Bonjour". Android willl automatically pick the right language depending on the language you use on your phone.

2)To create different layouts create different folders such as
layouts
layouts-land (For landscape)
layouts-large (For large devices)
layouts-large-land(For large devices and landscape)

3) For pixel densities you have xhdpi, hdpi,mdpi,ldpi

The ratio is
2.0
1.5
1.0
.75

So if you're creating an image for xhdpi as 200x200 pixels , you'll need to resize the image to 150x150 and place it in the hdpi drawable folder for phones of hdpi.

4) If you need to check the version of android you're running, and add features depending on that use

build.version.sdk_int > build.version_codes.(Your android version)

Eg. You can set action bars if the version.code is greater than honeycomb. Otherwise omit it.

Lesson 2- Action Bars

1) Action bars can be used for easy access of controls.
   (Though it does take up some screen space, I'm not sure how I feel about that. Maybe they could add a swipe feature to pull the action bar down)

2) It's only available from 3.0 or API 11. So update your min sdk to 11 if you want to use it. If you want to use sdk7, you'll have to use the support libraries and use a theme.AppCompat library.

3) You can add action buttons to the action bar by adding <item> to res/menu/main.xml

    ShowasAction is an attribute that determines whether to show the button or not.
   Eg. Ifroom means that, if there's room on the actionbar then display it.

4) You can add the items to the action bar in the onCreateOptionsMenu(Menu menu) method.

5) To specify the action,  use a switch case in onOptionsItemSelected(Menuitem item) to match the clicked buttons id with the ids present and implement a method once the case is found.

6) Instead of creating a button and intents to return back to the home screen, you can define the parent activity in you manifest file. Under your child activity, specify parentActivityName to your parent activity. Then to enable the up button, go to your child class java file and add getActionBar().setDisplayHomeAsUpEnable(true); This is a clean and easy way to return back to your parent activity.

7) You can use a theme by using application android:theme. You have a bunch of predefined themes.

Holo
Holo light etc

Use an appcompat theme if you're on sdk 7


8) To style the action bar, create an XML file, and give your style a name. Reference this name from the theme tag in your manifest file.

Set the parent to the theme you want your application to use. From there, you can specify item such as actionbarstyle and give it a name.

Define the name as a separate tag, giving it the name, parent theme, and item tags such as background.

9) If you want to overlay your action bar just set windowActionBarOverlay to true

 
 

Tuesday, 14 July 2015

Lesson 1

Notes:

1) ViewGroups are containers like relativelayout or linear layouts.
    Views are the widgets like buttons, edittext, etc.
    Viewgroups define how views are to be presented.

2) Use string resources rather than hardcoding strings. It'll make the updation of strings much more simpler if needed, as all the strings will be located in a single XML file.

3) layout_weight. It's an attribute for a widget in a XML file.

   It gives proportions.

  Like a recipe is 2 parts lemon and 1 part soda. If you give a weight to one widget a 1 and another as  2. 1+2=3. So your first widget will have 1/3 of the remaining free space and the other will have 2/3.
Remember to set width as 0dp to avoid confusion.

4) Intents are defined as
Intent intent=new Intent(this,theactivitytobeinvoked.class);
intent.putExtra(keyvalue,actual value);
//define the keyvalue as a constant string so it doesn't collide with other app interactions

5) Receiving it at the other end

Intent intent=getIntent();
String string=intent.getStringExtra(theoriginalactivity.keyname);