Button not working when activity is changed

android.intent.action.media_button not working
up button android
android:parentactivityname not working
android up button not working
harmony smart control activity buttons
google sign-in android example
android headset button event
android set parent activity programmatically

I am trying to write a simple to-do list, consisting of one title screen (which is a main activity) with a button that switches to the actual list (which is an another activity). However, upon clicking the 'Add' button on that another activity, nothing happens - the onClick function is not even called. How am I supposed to make this work?

I have tried to rewrite the code according to this solution, but nothing has changed: Android Studio onClick and OnclickListener not working once changed to another Activity Moreover, I tried to implement the code using the first approach mentioned here, to no avail: https://medium.com/@CodyEngel/4-ways-to-implement-onclicklistener-on-android-9b956cbd2928

Here's the MainActivity class:

package com.example.kandydatpl;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void openChecklist(View view) {
        Intent intent = new Intent(this, TaskListActivity.class);
        startActivity(intent);
    }

}

MainActivitty.xml

<?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=".MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="openChecklist"
        android:text="Open Checklist"
        tools:layout_editor_absoluteX="136dp"
        tools:layout_editor_absoluteY="407dp" />

</android.support.constraint.ConstraintLayout>

as well as TaskListActivity class:

package com.example.kandydatpl;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

import java.io.File;
import java.util.ArrayList;

public class TaskListActivity extends AppCompatActivity implements View.OnClickListener, AdapterView.OnItemClickListener {

    private EditText itemET;
    private Button btn;
    private ListView itemsList;
    private ArrayList<String> items;
    private ArrayAdapter<String> adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_task_list);

        itemET = findViewById(R.id.item_edit_text);
        btn = findViewById(R.id.add_btn);
        itemsList = findViewById(R.id.items_list);

        items = FileHelper.readData(this);

        adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, items);
        itemsList.setAdapter(adapter);

        btn.setOnClickListener(this);
        itemsList.setOnItemClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.add_btn:
                String itemEntered = itemET.getText().toString();
                adapter.add(itemEntered);
                itemET.setText("");
                FileHelper.writeData(items, this);
                Toast.makeText(this, "Item Added!", Toast.LENGTH_SHORT).show();
                break;
        }
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        items.remove(position);
        adapter.notifyDataSetChanged();
        FileHelper.writeData(items, this);
        Toast.makeText(this, "Delete", Toast.LENGTH_SHORT).show();
    }
}

and the correspoding XML:

<?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"
    android:orientation="vertical"
    tools:context=".TaskListActivity">

    <LinearLayout
        android:id="@+id/linearLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <EditText
            android:id="@+id/item_edit_text"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="4"
            android:hint="Enter Item" />

        <Button
            android:id="@+id/add_btn"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:onClick="onClickAdd"
            android:text="Add" />

    </LinearLayout>

    <ListView
        android:id="@+id/items_list"
        android:layout_width="395dp"
        android:layout_height="667dp"
        android:layout_marginTop="8dp"
        android:layout_marginBottom="8dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/linearLayout"
        tools:layout_editor_absoluteX="8dp" />

</android.support.constraint.ConstraintLayout>

Since you are using android:onClick="onClickAdd in your xml file, you need to add the onClickAdd method to your TaskListActivity as follows:

public void onClickAdd(View view) 
{
      switch (view.getId()) 
      {
           case R.id.add_btn:
             String itemEntered = itemET.getText().toString();
             adapter.add(itemEntered);
             itemET.setText("");
             FileHelper.writeData(items, this);
             Toast.makeText(this, "Item Added!", Toast.LENGTH_SHORT).show();
             break;
      }
}

When you are using the onClick attribute in your xml file, your TaskListActivity doesn't need to implement the View.OnClickListener interface and thus you don't need to add the onClickListener to your Button.

Add an up action, Overview · Handle configuration changes Responding to media buttons · Handling changes in audio output This lesson shows you how to add an Up button to an activity by activity's parent in the manifest, and enabling the app bar's Up button. You do not need to catch the up action in the activity's  Reassigned button commands may not work as expected for some programs or games. Some programs or games may not support customized button assignments. Some mouse models include specialized buttons that cannot be reassigned.

Try to set the listener below the button:

btn = findViewById(R.id.add_btn);
btn.setOnClickListener(this);

And use Invalidate Caches and Restart option

Responding to media buttons, If the foreground activity does not handle the event, Android will try to find a media session that can handle it. Again, depending on the running  If an activity is in the foreground, and the user taps the Back button, the activity transitions through the onPause (), onStop (), and onDestroy () callbacks. In addition to being destroyed, the activity is also removed from the back stack. It is important to note that, by default, the onSaveInstanceState () callback does not fire in this case.

I agree with the kAliert

Use either one A) Implement View.OnClickListener which you had done so far, but delete this line in xml

android:onClick="onClickAdd"

B) Do not implement View.OnClickListener, and use

public void onClickAdd(View view) 
{
  switch (view.getId()) 
  {
       case R.id.add_btn:
         String itemEntered = itemET.getText().toString();
         adapter.add(itemEntered);
         itemET.setText("");
         FileHelper.writeData(items, this);
         Toast.makeText(this, "Item Added!", Toast.LENGTH_SHORT).show();
         break;
  }
}

while maintaining this line in xml

android:onClick="onClickAdd"

Let us know if it still not working.

The Counselor and the Group: Integrating Theory, Training, and , Change. This activity can be used to facilitate a work oriented focus on specific problems of resolving their problems because they do not know exactly what they want to change. Paper, pen, and a box of buttons are needed for this activity. How can I change this back? How can I search the web using the SwagButton? Can I continue to search and win SB? Will I still receive Swag Code alerts and be able to redeem Swag Codes from the SwagButton? Help, I can’t install the SwagButton on my work computer. Are SwagButton Exclusive Swag Codes taking the place of regular daily Swag Codes?

[PDF] Harmony Smart Control™ User Guide, Changing Activity settings with the Harmony App .. Smart Control remote Off button to power off all of the devices for an Activity with one touch. cause devices to change to the wrong input, miss commands, or not function at all during  Is the smartphone you are using having an issue? Android buttons not working or android softkeys are not working issue. Get fixed with the 100% working steps by Techyuga. Watch the Step by Step guide video also ☝.

Integrating Google Sign-In into Your Android App, In your sign-in activity's onCreate method, configure Google Sign-In to only request the scopes that are required for your app to minimally function. Update your UI accordingly—that is, hide the sign-in button, launch your main activity, Note: If you need to detect changes to a user's auth state that happen outside your  Many people have reported this to work for them when everything they had tried had failed. With their Windows 10 start bar not working, many tried this final fix and reported that it worked for them. If your Windows 10 start button is not working, then one of these fixes above will likely solve the problem.

Harmony 665, Walk through our setup tutorial: adding your devices, setting up Activities, and learn more about the usage of your remote. Already set up and something's not working? Click the Sync button to transfer these settings to your remote. the stereo, and channels will automatically change your TV or cable/satellite box. J7 Not Turning On After Getting Wet. Problem: My son bought a J7 . It got wet (though well within what should be ok for it), and has been not working properly. At first, it wouldn’t turn on.

Comments
  • Your TaskListActivity class looks okay (it implements View.OnClickListener, and sets the onClickListener correctly to the button, and finally handles the onClick() event correctly). That said, can you include your layout files (XML)? Have you tried debugging to see if the onClick() event fires at all?
  • @TomLarcher yes, I tried to debug the code, that's how I found out that this function is not invoked. I'll add XML files as we speak.
  • Would it work if I changed android:onClick="onClickAdd" to android:onClick="onClick?
  • I'm not quite sure since i rarely using the onClick attribute, but it should work. In my opionen you should either implement the View.OnClickListener or add the attribute to your xml file. Both makes no sense to me (except you have more views, where some are using the attribute and others using the View.OnClickListener interface)
  • I added the method you mentioned, it still does not work properly. What could be the problem here?
  • Please remove this line: btn.setOnClickListener(this). Just use the attribute and see if it works - OR - remove the attribute and use btn.setOnClickListener(this) to handle the click event.
  • I decided to stick to the second option, removed onClick attribute from the XML file and still no success. Here's my current code for TaskListActivity: pastebin.com/dgT3rHzv
  • None of these methods work. I decided to stick with the method A), I got rid of "onClick" attribute from the XML file, and the button still does not respond properly. This is my current state of TaskListActivity code: pastebin.com/dgT3rHzv
  • For code in pastebin 1. Kindly include the xml in the pastebin. 2. From 'public void onClickAdd(View view)' to 'public void onClickAdd()' 3. From 'onClickAdd(v)' to 'onClickAdd()' under your 'public void onClick(View v)'
  • Read this [geeksforgeeks.org/handling-click-events-button-android/….
  • I followed the instructions in this link and your advice, but still, no effect.
  • Here's the full code: pastebin.com/BdR9gkr2