I have the following problem:
I want to use the text of a TextView which is changing every 10 seconds. With a Button, I want to use the text of the TextView and show it in another TextView.
I tried it with the following code.
buttonSave.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
buttonSave.setClickable(false);
buttonSave.setVisibility(View.GONE);
if(buttonSave.isEnabled()) {
String copy = textView.getText().toString();
settingsview.append("\n" + copy);
}
buttonSave.setEnabled(false);
}
My problem now is that it works fine until the TextView refreshes and then the TextView is clearing everything without pressing the button.
This is the code which changes my textfild. The toCheck StringBuilder is a input of OCR which is refreshing every 10secs
public void filterit(StringBuilder toCheck){
Pattern patternDate = Pattern.compile("\\d{2}\\.\\d{2}\\.\\d{4}");
Matcher matcher = patternDate.matcher(toCheck.toString());
while (matcher.find()) {
//textView.setText(matcher.group());
settingsview.setText(matcher.group());
}
}
try this way
buttonSave.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
if(buttonSave.isEnabled()) {
String copy = textView.getText().toString();
ettingsview.setText(settingsview.getText().toString()+"\n" + copy);
buttonSave.setClickable(false);
buttonSave.setVisibility(View.GONE);
}
}
Related
I am trying to create a repeating text app. So I use a for loop for repeating the text and display this text in a textview.
When I press a button then I want it to generate the text as many times as the loop runs.
Here is my code:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
enterText = findViewById(R.id.editText);
repeatText = findViewById(R.id.repeatTime);
genTxt = findViewById(R.id.genText);
genrate = findViewById(R.id.generate);
reset = findViewById(R.id.reset);
copy = findViewById(R.id.copyButton);
share = findViewById(R.id.shareButton);
genrate.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//Storing text in Gen Text Area
String txt = enterText.getText().toString().trim();
//Storing Repeat value
String repeats = repeatText.getText().toString().trim();
int repealVal = Integer.parseInt(repeats);
for(int i=1;i<=repealVal;i++){
genTxt.setText(txt);
Log.d("tets","loop "+i+txt);
}
}
});
}
public void reset(View view){
enterText.setText("");
repeatText.setText("");
genTxt.setText("");
}
When I run it I only get the text one time in my textview.
Try changing your onClick method to the following:
genrate.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//Storing text in Gen Text Area
String txt = enterText.getText().toString().trim();
//Storing Repeat value
String repeats = repeatText.getText().toString().trim();
int repealVal = Integer.parseInt(repeats);
for(int i=1;i<=repealVal;i++){
genTxt.setText(genTxt.getText() + txt);
Log.d("tets","loop "+i+txt);
}
}
});
Note that inside the loop you are only switching the text, not adding to the text.
To even further optimize solution, you should consider using .append() instead of .setText()
Here you are setting text to same TextView again and again.
If you want to dynamically generate multiple TextViews you can try below solution.
Give an id to your root layout in xml where you want to add text. Here I am using LinearLayout. Add it in your code as below:
LinearLayout linearLayout = findViewById(R.id.ll) //ll is the id of LinearLayout
Then add this in your onclick
TextView txtView;
for(int i = 1; i <= repealVal; i++) {
txtView = new TextView(MainActivity.this);
txtView.setText(txt);
linearLayout.addView(txtView);
}
i am new to development. i am creating an android calculator app with advanced functionality.The thing is i am using text view for taking and displaying inputs/outputs. My question is, how can i take Multiple inputs in multiple Textviews.
For example i have 3 text views,when user will enter 1st input in first textview(by default) and when user press the specific button it moves automatically to next textview . In some cases i want to take 2 inputs and in some cases i want to take 3 ,
How can i achieve this
Note: I dont want to use edit text , coz all buttons of already available in my app.Using Edit text will make softkeyboard to appear, and then for hiding the softkeyboard, i need to use hiding code lines in every class
You can do something like following:
private TextView[] textViews;
private TextView tvCurrentEditing;
private Button btnNext;
private Button btnPrev;
private Button btnSetText;
private int index = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
textViews = new TextView[3];
//Initialize all your textviews like textViews[0] = findViewById(<textview-id1>);
//textViews[1] = findViewById(<textview-id2>);
//textViews[2] = findViewById(<textview-id3>);
tvCurrentEditing = textViews[index];// I am assuming this is your first
//initialzie btnSettext
btnSettext.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
tvCurrentEditing.setText("<what ever you want");
}
});
//initialize next buton
btnNext.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(index < textViews.length) {
index++;
}
tvCurrentEditing = textViews[index];
}
});
//Initialize previous button
btnPrev.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(index > 0) {
index--;
}
tvCurrentEditing = textViews[index];
}
});
}
The names of the views could be different. The point is always use tvCurrentEditing whenever you want to change data of TextView. And update tvCurrentEditing whenever needed.
I want to update the text in a clickable button after I click it.
I have a button for liking a product, its text shows how many likes the product has.
<Button
android:id="#+id/like"
android:text= "#string/like"
android:drawableTop="#drawable/like"/>
<string name="like">Likes: </string>
So in my onClick() method for this button I use like.setText("Likes: " + product.getLikes()); in order to update the text:
Button like = (Button) v.findViewById(R.id.like);
like.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
like(product);
like.setText("Likes: " + product.getLikes());
}
});
The like() method does work - I checked that. The method product.getLikes() returns the correct value.
However the button stays the same. I canĀ“t figure out what the issue is.
UPDATE:
I noticed the text updates itself once i scroll down to other items and up again....
Try this.
final Button btn = (Button)findViewById(R.id.btn);
final Handler handler = new Handler(){
#Override
public void handleMessage(Message msg) {
btn.setText("new text");
}
};
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
handler.sendEmptyMessage(0);
}
});
Try this:
String mlikes= product.getLikes().toString();
like.setText("Likes: " + mlikes);`
I am new to programming in Java, i've managed to create a little calculator as a little test app.
But i think i am using way to much code for my needs.
So i've given a Button a name: buttonname
Now to change it's text when clicked i need to:
public class MyActivity extends Activity {
Button buttonname;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
buttomname = (Buttom) findViewById(R.id.buttomname);
}
public void buttonnameOnClick(View v) {
button1.setText ("NewText")
}
}
(i've bolted everything i had to add)
So i had to do everything above + connect the buttonClick through the xml file.
So i was wondering if there is a easier way to define all objects so i dont have to do: Button buttonname; and buttomname = (Buttom) findViewById(R.id.buttomname); all the time.
And i was wondering if there is a easier way to auto create button events.
(I am used to Visual Studio, but now i am kinda lost in Android Studio. So on Visual Studio i just had to double click the button and type: buttonname.Text = "NewText";)
There is a library called Butter Knife to do approximately that
However, I'm not sure if you really need it.
Oh, and you don't have to find the same Button every time. You find it once in onCreate and store in a field.
First of all you have typo in
buttomname = (Buttom) findViewById(R.id.buttomname);
It should be
buttomname = (Button) findViewById(R.id.buttomname);
and you forgot ; in one line "didn't your IDE show error to you!!" and also small correction in
public void buttonnameOnClick(View v) {
button1.setText ("NewText")
}
it should be
buttomname.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
buttomname.setText ("NewText");
}
});
inside protected void onCreate.
2nd method:
And if you have define android:onclick="buttonnameOnClick" in XML then
public void buttonnameOnClick(View v) {
button1.setText ("NewText")
}
To be corrected to
public void buttonnameOnClick(View v) {
buttomname.setText ("NewText");
}
You can do it in a loop if you have a lot of identical buttons to process
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
for (int btn_id : new int[]{
R.id.buttomname
, R.id.buttomname2
, R.id.buttomname3
}) {
View v = view.findViewById(btn_id);
if (v != null) {
v.setOnClickListener(onClickButton);
}
}
}
//
private View.OnClickListener onClickButton = new View.OnClickListener() {
#Override
public void onClick(View view) {
// .. handle click
if (view.getId()==R.id.buttomname2){
}
}
Your code is partly correct,
however the
(Buttom) is wrong change it to (Button)
the other thing
public void buttonnameOnClick(View v) {
button1.setText ("NewText")
}
can just be changed to:
public void buttonnameOnClick(View v) {
Button buttonTemp = (Button)v;
buttonTemp.setText ("NewText");
}
Assuming you are calling the method from layout xml file.
you must use the onClickListener() method for Button object.
Your code like this structure;
buttonname = (Button)findViewById(R.id.buttonname);
buttonname.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
}
});
I recommend to your visit Button | Android Dev page for Button.
I am using an Adapter to generate buttons in a GridView.
Using an OnClickListener, whenever I click a button from the GridView, it is possible to get the String of the button in a variable. However, I want to click a button, store its text in a variable, then click a different button, store its text in another variable so later I can compare the two texts. How could I do that?
gI'm using OnClickListener, therefore my previous implementation for one button was the following:
public void onClick(View v) {
Button b = (Button)v;
String text = b.getText.toString();
With this approach, I can only get and store text from first button clicked, unless I store all the values in an array.
Ok so from what I can understand, you want to compare the strings of the two items in a grid view. If this is what you want, heres how I would do it(just a psuedocode):
String mValue1, mValue2;
boolean isOneClicked = false;
onItemClickedListener(item, position){
if(!isOneClicked){
mValue1 = item.stringValue;
isOneClicked = true;
}
else{
mValue2 = item.stringValue;
//do whatever you want here
//Reset when done
mValue1 = "";
mValue2 = "";
isOneClicked = false;
}
}
Please you can share your code to turn the things more comprehensible? Edit your question please... Anyways:
MainActivity.java
public class MainActivity extends Activity {
private Button button1;
private Button button2;
private String butString, butString2;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
button1 = (Button) findViewById(R.id.button1);
button2 = (Button) findViewById(R.id.button2);
button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
butString = (String) button1.getText();
}
});
button2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
butString2 = (String) button1.getText();
}
});
}
}
After that you can compare the two strings.