Reputation: 1
here is the code of my xml.
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btn2"
android:layout_gravity="center_horizontal"
android:text="Make New Account"
android:onClick="new"
android:textColor="#E74C3C"
android:textStyle="bold"
android:paddingTop="20dp"/>
and here is my code of java.
public Button btn2;
public void onClick(){
btn2=(Button)findViewById(R.id.btn2);
btn2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this,submit.class);
startActivity(intent);
}
});
}
my button is not working yet, even an message will appear after clicking on button that unfortunately application has stopped... what is the reason??
Upvotes: 0
Views: 78
Reputation: 121
Activity
is the most basic Android components is also the most common use of the four components (Activity
, Service
, Content Provider
, BroadcastReceiver
).
The steps to create an Activity
:
Create a new Java class And extends the Activity
Add in the AndroidManifest
<activity android:name=".ActivityClassName"/>
If is to start the interface
<activity android:name=".ActivityClassName">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
Override the onCreate()
function and load layout
Note: The Activity of the Java classes generally ends in Activity
Upvotes: 0
Reputation: 101
You have two ways:
Either
1) Set an onClick listener on the button
Or
2) Set an onClick attribute on the button and create a method
Method 1
Xml file
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btn2"
android:layout_gravity="center_horizontal"
android:text="Make New Account"
android:textColor="#E74C3C"
android:textStyle="bold"
android:paddingTop="20dp"/>
Java File
public Button btn2;
btn2 = (Button) findViewById(R.id.btn2);
btn2.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
Intent intent = new Intent(MainActivity.this,submit.class);
startActivity(intent);
}
});
Method 2
Xml file
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btn2"
android:layout_gravity="center_horizontal"
android:onclick="newAccount"
android:text="Make New Account"
android:textColor="#E74C3C"
android:textStyle="bold"
android:paddingTop="20dp"/>
Java file
public Button btn2;
btn2=(Button)findViewById(R.id.btn2);
public void newAccount(View v) {
Intent intent = new Intent(MainActivity.this,submit.class);
startActivity(intent);
}
Upvotes: 1
Reputation: 2170
The thing is you are calling the onClick function when you have declared the onClick of button as new
Try this :
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btn2"
android:layout_gravity="center_horizontal"
android:text="Make New Account"
android:onClick="submit"
android:textColor="#E74C3C"
android:textStyle="bold"
android:paddingTop="20dp"/>
And then in your activity class declare this function:
public void submit(View view){
Intent intent = new Intent(MainActivity.this,submit.class);
startActivity(intent);
}
That's it. Hope this helps.
Upvotes: 0
Reputation: 3422
Your code is perfect Nida.. Just Add Your activity name in menifest file like below code,
<activity android:name=".submit"/>
Upvotes: 1
Reputation: 1474
Try this code:-
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btn2"
android:layout_gravity="center_horizontal"
android:text="Make New Account"
android:textColor="#E74C3C"
android:textStyle="bold"
android:paddingTop="20dp"/>
Put this in your activity:-
btn2 = (Button) findViewById(R.id.btn2);
btn2.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
Intent intent = new Intent(MainActivity.this,submit.class);
startActivity(intent);
}
});
Upvotes: 1