Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8281885
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T10:11:23+00:00 2026-06-08T10:11:23+00:00

I have an Android app where i’m using tabs (with ActionBarSherlock). So my main

  • 0

I have an Android app where i’m using tabs (with ActionBarSherlock). So my main activity creates the tabs for me and from there i load in the fragment layouts.

In my MainActivity.java i create a tab (this is just a snippet):

mTabsAdapter = new TabsAdapter(this, mViewPager);

mTabsAdapter.addTab(
    bar.newTab().setText("Fragment 1"),
    MainMenuFragment.class, null);

My MainMenu.java looks like this:

public class MainMenuFragment extends SherlockFragment
{
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState)
    {
        View view = inflater.inflate(R.layout.mainmenu_fragment, container, false);

        return view;
    }

    public void showMainMenu(View view)
    {
        Log.e("app", "olol: button!"); // never called!!
    }
}

And this is mainmenu_fragment

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:background="#000000" >

    <Button
        android:id="@+id/btnMenu"
        android:layout_width="170dp"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="41dp"
        android:text="@string/mainmenu"
        android:onClick="showMainMenu" />
</RelativeLayout>

Now all i have to do is place the method showMainMenu(View view) somewhere. I thought this would go in the corresponding java file (MainMenuFragment.java in my case). But it only works when i put the method in the MainAvtivity.java file.

That means that all my button actions from all kinds of fragment layouts will go in one (the main) java file. Why can’t i simply place it inside the java file that calls the Fragment layout..??

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T10:11:26+00:00Added an answer on June 8, 2026 at 10:11 am

    The short answers is (like already pointed out), you can’t.

    The only way to do this is by creating an onClick even listener. In the MainMenuFragment.java in the onCreate method, do something like this:

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState)
    {
        View view = inflater.inflate(R.layout.scan_fragment, container, false);
    
        Button menuButton = (Button)view.findViewById(R.id.btnMenu);
        menuButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                Log.e("app", "onclick listener");
            }
        });
    
        return view;
    }
    

    You can remove the onClick attribute from the layout xml.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an android app where in my Main activity I can play music
I have an Android app that fetches data from the web service using ksoap2,
In my Android app, I have a main activity that serves as an entry
I have android app . i should pass the data from android to web
I have Android client app that communicates with server using Socket . On my
I have an Android app, that is using the SIM Toolkit. The application has
I have an Android App which consists on different modules. The Main module is
I have an android app which needs to be signed using a specific keystore
I have an android-app with a listview in an activity. The listview has, if
I have PhoneGap-Android app and I am using Jquery. I am doing ASYNCHRONOUS AJAX

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.