setOnClickListener never fired - Android/Eclipse - java

I'm having a problem that seems to be quite common but none of the solutions on here so far have helped me.
I am trying to add a simple button to my app which will start a new activity when clicked.
The button displays ok in my app but when I touch it nothing happens.
My problem is that setOnClickListener() is never fired - when I debug it just skips right over it and no logging message is displayed in LogCat.
Here is my layout file:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="sarah.tourapplication.MainActivity"
tools:ignore="MergeRootFrame" >
<fragment
android:id="#+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.MapFragment" />
<RelativeLayout
android:id="#+id/belowlayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:orientation="horizontal" >
<Button
android:id="#+id/cam_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
android:text="Take Picture" />
</RelativeLayout>
And this is my activity:
public class MainActivity extends FragmentActivity implements OnClickListener {
Button camButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent locationUpdatesIntent = new Intent(this, GetLocationUpdates.class);
startActivity(locationUpdatesIntent);
camButton = (Button)findViewById(R.id.cam_button);
//add button functionality
camButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Log.d("TEST", "Button was clicked.");
Intent cameraIntent = new Intent(getApplicationContext(), CameraActivity.class);
startActivity(cameraIntent);
}
});
}
Can anyone tell me where I've gone wrong? I'd really appreciate any pointers.
I'm using Eclipse on Windows 8 and everything I'm using is up to date.
Thanks in advance!

Thanks for all the help. It turns out that I was calling the click handler in the wrong activity.

Try removing the lines
Intent locationUpdatesIntent = new Intent(this, GetLocationUpdates.class);
startActivity(locationUpdatesIntent);
It looks like you're starting a different activity before you actually get to the point where you're adding the listener...

Sorry I'm on my phone but try to use setOnClickListener(this); and override the onClick method. Inside the onClick method use if (view == yourview) to do w/e you want. :). If it still doesn't work try removing the startActivity method at the very beginning of your codes

Related

The button of a Android project simply does not work [duplicate]

I am developing a small project of test, and I wrote the following code.
I already created in the xml file, a button with id called "registerBtn".
I erased the imports of this source code to shorten space of this source code.
In the java file, I created a variable called mRegisterBtn, in the type of Button.
Inside the method called onCreate(Bundle savedInstanceState) the mRegisterBtn receives the method called findViewById(R.id.registerBtn);
However, in the mRegisterBtn.setOnClickListener, the part of new View.OnClickListener appears in gray color, and it is not working when trying to test this code.
This image shows what I really mean. Please, perceive that the the part of new View.OnClickListener appears in gray color. It means a error. But trying to compile, this code runs, but the button simply does not work.
Can anyone know how to fix this error, please?
public class Register2 extends AppCompatActivity {
Button mRegisterBtn;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register2);
mRegisterBtn = findViewById(R.id.registerBtn);
mRegisterBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(getApplicationContext(), "Testing", Toast.LENGTH_LONG).show();
}
});
}
}```
<?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:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#007FFF"
tools:context=".Register">
<Button
android:id="#+id/registerBtn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:backgroundTint="#color/white"
android:text="Register"
android:textColor="#color/black"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.499" />
</androidx.constraintlayout.widget.ConstraintLayout>
Try using the Activity itself as the Context.
If you want a log, then make one
If you want the gray to go away, use a lambda
Log.d("REGISTER", "Setting listener");
mRegisterBtn.setOnClickListener(view -> {
Log.d("REGISTER", "Clicked!");
Toast.makeText(Register2.this, "Testing", Toast.LENGTH_LONG).show();
});

How to use a ScrollView?

This is suppose to be a scroll view with all the content added from the Java code when response is received from the API.
The problem is that I can't find a way to display the information like this in a ScrollView. I tried using an ImageButton but I couldn't get the content in it then I tried using a Button but still couldn't achieve the desired effect please can someone suggest a way I could do this.
private Button makeButton(String targetName, final String i, LinearLayout.LayoutParams buttonLayoutParams) {
Button in = new Button(this);
in.setBackground(getResources().getDrawable(R.drawable.rectangle14));
in.setText(targetName);
in.setWidth(360);
in.setHeight(72);
in.setLayoutParams(buttonLayoutParams);
in.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent myIntent = new Intent(HomeActivity.this,XSavingDetailsActivity.class);
myIntent.putExtra("i" ,i);
HomeActivity.this.startActivity(myIntent);
}
});
return in;
}
You should use a RecyclerView .Each and every component within the RecyclerView is a CardView . Also you should learn about Material Design.
Apart from the above some useful links:
https://developer.android.com/guide/topics/ui/layout/cardview.html
https://www.androidhive.info/2016/01/android-working-with-recycler-view/
https://medium.com/#nileshsingh/android-cardview-101-everything-you-should-know-5bbf1c873f5a
Just make the top-level layout a ScrollView:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<TableLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:stretchColumns="1">
<!-- everything else -->
</TableLayout>

I need help getting an intent to open another activity

I need to be able to touch a TextView and have it open up another page, however, when I run the app and touch the TextView the app crashes.
Here's the code:
<?xml version="1.0" encoding="utf-8"?>
<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:padding= "10dp"
android:background="#drawable/background"
android:orientation="vertical"
tools:context="com.example.android.practiceapp.MainActivity">
<TextView
android:id="#+id/numbers"
style="#style/CategoryStyle"
android:layout_marginBottom="16dp"
android:background="#drawable/numbers"
android:text="#string/category_numbers"
android:onClick="openNumbersList"/>
Here's the java code:
package com.example.android.practiceapp;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void openNumbersList(View view) {
Intent i = new Intent(this, Numbers.class);
startActivity(i);
}
}
Make sure you have the Numbers activity exists and is in your Android.manifest.
All activities must be represented by elements in the manifest file. Any that are not declared there will not be seen by the system and will never be run.
First, you are not initialized your TextView
textView = (TextView)findViewById(R.id.numbers)
Second, as others suggested, add Numbers activity in your Android.manifest
You did not add the clickable attribute, without it, the click handler isn't called. So, add it...
...
<TextView
android:id="#+id/numbers"
style="#style/CategoryStyle"
android:layout_marginBottom="16dp"
android:background="#drawable/numbers"
android:text="#string/category_numbers"
android:onClick="openNumbersList"
android:clickable="true"/>
...
thanks for the responses, i actually ran the debugger and it turns out i had to change my java code in the numbers file from "extends AppCompatActivity" to "extends Activity" like the MainActivity java file
Try inserting
MainActivity.this
instead of
this
like so
Intent i = new Intent(MainActivity.this, Numbers.class);

Using imageButtons to switch activities. How can I switch image in second activity based on button pressed?

I am just starting to dabble with Android development. Right now, I have a program in Android Studio that has two columns of images about 6 images each. I want to make it so if I click that image, it will appear in the new activity. Right now, I am just loading the layout like this:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = getIntent();
String name = intent.getStringExtra(Main.CHAMPION_NAME);
setContentView(R.layout.activity_champion_page);
}
With the layout in setContentView looking like this:
<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:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context="atree496.leaguestats.ChampionPage">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/azir"
android:id="#+id/imageView" />
</LinearLayout>
As of now, it opens to the one image because that is how it is set up, but I want to be able to have the image be the one that I click. What changes do I need to do in order to achieve this? Again, I am new to this. Thank you for any help you can give.
You could add to the intent the int resource of your pressed image if you give it file name to Tag attribute in xml:
ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/azir"
android:tag="theImageFileName.png"
android:id="#+id/imageView" />
Then you could retreive it when pressed and add it to the Intent you are using:
#Override
public void onClick(View v) {
String fileName = (String) v.getTag();
Intent intent = new Intent(this, SecondActivity.class);
intent.putExtra("fileName", fileName);
startActivity(intent);
}
In second activity after you initialize the secondImageView:
String fileName = getIntent().getStringExtra("fileName");
int resId = getResources().getIdentifier(fileName,"drawable", getPackageName());
secondImage.setImageDrawable(getResources().getDrawable(resId));

Android app and java activities, issues with onCreate() method and XML

I am trying use the onClick() function in an activity for an android app. So far I have:
public class Activity2 extends Activity implements OnClickListener {
private ImageButton closeButton;
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity2);
Button myButton = (Button) findViewById(R.id.wowButton);
myButton.setOnClickListener(this);
this.closeButton = (ImageButton)this.findViewById(R.id.close);
this.closeButton.setOnClickListener(new OnClickListener(){
public void onClick(View v){
finish();
}
});
}
public void onClick(View v) {
// TODO Auto-generated method stub
TextView lowerText = (TextView) findViewById(R.id.textView2);
EditText boxText = (EditText) findViewById(R.id.editText1);
lowerText.setText(boxText.getText());
}
}
This is just a stripped down version of my project. When I enter my app as usual in the emualtor, everything works fine. When I click the button to open up this particular activity, everything crashes. I assume the issue lies within the onClick() method. This code was sent to me by my project partner without the XML files so I think thats where the problem lies.
here is my basic XML file that I thought should work:
<?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">
<Button android:id="#+id/wowButton" android:layout_width="fill_parent"
android:layout_height="200dp" android:text="WoW" android:typeface="sans"
android:background="#drawable/btn_default_normal_red" />
<EditText
android:id="#+id/editText1"
/>
<TextView
android:id="#+id/textView2"
/>
</LinearLayout>
While very basic, I though it should work. Thanks for your help.
you instantiate an ImageButton through this.closeButton = (ImageButton)this.findViewById(R.id.close);
But you don't declare it in the xml file. That's why it's null and throws a NullPointer Exception.

Categories

Resources