I'm making an app and when it launches it starts Mainactivity.java
Mainactivity.java opens a layout with 9 Imagebuttons.
How can I implement in my code in Mainactivity.java that once one is clicked it opens another activity (like telefoonnummers.java)?
Sorry for my bad English but I'm dutch and a non-native speaker.
I have this code in Mainactivity.java:
package com.example.rome;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.EditText;
import android.widget.Button;
import android.view.View;
import android.widget.Toast;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
Very clean as you see, but how can I add the implementation, would you guys please help???
My Imagebuttons are all just called imagebutton1, imagebuttton2 etc. btw.
After
setContentView(R.layout.activity_main);
add for each ImageButton:
findViewById(R.id.imagebutton1).setOnClickListener(this);
Make class implement OnClickListener
class MainActivity extends Activity implements View.OnClickListener {
and add this method:
#Override
public void onClick(View v){
switch(v.getId()){
case R.id.R.id.imagebutton1:
startActivity(new Intent(telefoonnummers.class));
break;
case R.id.R.id.imagebutton2:
startActivity(new Intent(telefoonnummers.class));
break;
//-- more cases --
}
}
Related
Can anyone please help me out. I'm having problems changing my array list properties. I am more familiar with changing properties via the XML file. Here is my code:
package com.example.examproject;
import java.util.Random;
import android.os.Bundle;
import android.app.ListActivity;
import android.view.Menu;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.content.Intent;
public class Menu_Lists extends ListActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String[] myAttractions = new String[2];
myAttractions[0] = "Grocery List";
myAttractions[1] = "Go Back";
setListAdapter(new ArrayAdapter(this, android.R.layout.simple_list_item_1, myAttractions));
}
protected void onListItemClick(ListView l, View v, int position, long id) {
switch(position) {
case 0: {
startActivity(new Intent(Menu_Lists.this, MainActivity.class));
break;
}
default: {
startActivity(new Intent(Menu_Lists.this, MainActivity.class));
}
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
What I would like to do is to change the background color. That's my main goal. I would also like each option to change color whenever the user hovers over a specific option.
Please go easy with the terminologies as well. I am still a beginner with Android SDK.
Thanks in advance.
Could someone please explain why I am getting this error?
Java.lang.ClassCastException: android.widget.Button cannot be cast to android.view.View$OnKeyListener
Below is the java code. I am trying to follow a tutorial and I can't get it started. The RAZR emulator only shows Unfortunately JimYamba quit working. The error is from Eclipse LogCat.
package com.example.jimyamba;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View.OnClickListener;
import android.view.View;
import android.view.View.OnKeyListener;
import android.widget.Button;
import android.widget.EditText;
import android.util.Log;;
public class MainActivity extends Activity implements OnClickListener{
Button buttonUpdate;
EditText editStatus;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.status);
buttonUpdate = (Button)findViewById(R.id.button_update);
editStatus = (EditText) findViewById(R.id.edit_status);
buttonUpdate.setOnKeyListener((OnKeyListener) this.buttonUpdate);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public void onClick(View v){
String statusText = editStatus.getText().toString();
Log.d("StatusActivity","onClicked with text:" + statusText);
}
}
The error is in the line buttonUpdate.setOnKeyListener((OnKeyListener) this.buttonUpdate);. Just as the error says, you cannot cast a button to an OnKeyListener. Explicit casting requires a relationship via class hierarchy - Button and OnKeyListener are not related (OnKeyListener is an interface).
If you want to maintain your set up you can create a class that extends Button and Implements OnKeyListener. You will have to edit you xml to have your custom view in place of the button.
buttonUpdate.setOnKeyListener((OnKeyListener) this.buttonUpdate);
should be
buttonUpdate.setOnKeyListener(new View.OnKeyListener() {
#Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
return false;
}
});
Look at http://developer.android.com/reference/android/view/View.OnKeyListener.html
What are you trying to accomplish? You have implemented OnClickListener so if all you need is to execute what's inside of the onClick method with an OnClickListener, just change
buttonUpdate.setOnKeyListener((OnKeyListener) this.buttonUpdate);
With
buttonUpdate.setOnClickListener(this);
Or if you want OnKeyListener do what techiServices suggested
So, I've just started views and moving between activities. I thought I got what I was looking for. I fixed all my errors, but now when I test the application it crashes. I'm new to android and eclipse. So I'm not exactly sure what's happening. Here's my MainActivity.java:
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent intent = getIntent();
String info = intent.getStringExtra("info_key");
TextView text = (TextView) findViewById(R.id.nametext);
text.setText(info);
}
//opens a new activity.
public void openAddItem (View v){
Intent intent = new Intent (this, Additem.class);
startActivity(intent);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
And here's my second activity; Additem.java:
package com.grocerylist;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
public class Additem extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_additem);
}
public void additem (View v){
EditText text = (EditText)findViewById(R.id.itemname);
String info = text.getText().toString();
Intent intent = new Intent(this, MainActivity.class);
intent.putExtra("info_key", info);
startActivity(intent);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_additem, menu);
return true;
}
}
Thanks, and I'm positive the problem lies where I've placed my id's.
Your first problem I see is here
Intent intent = getIntent();
String info = intent.getStringExtra("info_key");
TextView text = (TextView) findViewById(R.id.nametext);
text.setText(info);
there is no Intent to "get" if this is your first Activity. That is used when you start an Activity with an Intent and send extras. So, naturally, info is null. There will be something there when you create it from your second Activity but not when you first run your app. Also, you may want to check into using startActivityForResult in your first Activity
Second, I don't see where you call openAddItem() in your first Activity. It may be from a Button but I don't see any Buttons
It looks like you are missing some key understandings of the fundamentals of the Android framework. I suggest you start with the Docs Here if you haven't been through them already. Good luck to you
Also, this
Intent intent = new Intent (this, Additem.class);
should be this
Intent intent = new Intent (MainActivity.this, Additem.class);
the first problem I also see was already mentioned by codeMagic.
The second thing: in case you just call your methods which start another Activity in onCreate(), you will get a pseudo-infinite number of those Activities in the stack and your app will crash.
Consider watching these tutorials, it may help you to get started
I have the following code and I can't find a way to get rid of these errors:
The method setOnClickListener(View.OnClickListener) in the type View is not applicable for the arguments (MainActivity)
This applies to the lines 17, 18, 19, 20, 21, 22, 23, 24, 25 containing:
findViewById(R.id.imageButton9).setOnClickListener(this);
In line 31 (the line where the new class is created), I get:
The nested type MainActivity cannot hide an enclosing type
This is the code I'm working with:
package com.example.rome;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.EditText;
import android.widget.Button;
import android.view.View;
import android.widget.Toast;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.imageButton1).setOnClickListener(this);
findViewById(R.id.imageButton2).setOnClickListener(this);
findViewById(R.id.imageButton3).setOnClickListener(this);
findViewById(R.id.imageButton4).setOnClickListener(this);
findViewById(R.id.imageButton5).setOnClickListener(this);
findViewById(R.id.imageButton6).setOnClickListener(this);
findViewById(R.id.imageButton7).setOnClickListener(this);
findViewById(R.id.imageButton8).setOnClickListener(this);
findViewById(R.id.imageButton9).setOnClickListener(this);
}
class MainActivity extends Activity implements View.OnClickListener {
#Override
public void onClick(View v){
switch(v.getId()){
case R.id.R.id.imagebutton1:
startActivity(new Intent(telefoonnummers.class));
break;
case R.id.R.id.imagebutton2:
startActivity(new Intent(telefoonnummers.class));
break;
//-- more cases --
case R.id.R.id.imagebutton9:
startActivity(new Intent(telefoonnummers.class));
break;
}
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
Remove the errant class definition:
class MainActivity extends Activity implements View.OnClickListener {
And add implements View.OnClickListener to the real class definition:
public class MainActivity extends Activity implements View.OnClickListener {
// Add this to the "real" MainActivity ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Take a moment to make sure you have properly closed every brace ({}).
I'm extremely new to all of this coding stuff, and I've gotten pretty far on my own, but I can't seem to figure out this error. All help is appreciated.
"No enclosing instance of the type DonationsActivity is accessible in scope"
package com.ganttbros.shadowui;
import org.donations.DonationsActivity;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.content.Intent;
public class DonateActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_donate);
final Button donate = (Button) findViewById(R.id.donatebutton);
donate.setOnClickListener(new View.OnClickListener() {
public void onClick(View v)
{
startActivity(new Intent(DonationsActivity.this, DonationsActivity.class));
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.donate, menu);
return true;
}
}
I should tell you that I am attempting to implement this library: https://github.com/dschuermann/android-donations-lib#readme
I have set up the button, but I just need to get it to launch the "DonationsActivity" when pressed.
use
startActivity(new Intent(DonateActivity.this, DonationsActivity.class));
OR
startActivity(new Intent(v.getContext(), DonationsActivity.class));
instead of
startActivity(new Intent(DonationsActivity.this, DonationsActivity.class));
for starting DonationsActivity Activity from DonateActivity Activity
Change this:
startActivity(new Intent(DonationsActivity.this, DonationsActivity.class));
to:
startActivity(new Intent(DonateActivity.this, DonationsActivity.class));