As we want to perform an event of click listener on a button so first of all you should declare a widget of button in your xml file such as
name as ist_activity
name as ist_activity
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/ist_activity"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.stck.fun.contactsofvipsIstActivity"> <Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btn1" android:text="Hello World!"
/>
</RelativeLayout>
then you should go to your java file.and your java file should be like this
named as IstJava
package com.stck.fun.contactsofvips;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
public class IstActivityextends AppCompatActivity {
Button btn1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.ist_activity);
btn1=(Button) findViewById(R.id.btn1);
btn1.setOnClickListener(new View.onClickListener){
pulic void onClick(View v)
{
////perform your desired work
});
}
}
Should Mention tour activity name in android studio's xml file
mine is following
<?xml version="1.0" encoding="utf-8"?><manifestxmlns:android="http://schemas.android.com/apk/res/android"package="contctsofvips.fun.stack.com.myapp"> <applicationandroid:allowBackup="true"android:icon="@mipmap/ic_launcher"android:label="@string/app_name"android:supportsRtl="true"android:theme="@style/AppTheme"> <activity android:name=".IstActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>