user3617734
user3617734

Reputation: 21

Android Eclipse: trouble with button click code

I started off by trying to make a simple app on Eclipse. the target for me was to create a button which simply calls a number. however, after many tutorials and such, I still couldn't figure out where to write the code and what code was the best.

here is my Manifest . I inserted uses permission CALL_PHONE since it is required (As I learned from some tutorials)

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.projectrandomfox.randomfox"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="18" />

   <uses-permission android:name="android.permission.CALL_PHONE" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.projectrandomfox.randomfox.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>
</application>

here is my activity_main

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >


<TextView
    android:id="@+id/txt"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Select VAS"
    android:textSize="40dp"
    android:layout_gravity="center" />

<Button
    android:id="@+id/balance"
    android:layout_width="250dp"
    android:layout_height="wrap_content"
    android:text="Check Balance"
    android:layout_gravity="center"
     /> </LinearLayout>

here is my activity java

package com.projectrandomfox.randomfox;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;

public class MainActivity extends ActionBarActivity {

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


}

}

THATS ALL. please help me figure out where to write code in activity, and what code to write it would mean a lot to me :)

Upvotes: 0

Views: 54

Answers (2)

user184994
user184994

Reputation: 18271

Add this inside your onCreate method:

Button button = (Button) findViewById(R.id.balance);

myButton.setOnClickListener(new OnClickListener() 
{
    @Override
    public void onClick(View view)
    {
        Intent intent = new Intent(Intent.ACTION_CALL);
        intent.setData(Uri.parse("tel:" + telephoneNumber));
        startActivity(intent);
    }
}

All you need to do is set the telephoneNumber variable

Upvotes: 1

ashishduh
ashishduh

Reputation: 6699

First you have to get a reference your Button.

Button myButton = findViewById(R.id.balance);

Then add the onClickListener.

myButton.setOnClickListener(new OnClickListener()
{
    @Override
    public void onClick(View view)
    {
        // Do whatever
    }
}

Upvotes: 0

Related Questions