I've looked everywhere, but I can't find what I'm looking for.
I'm trying to make a stopwatch app for android using a Chronometer inside of a Fragment, but I'm getting errors on the setOnClickListener for some buttons.
It's the variables inside of parentheses "()" fx. (mStartListener), I get the error mStartListener cannot be resolved to a variable.
please help me, I really wanna get into android developing. Thank you
Here's the code
b = (Button) getView().findViewById(R.id.start);
b.setOnClickListener(mStartListener); //--- error
b = (Button) getView().findViewById(R.id.stop);
b.setOnClickListener(mStopListener); //--- error
b = (Button) getView().findViewById(R.id.reset);
b.setOnClickListener(mResetListener); //--- error
Full Code:
import android.app.Fragment;
import android.os.Bundle;
import android.os.SystemClock;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Chronometer;
public class StopWatchFragment extends Fragment {
Chronometer mChronometer;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.stopwatch, container, false);
Button b;
mChronometer = (Chronometer) getView().findViewById(R.id.chronometer);
// Watch for button clicks.
b = (Button) getView().findViewById(R.id.start);
b.setOnClickListener(mStartListener); //--- error
b = (Button) getView().findViewById(R.id.stop);
b.setOnClickListener(mStopListener); //--- error
b = (Button) getView().findViewById(R.id.reset);
b.setOnClickListener(mResetListener); //--- error
return view;
View.OnClickListener mStartListener = new OnClickListener() {
public void onClick(View v) {
mChronometer.start();
}
};
View.OnClickListener mStopListener = new OnClickListener() {
public void onClick(View v) {
mChronometer.stop();
}
};
View.OnClickListener mResetListener = new OnClickListener() {
public void onClick(View v) {
mChronometer.setBase(SystemClock.elapsedRealtime());
}
};
}
}
Move where you declare each Listener to be class variables:
public class StopWatchFragment extends Fragment {
Chronometer mChronometer;
View.OnClickListener mStartListener;
...
Or just declare your listeners before you try to use them:
Button b;
mChronometer = (Chronometer) getView().findViewById(R.id.chronometer);
View.OnClickListener mStartListener = new OnClickListener() {
public void onClick(View v) {
mChronometer.start();
}
};
...
// Watch for button clicks.
b = (Button) getView().findViewById(R.id.start);
b.setOnClickListener(mStartListener); //--- no more error
You are defining and initializing the mStopListener inside the onCreateView method, AFTER you try to set them as the OnClickListener.
You should either move them to the start of your method:
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.stopwatch, container, false);
View.OnClickListener mStartListener = new OnClickListener() {
public void onClick(View v) {
mChronometer.start();
}
};
View.OnClickListener mStopListener = new OnClickListener() {
public void onClick(View v) {
mChronometer.stop();
}
};
View.OnClickListener mResetListener = new OnClickListener() {
public void onClick(View v) {
mChronometer.setBase(SystemClock.elapsedRealtime());
}
};
Button b;
mChronometer = (Chronometer) getView().findViewById(R.id.chronometer);
// Watch for button clicks.
b = (Button) getView().findViewById(R.id.start);
b.setOnClickListener(mStartListener); //--- error
b = (Button) getView().findViewById(R.id.stop);
b.setOnClickListener(mStopListener); //--- error
b = (Button) getView().findViewById(R.id.reset);
b.setOnClickListener(mResetListener); //--- error
return view;
}
Or, put them outside your method as global variables.
Related
Hi I am new in android learning and I'm practice it. I Want to implement click listener on my button but it is giving me error. even i imported android.view.View.OnClickListener; and other all libraries.
please have look at my code check where is my mistake
Cannot Resolve symbol onClickelistner.
package com.example.nexus.myapp;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import android.view.View.OnClickListener;
public class MainActivity extends AppCompatActivity {
Button button;
TextView loginresulttext;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.loginbutton);
loginresulttext = (TextView) findViewById(R.id.loginresult);
}
button.OnClickListener(new OnClickListener()
{
public void onClick(View v)
{
Toast.makeText(getApplicationContext(),"Button Is Clicked",Toast.LENGTH_LONG).show();
}
});
}
Try this and put it in onCreate method
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//do something
}
});
Combine R2R and Toan Tran answers will give you the best solution:
public class MainActivity extends AppCompatActivity {
Button button;
TextView loginresulttext;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.loginbutton);
loginresulttext = (TextView) findViewById(R.id.loginresult);
button.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
Toast.makeText(getApplicationContext(),"Button Is Clicked",Toast.LENGTH_LONG).show();
}
});
}
This should work.
Button button = (Button) findViewById(R.id.testButton);
button.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
// TODO Auto-generated method stub
}
});
Try button click inside Oncreate()
public class MainActivity extends AppCompatActivity {
Button button;
TextView loginresulttext;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.loginbutton);
loginresulttext = (TextView) findViewById(R.id.loginresult);
button.setOnClickListener(new OnClickListener(){
public void onClick(View v)
{
Toast.makeText(getApplicationContext(),"Button Is Clicked",Toast.LENGTH_LONG).show();
}
});
}
}
I want to change the TextView of editor activity by clicking on Button buttonExpense which should also open editor activity and show value of the result in
EditText mAmountEditText.
I am getting java.lang.ClassCastException in which it is clearly written that MainActivity cannot be cast to EditorActivity
Calculator.java
package com.example.harshitbahri.expensebook;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class Calculator extends android.support.v4.app.Fragment {
Button button0;
Button button1;
Button button2;
Button button3;
Button button4;
Button button5;
Button button6;
Button button7;
Button button8;
Button button9;
Button buttonAdd;
Button buttonSubstract;
Button buttonMul;
Button buttonDiv;
Button buttonClear;
Button buttonEqual;
Button buttonExpense;
String result;
String tmp;
String operator;
TextView resultTextView;
FragmentActivity fa2;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
fa2 = (FragmentActivity) super.getActivity();
final View RootView= inflater.inflate(R.layout.activity_calculator, container, false);
button0 = (Button)RootView.findViewById(R.id.button0);
button1 = (Button)RootView.findViewById(R.id.button1);
......
buttonAdd = (Button)RootView.findViewById(R.id.buttonAdd);
buttonClear = (Button)RootView.findViewById(R.id.buttonClear);
buttonSubstract = (Button)RootView.findViewById(R.id.buttonSub);
buttonMul = (Button)RootView.findViewById(R.id.buttonMul);
buttonDiv = (Button)RootView.findViewById(R.id.buttonDiv);
buttonEqual = (Button)RootView.findViewById(R.id.buttonEqual);
buttonExpense= (Button)RootView.findViewById(R.id.buttonExp);
resultTextView = (TextView)RootView.findViewById(R.id.text_view_result);
button0.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
onNumberButtonClicked("0");
}
});
button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
onNumberButtonClicked("1");
}
});
button2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
onNumberButtonClicked("2");
}
});
button3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
onNumberButtonClicked("3");
}
});
button4.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
onNumberButtonClicked("4");
}
});
button5.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
onNumberButtonClicked("5");
}
});
button6.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
onNumberButtonClicked("6");
}
});
button7.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
onNumberButtonClicked("7");
}
});
button8.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
onNumberButtonClicked("8");
}
});
button9.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
onNumberButtonClicked("9");
}
});
buttonClear.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
onClearButtonClicked();
}
});
buttonSubstract.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v){
onOperatorButtonClicked("-");
}
});
buttonAdd.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
onOperatorButtonClicked("+");
}
});
buttonMul.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
onOperatorButtonClicked("X");
}
});
buttonDiv.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
onOperatorButtonClicked("/");
}
});
buttonEqual.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
onEqualButtonClicked();
}
});
buttonExpense.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(fa2, EditorActivity.class);
startActivity(intent);
((EditorActivity) getActivity()).updateGoldTextView(result);
/*Intent intent = new Intent(fa2, EditorActivity.class);
intent.putExtra(, result);
final int result2=1;
startActivityForResult(intent, result2);*/
}
});
return RootView;
}
private void onEqualButtonClicked() {
int res = 0;
try {
int number = Integer.valueOf(tmp);
int number2 = Integer.valueOf(resultTextView.getText().toString());
switch (operator) {
case "+":
res = number + number2;
break;
case "/":
res = number / number2;
break;
case "-":
res = number - number2;
break;
case "X":
res = number * number2;
break;
}
result = String.valueOf(res);
resultTextView.setText(result);
}
catch (Exception e) {
e.printStackTrace();
}
}
public void onOperatorButtonClicked(String operator) {
..
}
public void onClearButtonClicked() {
...
}
public void onNumberButtonClicked(String pos) {
..
}
public interface OnFragmentInteractionListener {
// TODO: Update argument type and name
void onFragmentInteraction(Uri uri);
}
}`
EditorActivity.java
package com.example.harshitbahri.expensebook;
import android.app.AlertDialog;
import android.app.LoaderManager;
...
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
public class EditorActivity extends AppCompatActivity implements
LoaderManager.LoaderCallbacks<Cursor> {
private static final int EXISTING_EXPENSE_LOADER = 0;
private Uri mCurrentExpenseUri;
private EditText mAmountEditText;
private EditText mDescEditText;
private Spinner mCategorySpinner;
private String mCategory = ExpenseEntry.CATEGORY_GENERAL;
private View.OnTouchListener mTouchListener = new View.OnTouchListener() {
#Override
public boolean onTouch(View view, MotionEvent motionEvent) {
mExpenseHasChanged = true;
return false;
}
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_editor);
// Examine the intent that was used to launch this activity,
// in order to figure out if we're creating a new pet or editing an existing one.
Intent intent = getIntent();
mCurrentExpenseUri = intent.getData();
// If the intent DOES NOT contain a pet content URI, then we know that we are
// creating a new pet.
if (mCurrentExpenseUri == null) {
// This is a new pet, so change the app bar to say "Add a Pet"
setTitle("Add Expense");
yet.)
invalidateOptionsMenu();
} else {
// Otherwise this is an existing pet, so change app bar to say "Edit Pet"
setTitle("Edit Expense");
getLoaderManager().initLoader(EXISTING_EXPENSE_LOADER, null, this);
}
mAmountEditText = (EditText) findViewById(R.id.exp_amount);
mDescEditText = (EditText) findViewById(R.id.description);
mCategorySpinner = (Spinner) findViewById(R.id.spinner_category);
.....
}
......
finish();
}
public void updateGoldTextView(String goldAmount) {
mAmountEditText.setText(goldAmount);
}
}
MainActivity.java
package com.example.harshitbahri.expensebook;
import android.net.Uri;
import android.support.design.widget.TabLayout;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity implements
Calculator.OnFragmentInteractionListener ,
Entries.OnFragmentInteractionListener,Info.OnFragmentInteractionListener{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TabLayout tabLayout = (TabLayout)findViewById(R.id.tablayout);
tabLayout.addTab(tabLayout.newTab().setText("Tab 1"));
tabLayout.addTab(tabLayout.newTab().setText("Tab 2"));
tabLayout.addTab(tabLayout.newTab().setText("Tab 3"));
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
final ViewPager viewPager = (ViewPager)findViewById(R.id.pager);
final Pageradapter adapter = new Pageradapter(getSupportFragmentManager(),tabLayout.getTabCount());
viewPager.setAdapter(adapter);
viewPager.setOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
viewPager.setCurrentItem(1, false);
tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
#Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(TabLayout.Tab tab) {
}
#Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
}
#Override
public void onFragmentInteraction(Uri uri) {
}
}
Your fragment is loaded from MainActivity and not EditorActivity, hence the class cast exception. If you want to pre-fill some data in the editor activity, you can pass that in the intent ( the code you commented on your onclick listener) and retrieve in the EditorActivity and set your UI elements.
Edited
buttonExpense.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(fa2, EditorActivity.class);
intent.putExtra("result", result);
startActivity(intent);
}});
In your EditorActivity class, in on create
String result=getIntent().getStringExtra("result")
updateGoldTextView(result)
I have no idea why this code does not working. I have a TextView called text1 and Button called button1. I want to change the text in text1 after clicking button1.
I noticed that program works when I comment line button1.setOnClickListener(this); but consequently nothing happened.
package com.example.testowaniefragmentow;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends ActionBarActivity implements OnClickListener {
TextView text1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button1 = (Button) findViewById(R.id.button_B1);
button1.setOnClickListener(this);
TextView text1 = (TextView) findViewById(R.id.textview_text);
if (savedInstanceState == null)
{
getSupportFragmentManager().beginTransaction().add(R.id.container, new PlaceholderFragment()).commit();
}
}
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,false);
return rootView;
}
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button_B1:
text1.setText("Text has been changed");
break;
default:
break;
}
}
}
Change
setContentView(R.layout.activity_main);
to
setContentView(R.layout.fragment_main);
Hope it helps..
OR
as Mike said copy the whole contents of fragment_main.xml to activity_main.xml..
You have incorrectly defined your TextView. You have two text TextViews in your code. one is in your MainActivity class and the second is local variable for onCreate method. so in your onClicklistener when you try to set the text for text1 it is actually not been initialised. Please change your code in MainActivity class and onCreate method as following and keep everything as same.
//declare the view and button here.
TextView text1;
Button button1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//initialise the view and button here but do not declare it again or else they will become local variable
button1 = (Button) findViewById(R.id.button_B1);
button1.setOnClickListener(this);
text1 = (TextView) findViewById(R.id.textview_text);
if (savedInstanceState == null)
{
getSupportFragmentManager().beginTransaction().add(R.id.container, new PlaceholderFragment()).commit();
}
}
Try this bro:
button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.button_B1:
text1.setText("Text has been changed");
break;
default:
break;
}
}
});
Can anyone tell me why this is not working? I have no errors at all but simply doesnt work.
This actually used to work fine until I added the ScreenSwipe, now I cant figure out whats wrong.
Thanks
package com.sample.sample
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
public class PageOneFragment extends Fragment {
ImageButton btn1;
static int i = 0;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
ViewGroup rootView = (ViewGroup) inflater.inflate(
R.layout.page1_layout, container, false);
btn1 = (ImageButton) rootView.findViewById(R.id.m1_btn);
return rootView;
}
public void setupPlayPauseButton1(View view) {
btn1 = (ImageButton) view.findViewById(R.id.m1_btn);
btn1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
i +=1;
if (i % 2 == 0) {
btn1.setImageResource(R.drawable.pause);
} else {
btn1.setImageResource(R.drawable.play);
}
}
});
}
}
EDIT
Is this right?
package com.wepromoteme.brapp;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
public class PageOneFragment extends Fragment {
ImageButton btn1;
int i = 0;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
ViewGroup rootView = (ViewGroup) inflater.inflate(
R.layout.page1_layout, container, false);
btn1 = (ImageButton) rootView.findViewById(R.id.m1_btn);
setupPlayPauseButton1(rootView);
return rootView;
}
private void setupPlayPauseButton1(View view) {
btn1 = (ImageButton) view.findViewById(R.id.m1_btn);
btn1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
i +=1;
if (i % 2 == 0) {
btn1.setImageResource(R.drawable.pause);
} else {
btn1.setImageResource(R.drawable.play);
}
}
});
}
}
You did not tell what "doesn't work", but I am willing to guess this is the cause: static int i = 0;
static here means that the value of i is shared by all instances of PageOneFragment.
If you are using static to keep the variable value when the screen is rotated, etc., you should use the fragment onSaveInstanceState() method instead. You can find more information about this on the official documentation:
Handling the Fragment Lifecycle
As it happens I have figured it out, I decided to let my brain have a few hours to rest as I think I got into a downward spiral of not being able to see that wood for the trees :P
Here is my code for anyone else who stumbles on the same problems I have. This actually works:
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
public class PageTwoFragment extends Fragment {
int i = 0;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
ViewGroup rootView = (ViewGroup) inflater.inflate(
R.layout.page2_layout, container, false);
final ImageButton btn1 = (ImageButton) rootView.findViewById(R.id.m1_btn);
final ImageButton btn2 = (ImageButton) rootView.findViewById(R.id.m2_btn);
final ImageButton btn3 = (ImageButton) rootView.findViewById(R.id.m3_btn);
final ImageButton btn4 = (ImageButton) rootView.findViewById(R.id.m4_btn);
final ImageButton btn5 = (ImageButton) rootView.findViewById(R.id.m5_btn);
btn1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
i +=1;
if (i % 2 == 0) {
btn1.setImageResource(R.drawable.pause);
} else {
btn1.setImageResource(R.drawable.play);
}
}
});
btn2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
i +=1;
if (i % 2 == 0) {
btn2.setImageResource(R.drawable.pause);
} else {
btn2.setImageResource(R.drawable.play);
}
}
});
btn3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
i +=1;
if (i % 2 == 0) {
btn3.setImageResource(R.drawable.pause);
} else {
btn3.setImageResource(R.drawable.play);
}
}
});
btn4.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
i +=1;
if (i % 2 == 0) {
btn4.setImageResource(R.drawable.pause);
} else {
btn4.setImageResource(R.drawable.play);
}
}
});
btn5.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
i +=1;
if (i % 2 == 0) {
btn5.setImageResource(R.drawable.pause);
} else {
btn5.setImageResource(R.drawable.play);
}
}
});
return rootView;
}
}
package com.purelymean.earnings;
import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.TextView;
public class Main extends Activity{
/**Called when activity is first created. */
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button b = (Button) findViewById(R.id.button);
TextView tx = (TextView) findViewById(R.id.textView1);
}
}
What do I need to put so that when the button is clicked it will change the textview to whatever i put?
You should add an OnClickListener to your button and override onClick function to set your desire text in your TextView
Button b = (Button) findViewById(R.id.button);
b.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
TextView tx = (TextView) findViewById(R.id.textView1);
tx.setText("yourtext");
}
});
Add an OnClickListener to your button and thats all you need.
b.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
tx.setText("It Works!");
}
});