Sunday 19 August 2012

Creating Fragments in the simplest possible way-1


What are fragments?


Fragments in Android are like modular sections of a single activity having their own life-cycle.Umm..sort of sub-activity.

Fragments life cycle methods are affected by its host(Parent) life cycle.For ex:When parent activity is paused,so are all the fragments.When parent activity is destroyed,so are all the Fragment.When an activity is running,each of the fragments can be manipulated(Well i haven't tried it yet..:P)independently.

You can read more about fragments in 

http://developer.android.com/guide/components/fragments.html


Creating fragments

Like Activities,fragments also have their Life cycle callback methods.


To create a fragment you need to override at least its "onCreateView(Bundle)".This method has to return a view and that view is the root of your fragments layout.

Steps to create fragments

Step1:Create a layout XML file(simple_text_fragment.xml)
---------------------------------------------------------------------------------------------

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

    <TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="TextView" />

    <TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Large Text"
    android:textAppearance="?android:attr/textAppearanceLarge" />

</LinearLayout>
---------------------------------------------------------------------------------------------

Step 2:Create a class that extends Fragment and Override its onCreateView method
---------------------------------------------------------------------------------------------
public  class Example_fragment extends Fragment {
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
Log.v("Fragments","onAttach");
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.v("Fragments","onCreate");
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Log.v("Fragments","onCreateView");
                //1st Arg:Fragments Layout
               //2nd Arg:container(parent)
               //3rd Arg:Boolean arg indicating whether the fragment layout should be attached to       the viewGroup(2nd Arg).In our case,it is no as the system itself is doing it.

return inflater.inflate(R.layout.simple_text_fragment,container,false);
}
@Override
public void onStart() {

super.onStart();
Log.v("Fragments","onStart");

}
@Override
public void onResume() {

super.onResume();
Log.v("Fragments","onResume");
}
@Override
public void onPause() {
super.onPause();
Log.v("Fragments","onResume");
}
@Override
public void onStop() {
super.onStop();
Log.v("Fragments","onResume");
}
@Override
public void onDestroyView() {
super.onDestroyView();
}
@Override
public void onDestroy() {
super.onDestroy();
}
@Override
public void onDetach() {
super.onDetach();
}
}
---------------------------------------------------------------------------------------------
Step 3:
In you parent layout's XML file include the below snippet 
---------------------------------------------------------------------------------------------

    <fragment 
    android:name="com.example.apidemo.Example_fragment"
    android:id="@+id/myfragment"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
    />

---------------------------------------------------------------------------------------------



Step 4:

Create Root Activity that inflates parents layout XML.

That's it.Your activity should  look something similar like this


Happy Coding,
-Rajeev










Sunday 12 August 2012

How to Extend Array Adapters to Create customized listView ?


The answer i got in the web to the above question is "SCRATCH YOUR OWN ITCH".Hence,am putting on this post so that people who are struggling to get this thing right,need not have to spend lot of time scratching their own itch..:p.

You can use the below code to use ListViews along with other views in the layout or ListView alone.
Lets get Started??.. Yeah.
There are three simple steps in creating a customized List View

  • Get the ListView in your layout by specifying its resource ID
  • Get the instance of your own ArrayAdapter class
  • Bind ListView to the adapter



Get the ListView in your layout by specifying its resource ID

Get the ListView instance in layout by its ID.

// Get the list View
mCustomListView = (ListView) findViewById(R.id.db_menu);

Get the instance of your own ArrayAdapter class and Bind ListView to the adapter

We can  combine steps 2 and 3 and implement it as below

// set the Adapter
mCustomListView.setAdapter(new DashBoardListAdapter(
getApplicationContext(), R.layout.dashboard_custom_list));

the argument R.layout.dashboard_custom_list is the resource ID of your customized view which has to created separately to achieve customization, not your ListView ID in the main layout.

Your Array adapter class should override getView method and getCount method of the parent.
Here's the implementation,
-------------------------------------------------------------------------------------------------
class DashBoardListAdapter extends ArrayAdapter<String> {
private Context mContext;
private String[] mTitle_content, mDesc_content;
private TextView mTitle, mDescription;

public DashBoardListAdapter(Context context, int layout) {
super(context, layout);
this.mContext = context;
setContentValues();
}

@Override
public int getCount() {
return mTitle_content.length;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v;
if (null == convertView) {
LayoutInflater inflator = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = inflator.inflate(R.layout.dashboard_custom_list, null);
} else {
v = convertView;
}
mTitle = (TextView) v.findViewById(R.id.db_topic);
mTitle.setText(mTitle_content[position]);
mDescription = (TextView) v.findViewById(R.id.db_description);
mDescription.setText(mDesc_content[position]);
return v;
}

private void setContentValues() {
mTitle_content = mContext.getResources().getStringArray(
R.array.bashboard_menu);
mDesc_content = mContext.getResources().getStringArray(
R.array.db_list_desc);
-------------------------------------------------------------------------------------------------------

Thats it,your listView is ready to use.In the listView by taking a seperate layout,i have changed the textView appearance in the listView and it looks lyk,



Happy Coding,
-Rajeev


Saturday 11 August 2012

Messing with Scroll Views in android

The post is regarding the usage of scroll views in android.The layout for the same is as below.
--------------------------------------------------------------------------------------------------
<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" >

    <ScrollView
        android:id="@+id/tc_termsConditions"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/tc_buttoncontainer" >

        <RelativeLayout
            android:id="@+id/tc_textHolder"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >

            <TextView
                android:id="@+id/tc_title"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerHorizontal="true"
                android:text="@string/tc_title"
                android:textSize="20sp"
                android:textStyle="bold"
                android:typeface="serif" >
            </TextView>

            <TextView
                android:id="@+id/tc_description"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@id/tc_title"
                android:fitsSystemWindows="false"
                android:singleLine="false"
                android:text="@string/tc_content_description" />
        </RelativeLayout>
    </ScrollView>

    <LinearLayout
        android:id="@+id/tc_buttoncontainer"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:layout_alignParentBottom="true"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/tc_btn_decline"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="@string/btn_decline"
            android:textSize="15dip"
            android:typeface="monospace" />

        <Button
            android:id="@+id/tc_btn_accept"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="@string/btn_accept"
            android:textSize="15dip"
            android:typeface="monospace" />
    </LinearLayout>

</RelativeLayout>
----------------------------------------------------------------------------------------------------------