bob tahog
bob tahog

Reputation: 11

Android Button click wont work

None of the buttons in one of my Activities will work.

I used android:onClick="openNewCategory" and when this didn't work, I also tried implementing an onClickListener which still didn't work. Then, I tried explicitly using android:clickable="true" which also did not work.

I have isolated the problem to be linked to the code in my onCreate method as it works when I comment it out, but I cannot figure out exactly what it could be as it is all just formatting the view.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    ScrollView scrollView = new ScrollView(this);
    RelativeLayout relativeLayout = new RelativeLayout(this);

    ArrayList<Category> categoryArrayList;

    try {
        categoryArrayList = Utils.loadCategoryList();

        for (int i=0; i<categoryArrayList.size(); i++) {
            TextView textView = new TextView(this);
            textView.setText(categoryArrayList.get(i).getName());
            textView.setTextSize(24);
            textView.setPadding(10, 10, 10, 10);
            textView.setId(i + 1);

            RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);

            if (i == 0) {} else {
                layoutParams.addRule(RelativeLayout.BELOW, i);
            }
            layoutParams.addRule(RelativeLayout.ALIGN_PARENT_START);
            textView.setLayoutParams(layoutParams);
            relativeLayout.addView(textView)
        }

        for (int i=0; i<categoryArrayList.size(); i++) {
            EditText editText = new EditText(this);
            editText.setId(i + 101);
            DecimalFormat decimalFormat = new DecimalFormat("00");
            editText.setText(decimalFormat.format(categoryArrayList.get(i).getPercentage()*100));
            editText.setTextSize(24);
            RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            layoutParams.addRule(RelativeLayout.ALIGN_PARENT_END);

            RelativeLayout.LayoutParams lp2 = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);


            if (i == 0) {} else {
                layoutParams.addRule(RelativeLayout.BELOW, i);
            }

            editText.setLayoutParams(layoutParams);
            relativeLayout.addView(editText);


            TextView textView = new TextView(this);
            textView.setText("%");
            textView.setTextSize(24);
            lp2.addRule(RelativeLayout.ALIGN_BOTTOM, i + 101);
            lp2.addRule(RelativeLayout.LEFT_OF, i + 101);
            textView.setLayoutParams(lp2);
            relativeLayout.addView(textView);

        }


    } catch (IOException e) {
        e.printStackTrace();
    }
    setContentView(R.layout.edit_categories);
    RelativeLayout mainLayout = (RelativeLayout) findViewById(R.id.edit_categories_main_container);

    RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
    layoutParams.addRule(RelativeLayout.ALIGN_TOP);
    scrollView.setLayoutParams(layoutParams);
    scrollView.addView(relativeLayout);
    mainLayout.addView(scrollView);
    setContentView(mainLayout);

}

public void openNewCategory (View view) {
    Intent openNewCategoryIntent = new Intent(this, MainActivity.class);
    startActivity(openNewCategoryIntent);
}

What in it could be causing this problem?

SOLVED: I just got it completely solved! I made the edit_categories_button_container the root layout in the xml. I defined the mainContainer as a new layout in the java code and then added the button container. Working code block:

setContentView(R.layout.edit_categories); //to make xml elements accessible
RelativeLayout mainLayout = new RelativeLayout(this);
RelativeLayout buttonContainer = (RelativeLayout) findViewById(R.id.edit_categories_button_container);

ViewGroup buttonContainerParent = (ViewGroup) buttonContainer.getParent();
buttonContainerParent.removeView(buttonContainer); //necessary to avoid "child already has parent error"

mainLayout.addView(buttonContainer);
setContentView(mainLayout);

Thanks everybody for your input!

Upvotes: 1

Views: 68

Answers (2)

Hamad
Hamad

Reputation: 5152

See i have tried this and your problem is solved here! using onClock property in XML:

1. Activity Home:

package com.hamadshaikh.budgetmanagement;

import android.app.Activity;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;

import java.io.IOException;
import java.text.DecimalFormat;
import java.util.ArrayList;


public class Home extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        ScrollView scrollView = new ScrollView(this);
        RelativeLayout relativeLayout = new RelativeLayout(this);

        ArrayList<Category> categoryArrayList;

        try {
            categoryArrayList = Utils.loadCategoryList();

            for (int i=0; i<categoryArrayList.size(); i++) {
                TextView textView = new TextView(this);
                textView.setText(categoryArrayList.get(i).getName());
                textView.setTextSize(24);
                textView.setPadding(10, 10, 10, 10);
                textView.setId(i + 1);

                RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);

                if (i == 0) {} else {
                    layoutParams.addRule(RelativeLayout.BELOW, i);
                }
                layoutParams.addRule(RelativeLayout.ALIGN_PARENT_START);
                textView.setLayoutParams(layoutParams);
                relativeLayout.addView(textView);
            }

            for (int i=0; i<categoryArrayList.size(); i++) {
                EditText editText = new EditText(this);
                editText.setId(i + 101);
                DecimalFormat decimalFormat = new DecimalFormat("00");
                editText.setText(decimalFormat.format(categoryArrayList.get(i).getPercentage()*100));
                editText.setTextSize(24);
                RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                layoutParams.addRule(RelativeLayout.ALIGN_PARENT_END);

                RelativeLayout.LayoutParams lp2 = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);


                if (i == 0) {} else {
                    layoutParams.addRule(RelativeLayout.BELOW, i);
                }

                editText.setLayoutParams(layoutParams);
                relativeLayout.addView(editText);


                TextView textView = new TextView(this);
                textView.setText("%");
                textView.setTextSize(24);
                lp2.addRule(RelativeLayout.ALIGN_BOTTOM, i + 101);
                lp2.addRule(RelativeLayout.LEFT_OF, i + 101);
                textView.setLayoutParams(lp2);
                relativeLayout.addView(textView);

            }


        } catch (Exception e) {
            e.printStackTrace();
        }
        setContentView(R.layout.activity_home);
        RelativeLayout mainLayout = (RelativeLayout) findViewById(R.id.edit_categories_main_container);

        RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        layoutParams.addRule(RelativeLayout.ALIGN_TOP);
        scrollView.setLayoutParams(layoutParams);
        scrollView.addView(relativeLayout);
        mainLayout.addView(scrollView);
        setContentView(mainLayout);

    }

    public void openNewCategory (View view) {
        Toast.makeText(this,"Click Performed",Toast.LENGTH_SHORT).show();
        //Intent openNewCategoryIntent = new Intent(this, MainActivity.class);
        //startActivity(openNewCategoryIntent);
    }


}

2. main_layout:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:id="@+id/edit_categories_main_container"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".Home">

    <Button android:text="@string/hello_world" android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:onClick="openNewCategory"/>

</RelativeLayout>

3. Utils Class

import java.util.ArrayList;

/**
 * Created by hamadshaikh on 13/08/15.
 */
public class Utils {
    public static ArrayList<Category> loadCategoryList() {
        ArrayList<Category> categories=new ArrayList<Category>();
        Category category=new Category();
        category.setName("Hamad");
        category.setPercentage(99);
        categories.add(category);

        return categories;
    }
}

4. Category Class

public class Category {
    private String name;
    private int percentage;

    public String getName() {
        return name;
    }

    public int getPercentage() {
        return percentage;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setPercentage(int percentage) {
        this.percentage = percentage;
    }
}

I have tried this and onClock property is working here, I have tested it by showing Toast "Click Performed" on onClick of button. I hope your problem is solved now!

Upvotes: 0

yshahak
yshahak

Reputation: 5096

You called to:

 setContentView()

3 times in your code. Are you sure you understand what it does?

Upvotes: 1

Related Questions