Friday, March 25, 2011

Simple but informative Example

Hello Everyone...

Lets take an example of adding two numbers and print the result on a new activity....

For this, Create a simple project and directly go into the main.xml file and make the xml looks like following:

main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
    <TextView 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="First Number:"/>
    <EditText 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/firstedittext"/>
    <TextView 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Second Number:"/>
    <EditText 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/secondedittext"/>
    <Button
        android:id="@+id/showresultbutton"
        android:text="Show Result"
        android:layout_width="150dp"
        android:layout_height="wrap_content"
        android:onClick="@string/HandleShowResultButton"
        android:layout_gravity="center"/>   
</LinearLayout>

Now in above layout i have tried to tell as many things, which could be helpful if we know how to use them and when. Lets discuss them first,

  • android:layout_width="150dp"
as in other places, i have used "fill_parent" or "wrap_content"; meaning is clear from the name itself, like "fill_parent" will fill the parent control and etc.. now if we want to limit the size then better approach then giving size in 'pixels' is to give in 'dp'... by giving in 'dp' the limitation of variable pixel size screens is rule out.

  • android:onClick="@string/HandleShowResultButton"
When declaring '@string' to a property, now it treat the value as name and we must provide the value of this variable in "strings.xml" like:

<string name="HandleShowResultButton">OnClickShowResultButton</string>

Now coming to activity file,

InputNumbers.java
  public class InputNumbers extends Activity
{
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
       
    }
   
    public void OnClickShowResultButton(View view)
    {
        EditText firstNumber = (EditText)findViewById(R.id.firstedittext);
        EditText secondNumber = (EditText)findViewById(R.id.secondedittext);
       
        String str_firstNumber = firstNumber.getText().toString();
        String str_secondNumber = secondNumber.getText().toString();
       
        int i_firstNumber = Integer.parseInt(str_firstNumber);
        int i_secondNumber = Integer.parseInt(str_secondNumber);
       
        //    CONSIDERING WE ARE ADDING TWO INTEGERS    //
       
        int result = i_firstNumber + i_secondNumber;
       
        //    START NEW ACTIVITY AND PASS SOME DATA//
       
        Bundle b = new Bundle();
        b.putInt("Result", result);
       
        Intent newIntent = new Intent(getApplicationContext(), ShowResult.class);
        newIntent.putExtras(b);
       
        startActivity(newIntent);
       
        //    TO DESTROY CURRENT ACTIVITY    //
        this.finish();
       
    }
}

I dont think i have to explain further.. its pretty much clear now...
In above example:
  • you can create new activity
  • pass some value around an activity
  • destroy/finish current activity
  • Retrieve GUI controls from code
  • Implement a Click Listener for a button etc...
If you didn't understand anything, feel free to ask. Source code is attached.

Regards,

5 comments:

  1. hi man!
    i was on stackoverflow, searching for a solution then i saw ur question (http://stackoverflow.com/questions/6097319/save-create-mms-in-inbox-android) i am facing the same problem, i want to programatically insert an MMS into the messaging (inbox), im working on android 1.5.
    did u find any solution for this problem? do share it with me!
    thanks!!

    ReplyDelete
  2. you can reply me back on farhan_pk1988@yahoo.com

    ReplyDelete
  3. ok we have two farhans here... :P first thing is, why dont you use the SO forum for your problem, secondly it was not an easy job to insert mms programmatically but i managed, soon i'll be updating the blog with a post of mms creating. The basic logic is to insert the things as you read them from database.

    ReplyDelete
  4. yup the name confused me too! i was searching SO and what i saw? the problem i wana discuss is already posted on SO and wow, with my name too! :P i posted my question there but no one has replied yet!
    waiting for ur blog post, plz make it as early as possible!

    thanks!

    ReplyDelete
  5. man i am still waiting for your post!!! :(

    ReplyDelete