Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Onclick method is not working in android Button

I was studying of Onclick events and the listeners in android. On the way, I created a sample app and my aim is to save the given number (register.java) in the database and to show it in an another activity (main.java). But, now on clicking the 'save' button, nothing has been happening. Even the toast method is also not working.

This is my code:

 protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    data = register.this.openOrCreateDatabase("Number", MODE_PRIVATE, null);
    data.execSQL("CREATE TABLE IF NOT EXISTS table1(number varchar(15));");

    e1 = (EditText)findViewById(R.id.mob_num);
    b1 = (Button)findViewById(R.id.save);
    b2 = (Button)findViewById(R.id.go);

b1.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        number = e1.getText().toString();
        data.execSQL("INSERT INTO table1 VALUES('"+number+"')");
        Toast.makeText(getApplicationContext(), "'"+number+"'successfully inserted",Toast.LENGTH_SHORT).show();
        Intent i = new Intent(register.this, main.class);
        startActivity(i);
        finish();

    }
});

b2.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        Intent i =new Intent(register.this,main.class);
        startActivity(i);
        data.close();
        finish();
    }
});

}

Here is my manifest.xml:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="a.a.a"
android:versionCode="1"
android:versionName="1.0" >

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

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


<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >

    <activity android:name=".main"
              android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />

        </intent-filter>

        <intent-filter>
            <action android:name="android.intent.action.PHONE_STATE"/>     
        </intent-filter>


    </activity>
</application>

</manifest>

I know this a very basic thing in android. But, I hope you may help me in this. Sorry and Thanks for your time.

like image 896
Tony Avatar asked Feb 26 '13 11:02

Tony


1 Answers

There's an easy way to register onClickListeners in Android: In your declaration of the button add android:onClick="onClick" and create a method in the Activity containing the button called onClick(View v). From here you can just

switch(v) {
    case R.id.save:
         Toast.makeText(this, "save button has been pressed", Toast.LENGTH_LONG).show();
         break;
    case R.id.go:
         Toast.makeText(this, "go button has been pressed", Toast.LENGTH_LONG).show();
         break;
         }

and so on until you've registered all the buttons in the activity. Of course you can use other variable names than what I did in this example, just remember the onClick method (or whatever you name it) MUST be called with View as parameter and the switch MUST run on that parameter.

like image 158
Zuenonentu Avatar answered Sep 19 '22 10:09

Zuenonentu