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 9225643
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T04:37:50+00:00 2026-06-18T04:37:50+00:00

Can you help me? When I press the button, it stays pressed, but don’t

  • 0

Can you help me? When I press the button, it stays pressed, but don’t do nothing.

Sorry for my bad English! 🙂

Thanks!

fasmenuprincipal.xml File:

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Button" />

I want to start a new activity from inflate layout. I’ve made a scrollview, and then I add views to the scrollview, inflating the layouts.
Here is de java code:

public class ProvaPedidoScroll extends Activity {

    Context mContext;
    HorizontalScrollView mScrollView;
    LinearLayout mLinearLayout;
    LinearLayout.LayoutParams mLinearLayoutParams;
    Display mDisplay;
    // scroll behaviour
    private int mScrollStartPosition;
    private static final float SCROLL_MARGIN = 0.2f;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = this;
        // load layout from xml and get references to sub-views
        setContentView(R.layout.scrollview);
        mScrollView = (HorizontalScrollView) findViewById(R.id.scrollview);
        mLinearLayout = (LinearLayout) findViewById(R.id.scrollviewlinearlayout);
        // get a display reference (used to find screen size)
        mDisplay = ((WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
        // get the layout parameters to apply to the sub-views
        mLinearLayoutParams = new LayoutParams(mDisplay.getWidth(), mDisplay.getHeight());
        // add some views to the ScrollView
        addViewsToScrollView();
    }

    /**
     * Inflates and adds some views to the ScrollView
     */
    private void addViewsToScrollView() {
        LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);

        View menuprincipal = inflater.inflate(R.layout.fasmenuprincipal, null);
        menuprincipal.setLayoutParams(mLinearLayoutParams);
        mLinearLayout.addView(menuprincipal);

        Button ClientesMenu = (Button) menuprincipal.findViewById(R.id.button1);

        ClientesMenu.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                int iOpcio = 1;
                Intent VeureClients = new Intent(ProvaPedidoScroll.this, buscar_client.class);
                VeureClients.putExtra("Opcio", iOpcio);
                startActivity(VeureClients);

            }
        });

        View view2 = inflater.inflate(R.layout.fasmenugestion, null);
        view2.setLayoutParams(mLinearLayoutParams);
        mLinearLayout.addView(view2);

    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        int viewWidth = mDisplay.getWidth(); // width of each view
        int triggerWidth = (int) (SCROLL_MARGIN * viewWidth); // amount user has to scroll to move to next view
        int pos = mScrollView.getScrollX();
        int diff = pos % viewWidth; // offset of current scroll from leftmost view's snap position
        int posLeftView = pos - diff; // absolute snap position of the leftmost view on screen
        switch (ev.getAction()) {
            case MotionEvent.ACTION_DOWN:
                // Record the starting scroll position. This is used to decide the scroll direction.
                mScrollStartPosition = pos;
                break;
            case MotionEvent.ACTION_UP:
                if (pos > mScrollStartPosition) {
                    // Scrolling right
                    if (diff > triggerWidth) mScrollView.smoothScrollTo(posLeftView + viewWidth, 0);
                    else mScrollView.smoothScrollTo(posLeftView, 0);
                } else {
                    // Scrolling left
                    if (diff > (viewWidth - triggerWidth)) mScrollView.smoothScrollTo(posLeftView + viewWidth, 0);
                    else mScrollView.smoothScrollTo(posLeftView, 0);
                }
                // replacing our scrollTo command with it's own
                return true;
        }
        return super.dispatchTouchEvent(ev);
    }


}
  • 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-18T04:37:52+00:00Added an answer on June 18, 2026 at 4:37 am

    try this code

    may be find error

    HorizontalScrollView scrollView = (HorizontalScrollView) findViewById(R.id.scrollView1);
    
            LinearLayout topLinearLayout = new LinearLayout(this);
           // topLinearLayout.setLayoutParams(android.widget.LinearLayout.LayoutParams.FILL_PARENT,android.widget.LinearLayout.LayoutParams.FILL_PARENT);
            topLinearLayout.setOrientation(LinearLayout.HORIZONTAL); 
    
            for (int i = 0; i < 15; i++){
    
    
    
                final ImageView imageView = new ImageView (this);
    
                imageView.setTag(i);
    
                imageView.setImageResource(R.drawable.ic_launcher);
    
                topLinearLayout.addView(imageView);
    
                imageView.setOnClickListener(new OnClickListener()
                {
    
                    @Override
                    public void onClick(View v)
                    {
                        // TODO Auto-generated method stub
                        Log.e("Tag",""+imageView.getTag());
                    }
                });
    
    
            }
    
            scrollView.addView(topLinearLayout);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This code shows hashcodes of pressed button, but when i press on Control button
When i press button listview does not fills. can anyone help me? public ObservableCollection
Can someone help me?: I have two diffrent buttons and when I press Button
Can anyone help with how to make a button unselected after I press another
I hope you can help, I've tried for hours to understand mod rewrite but
I am searching how to press the Subscribe button from Youtube Subscribe Form, but
Can someone help with this code. what am looking for is for the button
Who can help me to fix the following problem? Here is the issue: in
Anyone can help me? Trying to Add a Slow function with my smoothscroll and
Who can help me to translate this XML Schema pattern [0-9]+-([0-9]|K) to java regular

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.