Monday, 12 December 2011

Dynamic change fragment using Java code

Last exercise show how to "Programmatically add fragment". Such that we can change fragment in run-time using using Java code.

Dynamic change fragment using Java code

Modify from the last exercise, keep MyFragment.java and fragmentlayout.xml no change.

Create a new /res/layout/fragmentlayout2.xml to define our second fragment.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="It's Fragment 2" />
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="center"
android:src="@drawable/ic_launcher"/>
</LinearLayout>


Create MyFragment2.java to implement our second fragment class.
package com.exercise.FragmentTest;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class MyFragment2 extends Fragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View myFragmentView = inflater.inflate(R.layout.fragmentlayout2, container, false);

return myFragmentView;
}

}


Modify main.xml, to add buttons to change fragment.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >

<LinearLayout
android:layout_width="0px"
android:layout_weight="1"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Select Fragment:" />
<Button
android:id="@+id/displayfragment1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Fragment 1" />
<Button
android:id="@+id/displayfragment2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Fragment 2" />
</LinearLayout>
<LinearLayout
android:id="@+id/myfragment"
android:layout_width="0px"
android:layout_weight="3"
android:layout_height="match_parent" />

</LinearLayout>


Modify main activity FragmentTestActivity.java to switch between fragments when button click. You should try to switch between fragments and the BACK key, to see how the fragment transaction work.
package com.exercise.FragmentTest;

import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class FragmentTestActivity extends Activity{

Fragment fragment;
Button btnFragment1, btnFragment2;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btnFragment1 = (Button)findViewById(R.id.displayfragment1);
btnFragment2 = (Button)findViewById(R.id.displayfragment2);

// get an instance of FragmentTransaction from your Activity
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

//add a fragment
MyFragment myFragment = new MyFragment();
fragmentTransaction.add(R.id.myfragment, myFragment);
fragmentTransaction.commit();

btnFragment1.setOnClickListener(btnFragmentOnClickListener);
btnFragment2.setOnClickListener(btnFragmentOnClickListener);
}

Button.OnClickListener btnFragmentOnClickListener
= new Button.OnClickListener(){

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Fragment newFragment;

// Create new fragment
if(v == btnFragment1){
newFragment = new MyFragment();
}else{
newFragment = new MyFragment2();
}

// Create new transaction
FragmentTransaction transaction = getFragmentManager().beginTransaction();

// Replace whatever is in the fragment_container view with this fragment,
// and add the transaction to the back stack
transaction.replace(R.id.myfragment, newFragment);
transaction.addToBackStack(null);

// Commit the transaction
transaction.commit();
}};

}


Download the files.

Related article:
- Lifecycle of Fragment: onCreate(), onCreateView() and onPause().



Programmatically add fragment

Last post show how to inflate layout from XML. The Fragment is hard coded in layout XML, "fragment" element in main.xml. Alternatively, it can be specified as ViewGroup, LinearLayout in my example here. Then get an instance of FragmentTransaction and add the fragment in the ViewGroup Programmatically.

Programmatically add fragment

Modify the main layout, main.xml, to specify the fragment as LinearLayout instead of MyFragment.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >

<TextView
android:layout_width="0px"
android:layout_weight="1"
android:layout_height="match_parent"
android:text="It's TextView in main Activity"
android:background="#555555"/>
<LinearLayout
android:id="@+id/myfragment"
android:layout_width="0px"
android:layout_weight="4"
android:layout_height="match_parent" />

</LinearLayout>


Modify onCreate() method of the main activity to get an instance of FragmentTransaction and add the fragment in the LinearLayout.
package com.exercise.FragmentTest;

import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;

public class FragmentTestActivity extends Activity{

Fragment fragment;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

// get an instance of FragmentTransaction from your Activity
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

//add a fragment
MyFragment myFragment = new MyFragment();
fragmentTransaction.add(R.id.myfragment, myFragment);
fragmentTransaction.commit();

}

}


Both MyFragment.java and /res/layout/fragmentlayout.xml kept no change as last post.

MyFragment.java
package com.exercise.FragmentTest;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class MyFragment extends Fragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View myFragmentView = inflater.inflate(R.layout.fragmentlayout, container, false);

return myFragmentView;
}

}


/res/layout/fragmentlayout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="It's Fragment" />
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/ic_launcher"/>
</LinearLayout>



Download the files.

next:
- Dynamic change fragment using Java code



Sunday, 11 December 2011

A simple exercise of Fragment, inflate layout from XML

A simple exercise of Fragment, inflate layout from XML

Create a new Android project target Android 3.0.

Modify layout, main.xml, to add a our fragment.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >

<TextView
android:layout_width="0px"
android:layout_weight="1"
android:layout_height="match_parent"
android:text="It's TextView in main Activity"
android:background="#555555"/>
<fragment
class="com.exercise.FragmentTest.MyFragment"
android:id="@+id/myfragment"
android:layout_width="0px"
android:layout_weight="4"
android:layout_height="match_parent" />

</LinearLayout>


Implement /res/layout/fragmentlayout.xml, to define the layout of our fragment.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="It's Fragment" />
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/ic_launcher"/>
</LinearLayout>


Add a new class MyFragment extends Fragment. Inside onCreateView(), inflate R.layout.fragmentlayout to generate our view and return it.
package com.exercise.FragmentTest;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class MyFragment extends Fragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View myFragmentView = inflater.inflate(R.layout.fragmentlayout, container, false);

return myFragmentView;
}

}


Keep the main activity no change from the auto-generated version.

Related article:
- Programmatically add fragment



Friday, 9 December 2011

A simple exercise of Fragment

A simple exercise of Fragment

Create a new Android project target Android 3.0.

Modify layout, main.xml, to add a our fragment
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >

<TextView
android:layout_width="0px"
android:layout_weight="1"
android:layout_height="match_parent"
android:text="It's TextView in main Activity"
android:background="#555555"/>
<fragment
class="com.exercise.FragmentTest.MyFragment"
android:id="@+id/myfragment"
android:layout_width="0px"
android:layout_weight="4"
android:layout_height="match_parent" />

</LinearLayout>


Add a new class MyFragment extends Fragment.
package com.exercise.FragmentTest;

import android.app.Fragment;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

public class MyFragment extends Fragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
Context context = getActivity().getApplicationContext();
LinearLayout layout = new LinearLayout(context);
TextView text = new TextView(context);
text.setText("It's Fragment");
layout.addView(text);

return layout;
}

}


Modify the main activity to add our MyFragment.
package com.exercise.FragmentTest;

import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;

public class FragmentTestActivity extends Activity{

Fragment fragment;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

}

}




Wednesday, 7 December 2011

Free Screen Sharing (VNC) Client on Mac OS X Lion/Snow Leopard

Did you know that there is a free Screen Sharing app included on Mac OS X Lion and Snow Leopard? Obviously I didn't until a few days ago. You will find the Screen Sharing app in the following directory:



/System/Library/CoreServices



You can connect to any other Mac simply by launching the app and typing in the IP address of another Mac. Of course that Mac must have Screen Sharing turned on. I have also tried to connect to a RealVNC server on Windows 7 and that does not seem to work. I have not yet tested other VNC server. I will provide a more complete write up in another post.

Tuesday, 6 December 2011

MacLife January Highlights - Chips, Siri, and Rays

Here are some of the highlights from this months MacLife magazine available on the web.



MacLife: The Future of ARM - A great story on where ARM is headed with their processes. More power, less electricity should make for some great devices.



MacLife: iPhone 4s Review - The scoop on what is new with the iPhone 4s.



MacLife: 10 Questions to Ask Siri - I like the fact that Siri has a sense of humor. I look forward to this sort of tech.



Ray Solar Charger - For $50, get your own Solar charger for your iPhone.

Monday, 5 December 2011

Samsung Series 7 Chronos Amazing Laptops Review, Specs and Price

new Samsung Series 7 Chronos
Samsung one of the well-known notebook maker has announced the launch of its new notebook namely, Samsung Series 7 CHRONOS premium notebooks at IFA 2011. The Notebook which has dubbed MAX screen technology. The 7 chronos models will come with an Intel Core i7 processor, a spacious 750GB HDD and 8GB of DDR3 system memory. There's also some extra flash memory on the motherboard that's said to result in faster startup times and enhanced performance.

The new Series 7 CHRONOS notebook can wake from sleep in around two seconds and is ready to go from cold in just 19 seconds, representing a 33 percent improvement on previous models. This is achieved thanks to ExpressCache technology that sees an extra 8GB of flash memory on the motherboard, coupled with the raw power offered by Intel's Core i7 2675QM Quad Core processor running at 2.2GHz.latest Samsung Series 7 Chronos

The CHRONOS will be available in two screen sizes, both with an outdoor-friendly 300-nit brightness and 1600 x 900 resolution. The model featuring the anti-reflective, LED backlit, slim bezel 15-inch display weighs just 4.78 pounds (2.17kg), and the 15.6-inch screen version weighs 5.09 pounds (2.31kg). Both versions are 0.94-inch (23.9mm) thin and feature an AMD Radeon HD6750M GPU with a gigabyte of dedicated GDDR5 memory.

High definition audio is supplied by Samsung's SoundAlive audio technology through a pair of 2W stereo speakers and a 2W embedded subwoofer. The notebooks run on Windows 7 Professional (64-bit edition), have a backlit keyboard that incorporates useful ambient light sensing auto adjustment and are shipped with Samsung's Easy Migration software that help take the strain out of file transfer from an old computer over USB connection, Ethernet or Wi-Fi network.
Samsung Series 7 Chronos
Finishing off the specs are two USB 3.0 ports and a USB 2.0 port, a 4-in-1 media card reader, a Super-Multi optical drive, HDMI-out, a 1.3 megapixel high definition webcam and wireless connectivity in the shape of Bluetooth 3.0 and 802.11b/g/n Wi-Fi.

The included 80Wh, 8-cell battery is said to offer up to eight hours on a single charge, and Samsung's Battery Life Plus technology is also reported to significantly extend the useful life of the battery.

Price about : $1299.99 or �999