Android Buttons Can't be Clicked

android navigation buttons
android phone physical buttons
what are the 3 buttons at the bottom of android called
android overview button
android button style
android home button settings
on-screen navigation buttons android
change navigation buttons android

I am creating an app which uses a Toolbar like fragment at the top with a few buttons. The buttons are added programmatically.

The problem is that when I launch the app, the toolbar and buttons appear, but the buttons can't be clicked. When the buttons are moused over (I use an emulator), or clicked, they don't change at all, and don't notify my OnClickListener. However, the buttons and other components in the Fragment below it work perfectly.

The Toolbar's code:

public class ToolbarFragment extends Fragment implements 
View.OnClickListener{

public static final String LOG_KEY = "SM_TOOLBAR";

public static final String TO_ADD_FEED_KEY = "TO_ADD_FEED_KEY";
public static final String TO_ADD_PROFILE_KEY = "TO_ADD_PROFILE_KEY";
public static final String TO_ADD_FRIENDS_KEY = "TO_ADD_FRIENDS_KEY";

private Button feed;
private Button profile;
private Button friends;
private Button logout;

public ToolbarFragment() {
    // Required empty public constructor
}

private Button addButton(int stringID, LinearLayout linearLayout) {
    Button button = new Button(getContext());
    button.setText(stringID);
    button.setOnClickListener(this);
    linearLayout.addView(button);
    return button;
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    LinearLayout linearLayout = (LinearLayout)inflater.inflate(R.layout.fragment_toolbar, container, false);


    Bundle arguments = getArguments();
    if (arguments.getBoolean(TO_ADD_FEED_KEY)) {
        Log.i(LOG_KEY, "Created feed key");
        feed = addButton(R.string.feed, linearLayout);
    }
    if (arguments.getBoolean(TO_ADD_PROFILE_KEY)) {
        Log.i(LOG_KEY, "Created profile Key");
        profile = addButton(R.string.profile, linearLayout);
    }
    if (arguments.getBoolean(TO_ADD_FRIENDS_KEY)) {
        Log.i(LOG_KEY, "Created friends key");
        friends = addButton(R.string.friends, linearLayout);
    }
    logout = addButton(R.string.logout, linearLayout);

    return linearLayout;
}

@Override
public void onClick(View view) {
    Log.i(LOG_KEY, "A button was clicked.");
    if (getActivity() instanceof IToolbarCallback) {
        IToolbarCallback itc = (IToolbarCallback) getActivity();
        if (view.equals(feed)) {
            itc.feed();
        }
        if (view.equals(profile)) {
            itc.profile();
        }
        if (view.equals(friends)) {
            itc.friends();
        }
        if (view.equals(logout)) {
            itc.logout();
        }
    }
}

}

There's no other code pertinent to this, besides the callback interface.

public interface IToolbarCallback {

void feed();

void profile();

void logout();

void friends();

}

This is just used to let the host activity know what was clicked. Finally, the 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"
tools:context="project3.csc214.project23.Fragments.Toolbar.ToolbarFragment"
android:orientation="horizontal">

I use a builder for the fragment, and here's the code:

public class ToolbarBuilder {

private boolean addFeed;
private boolean addProfile;
private boolean addFriends;

private FragmentManager fragmentManager;
private int addToID;

public ToolbarBuilder(FragmentManager fragmentManager, int addToID) {
    this.fragmentManager = fragmentManager;
    this.addToID = addToID;
    addFeed = false;
    addProfile = false;
    addFriends = false;
}

public ToolbarBuilder addFeed() {
    addFeed = true;
    return this;
}

public ToolbarBuilder addProfile() {
    addProfile = true;
    return this;
}

public ToolbarBuilder addFriends() {
    addFriends = true;
    return this;
}

public void build() {
    Bundle bundle = new Bundle();
    bundle.putBoolean(ToolbarFragment.TO_ADD_FEED_KEY, addFeed);
    bundle.putBoolean(ToolbarFragment.TO_ADD_FRIENDS_KEY, addFriends);
    bundle.putBoolean(ToolbarFragment.TO_ADD_PROFILE_KEY, addProfile);
    ToolbarFragment toolbarFragment = new ToolbarFragment();
    toolbarFragment.setArguments(bundle);
    fragmentManager.beginTransaction().add(addToID, toolbarFragment).commit();
}

}

Just to clarify, the buttons are receiving no inputs as far as I can tell. They aren't just failing to call onClick, they're failing to react in any way at all. As far as I know, onClick is set up correctly, the buttons are just broken on some fundamental level.

The plot has thickened. Using the exact same setups in other activities seems to make it work fine... As far as I can tell, there is were no changes.

Regardless, I decided just to hard code it for this activity so I could move on to other parts of the app. Thank you all for the consideration of the problem. I'll post again should I ever figure out what happened.

Don't compare views to see if their equal, compare ids. Here:

@Override
public void onClick(View view) {
    Log.i(LOG_KEY, "A button was clicked.");
    if (getActivity() instanceof IToolbarCallback) {
        IToolbarCallback itc = (IToolbarCallback) getActivity();
        if (view.equals(feed)) {
            itc.feed();
        }
        if (view.equals(profile)) {
            itc.profile();
        }
        if (view.equals(friends)) {
            itc.friends();
        }
        if (view.equals(logout)) {
            itc.logout();
        }
    }
}

It should be:

@Override
public void onClick(View view) {
    Log.i(LOG_KEY, "A button was clicked.");
    if (getActivity() instanceof IToolbarCallback) {
        IToolbarCallback itc = (IToolbarCallback) getActivity();
        if (view.getId() == feed.getId()) {
            itc.feed();
        }
        if (view.getId() == profile.getId()) {
            itc.profile();
        }
        if (view.getId() == friends.getId()) {
            itc.friends();
        }
        if (view.getId() == logout.getId()) {
            itc.logout();
        }
    }
}

Moreover, as you're creating the views yourself you need to also give them ids. If you're on API level 17+ you can simply call generateViewId() on the view and Android will create an unique id for you. So do it like this:

private Button addButton(int stringID, LinearLayout linearLayout) {
    Button button = new Button(getContext());
    button.setText(stringID);
    button.setOnClickListener(this);
    linearLayout.addView(button);
    button.generateViewId();
    return button;
}

EDIT: Your code seem fine, apart from what I've specified above. One thing I would try is setting the listener outside your addButton method:

private Button addButton(int stringID, LinearLayout linearLayout) {
    Button button = new Button(getContext());
    button.setText(stringID);
    linearLayout.addView(button);
    button.generateViewId();
    return button;
}


if (arguments.getBoolean(TO_ADD_FEED_KEY)) {
        Log.i(LOG_KEY, "Created feed key");
        feed = addButton(R.string.feed, linearLayout);
        feed.setOnClickListener(this);
    }
    if (arguments.getBoolean(TO_ADD_PROFILE_KEY)) {
        Log.i(LOG_KEY, "Created profile Key");
        profile = addButton(R.string.profile, linearLayout);
        profile.setOnClickListener(this);
    }
    if (arguments.getBoolean(TO_ADD_FRIENDS_KEY)) {
        Log.i(LOG_KEY, "Created friends key");
        friends = addButton(R.string.friends, linearLayout);
        friends.setOnClickListener(this);
    }
    logout = addButton(R.string.logout, linearLayout);
    logout.setOnClickListener(this);

If that doesn't work, try to check if there isn't any other view on top of your buttons that's intercepting the click events.

Say Farewell to Android's Familiar Navigation Buttons, . The center button took users back to the homescreen or desktop view. A Button is a Push-button which can be pressed, or clicked, by the user to perform an action. Button Attributes. Following are the important attributes related to Button control. You can check Android official documentation for complete list of attributes and related methods which you can use to change these attributes are run time.

This is probably because your Activity host haven't implementing the IToolbarCallback. You need to check for it with something like this:

@Override
public void onClick(View view) {
  Log.i(LOG_KEY, "A button was clicked.");

  if (getActivity() instanceof IToolbarCallback) {
    Log.i(LOG_KEY, "IToolbarCallback is implemented.");
  } else {
    Log.e(LOG_KEY, "No IToolbarCallback implemented!!!");
  }
}

Revert to 3 Button System - Pixel Phone Community, How do I change the 3 buttons on my Android? The app lists all the buttons on your device, so that’s typically the volume buttons and, if you have them, the button on a connected pair of headphones. You might also see extras like Active Edge, if they’re supported by your phone. There’s no option to remap the power button though—it’s just not possible on Android.

The problem is you did not set any id to button. So in that case all buttons have same id. You have to set a unique id explicitly to each button .

 private Button addButton(int stringID,int id, LinearLayout linearLayout) {
    Button button = new Button(this);
    button.setText(stringID);
    button.setId(id);
    button.setOnClickListener(this);
    linearLayout.addView(button);
    return button;
}

Then add a Id to each button . Keep that in mind that id should be unique and it must be a positive int. Or you can use view.generateViewId(); directly.

feed = addButton(R.string.feed,1, linearLayout);
profile = addButton(R.string.profile,2, linearLayout);
friends = addButton(R.string.friends,3, linearLayout);
logout = addButton(R.string.logout, 4,linearLayout);

Then modify your onClick() as follows

@Override
public void onClick(View v) {
    switch (v.getId()) {
        case feed.getId():
            itc.feed();
            break;
        case profile.getId():
            itc.profile();
            break;
        case friends.getId():
            itc.friends();
            break;
        case logout.getId():
            itc.logout();
            break;
        default:
            break;
    }
}

How to Remap Buttons on Android Devices, does, tap on it then pick your preferred function. Available options include going to the home screen, going back a screen, returning to the last app, taking a screenshot and turning the flashlight on. Well, something is better than nothing. Today, we are going to talk about an app which is actually pretty good. you can customize it, according to your choice and it solves the android buttons not working issue. Android buttons not working : The app I am talking about is SIMPLE CONTROL app. It’s an external softkey app for your android

Buttons, You can also declare the click event handler programmatically rather than in an XML layout. This might be necessary if you instantiate the Button  Android Button Example with Listener. Here, we are going to create two textfields and one button for sum of two numbers. If user clicks button, sum of two input values is displayed on the Toast. We can perform action on button using different types such as calling listener on button or adding onClick property of button in activity's xml file.

Button, A user interface element the user can tap or click to perform an action. To display a button in an activity, add a button to the activity's layout XML  To add both image and text in android button, you should use button property android:drawableTop, android:drawableBottom, android:drawableLeft or android:drawableRight. The property value is just a drawable object id exist in the android project. Below example will set the settings icon image at the top of the button text.

Buttons - Material Components for Android, Toggle button. "Button types - text, outlined, contained and toggle." Using buttons. Before you can use Material buttons, you need  Hello Reader! In this post, we are going to learn about creating android button programmatically in android. We will go through different steps that explains how to create and use android button programmatically in any android application.

Comments
  • Try adding the click listener in the onViewCreated method not the onCreateView method. If that doesn't work, could you add some context on how the fragment is implemented.
  • I don't believe that the issue is with OnClick (I tried it, and it didn't help). I think something about the way in which I created the buttons is broken.
  • Did you try adding it in onViewCreated and not onCreateView? It is a different process that I have seen impact interesting things as far as view instantiation is concerned.
  • Yeah, no effect still.
  • While I am sure that this will help me later down the road, the problem is that onClick isn't getting called at all. I included a log statement at the start of onClick, and it never gets called. As far as I can tell, the buttons are receiving no mouse inputs at all. Thank you for saving me some trouble down the road though.
  • I gave it a shot. The buttons still aren't reacting at all to clicks (no color changes, no notifications, nothing).
  • No, it has it. I still don't think it's a problem with assigning OnClickListeners, since these buttons aren't functioning like normal buttons (there is no color change on click, which should happen for buttons even without a listener assigned).