I know this has been asked before but I cannot make this work so here is what I have so far
class Click extends Activity {
int i=0;
protected void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.activity_main);
final TextView mTextView = (TextView) findViewById(R.id.Counter);
mTextView.setText(""+i);
final Button button = (Button) findViewById(R.id.AddOne);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
TextView tv= (TextView) findViewById(R.id.Counter);
i=i+1;
mTextView.setText(Integer.toString(i));
}
});
}
Every time I run the app in an emulator it crashes
java.lang.IllegalStateException: Could not find a method Click(View) in the activity class com.scouting.corbin.frc_201415_scouting.MainActivity for onClick handler on view class android.widget.Button with id 'AddOne'
I know this is probably something completely stupid but I am new to this and need help thank you in advance.
As per your logcat.
java.lang.IllegalStateException: Could not find a method Click(View)
in the activity class
com.scouting.corbin.frc_201415_scouting.MainActivity for onClick
handler on view class android.widget.Button with id 'AddOne'
I suggest you to add Click(View v) in your MainActivity
public void Click(View v)
{
}
You need to take the root element here. Depending on parent layout include this line in the activity after setContentView().
RelativeLayout layout=(RelativeLayout)findViewById(R.id.yourLayoutId);// If its some other layout change "RelativeLayout" to your opted layout.
and in onClick() method of button, add following.
layout.add(tv);
Yopu want To add one Linearlayout in xml file
and set id for your LinearLayout.
android:id="#+id/linearlayout"
And change your addTextView method to following
public void addTextView(String text){
LinearLayout layout=(LinearLayout)findViewById(R.id.linear);
TextView textView=new TextView(this);
textView.setText(text);
textView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
layout.addView(textView);
}
and call this method from your Forloop
Perhaps consider using the android:onClick="example_method" attribute for the button in your xml file. Then create the appropriate method in the class. public void example_method(View v) {} Then place the code you have in your onClick function into the new one. It's easier than using an listener.
Ok so all of you helped I completely got rid of that code which was too comlicated for what I was trying to do. After taking bits of suggestions and some reasearch I came up with this
public void AddOne(View v) {
TextView tv= (TextView) findViewById(R.id.Counter);
i=i+1;
tv.setText(""+i);
}
As you can see much simpler than what I had before and this one works thank you all
Related
[SOLVED] Silly typo: This code solved my problem:
dateTimeEasyText.setText (""); changed to dateAndTimeEasyText.setText ("");
.
PROBLEM:
I have an ImageView which on click should reset two of my TextViews, one containing HighScore (numbers) and the other TextView containing Date & Time (String).
My coding:
public void resetHighcoreButtonEasy(View v) {
highscoreEasyText.setText("");
dateTimeEasyText.setText ("");
}//resetHighcoreButtonEasy ends here
.
Printscreen on the coding and the message:
.
JAVA-file:
public class HighScoreActivity extends AppCompatActivity implements View.OnClickListener {
TextView highscoreEasyText;
TextView dateAndTimeEasyText;
ImageView resetHighcoreButtonEasy;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_high_score);
resetHighcoreButtonEasy = (ImageView) findViewById(R.id.resetHighcoreButtonEasy);
SharedPreferences sharedPrefsEasyHighScore = getSharedPreferences("Prefs_EasyHighScore",MODE_PRIVATE);
int storedEasyHighScore = sharedPrefsEasyHighScore.getInt("easy_highScore",0);
highscoreEasyText = (TextView)findViewById(R.id.highscoreEasyText);
highscoreEasyText.setText("" + storedEasyHighScore + " p");
highscoreEasyText.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.highscore_text));
SharedPreferences sharedPrefsEasyDateTime = getSharedPreferences("Prefs_EasyDateTime",MODE_PRIVATE);
String dateTime = sharedPrefsEasyDateTime.getString("easy_date_time", null);
dateAndTimeEasyText = (TextView)findViewById(dateTimeEasyText);
dateAndTimeEasyText.setText(dateTime);
dateAndTimeEasyText.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.highscore_text));
}//onCreate ends here
public void resetHighcoreButtonEasy(View v) {
highscoreEasyText.setText("");
dateTimeEasyText.setText ("");
}//resetHighcoreButtonEasy ends here
You are using wrong variable to access dateTimeEasyText.
As per your declaration it is suppose to be dateAndTimeEasyText.
Spell mistake.
Just a typo. You have:
TextView dateAndTimeEasyText;
And in your method you use it without And:
dateTimeEasyText.setText ("");
You should use:
dateAndTimeEasyText.setText("");
So dateTimeEasyText isn't a TextView, and it hasn't got a method setText(java.lang.String).
dateTimeEasyText should be `dateAndTimeEasyText`.
dateAndTimeEasyText.setText("");
In layout xml for ImageView resetHighcoreButtonEasy onClick should be there:
<ImageView
android:id="#+id/resetHighcoreButtonEasy"
...
...
android:onClick="resetHighcoreButtonEasy"
..... />
This line of code should be there to give click event from xml
android:onClick="resetHighcoreButtonEasy"
And your function should be public and have a param View in it, as you have done already
public void resetHighcoreButtonEasy(View v) {
I have this FlowLayout where I have a set of TextView's which I build programatically. After getting the wanted names, I create a TextView for each name inside the layout.
What I want to do, if I click on the TextView, I want to move it into another layout. I manage to do that but I also want to move it back. I could also do that until I program it to, but I can't program it to be like a infinite loop.
This is a piece of code which will make you understand better what I'm talking about hopefully.
TextView tv = new TextView(new ContextThemeWrapper(getActivity(), R.style.FlowLayoutTextView));
tv.setText("Test");
tv.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
TextView tvSelected = new TextView(new ContextThemeWrapper(getActivity(), R.style.FlowLayoutTextView));
tvSelected.setText(tv.getText().toString());
tvSelected.setLayoutParams(params);
tv.setVisibility(View.GONE);
filteredLayout.addView(tvSelected);
}
});
unfilteredLayout.addView(tv);
Is it possible to make it work? Thanks.
LE: As you can see in the onClickListener event of the TextView, I create the other TextView I add in the other layout, to move it back I could also add an onClickListener event to this TextView but this is not the solution.
Try following:
boolean isInFilterLayout = false; //Class variable
TextView tv = new TextView(new ContextThemeWrapper(getActivity(), R.style.FlowLayoutTextView));
tv.setText("Test");
tv.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(isInFilterLayout){
filteredLayout.remove(tv);
unfilteredLayout.addView(tv);
isInFilterLayout = false;
}else{
unfilteredLayout.remove(tv);
filteredLayout.addView(tv);
isInFilterLayout = true;
}
}
});
unfilteredLayout.addView(tv);
Currently i am having some problem with implementing onclicklistener to a dynamically creating textview. I will explain the problem more detailed. What i need to do is, i need to create textviews when i click a button in an activity and when i click on that textview it should get removed. but i am not able to set onclicklistener to each textview. Since, set onclicklistener of textviews are written inside the onclick function of the above said button(button used for creating the textview), its scope get over when it exits from onclick function of the button(i think this is the problem). So i tried using visible and invisible feature, which will create the textviews before hand and make them invisible and they are made visible only when the button(button used for creating the textview)is clicked. But here even though it is invisible the space will be allocated(ie, blank space will be availabe).
Here is my code
This button addphone will dynamically create textview by inserting the value present in the edittext phoneno
addphone.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(phoneno.getText().toString().length() > 0 && counter < MAX)
{
addphoneno[counter] = phoneno.getText().toString();
phoneno.setText("");
final TextView mybox = new TextView(getApplicationContext());
mybox.setText(addphoneno[counter]);
mybox.setPadding(5, 5, 5, 5);
mybox.setBackgroundColor(Color.rgb(99, 99, 99));
contactbox[counter] = mybox;
contactbox[counter].setLayoutParams(new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
contactbox[counter].setId(100+counter);
contactbox[counter].setText(addphoneno[counter]+" "+"X");
contactbox[counter].setClickable(true);
contactbox[counter].setOnClickListener(this); //This doesn't work!!!!!
counter = counter+1;
}
}
});
But the setOnClickListener in the above line is not working
So can anyone pls help me with this problem. I hope you are clear with my question.
Thank You!
You can try this:
private OnClickListener phoneViewClickListener = new OnClickListener() {
#Override
public void onClick(View v) {
// your code
}
};
and use that listener in your TextViews:
contactbox[counter].setOnClickListener(phoneViewClickListener);
You will have to actually define a onClickListener instead of simply setting it as a boolean value.
contactbox[counter].setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//this is where you would handle your click event
}
});
Good luck!
If your button was defined on the xml layout you can do that.
In your xml layout you can define which method will be called when a user click on your button:
<?xml version="1.0" encoding="utf-8"?>
<Button xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/add_phone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/button_send"
android:onClick="addTextView" /> // This is most imporant line
Your activity must have a method with the same name with a View parameter, like that:
/** Called when the user touches the button */
public void addTextView(View view) {
// Do something in response to button click
if(phoneno.getText().toString().length() > 0 && counter < MAX)
{
addphoneno[counter] = phoneno.getText().toString();
phoneno.setText("");
final TextView mybox = new TextView(getApplicationContext());
mybox.setText(addphoneno[counter]);
mybox.setPadding(5, 5, 5, 5);
mybox.setBackgroundColor(Color.rgb(99, 99, 99));
contactbox[counter] = mybox;
contactbox[counter].setLayoutParams(new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
contactbox[counter].setId(100+counter);
contactbox[counter].setText(addphoneno[counter]+" "+"X");
contactbox[counter].setClickable(true);
contactbox[counter].setOnClickListener(this); //This will work \o/
counter = counter+1;
}
}
}
On this method you should put your code to addViews.
As the behavior of all added textview must to be the same( i understood in that way), be removed when a user clicked on it, you can make your activity implements onClickListener and with that you just need to implement correctly the onClick method of your activity.
I thought making thing part of the app would be easy, however I was wrong. I wish to have a textView display whatever the user wrote in the editText. This is what I tried.
add.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
// TODO Auto-generated method stub
myTextView.setText(myEditText.getText().toString());
// of course I would use variables in place of the
// myTextView and myEditText
}
});
This is another way I tried to get this done.
add.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
// TODO Auto-generated method stub
//num1 is my String variable
num1 = myEditText.getText().toString();
myTextView.setText(num1);
}
});
Both times the textView comes up with nothing in it.
Thank you for any help!
onClickListener merely responds to user clicks. You need to implement a TextWatcher on your EditText. The most straightforward way of doing this is to implement TextWatcher in your class, then make a call to myEditText.addTextChangedListener(this).
I recommend adding something like the following to your onTextChanged method:
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
myTextView.setText(myTextView.getText()+s);//or something like this...
}
I usually use GetDlgItemText.
char Buffer[120];
GetDlgItemText(hwndDlg, (control), buffer, sizeof(buffer));
This will read it and store it in buffer.
In the EditText the getText call should you return the String, I don't believe you need to call the ToString method on it. The way you are using it in the onClickListener implies you have a button that should be calling a function to set the text into the textview. If you want it dynamically you should be able to use onTextChanged to fill in the data.
First of all check whether the control is coming to your setOnClickListener(). Put in a Log to find that out.
Next make sure that "add" is the button or item that u r using to initiate the copy process.
This statement of yours is correct.
myTextView.setText(myEditText.getText().toString());
Though you do not require the toString(). Doesnt really make a difference. I suggest you check that your textview and edittext is fine.
have you check the visibility of textview ?before clicking add button it is invisible rite?then u have to set the visibility on add button click.
From your code i understood that there is a button here too so try this should work:
public class Activity1 extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button btn = (Button)findViewById(R.id.mybutton);
btn.setOnClickListener(btncall);
}
private OnClickListener btncall = new OnClickListener()
{
public void onClick(View v)
{
TextView mytextView = (TextView) findViewById(R.id.MytextView);
EditText myeditText = (EditText) findViewById(R.id.MyeditText);
mytextView.setText(myeditText.getText().toString());
}
};
}
I have a LinearLayout comprising of a few Buttons and I add this to my activity in the onCreate(..) method with setContentView(R.layout.myscreen). No surprises so far.
How do I get a reference to an iterator to these buttons? I'd like to add listeners to them but I'd rather not directly reference the Button's using their android:id.
Similar questions have been asked here and here but they don't quite answer my question.
Try something like this provide an id root_layout in xml to LinearLayout
LinearLayout mLayout = (LinearLayout) findViewById(R.id.root_layout);
for(int i = 0; i < mLayout.getChildCount(); i++)
{
Button mButton = (Button) mLayout.getChildAt(i);
mButton.setOnClickListener(this);
}
Where mLayout is object of you Linear Layout and Your activity must implements OnClickListener and here goes general listener
#Override
public void onClick(View v)
{
Button mButton = (Button)v;
String buttonText = mButton.getText().toString();
}
NOTE: For this to work properly you Linear Layout must only contains button no other views
You should take a look at my answer here.
In short. I'd assign the buttons a listener by setting the onClick attribute in the XML layout on each Button.
Inside of your Activity you'll need a public method like the one below which basically is what you want to do in your listener.
public void myFancyMethod(View v) {
// do something interesting here
}
If you want to go for accessing other elements you may try following syntax:
<ElementClass> <referencevariable> = (<ElementClass>) findViewById(R.id.<id_of_the_element>);
For Example:
TextView textView= (TextView) findViewById(R.id.t1); //I used t1 to refer my textview in the Layout.
This might work.
Then you can use these views with their inbuilt methods to perform as many as work you want.