I want to forward from current activity to other Activity. But when i click the button i am not able to perform it.I want to move from MainActivity to FeedActivity Please tell what is fault in my code.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
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.sample.test.MainActivity" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView1"
android:layout_below="#+id/textView1"
android:layout_marginLeft="53dp"
android:layout_marginTop="92dp"
android:text="Button" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/button1"
android:layout_marginLeft="20dp"
android:layout_toRightOf="#+id/button1"
android:text="#string/button2" />
</RelativeLayout>
MainActivity.java
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn = (Button) findViewById(R.id.button1);
Button btn2 = (Button) findViewById(R.id.button2);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
Toast.makeText(getApplicationContext(),
"Welcome to android by Vivek", Toast.LENGTH_LONG)
.show();
}
});
btn2.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
Intent intent = new Intent(view.getContext(), FeedActivity.class);
startActivity(intent);
}
});
}
}
When you are creating a new instance of Intent, you need to give it two parameters:
current activity class instance
goto activity class instance
Also, you need to either import View library or use your new OnClickListener as new View.OnClickListener
You can do that by adding this to import:
import android.view.View;
import android.view.View.OnClickListener;
Change your btn2.onClickListener to :
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this, FeedActivity.class);
startActivity(intent);
}
Related
I'm simply trying to use 2 buttons in my MainActivity. One opens a website (which works), the other should directly jump into my second class (which for simplification here, I only set as a second website). However, although button setup is similar to other button, using setOnClickListener as well as variable defined in resources, I'm receiving the error message below.
I already excluded several fragments - the error message seems to be concerning the button for accessing the 2nd class.
Apologies for getting lost with those trivialities...I'm lost but grateful for any hint.
java.lang.RuntimeException: Unable to start activity ComponentInfo{paperpad.app/paperpad.app.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
strings.xml
<resources>
<string name="logo">Logo</string>
<string name="app_name">PaperPad2</string>
<string name="button_go2activity">Start Drawing</string>
<string name="button_openPicture">Load Picture</string>
<string name="button_closePicture">Stop</string>
<string name="button_help">help</string>
<string name="websiteAddress">PaperPad.app</string>
</resources>
MainActivity.java
package paperpad.app;
import androidx.annotation.NonNull;
//etc.
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button_openHelp = findViewById(R.id.button_openHelp);
button_openHelp.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent openHelp = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.youtube.com"));
startActivity(openHelp);
}
});
Button button_go2activity = findViewById(R.id.button_go2activity);
button_go2activity.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent openLink = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com"));
startActivity(openLink);
}
});
}
}
my activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/parentLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="false"
tools:context=".MainActivity">
<ImageView
android:id="#+id/imageView"
android:layout_width="254dp"
android:layout_height="237dp"
android:layout_marginTop="48dp"
android:layout_marginBottom="8dp"
android:contentDescription="#string/logo"
app:layout_constraintBottom_toTopOf="#+id/imageView2"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#drawable/logo_paperpad" />
<ImageView
android:id="#+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="59dp"
android:layout_marginTop="96dp"
android:layout_marginBottom="100dp"
app:layout_constraintBottom_toTopOf="#+id/button_openPicture"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/imageView"
app:srcCompat="#drawable/logo_titel"
tools:ignore="ContentDescription" />
<Button
android:id="#+id/button_go2activity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="269dp"
android:text="#string/button_go2activity"
android:textSize="24sp"
android:typeface="normal"
app:layout_constraintBottom_toTopOf="#+id/button_openHelp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/imageView2" />
<Button
android:id="#+id/button_openHelp"
android:layout_width="45dp"
android:layout_height="25dp"
android:layout_marginBottom="10dp"
android:background="#FFFFFF"
android:text="#string/button_help"
android:textColor="#android:color/black"
android:textSize="12sp"
app:layout_constraintBottom_toTopOf="#+id/linkWebsite"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/button_go2activity" />
<TextView
android:id="#+id/linkWebsite"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:inputType="none"
android:text="#string/websiteAddress"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/button_openHelp" />
</androidx.constraintlayout.widget.ConstraintLayout>
Your button is not in the activity_main layout xml (currently). If in a fragment, it has not been loaded yet.
If it's defined in a fragment you should move this code to the fragment.
Try to declare Buttons on class level and initialize them later:
public class MainActivity extends AppCompatActivity {
Button button_openHelp;
Button button_go2activity;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button_openHelp = findViewById(R.id.button_openHelp);
button_openHelp.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent openHelp = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.youtube.com"));
startActivity(openHelp);
}
});
button_go2activity = findViewById(R.id.button_go2activity);
button_go2activity.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent openLink = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com"));
startActivity(openLink);
}
});
}
}
I have a ListView and a button i want when I press the button it takes me to a Dialogue it has a Text Field and another Button when I fill the text and press the button it will be saved in the list view ( I know how to add items from the same activity to the ListView) but I don't how to add items from another activity or AlterDialoge .
public class Main3Activity extends AppCompatActivity {
EditText et;
ListView lv;
Button bt;
ArrayList<String> arrayList;
ArrayAdapter<String> adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main3);
et = (EditText) findViewById(R.id.TextAdd);
bt = (Button) findViewById(R.id.btnadd);
lv = (ListView) findViewById(R.id.listView);
arrayList = new ArrayList<String>();
adapter= new ArrayAdapter<String>(Main3Activity.this,android.R.layout.simple_list_item_1,arrayList);
lv.setAdapter(adapter);
Button mShowDialog = (Button) findViewById(R.id.btnSave);
mShowDialog.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
AlertDialog.Builder mBuilder = new AlertDialog.Builder(Main3Activity.this);
View mView = getLayoutInflater().inflate(R.layout.dialog_add,null);
final EditText mUser = (EditText) mView.findViewById(R.id.TextAdd);
Button mAdd = (Button) mView.findViewById(R.id.btnadd);
mAdd.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(!mUser.getText().toString().isEmpty()) {
String result = et.getText().toString();
arrayList.add(result);
adapter.notifyDataSetChanged();
Toast.makeText(Main3Activity.this, "Success", Toast.LENGTH_SHORT).show();
}
else {
Toast.makeText(Main3Activity.this, "Error pls Write", Toast.LENGTH_SHORT).show();
}
}
});
mBuilder.setView(mView);
AlertDialog dialog = mBuilder.create();
dialog.show();
}
});
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="5dp">
<TextView
android:id="#+id/Add"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:layout_margin="5dp"
android:textSize="25sp"
android:text="POP UP Window" />
<EditText
android:id="#+id/TextAdd"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:layout_marginTop="10dp"
android:inputType="textPersonName"
android:hint="write"/>
<Button
android:id="#+id/btnadd"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/colorAccent"
android:textColor="#android:color/white"
android:text="Add" />
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.jim_a.testapp.Main3Activity"
tools:layout_editor_absoluteY="81dp"
tools:layout_editor_absoluteX="0dp">
<Button
android:id="#+id/btnSave"
android:layout_height="73dp"
android:text="Open Window"
android:layout_width="0dp"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="16dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
tools:layout_constraintRight_creator="1"
tools:layout_constraintLeft_creator="1" />
<ListView
android:id="#+id/listView"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="13dp"
tools:layout_constraintTop_creator="1"
tools:layout_constraintRight_creator="1"
tools:layout_constraintBottom_creator="1"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginEnd="13dp"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginTop="33dp"
app:layout_constraintTop_toBottomOf="#+id/btnSave"
tools:layout_constraintLeft_creator="1"
android:layout_marginBottom="31dp"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginLeft="13dp"
android:layout_marginRight="13dp"></ListView>
</android.support.constraint.ConstraintLayout>
Okay so I made 2 minor modifications to get your code working -
Replace "et" with "mUser" to avoid Null Pointer Exception when calling getText()
Dismiss Dialog upon successful updation of list.
Below is the onClick() method body :
AlertDialog.Builder mBuilder = new AlertDialog.Builder(Main3Activity.this);
View mView = getLayoutInflater().inflate(R.layout.dialog_add, null);
final EditText editText_mUser = (EditText) mView.findViewById(R.id.TextAdd);
Button mAdd = (Button) mView.findViewById(R.id.btnadd);
mBuilder.setView(mView);
//create dialog instance here, so that it can be dismissed from within the OnClickListener callback
final AlertDialog dialog = mBuilder.create();
mAdd.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (!editText_mUser.getText().toString().isEmpty()) {
// Instead of et.getText(), call mUser.getText()
String result = editText_mUser.getText().toString();
arrayList.add(result);
adapter.notifyDataSetChanged();
Toast.makeText(Main3Activity.this, "Success", Toast.LENGTH_SHORT).show();
//dismiss dialog once item is added successfully
dialog.dismiss();
} else {
Toast.makeText(Main3Activity.this, "Error pls Write", Toast.LENGTH_SHORT).show();
}
}
});
dialog.show();
See this for more info.
For other cases, where you want communication between 2 Activities/Fragments for Event-Based List Updation, you can check out EventBus.
All I want to do is start a new intent on click of a button. Here is my code(I have removed irrelevant parts):
activity_login.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
tools:context="com.example.android.altro.LoginActivity">
<!-- Login progress -->
<ProgressBar
android:id="#+id/login_progress"
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:visibility="gone" />
<ScrollView
android:id="#+id/login_form"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="#+id/email_login_form"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<AutoCompleteTextView
android:id="#+id/email"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/prompt_email"
android:inputType="textEmailAddress"
android:maxLines="1"
android:textColor="#color/colorWhite"
android:singleLine="true" />
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="#+id/password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/prompt_password"
android:imeActionId="#+id/login"
android:imeActionLabel="#string/action_sign_in_short"
android:imeOptions="actionUnspecified"
android:inputType="textPassword"
android:maxLines="1"
android:singleLine="true" />
</android.support.design.widget.TextInputLayout>
<Button
android:id="#+id/email_sign_up_button"
style="?android:textAppearanceMedium"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/rounded_button"
android:onClick="registerNewUser"
android:text="#string/action_sign_up"
android:textAllCaps="false" />
</LinearLayout>
</ScrollView>
</LinearLayout>
On clicking #+id/email_sign_up_button registerNewUser is called. I have defined this fuction in LoginActivity.java:
public class LoginActivity extends AppCompatActivity implements LoaderCallbacks<Cursor> {
/**
* Id to identity READ_CONTACTS permission request.
*/
private static final int REQUEST_READ_CONTACTS = 0;
/**
* A dummy authentication store containing known user names and passwords.
* TODO: remove after connecting to a real authentication system.
*/
private static final String[] DUMMY_CREDENTIALS = new String[]{
"foo#example.com:hello", "bar#example.com:world"
};
/**
* Keep track of the login task to ensure we can cancel it if requested.
*/
private UserLoginTask mAuthTask = null;
// UI references.
private AutoCompleteTextView mEmailView;
private EditText mPasswordView;
private View mProgressView;
private View mLoginFormView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
// Set up the login form.
mEmailView = (AutoCompleteTextView) findViewById(R.id.email);
populateAutoComplete();
mPasswordView = (EditText) findViewById(R.id.password);
mPasswordView.setOnEditorActionListener(new TextView.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView textView, int id, KeyEvent keyEvent) {
if (id == R.id.login || id == EditorInfo.IME_NULL) {
attemptLogin();
return true;
}
return false;
}
});
Button mEmailSignInButton = (Button) findViewById(R.id.email_sign_in_button);
mEmailSignInButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
attemptLogin();
}
});
mLoginFormView = findViewById(R.id.login_form);
mProgressView = findViewById(R.id.login_progress);
/* Button mRegisterButton = (Button) findViewById(R.id.email_sign_up_button);
mRegisterButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(view.getContext(), RegisterActivity.class);
startActivity(intent);
}
});*/
}
public void registerNewUser(View view) {
Intent intent = new Intent(this, RegisterActivity.class);
startActivity(intent);
}
}
I also tried doing it by finding view and then creating an intent (the code is commented out in onCreate() method. That also gave me the same error:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.android.altro/com.example.android.altro.RegisterActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
You have only one Button with id email_sign_up_button
Please check your layout. Also try replacing
Button mEmailSignInButton = (Button) findViewById(R.id.email_sign_in_button);
With
Button mEmailSignInButton = (Button) findViewById(R.id.email_sign_up_button);
Good afternoon,
I am trying to create a basic menu in my andorid application which contains 5 buttons each bringing you to another form. I am trying to create the java to carry out this action but appear to be running into the following error with each of my buttons
"EXAMPLE cannot be resolved as a variable"
Please help me in a solution to my code or if there is a simpler way to allow me to execute this menu with 5 buttons each going to a different form
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
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.techblogon.loginexample.MainMenu" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:src="#drawable/pic" />
<Button
android:id="#+id/btnFootball"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/imageView1"
android:layout_centerHorizontal="true"
android:layout_marginTop="25dp"
android:text="Football"
android:onClick="btnFootball" />
<Button
android:id="#+id/btnHockey"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/btnFootball"
android:layout_centerHorizontal="true"
android:layout_marginTop="25dp"
android:text="Hockey"
android:onClick="btnHockey" />
<Button
android:id="#+id/btnLacrosse"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/btnLacrosse"
android:layout_centerHorizontal="true"
android:layout_marginTop="25dp"
android:text="Lacrosse"
android:onClick="btnLacrosse" />
<Button
android:id="#+id/btnCurling"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/btnLacrosse"
android:layout_centerHorizontal="true"
android:layout_marginTop="25dp"
android:text="Curling"
android:onClick="btnCurling" />
<Button
android:id="#+id/btnLogout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/btnCurling"
android:layout_centerHorizontal="true"
android:layout_marginTop="25dp"
android:text="Logout"
android:onClick="btnLogout" />
</RelativeLayout>
Here is the Java:
package com.techblogon.loginexample;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.view.View.OnClickListener;
import android.content.Context;
import android.view.Menu;
import android.view.MenuItem;
public class MainMenu extends Activity {
public void ButtonOnClick(View v) {
switch (v.getId()) {
case R.id.btnFootball:
startActivity(Football);
break;
case R.id.btnHockey:
startActivity(Hockey);
break;
case R.id.btnLacrosse:
startActivity(Lacrosse);
break;
case R.id.btnCurling:
startActivity(Curling);
break;
case R.id.btnLogout:
startActivity(HomeActivity);
break;
}
}
}
#Meryl2
When u are using
""android:onClick="btnLogout"""
Then you should have corresponding method
public void btnLogout(View view{
//your code here
}
Same applies for all buttons in your code
Your Mistakes ->
You have not used setcontentView() nor you overridden onCreate() method. One more thing, you are not sending intent correctly.
Here is how you can send intent
Intent intent = new Intent(getApplicationContext(), DestinationActivity.class);
startActivity(intent);
I have modified the class and the layout. You need to create the other classes for each action.
public class MainActivity extends Activity implements Button.OnClickListener{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public void onClick(View v) {
int id = v.getId();
Intent intent = null;
if(id == R.id.btnHockey) {
intent = new Intent(MainActivity.this, Hockey.class);
} else if(id == R.id.btnCurling) {
intent = new Intent(MainActivity.this, Curling.class);
} else if(id == R.id.btnFootball) {
intent = new Intent(MainActivity.this, Football.class);
} else if(id == R.id.btnLogout) {
intent = new Intent(MainActivity.this, Logout.class);
} else if(id == R.id.btnLacrosse) {
intent = new Intent(MainActivity.this, Lacrosse.class);
}
startActivity(intent);
}
}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/imageView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"/>
<Button
android:id="#+id/btnFootball"
android:layout_below="#id/imageView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="25dp"
android:text="Football"
android:onClick="onClick" />
<Button
android:id="#+id/btnHockey"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/btnFootball"
android:layout_marginTop="25dp"
android:text="Hockey"
android:onClick="onClick" />
<Button
android:id="#+id/btnLacrosse"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/btnHockey"
android:layout_marginTop="25dp"
android:text="Lacrosse"
android:onClick="onClick" />
<Button
android:id="#+id/btnCurling"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/btnLacrosse"
android:layout_centerHorizontal="true"
android:layout_marginTop="25dp"
android:text="Curling"
android:onClick="onClick" />
<Button
android:id="#+id/btnLogout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/btnCurling"
android:layout_centerHorizontal="true"
android:layout_marginTop="25dp"
android:text="Logout"
android:onClick="onClick" />
I think i got your problem. In each Button you set the name of the method that will be used when you click on it :
android:onClick="btnCurling"
android:onClick="btnHockey"
...
But you only have one method here which is :
public void ButtonOnClick(View v) {
...
}
One solution is to define each method like so :
public class MainMenu extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// here you tell your activity what layout he will display
setContentView(R.layout.TheNameOfYourLayout);
}
// it will get there when you click on the "#+id/btnHockey" Button
public void btnHockey(View v) {
Intent intent = new Intent(this, NameOfTheActivityToLaunch.class);
startActivity(intent);
}
// then you add the other ones btnCurling, ...
}
Another solution is to set the Listener of each button programmatically :
public class MainMenu extends Activity implements View.OnClickListener {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Here you tell your activity what layout he will display
setContentView(R.layout.TheNameOfYourLayout);
// This will find your button in the layout you just set
// and then set this Class as your Listener
findViewById(R.id.btnFootball).setOnClickListener(this);
// Then set the listener of all your other buttons
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btnFootball:
Intent intent = new Intent(this, NameOfTheActivityToLaunch.class);
startActivity(intent);
break;
case R.id.btnHockey:
Intent intent = new Intent(this, NameOfTheActivityToLaunch.class);
startActivity(intent);
break;
// ...
}
}
}
You also have to remove all the android:onClick="btnCurling" line in your XML Layout file for this solution to work.
Hope it helps cheers :)
Try this code;
public class MainMenu extends Activity implements OnClickListener {
Button btn_football,btn_hokey,btn_something; // add another button here
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.yourlayout);
btn_football = (Button)findViewById(R.id.btnFootball);
// add another button here same way
btn_football.setOnClickListener(this);
// add another button here same way
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId()){
case R.id.btnFootball:
Intent intent = new Intent(MainMenu.this,(nextclass_addhere).class);
startActivity(intent);
break;
case R.id.another button:
Intent intent = new Intent(MainMenu.this,(nextclass_addhere).class);
startActivity(intent);
break;
//.......add here another button with same way
}
}
}
I have a dialog that I launch with:
// custom dialog
final Dialog dialog = new Dialog(this);
dialog.setContentView(R.layout.add_taste_dialog);
dialog.setTitle("Add Taste");
Spinner spinner = (Spinner) dialog.findViewById(R.id.spinner1);
Spinner spinner2 = (Spinner) dialog.findViewById(R.id.spinner2);
Button dialogButton = (Button) dialog.findViewById(R.id.cancelButton);
// if button is clicked, close the custom dialog
dialogButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
The problem is that when I click the button it does not dismiss the dialog.
My dialog xml is:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/tastePickTitle"
android:text="Select a Taste: "
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:ems="10"
android:textSize="20sp"
android:textStyle = "bold"
android:padding="5dip"
>
</TextView>
<Spinner
android:id="#+id/spinner1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:entries="#array/taste_array"
/>
<View
android:layout_width="fill_parent"
android:layout_height="30dp">
</View>
<TextView
android:id="#+id/ammountPickTitle"
android:text="How much taste: "
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:ems="10"
android:textSize="20sp"
android:textStyle = "bold"
android:padding="5dip"
>
</TextView>
<Spinner
android:id="#+id/spinner2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:entries="#array/ammount_array"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="#+id/cancelButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Cancel"
android:layout_weight="1"
/>
<Button
android:id="#+id/addTasteButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add"
android:layout_weight="1"
android:onClick="addTasteNow" />
</LinearLayout>
</LinearLayout>
Here is the full taste tag java code to give the snipet above some more context:
public class TasteTags extends Activity {
BeerData e;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tastetag_page);
//get beer data
Intent intent = getIntent();
Bundle b = intent.getExtras();
e = b.getParcelable("myBeerObject");
TextView beerTitle = (TextView) findViewById(R.id.beerTitleTaste);
beerTitle.setText(e.beerName + " Taste Profile");
String url = "myURL";
url = url + "b=" +e.beerId;
//async task to get beer taste tag percents
new GetTasteJSON(this).execute(url);
}
public void addTaste(View v){
// custom dialog
final Dialog dialog = new Dialog(this);
dialog.setContentView(R.layout.add_taste_dialog);
dialog.setTitle("Add Taste");
Spinner spinner = (Spinner) dialog.findViewById(R.id.spinner1);
Spinner spinner2 = (Spinner) dialog.findViewById(R.id.spinner2);
Button dialogButton = (Button) dialog.findViewById(R.id.cancelButton);
// if button is clicked, close the custom dialog
dialogButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Log.d("dialog", "cancel pressed");
dialog.dismiss();
}
});
dialog.show();
}
}
make final Dialog dialog = null; a global variable (activity level), remove the final and initialize at it where you have it right now, that is fine.
Looking at your code nothing jumps out, if you post your full code or a simple example to replicate i'm sure someone could fully decipher the problem. I have included some sample snippets that show a dialog that work, take a look at those to see if they shed any insight as to why yours does not work.
One thing to check before looking at the working sample, make sure you don't have a button in your dialog view with id 'cancelButton' defined twice(common from copy/paste errors). This will cause the behavior you are seeing. If you take the working example below and use the following view it will reproduce your behavior:
Broken Dialog View
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="26dp"
android:text="close" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="26dp"
android:text="close" />
See below for working example.
Dialog View:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="26dp"
android:text="close" />
Application class
package com.example.dialog;
import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Dialog dialog = new Dialog(this);
dialog.setContentView(R.layout.dialog);
dialog.setTitle("Add Taste");
Button dialogButton = (Button) dialog.findViewById(R.id.button1);
// if button is clicked, close the custom dialog
dialogButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
}
}