Saturday 14 September 2013

Android - Tutorial 1 - Click Counter




  • Create a new Android Application Project named ClickCounter. Leave all options as their default values except the project name.
  • Go to the layout designer by double click on res\layout \activity_main.xml such as below picture: 
 
  • Now, you can design the graphical layout of your project by drag-and-drop the component from “Form Widgets”. By adding a Button and a Large Text, create the following layout. 
    • You can change the Text of your button using Properties window in the right side of your IDE such as you can see in the picture:

    • By clicking on each object within the graphical layout (such as the button), you can change the details of the object in Properties window. These properties are stored in an XML file which is named “activity_main.xml” in this project.
    •  You don’t need to edit the xml file directly
    •  For each graphical object, you need to know the id name which is necessary for manipulating its properties from your Java classes.
  •  In order to manage click activity for the button in Android, you can do as below:
    •  Open MainActivity class from the src folder in Package Explorer window (at the left side of your IDE)
    •  Then, declare your button in onCreate method of the class like
      final Button btn = (Button) findViewById(R.id.button1);
    • Then use the button variable as below:
      btn.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View v) {
      myClick(v); /* my method to call new intent or activity */
      } });
    •  Handle the click event:
      public void myClick(View v) {
      TextView txCounter = (TextView) findViewById(R.id.textView1);
      txCounter.setText("yourtext");
      }
    • Now, the following is the source code of class MainActivity for implementing our simple ClickCounter program.
      package com.example.clickcounter;
      import android.os.Bundle;
      import android.app.Activity;
      import android.view.Menu;
      import android.view.View;
      import android.widget.Button;
      import android.widget.TextView;
       

      public class MainActivity extends Activity {private long counter;
      @Override
      protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      // set default counter for the text box
      counter = 0;
      TextView txCounter = (TextView) findViewById(R.id.textView1); txCounter.setText(String.valueOf(counter));
      Button clickMeBtn = (Button) findViewById(R.id.button1);
      clickMeBtn.setOnClickListener(new View.OnClickListener() {
      public void onClick(View v) {
      myClick(v); /* my method to call new intent or activity */
      }
      });
      }
      @Override
      public boolean onCreateOptionsMenu(Menu menu) {
      // Inflate the menu; this adds items to the action bar if it is present.
      getMenuInflater().inflate(R.menu.main, menu);
      return true;
      }
      public void myClick(View v) {
      counter++;
      TextView txCounter = (TextView) findViewById(R.id.textView1); txCounter.setText(String.valueOf(counter));
      }
      }

No comments:

Post a Comment