I'm trying to create an activity with several buttons and I want that on click they open a new activity. I tried this code but it does not seem to work (I click the Puntos Image and nothing happens) Do you see what could be wrong?
public class MainMenu extends Activity implements OnClickListener{
ImageButton puntos;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mainmenu);
puntos = (ImageButton)findViewById(R.id.ImagePuntos);
}
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
if(arg0.getId() == R.id.ImagePuntos){
Intent openActivity1 = new Intent(MainMenu.this, MisPuntos.class);
startActivity(openActivity1);
}
}
}
In your xml layout, where you define ImagePuntos, give it the attribute
android:onClick="onClick"
Make sure that either the ImageButton has it's onClick attribute set to onClick in the XML or set it programmatically by adding puntos.setOnClickListener(this);
Related
I just started to learn Java. I know some C++, but you know, I am just a novice. I have a problem with a button. I a main activity there are 3 buttons with onClick discovered by switch. By clicking on one of the buttons you're redirected to another activity where I need to create a new button.
The code responsible for MainScreen buttons looks like this (and it works):
public class MainScreen extends Activity implements View.OnClickListener {
Button act_2x2, act_3x3, act_4x4;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_screen);
act_2x2 = (Button) findViewById(R.id.Activity_2x2);
act_3x3 = (Button) findViewById(R.id.Activity_3x3);
act_4x4 = (Button) findViewById(R.id.Activity_4x4);
act_2x2.setOnClickListener(this);
act_3x3.setOnClickListener(this);
act_4x4.setOnClickListener(this);
}
#Override
public void onClick(View view) {
switch(view.getId())
{
case R.id.Activity_2x2:
Intent inent1 = new Intent(this, macierz_2x2.class);
startActivity(inent1);
break;
case R.id.Activity_3x3:
Intent inent2 = new Intent(this, macierz_3x3.class);
startActivity(inent2);
break;
case R.id.Activity_4x4:
Intent inent3 = new Intent(this, macierz_4x4.class);
startActivity(inent3);
break;
}
And it is okay, I can normally enter the new activity, for example Activity_2x2.
Here, in 2x2 class I've created a new OnClickListener and when I click on it, nothing happens. I am sitting here for two hours with debugger, it is saying that I don't have permissions, but It is impossible, because it is just a simple button. I am using Android Studio and just don't know how to debug correctly.
Here is the definition:
public class macierz_2x2 extends MainScreen implements View.OnClickListener{
Button b_2x2;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_2x2);
b_2x2 = (Button) findViewById(R.id.button_2x2);
b_2x2.setOnClickListener(this);
}
public void OnClick(View view) {
what happens after clicking
}
I know, that this problem is somewhere in overriding and extending, but no idea, why the compiller is letting this being compiled.
If someone have any idea, I will be grateful.
ps. I don't need an answer, just a point, what is wrong.
public class macierz_2x2 extends MainScreen implements View.OnClickListener{
MainScreen already implements View.OnClickListener. Remove it from the definition of your class.
public class macierz_2x2 extends MainScreen {
is enough. You can override onClick on your macierz_2x2 activity
#Override
public void onClick(View view) {
switch(view.getId()) {
R.id.button_2x2:
// do something
break;
default:
super.onClick(view);
break;
}
}
I implemented a bottom button bar(something like a tab bar controller in iPhone). For this I created a common layout (button_bar.xml, 5 image buttons) and included in other activity xml files. and for the managing the click action I created a BaseActivity.java extended from Activity and perform the click actions. and I extend other activity which need button bar from this BaseActivity, which works fine. Now I want to include a selected state to these buttons, but when I access the buttons in base activity it give a null pointer error. How can I solve this.
button_bar.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/bottomButtonBar"
style="#android:style/ButtonBar"
... >
<ImageButton
android:id="#+id/btnGuests"
android:onClick="showAllGuests"
android:src="#drawable/ic_guest_list" />
<ImageButton
android:id="#+id/btnAddGuest"
android:onClick="selectGuestType"
android:src="#drawable/ic_add_guest" />
<ImageButton
android:id="#+id/btnAddParty"
android:onClick="showAddParty"
android:src="#drawable/ic_add_party" />
....
</LinearLayout>
public class BaseActivity extends Activity {
// common to other activities.
public void showAddParty(View view) {
//showAddParty is one one of the buttons click method. 4 more buttons are there
Intent intent = new Intent(this, AddPartyActivity.class);
startActivity(intent);
// I can get "view.findViewById(R.id.btnAddParty)" here
// but I can't get "findViewById(R.id.btnAddGuest)" here. how this possible
}
}
public class AddPartyActivity extends BaseActivity{
....
}
Here i can get the corresponding view from parameter view, and change backgroundIbageSource. but when it goes to "AddPartyActivity" that inherited from Baseactivity, the new image is replaced by old one. How can I implement the selected feature in BaseActivity itself?
you can hold the state of the Button(clicked or not) using booloean in BaseActivity and check the value of these variables in AddPropertyActivity's onCreate() method.
public class BaseActivity extends Activity {
protected static boolean isButton1Clicked = false;
protected static boolean isButton2Clicked = false;
protected static boolean isButton3Clicked = false;
protected static boolean isButton4Clicked = false;
.....
public void showAddParty(View view) {
isButton1Clicked = true;
//showAddParty is one one of the buttons click method. 4 more buttons are there
Intent intent = new Intent(this, AddPartyActivity.class);
startActivity(intent);
}
}
public class AddPartyActivity extends BaseActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_layout);
Button button1 = (Button)findViewById(R.id.button1);
if(isButton1Clicked) {
button1.setBackground(R.drawable.clicked);
} else {
button1.setBackground(R.drawable.not_clicked);
}
......
......
}
}
and remember that all boolean variables must be static variables. (every object will have it's own copy of instance variables but all objects will share a single copy of static members).
public class BaseActivity extends Activity {
protected static boolean isButton1Clicked = false;
protected static boolean isButton2Clicked = false;
protected static boolean isButton3Clicked = false;
protected static boolean isButton4Clicked = false;
protected Button button1;
protected Button button2;
protected Button button3;
protected Button button4;
protected void checkButtonsState() {
if (isButton1Clicked) {
button1.setBackgroundResource(R.drawable.image1);
} else {
button1.setBackgroundResource(R.drawable.image2);
}
..........
}
public void showAddParty(View view) {
isButton1Clicked = true;
// showAddParty is one one of the buttons click method. 4 more
// buttons are there
Intent intent = new Intent(this, AddPartyActivity.class);
startActivity(intent);
}
}
public class AddPartyActivity extends BaseActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button1 = (Button) findViewById(R.id.button);
button2 = (Button) findViewById(R.id.item1);
.....
checkButtonsState();
}
}
Your error come from because you are using view.findViewById(), but the view variable correspond of the object that catch the click event (look in your xml, the 'android:onClick="showAddParty"').
So, you have in parameter a view instance that point directly to the btnAddParty button.
Is why you can access with findViewById to btnAddParty id but not btnAddGuest id.
You can access to all the view hierarchy by using direct findViewById as method of the activity class:
View btAddParty = findViewById(R.id.btnAddParty)
View btAddGuest = view.findViewById(R.id.btnAddGuest)
Now, you can have a full solution, by implement a special method (setMyInternalContent in the sample below) in the BaseActivity and keep an instance of each button, after, you just have to update there state on click action:
public class BaseActivity extends Activity {
protected Button mBtAddParty;
protected Button mBtAddGuest;
// ...
protected void setMyContentView(int resId) {
setContentView(resId);
mBtAddParty = (Button)findViewById(R.id.btnAddParty);
mBtAddParty = (Button)findViewById(R.id.btnAddGuest);
// ...
}
public void showAddParty(View view) {
mBtAddParty.setClickable(true);
mBtAddGuest.setClickable(false);
// ...
//showAddParty is one one of the buttons click method. 4 more buttons are there
Intent intent = new Intent(this, AddPartyActivity.class);
startActivity(intent);
}
}
public class AddPartyActivity extends BaseActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setMyContentView(R.layout.activity_layout);
// Do not forget to set initial button state:
mBtAddParty.setClickable(true);
mBtAddGuest.setClickable(false);
// ...
}
}
I have two different layouts. One is which loads while start of the Activity and the other which loads after running some checks and creates a custom dialog. The Dialog has a button in it to trigger, at this point in time, onclick has a Toast message so I can confirm that the button has been clicked. Unfortunately I can't able to get any response when the button is clicked. I've been all over the web and I can't quite find what I'm missing.
public class myactivity extends Activity{
Dialog accesspopup;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_myactivity);
View inflatedView = getLayoutInflater().inflate(R.layout.dialoglayout, null);
final Button cabtn = (Button)inflatedView.findViewById(R.id.cb);
cabtn.setOnClickListener(cListener);
}
private OnClickListener cListener = new OnClickListener() {
public void onClick(View v) {
//Log.d("HiThereActivity", "THIS IS DEBUG OUTPUT TO LOGCAT");
Toast.makeText(myactivity.this, "The Start button was clicked.", Toast.LENGTH_LONG).show();
}
};
public void showPopup(){
accesspopup = new Dialog(myactivity.this);
accesspopup.setContentView(R.layout.pop_window);
accesspopup.setCancelable(false);
accesspopup.setTitle("Window Title");
accesspopup.show();
}
I did some more searching around and found that I need to create the OnClickListener inside the method which I am using to build and display the Dialog and not in the OnCreate.
use this way...
public class myactivity extends Activity{
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_myactivity);
View inflatedView = getLayoutInflater().inflate(R.layout.dialoglayout, null);
final Button cabtn = (Button)inflatedView.findViewById(R.id.cb);
cabtn.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
Toast.makeText(myactivity.this, "The Start button was clicked.", Toast.LENGTH_LONG).show();
}
});
}
May be still your R.layout.activity_myactivity is the controllable Contentview in your activity.
So you have to define your new layout as setContentView.
or you mentioned it is a Dialog box.
So you can add a content view for a dialog like the following,
Dialog d = new Dialog (this);
d.setContentView(your inflated view);
In my App I have a line showing an (ImageButton)icon and a (textView)title of an audio file to play and I use the setOnClickListener for an ImageButton to initiate a process that plays the file. I would also like to use the (textView)title as a clickable item to begin the same process.
I could simply duplicate all of the functionality in both setOnClickListeners but that does not seem to be the most efficient way to do it.
Now I am new so is there such a thing as
ImageButton.setOnClickListener() || textView.setOnClickListener() {
.
.
.
}
Basically if the ImageButton is clicked or the textView is clicked do this procedure.
I know the above syntax is not correct but it gives you an idea of what I want to do
Have the class that has both these elements ImageButton and textView implement the OnClickListener. OnClickListener is an interface that has the method onClick(View v) which will have the click implementation for both these elements. Then you can use imageButton.setOnClickListener(this) and textView.setOnClickListener(this).
Example Code:
public MyClass extends Activity implements OnClickListener {
ImageButton imageButton;
TextView textView;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
imageButton = (ImageButton) findViewById(R.id.btn);
textView = (TextView) findViewById(R.id.txt);
imageButton.setOnClickListener(this);
textView.setOnClickListener(this);
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
int viewId = v.getId() ;
if(viewId == R.id.btn || viewId == R.id.txt){
//common implementation of click event
}
}
}
Hope this explanation helps.
ImageButton iv=(ImageButton)findViewById(R.id.imagebutton1);
TextView tv=(TextView)findViewById(R.id.textview1);
iv.setOnClickListener(OnClick);
tv.setOnClickListener(OnClick);
add this in OnCreate() method and after that add this method as shown below
private OnClickListener OnClick=new OnClickListener(){
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId()){
case R.id.imagebutton1:{
// do here code what u want on imagebutton click
break;}
case R.id.textview1:{
// do here code what u want on textview click
break;}
}
}
}
};
Put both the imagebutton and textview in a layout and give it an ID. Then implement the onClickListener for the layout, by this way you need to write the code only once.
I am trying to run a test program that allows a user to click a button and move to a different screen. I have the Home(First Activity) and Away(Second Activity) classes and a xml file specifying the layout for each. My source code is as follows:
public class Home extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Button create = (Button)findViewById(R.id.create);
create.setOnClickListener(new View.OnClickListener() {
public void onClick(View v)
{
Intent intent = new Intent(Home.this, Away.class);
startActivity(intent);
}
});
setContentView(R.layout.main);
}
}
And Away.java
public class Away extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.away);
}
}
I get a NullPointerException in the DDMS trace
at Home.onCreate(line 17)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java: 2627)
Anyone see anything in my code that may be causing this?
findViewById() traverses the view hierarchy set up when setContentView() inflates your layout. You cannot retrieve a reference to a view in your XML before you have called setContentView(). Change your onCreate() method in Home to look like this:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Call me first
setContentView(R.layout.main);
Button create = (Button)findViewById(R.id.create);
create.setOnClickListener(new View.OnClickListener() {
public void onClick(View v)
{
Intent intent = new Intent(Home.this, Away.class);
startActivity(intent);
}
});
}
Otherwise, findViewById() will return null because there are no views in the tree...thus, a view with your requested id value doesn't exist.
Hope that helps!
If the findViewById call can't find the actual thing you are looking for, it could return a null object and cause the call to setOnClickListener to throw an NPE. Are you sure you've got the right ID (R.id.create) specified?