This is the start of the many of the Simplified android tutorials and it starts with a very simple android application.To make things easier this whole tutorial follows a set of steps.
- First, Create a Project Named ‘Application01’ by going to File->New->Android Application. Provide the Minimum and Maximum Android Version Requirements as you wish. Keep other project settings as usual and Click ‘Finish’.
- Go inside the Application01->res->layout->activity_main.xml and start adding form items. You can either copy the XML code I’ve provided below or add button yourself.
-
- Then, start Changing Main_Activity.java which is inside Application01->src->com.example.application01->Main_Activity.java. You can either copy or paste the code that I’ve provided below or write the code yourself.
package com.example.application01; import android.app.Activity; import android.app.ActionBar; import android.app.Fragment; import android.os.Bundle; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; import android.widget.Button; import android.widget.TextView; import android.os.Build; public class MainActivity extends Activity { Button dec,inc; TextView dis; int count=0; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); if (savedInstanceState == null) { getFragmentManager().beginTransaction() .add(R.id.container, new PlaceholderFragment()) .commit(); } /* MyCode Starts Here */ dec = (Button) findViewById(R.id.btn_dec); inc = (Button) findViewById(R.id.btn_inc); dis = (TextView)findViewById(R.id.display_value); dec.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub count++; dis.setText(String.valueOf(count)); } }); inc.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub count--; dis.setText(String.valueOf(count)); } }); /* My Code Ends Here */ } @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; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } /** * A placeholder fragment containing a simple view. */ public static class PlaceholderFragment extends Fragment { public PlaceholderFragment() { } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.fragment_main, container, false); return rootView; } } }
- If you intend to write code, make sure to add your Code inside protected void onCreate(Bundle savedInstanceState) method. Declare all the Variables inside public class MainActivity { } class.
- You should cast the Android Widget Button dec to XML Button like, dec = (Button) findViewById(R.id.btn_dec);
- Write the Button Click Event like follows.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/container" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.buttonclick.MainActivity" tools:ignore="MergeRootFrame" > <TextView android:id="@+id/textView_01" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="The Total Value Is : " android:layout_marginTop="10dp" android:textColor="#ff0000" android:textStyle="bold" /> <Button android:id="@+id/btn_incrementer" android:layout_width="fill_parent" android:layout_height="82dp" android:text="Increment" android:layout_marginTop="40sp"/> <TextView android:id="@+id/textView_02" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBaseline="@+id/textView_01" android:layout_alignBottom="@+id/textView_01" android:layout_centerHorizontal="true" android:text="" android:textAppearance="?android:attr/textAppearanceSmall" /> <Button android:id="@+id/btn_decrementer" android:layout_width="fill_parent" android:layout_height="82dp" android:layout_alignParentLeft="true" android:layout_below="@+id/btn_incrementer" android:text="Decrement" /> </RelativeLayout>
dec.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub count++; dis.setText(String.valueOf(count)); } });
No comments:
Post a Comment