I've been trying to search for solution for this but ain't lucky enough to find a correct answer.
So my problem is.. I want to remove the first Line break in my EditText after clicking a certain button.
First Line <- This should be removed after I click the button and the rest will remain.
Second Line
Third Line
Fourth Line
EditText txt = findViewById(R.id.editTextID);
Button btn = findViewById(R.id.btnID);
btn.setOnClickListener(new Button.OnClickListener(){
public void onClick(View v) {
String firstLine = txt.getText().toString().substring(0, txt.getText().toString().indexOf("\n"));
String removeFirstLine = firstLine.substring(0, firstLine.length()-1);
txt.setText(removeFirstLine);
}
});
But this leaves only 1 line each time I click the button.
I believe that this will do what you want:-
btn.setOnClickListener(new Button.OnClickListener() {
#Override
public void onClick(View view) {
txt.setText(txt.getText().toString().substring(txt.getText().toString().indexOf("\n")+1));
}
});
Related
I'm extremely new to coding, so apologies if this question is trivial or the answer is easily found somewhere. I have searched, but I cannot find anything that helps.
Basically, I'm trying to code a simple, 2 button app in Android Studio.
Button1 is meant to simply display a series of commands to the user via text box.
Button2 merely resets.
My problem is, I would like Button1 to change what's displayed in the text view each time it is pressed, but I cannot figure out how to do so. I don't want to make 6 or 7 buttons.
Basically I would like it to run as follows;
Text = "Pick a number"
user presses Button1
Text = "Add 15" (This is as far as I've gotten)
user presses Button1
Text = "Multiply times 5"
user presses Button1 etc. etc. etc.
If anybody could please explain or usher me in the right direction, I would be greatly appreciative.
You can use button.setOnClickListener
public class MyActivity extends Activity {
EditText et;
Button button;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.content_layout_id);
et = (EditText)findViewById(R.id.edittext);
button = (Button)findViewById(R.id.button_id);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//Perform your Logic here.
et.setText("New text");
}
});
}
}
you can use a globle and a swithchcase
public class MyActivity extends Activity {
EditText et;
int CLICKS=0;
Button button;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.content_layout_id);
et = (EditText)findViewById(R.id.edittext);
button1 = (Button)findViewById(R.id.button1);
button1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
CLICKS++;
switch(clicks)
{
case 1:
et.setText("Pick a number");
break;
case 2:
et.setText("Add 15");
break;
case 3:
et.setText("Multiply times 5");
break;
}
}
});
}
}
I have got a two EditText in activity. I need after some event put text in current position of cursor. How can I do that?
et1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
et1.setText("myString");
}
});
et1 is your edit text, do it for the second EditText either.
First off this question has been asked multiple times, however, none of these questions have been answered to any extent. I have one example that works in the main activity class:
final Button button = (Button) findViewById(R.id.viewcatalog);
button.setFocusable(true);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
setContentView(R.layout.find_item);
}
});
But all of my other attempts to replicate this in sequential pages has resulted in failure. I know the reason that they won't work the same way is that my buttons are instantiated in other classes and not in the host class. What is the correct way to fix this error?
The method that doesn't work for reference:
public void OnClickSearch(View view) {
final Button button = (Button) findViewById(R.id.button2);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
EditText text = (EditText)findViewById(R.id.editText);
String value = text.getText().toString();
setContentView(R.layout.search_results);
}
});
}
It sounds like you are mis-understanding how the UI works in Android.
It is not normally expected that you will change an Activity's view on the fly as your are doing in your OnClickListener.
Instead, you should do one of two things. Either switch to a new Activity, using an Intent and the Activity's startActivity method, or use Fragments, and replace a Fragment in your Activity with a new Fragment.
The previous snippet of code has been written down by the aid of random sites and answers from StackOverflow, but somehow not working. I should add that I am an absolute beginner at making apps and my experience with Java is very limited as well.
The errors is the following:
"setOnClickListener": Marked red.
"public void onClick(View v) {": Here "v" is marked red, for some
reason. It continues being red in "String text = v.toString();".
The program also finds my semicolon redundant at the end of the
snippet.
I am using the beta of Android Studio on Elementary OS, using OpenJDK.
Button button_1 = (Button) findViewById(R.id.btn_1);
button_1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String text = v.toString();
displayPassword(text);
}
});
I could have made any number of mistakes, that's for sure. But any nudge in the right direction would be very appreciated.
My suggestion is:Set the onClick in the XML file, and create the method in the current class.
<Button
android:id="#+id/btn_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="click" />
public void click(View arg0) {
}
Agree with Graph that you should have to #Override the onClick method. Not sure what's wrong with it there. In fact, when I typed your example into Android Studio, I got 3 letters into OnClickListener and it automatically filled in the rest, including the #Override.
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String text = v.toString();
// do something with the text.
}
});
Also, I don't think v.toString() is going to get you any useful information. If you want the text off the button, you're going to want to cast it to a button then call getText():
Button button = (Button) v;
String text = button.getText().toString();
or, you could do:
String text = ((Button)v).getText().toString();
Simply calling v.toString() is going to get you a description of that button, not the text on it.
I believe you need to #Override the onClick method.
Button button_1 = (Button) findViewById(R.id.btn_1);
button_1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String text = v.toString();
displayPassword(text);
}
});
Just press " ctrl+shift+o " and add 1 library which you shows on screen prob solved
I am splitting a string and putting the result in edittexts using a loop so that the user can edit the data.thereafter he can save all the data in
the edittexts by just pressing the final button.Problem is i don't know how to get each value from the edittext when he pressses the save button.this is my code:
EditText etstringone,etstringtwo;
Button btn_save;
btnsave=new Button(this);
String mystring="somevalue";
String del="\\|";
String[] splitResult = mystring.split(del);
for (String e : splitResult)
{
etstringone=new EditText(this);
etstringtwo=new EditText(this);
etstringone.setText(splitResult[0]);
etstringtwo.setText(splitResult[1]);
mylayout.addView(etstringone);
mylayout.addView(etstringtwo);
}
mylayout.addView(btnsave);
btnsave.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// how to get each value from the edittexts and output each to logcat,,i'll do the saving and the rest
}
});
how do i go about this?thanks.
NB:emphasis on using a single button to get all the data,i managed to do a scenario for apppending a new save button each time the
loop runs but its not neat.
The edit texts are dynamically created, but you can still access the object. Either cache a reference to the edit texts or walk the child-views of your "mylayout".
EDIT
From your code, you declare
EditText etstringone,etstringtwo;
You instantiate them
etstringone=new EditText(this);
etstringtwo=new EditText(this);
Now I don't know the scope of this (are they local vars, class/member vars?) if class vars, you can reference them in the
btnsave.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// how to get each value from the edittexts and output each to logcat,,i'll do the saving and the rest
}
});
body. eg
btnsave.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
String e1 = etstringone..getText().toString();
}
});