ajay
ajay

Reputation: 125

How to change Fragment in Android

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 ?)

Upvotes: 0

Views: 183

Answers (3)

Panther
Panther

Reputation: 9408

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

Upvotes: 1

Giru Bhai
Giru Bhai

Reputation: 14398

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")){

Upvotes: 0

Simas
Simas

Reputation: 44118

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();

Upvotes: 1

Related Questions