The Android Button

Button


The button can be define as that part of the widget which is responsible for listening to on click event. It enable user to perform what is called an OnClickListener. It is getton from the class of import.widget.Button.
It can be declared in the .XML or programmatically written in the .JAVA

main.XML

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center">

    <Button
android:id="@+id/btn"
        android:text="click me"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>



MainActivity.java


import android.app.*;
import android.os.*;
import android.widget.*;
import android.view.*;
import android.content.*;

public class MainActivity extends Activity
{
Button btn;
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
btn = (Button) findViewById(R.id.btn);
btn.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
Toast.makeText(getApplicationContext(),"Respond Button",Toast.LENGTH_LONG).show();









}
});

}
}






When you run your app this is what you will get after you click on the button.
Firstly, we declared our button in the main.XML as part of our interface. What we should always take note is the ID. Then an instance variable was created in our MainActivity
Button btn;
We then find the button id
btn = (Button)FindViewById(R.id.btn);
Here is the most important part. Listening to on click listeners

btn.SetOnClickListener(View.OnClickListener (){

What this method means is that the button is ready to respond to any click. Our best friend Toast completed the remaining things for us.


The Big Story
On our big story, we shall create a button that we can use to navigate to another activity.
Firstly, create your main.XML
Secondly, create your MainActivity.java

Thirdly, create another XML and called it main2.XML
Lastly, create another java and called it MainActivity2.java

Here, we are mostly interested in what is going on our manifest.XML

main.XML




<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center">

    <Button
android:id="@+id/btn"
        android:text="click me"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>




MainActivity.java

import android.app.*;
import android.os.*;
import android.widget.*;
import android.view.*;
import android.content.*;

public class MainActivity extends Activity
{
Button btn;
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
btn = (Button) findViewById(R.id.btn);
btn.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
Toast.makeText(getApplicationContext(),"Respond Button",Toast.LENGTH_LONG).show();
Intent i = new Intent(getApplicationContext(), MainActivity2.class);
startActivity(i);
}
});


    }
}



main2.xml


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
android:gravity="center">
  <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="This is Activity 2"
android:textSize="80dp" />
</LinearLayout>





MainActivity2.java


import android.app.*;
import android.os.*;
import android.widget.*;
import android.view.*;

public class MainActivity2 extends Activity
{
Button btn;
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main2);
}}





Manifest.XML


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.mycompany.myapp2" >

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

<activity
android:name=".MainActivity2">
</activity>

    </application>

</manifest>















When you click on the button, it navigate you to another activity through the help of the intent. We shall not be talking more about the Intent class because it is covered up in another tutorial.



  @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main2);

This code is found in the MainActivity2 and it totally different from what is seen in the first activity. What made it to be different is the method setContentView(R.layout.main2)
That is, we set the content view of the MainActivity2 to main2. Think of it as a way of connecting the MainActivity2 with the main2.java.

In our manifest what should be taken note of is the element activity declared.

<activity
android:name=".MainActivity2">
</activity>

Watch closely you will see a dot MainActivity2 before the closing of the activity tag.


Here completed our big story of navigating to another activity through the help of a click button listener.
Many application uses button in design and productivity especially app like calculator. We shall be seeing how to program a complex calculator later in other tutorial.



Goodbye....
























No comments:

Post a Comment