Wednesday, February 13, 2013

Fragments Simple Example - Version 1


We will make an Activity that will display 2 fragments in its layout.
One fragment will be above the other just like the screen. Both fragments will be of the TextView type and will display text.

So here is what we 're going to have:



Where in the above image is shown the TextView that we are going to define in the layout for Fragment A and respectively down the TextView in layout for Fragment B.




public class Fragment_A extends Fragment {

  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

          return inflater.inflate(R.layout.fragment_a_layout, container, false);

     }
}
and the fragment_a_layout as we said is a TextView :
<linearlayout 
    android:layout_height="match_parent" 
    android:layout_width="match_parent" 
    android:orientation="vertical" 
    xmlns:android="http://schemas.android.com/apk/res/android">
    
<textview 
    android:id="@+id/tv" 
    android:layout_height="wrap_content" 
    android:layout_width="fill_parent" 
    android:text="Fragment A">
</linearlayout>
Respectively we make up the class for the Fragment B and layout as fragment_a_layout. Now what's left is to bind the 2 fragments on Activity's layout.

main_layout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainLayoutActivity" >

    
      <fragment 
          android:name="org.example.fragmentsapp.Fragment_A"
          android:id="@+id/id_frag_A"
          android:layout_width="100dp"
          android:layout_height="50dp"
          android:layout_marginBottom="20dp" />

      <fragment
          android:id="@+id/id_frag_B"
          android:name="org.example.fragmentsapp.Fragment_B"
          android:layout_width="100dp"
          android:layout_height="50dp"
          android:layout_below="@id/id_frag_A" />

</RelativeLayout>


What you must take care is that we use RelativeLayout because its much more dynamic and flexible than LinearLayout and thats something we are going to need for the next Fragment Tutorials.
Also every <fragment> element has its own name, ie for Fragment A we call it as android:name="org.example.fragmentsapp.Fragment_A" and the same for B. Later when we are going to use them inside the Activity scope that will be based on the id we have define.

So our Activity is :
package org.example.fragmentsapp;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;

public class MainLayoutActivity extends FragmentActivity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main_layout);
	
	}
}


Keep in mind that we refer also to APIs lower than API 11 so we extend FragmentActivity and not Activity.
If you target device with android 3.0 or later then you can change FragmentActivity with Activity.


FragmentActivity is something that we can use because of the  Support library

Tuesday, November 27, 2012

No enclosing instance of type First is accessible.

Error :  "No enclosing instance of type First is accessible. Must qualify the allocation with an enclosing instance of type First "

Solution: You cannot instantiate a non-static inner class from a static context like main.

Sunday, November 25, 2012

ListView with Objects

Let's say you have just parsed some JSON data and you want to display them to a ListView.
For Simplicity sake I will assign all the properties of each item to only one TextView so we won't need to create a custom ArrayAdapter.

Assume that's our JSON data :

[  {"id":"1","title":"Homeland"},{"id":"2","title":"Breaking Bad"}{"id":"3","title":"Dexter"},{"id":"4","title":"Californication"}  ]

There's a JSON Array and that's everything in between [, ] and there are four  JSON Objects all of them with id and title properties.

First of all we are gonna create a class for our Objects :

public class TvShow() {
int id;
String title;
     public TvShow( int id, String title) {
        this.id = id;
    this.title = title;
       }
     public int getID() {
    return id;
    }
        public String getTitle() {
    return title;
    }
     @Override
    public String toString(){
String toReturn = "ID = "+id + " Title= " +title ;
    return  toReturn;
    }
}
Note that we need to Override the toString method for the Object TvShow so that every TextView in the list will display strings.


Initialize an ArrayList of  <TvShow> Obejcts :

     ArrayList<TvShow> alist = new ArrayList<TvShow>();

 So finally we store our data to a JSONArray object :

   JSONArray jArray = new JSONArray(result); //result is the String response we got from our web service

and get the length of the array. int len = jArray.length();

All we need to do now is a for statement to run the array for each item.

   for (int i = 0; i < len; i++) {
JSONObject jObj = jArray.getJSONObject(i);
            int _id = jObj.getInt("id");
String _title = jObj.getString("title");
                            JSONData jData = new JSONData(_id, _title);
                            alist.add(jData);  
}
   } catch (Exception e) {
// TODO: handle exception
    e.printStackTrace();
return alist;

Now we have our ArrayList alist with TvShow Object's data.

            ArrayList<TvShow> results = GetResults(); //assuming the GetResults returns the alist from above


In our XML file we create a ListView and grab its id to the ListView item :

        ListView lv= (ListView)findViewById(R.id.list );

Next we need to define our Adapter :
            ArrayAdapter<TvShow> arrayAdapter = new ArrayAdapter<TvShow>(this, android.R.layout.simple_list_item_1 , results );
The constructor is : 

public ArrayAdapter (Context context, int textViewResourceId, T[] objects)



The last thing we need to do is to assign the adapter to the ListView :
ls.setAdapter(arrayAdapter);

Now our ListView assings to an Object for every TextView item in the list.

Wednesday, October 17, 2012

Duplicate Lines Eclipse shorcut

Ubuntu 12.04 has a problem with Ctrl + Alt + Down arrow key  since this compination  it is used to change Workspaces.

You can just disable it from compiz so you can still change workspaces with right/left arrow and also can use it in Eclipse to duplicate lines.

The procedure is :


System->Preferences-> ConpizConfigSettingsManager
Click "Advanced Search"
Type "down" in the filter box.
Click on "Desktop wall"
Disable "Move Down".

Thursday, September 20, 2012

200, Stream not found, NetStream.Play.StreamNotFound, clip: '[Clip]

Most probable you have "ad block" add-on. Some videos have ads before they start and this add-on blocks them.
Try to disable it.

Thank you

Tuesday, May 15, 2012

Unable to instantiate activity in android

Most times this error has to do with the Manifest.xml file.

My advice is first to check  the Main Activity name if is the same with the Activity you use in your code:
<activity android:name=".MainActivity" />

And then check if your package got the same name as you named it when you create the Project.
You can find "package" property inside :
 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="" >


It's a common mistake if you import your project code from another project or you change the main activity name.

Tuesday, May 8, 2012

Add project to working set - eclipse

right click on the working set you want and then click on Properties.

Select the project and Add it to the Working set content.