0

I am learning Android and stuck at this. I want to make a project in Android. In MainActivity I have a button and want to set onClick() event such that pressing it, if button's text is "SOLVE" then add fragment Output and change button's name to "BACK" and if button's text is "BACK" then add fragment Input and change Button's text to "SOLVE".

Here's my MainActivity.java code

package com.example.sudokusolver;

import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.Button;

public class MainActivity extends ActionBarActivity {
Button a        
String check ;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        a=(Button) findViewById(R.id.press);
        check=a.getText().toString();


        a.setOnClickListener(new View.OnClickListener(){
             public void onClick(View v){

                if(check.equals("SOLVE")){

                     Output OP=new Output();

                     FragmentManager fragmentManager = getFragmentManager();
                     FragmentTransaction ft =
                     fragmentManager.beginTransaction();

                     ft.add(R.id.frag,OP);

                     ft.commit();

                     a.setText("BACK");
                }
                else
                {
                    Input IP=new Input();

                     FragmentManager fragmentManager = getFragmentManager();
                     FragmentTransaction ft =
                     fragmentManager.beginTransaction();

                     ft.add(R.id.frag,IP);

                     ft.commit();

                     a.setText("SOLVE");

                }

             }
        });

    }

}

here's main_activit.xml file

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical">


    <FrameLayout 
        android:id="@+id/frag"
        android:layout_weight="1"
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        />

    <RelativeLayout 
        android:layout_width="fill_parent"
        android:layout_height="90dp">

        <Button
            android:id="@+id/press"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:text="@string/show" />

    </RelativeLayout>

</LinearLayout>

The problem is (you might have seen) I can't compare ( check string) non-final variable inside an inner class. SO please help me with this and if you think my whole idea is wrong then suggest me proper way.

(if other files require then tell me, I will edit)

EDIT

There is something wrong with my logic. when I first press 'Solve' it turned to 'Back'. But when I press 'Back' it doesn't turn to 'Solve'. I want Solve > Back > Solve > Back... am i missing something? (maybe in add/replace transaction ?)

4
  • define views globally..no need to handle final.. Commented Oct 12, 2014 at 8:29
  • 2
    If the button has no use other than this no need to declare it final or declare it globally. Just use the view from the arguments in the onClick. The view there is the Button itself Commented Oct 12, 2014 at 8:33
  • There is something wrong with my logic. when I first press 'Solve' it turned to 'Back'. But when I press 'Back' it doesn't turn to 'Solve'. I want Solve > Back > Solve > Back... am i missing something? (maybe in add/replace transaction ?) Commented Oct 12, 2014 at 9:09
  • declare the button at class level it will solve your issue Commented Oct 12, 2014 at 9:39

3 Answers 3

1

To be able to access variable a (your Button) inside of anonymous classes created in some method, you need to declare the variable final. Example:

final Button a = (Button) findViewById(R.id.press);

Make check (your String) final too, as it's used in the anonymous class also:

final String check = a.getText().toString();
Sign up to request clarification or add additional context in comments.

1 Comment

@DhavalGondaliya OP said nothing about using this variable globally. Furthermore his given code doesn't imply that either. I don't see how that's good :) Just adds an unnecessary line of code.
1

Replace if(check.equals("SOLVE")) with if(((Button)v).getText().toString().equals("SOLVE")).

To set a new text use this ((Button)v).setText("YourText")

In OnClick the view is getting passed as the argument is the Button itself. So you can manipulate the view as Button and set or get its properties

3 Comments

Ya it works! but there is still problem with a.setText("BACK")
this answer has been flagged as a low quality answer, please provide some explanation
dont use a inside the onClick listener. Use the view that is passed as arguments
0

Define your button as class variable as

public class MainActivity extends ActionBarActivity {
Button a;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    a =(Button) findViewById(R.id.press);

and compare in listener as

if(a.getText().toString().equals("SOLVE")){

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.