onClick not executing on Buttons in Scrollview?

I have made a vertical linear layout made up of 6 horizontal linear layouts. Each horizontal layout contains 10+ buttons on a scrollview. Below is the main linear layout with one of the horizontal layouts included.

    <LinearLayout android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="10dp" 
android:gravity="center"
xmlns:android="http://schemas.android.com/apk/res/android">
<HorizontalScrollView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
<LinearLayout
    android:orientation="horizontal"
    android:layout_width="wrap_content"
    android:layout_height="75dp">

        <Button 
            android:id="@+id/button1"
            android:layout_width="75dp"
            android:layout_height="75dp"
            android:background="@drawable/button1"/>


        <Button
            android:id="@+id/button2"
            android:layout_width="75dp"
            android:layout_height="75dp"
            android:background="@drawable/button2"/>

        <Button
            android:id="@+id/button3"
            android:layout_width="75dp"
            android:layout_height="75dp"
            android:background="@drawable/button3"
            android:text="" />

        <Button
            android:id="@+id/button4"
            android:layout_width="75dp"
            android:layout_height="75dp"
            android:background="@drawable/button4"
            android:text="" />


        <Button
            android:id="@+id/button5"
            android:layout_width="75dp"
            android:layout_height="75dp"
            android:background="@drawable/button5"
            android:text="" />

        <Button
            android:id="@+id/button6"
            android:layout_width="75dp"
            android:layout_height="75dp"
            android:background="@drawable/button6"
            android:text="" />

        <Button
            android:id="@+id/button7"
            android:layout_width="75dp"
            android:layout_height="75dp"
            android:background="@drawable/button7"
            android:text="" />

        <Button
            android:id="@+id/button8"
            android:layout_width="75dp"
            android:layout_height="75dp"
            android:background="@drawable/button8"
            android:text=""
            android:textSize="" />

        <Button
            android:id="@+id/button9"
            android:layout_width="75dp"
            android:layout_height="75dp"
            android:background="@drawable/button9"
            android:text="" />

        <Button
            android:id="@+id/bCaitlyn"
            android:layout_width="75dp"
            android:layout_height="75dp"
            android:background="@drawable/caitlynsplash"
            android:text="" />

        <Button
            android:id="@+id/button10"
            android:layout_width="75dp"
            android:layout_height="75dp"
            android:background="@drawable/button10"
            android:text=""
            android:textSize="10.5dp" />

        <Button
            android:id="@+id/button11"
            android:layout_width="75dp"
            android:layout_height="75dp"
            android:background="@drawable/button11"
            android:text=""
            android:textSize="11dp" />

        <Button
            android:id="@+id/button12"
            android:layout_width="75dp"
            android:layout_height="75dp"
            android:background="@drawable/button12"
            android:text="" />

        <Button
            android:id="@+id/button13"
            android:layout_width="75dp"
            android:layout_height="75dp"
            android:background="@drawable/button13"
            android:text=""
            android:textSize="11dp" />
        </LinearLayout>
        </HorizontalScrollView>

There seems to be a problem with the onClick command not executing on press. The button is recognizing the touch(as it changes colors) however nothing seems to happen.

    package com.LeagueProject;

    import android.app.Activity;
    import android.content.Intent;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;

    public class Project extends Activity implements View.OnClickListener{

Button Button1, Button2, Button3, Button4, Button5, Button6, Button7, Button8,
    Button9, Button10, Button11, Button12, Button13;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    projectbuttons();   
    Button1.setOnClickListener(this);
    Button2.setOnClickListener(this);
    Button3.setOnClickListener(this);

}


// Assigned Buttons
private void projectbuttons() {

    Button1 = (Button) findViewById(R.id.button1);
    Button2 = (Button) findViewById(R.id.button2);
    Button3 = (Button) findViewById(R.id.button3);
    Button4 = (Button) findViewById(R.id.button4);
    Button5 = (Button) findViewById(R.id.button5);
    Button6 = (Button) findViewById(R.id.button6);
    Button7 = (Button) findViewById(R.id.button7);
    Button8 = (Button) findViewById(R.id.button8);
    Button9 = (Button) findViewById(R.id.button9);
    Button10 = (Button) findViewById(R.id.button10);
    Button11 = (Button) findViewById(R.id.button11);
    Button12 = (Button) findViewById(R.id.button12);
    Button13 = (Button) findViewById(R.id.button13);

}


public void onClick(View v) {
    int id = v.getId();
    switch (id) {
        case R.id.button1:
        setContentView(R.layout.screen1);
            break;

        case R.id.button2:
        setContentView(R.layout.screen2);
            break;

        case R.id.button3:
        setContentView(R.layout.screen3);
            break;

    }

}
    }

Any insight as well as tips/techniques are highly welcomed.

Answers


When you implements View.OnClickListener in your Activity then you have to Override the onClick() method else it never executes..

@Override
public void onClick(View v) {
  .
  .
  .
 }

i tried your code using default buttons without giving any images to button and it is working fine... it goes to the corresponding layout....


I'm not sure if this is what you need, but you can define the onClick() method within the layout file itself.

<Button 
        android:id="@+id/button1"
        android:layout_width="75dp"
        android:layout_height="75dp"
        android:background="@drawable/button1" 
        android:onClick="onClick"
/>

Can you try this?


Need Your Help

Ensime Vim .ensime file not found

scala vim ensime

I'm trying to get Ensime for Vim working, I compiled the project for ensime using sbt 'update' then 'dist', and compiled the c file, but when i try starting ensime in Vim with :Ensime it prints out...

Creating an li dynamically based on data-attribute using JQuery

jquery html5

I want something that functions like this: You click on a name such as Mary Smith and it dynamically generates the data-name value in another ul and li element. I have that part working,

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.