Thursday, February 23, 2012

Android User Interface, Common Layout Objects


The more common types of layout objects to use in android applications are as follows:

  • Frame Layout.
  • Linear Layout.
  • Table Layout.
  • Relative Layout.

Frame Layout:

  • Frame Layout is a blank space on the screen that we can later fill with a single object.
  • All child elements of the Frame Layout are pinned to the top left corner of the screen, cannot specify a location for a child views.
  • Later child views will simply be drawn over earlier ones.
Linear Layout:

  • Linear Layout  aligns all children in a single direction — vertically or horizontally, depending on how you define the orientation attribute.
  • All children are stacked one after the other, so a vertical list will only have one child per row, no matter how wide they are, and a horizontal list will only be one row high (the height of the tallest child, plus padding).
Table Layout:

  • Table Layout positions its children into rows and columns.
  • TableLayout containers do not display border lines for their rows, columns, or cells. The table will have as many columns as the row with the most cells.
  • A table can leave cells empty, but cells cannot span columns, as they can in HTML.

Relative Layout:

  • Relative Layout lets child views specify their position relative to the parent view or to each other (specified by ID). So you can align two elements by right border, or make one below another, centered in the screen, centered left, and so on.
Some other Important View Groups:

  • Gallery
  • GridView
  • ListView
  • ScrollView
  • Spinner




    Wednesday, February 8, 2012

    Passing values from one Activity to other using Bundle


    • In general Bundle is a bunch of things putting together.
    • Here, in following example I am going to send the data from one activity(SenderActivity) to other activity (ReceiverActivity) through bundle.
    • To start a new activity from existing activity we use Intents (description of an operation to be performed) in Android.
    • In this example  , on button click event I am sending bundle of an employee information from SenderActivity to ReceiverActivity( to display the data sent by SenderActivity).
    Step-1: Create a button in main.xml
                <Button         
                            android:layout_width="fill_parent"        
                            android:layout_height="wrap_content"        
                            android:id="@+id/button"        
                            android:text="send bundle data"        
                />
    Step-2: Add a listener for the button in SenderActivity.

                Button button=(Button)findViewById(R.id.button);
                button.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
    // TODO Auto-generated method stub
            
    }
    });
    • Bundle of information will be send between activities through Intent objects.So, now we need to create an Intent object.
    Step-3: Create an Intent object.
               While creating an Intent object specify  ReceiverActivity as the target  passing it to the Intent's constructor.

               Intent intent=new Intent(SenderActivity.this,ReceiverActivity.class);
    Step-4: Create  ReceiverActivity in the package.
                public class  ReceiverActivity extends Activity{
        @Override
                 public void onCreate(Bundle savedInstanceState) {
                 super.onCreate(savedInstanceState);
                 setContentView(R.layout.two);
               }
              }
    Step-5: Design two.xml layout for  ReceiverActivity to display the bundled information.
    • two.xml contains only TextView's to display the employee details.
            <TextView
            android:id="@+id/showName"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="" />
    <TextView
            android:id="@+id/showOrg"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="" />
    <TextView
            android:id="@+id/showAddr"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="" />
    Step-6: Add newly created activity to AndroidManifest.xml
               <activity
                         android:name=". ReceiverActivity " >
                </activity>
    Step-7: 
    • Create a bundle object inside the intent and send data to the bundle using putExtras(). And now pass the bundle object to intent.
                            Bundle b=new Bundle();
           b.putString("Name", "GVR");
           b.putString("Organisation", "Kclink");
           b.putString("Address", "Hyderabad");
           intent.putExtras(b);
    Step-8:
    • To launch an activity call startActivity() method and pass intent object to it. Then the intent will generate to start  ReceiverActivity .
              startActivity(intent);
    Step-9:
    • Now call getIntent.getExtras () from  ReceiverActivity , which will return all extras previously added through putExtras() of the intent that started this activity.
            Bundle bundle=getIntent().getExtras();
            String name=bundle.getString("Name");
            String org=bundle.getString("Organisation");
            String addr=bundle.getString("Address");

    Step-10:
    • Finally, set these values to the TextView's to display.
            tv1.setText(name);
            tv2.setText(org);
            tv3.setText(addr);