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 ?)
finalor declare it globally. Just use theviewfrom the arguments in theonClick. Theviewthere is theButtonitself