I am trying to create a simple android app using JAVA(Android Studio). What I am willing to implement is the background color of the app to change based on the content of the TextView (which is updated constantly between two values "Open" and "Closed"). I have seen that onChanged() must be used but I don`t really understand how to Implement the listener for the TextView.
I tried wrote this :
measurementValue = (TextView) findViewById(R.id.textbox_main); measurementValue.addTextChangedListener(new TextWatcher() {}
based on what I have found online, but can`t really figure out what the next step is. I am a total newbie so please be kind.
Best regards!
You can listen for text change on those 3 callbacks. It depends on your use case.
#Override
public void onTextChanged(CharSequence textInput, int start, int before, int count) {
String strInput = textInput.toString();
if ("open".equalsIgnoreCase(strInput)) {
// text is "open"
// change background to desired color
} else if ("closed".equalsIgnoreCase(strInput)) {
// text is "closed"
// change background to desired color
}
}
Related
Am new to javafx. I wanted to write a simple javafx program that when clicking a button for the first time, the color of a rectangle should change to something else, e.g green, and when clicking it for the second time, the color should change back to the original color.
But the button is only allowing a single click only?
Help if there is any solution on this.
Here is a code.
You can try using simple boolean value to check if color has been changed before:
boolean flag = false;
#FXML
private void onButtonClick(){
if (flag){
//change to e.g. red
} else {
//change to e.g. green
}
flag = !flag;
}
This code is simple and if you want only to change between two colors it will be enough :)
I my app I am fetch some text data from the server and showing this text data in the TextView. Here is something works fine. I am add an little arrow ImageView right to the TextView and this TextView is expandable so when TextView is more then 2 lines and if anyone click this TextView it expand and again click to shrink and I am also add an little arrow image right to the TextView (so user understant that it is an expandable text), here is everything is fine all code are works perfectly but now I want to remove this litter arrow image when the TextView is under 2 lines and when TextView is more then 2 lines it show. I want to tell you one more thing that I am also add a rotation in the arrow image so when the user click the text the little arrow image rotate the 180 degree and also text is expand and when user click the text second time arrow image again rotate to his previous position and text is shrink in 2 lines.
I want to remove this little arrow when the text is under 2 lies I do not want to remove the arrow image when text line more then 2, I'm guessing you understand.
I am new to the Java Code and I am learning is language so now I want to learn how to do this implementation in my app, I have add my code below so that you can understand batter.
textViewMyVideoTitle.setText(videoLists.get(0).getVideo_title());
my_title_layout_expand.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (isTextViewClicked) {
//This will shrink textview to 2 lines if it is expanded.
textViewMyVideoTitle.setText(videoLists.get(0).getVideo_title());
myTitleImageView.setRotation(imageView.getRotation() + 0);
myTitleImageView.setVisibility(View.VISIBLE);
textViewMyVideoTitle.setMaxLines(2);
isTextViewClicked = false;
} else {
//This will expand the textview if it is of 2 lines
textViewMyVideoTitle.setText(videoLists.get(0).getVideo_title());
myTitleImageView.setRotation(imageView.getRotation() - 180);
myTitleImageView.setVisibility(View.VISIBLE);
textViewMyVideoTitle.setMaxLines(Integer.MAX_VALUE);
isTextViewClicked = true;
}
}
});
So anybody can help me to achieve this code
As you say, your text doesnt have more than 2 lines, so your function wont work until the text has more than 2 lines.
You may try to use TextView's getLineCount() method to get this info and decide.
So I mean outside your onClickListener do something like this:
if (textViewMyVideoTitle.getLineCount() <= 2) {
my_title_layout_expand.setVisibility(View.VISIBLE);
} else {
my_title_layout_expand.setVisibility(View.GONE);
}
Or since it gives you the right number only after layout been 'rendered' you might need the following:
textViewMyVideoTitle.post(new Runnable() {
#Override
public void run() {
// Get the line count and put the if-else statement here
}});
So I have an "EditText" in my Layout for simple Textinput.
When I write a hashtag, for example "#hello", I want that hashtag to get highlighted in blue
editText.setOnTextChangedListener(...) {
public void afterTextChanged(...)
// replace current String with editText.setText(Html.parse(...));
}
I've already tried some ways to achieve that - SpannableStrings and HTML-based setTexts in my afterTextChangedListener - the only problem I got here was, my cursor jumped to the first position when I tried to type something - so a real-time highlighting doesn't work like that in Android Java I guess - not with my algorithm, at least.
Any suggestions?
As you already know how to color i will just suggest how you could do the trick changing the String keeping the cursor at position using the method that sunil sunny suggested.
editText.setOnTextChangedListener(...) {
public void afterTextChanged(...)
int currentSelectionStart = editText.getSelectionStart()
int currentSelectionEnd = editText.getSelectionEnd()
// replace current String with Spannable for every "Hashtag"
editText.setSelection(currentCursorPosition,currentSelectionEnd )
}
I'm programming Android for the first time and I'm having some difficulties. The idea is to make a guessing game app in which the user takes a number in his/her head and the app tries to guess it. The user will give hints like higher and lower to the app. For some reason the app crashes after I press the start button. Because of this, I know that there is an error in the onClick method but since it shuts down immediately after I press the start button, I can't use something like a println to debug.
So actually I have 2 questions:
Where does my reasoning fail? (or show me how to figure out my mistakes)
How can I debug things like this?
The start, higher and lower are all buttons in the program.
#Override
public void onClick(View arg0) {
int min = 0;
int max = 100;
Random random = new Random(100);
int answer = 0;
if (arg0 == start) {
answer = random.nextInt(100);
buttonTextView.setText(answer);
}
else if (arg0 == higher){
min = answer;
answer = random.nextInt((max - min) + min);
buttonTextView.setText(answer);
}
else if (arg0 == lower) {
max = answer;
answer = random.nextInt((max-1) - min);
buttonTextView.setText(answer);
}
}
where does my reasoning fail?
You are using the wrong setText() method. In the TextView Docs you will see that there is one which takes an int, this is for retrieving a String resource that you have in your strings.xml so you would pass it a resource id. So your setText() is looking for a resource with the id of whatever your answer variable is. You will want to convert this to a String with something like
buttonTextView.setText(String.valueof(answer));
or one of several different ways.
How can I debug things like this?
When your app crashes there will be an exception in your logcat. This answer can help you to read your logcat. To open your logcat window in Eclipse, if it isn't already, you can do this
Window --> Show View --> Other --> Android --> LogCat
A couple side notes
You should change your params like in onClick() to something meaningful so I would change
public void onClick(View arg0)
to something like
public void onClick(View v) // v for view, could also be view, btn
// whatever makes sense to you and others who may read it
You also should compare the id of your View clicked instead of the View itself. So you would compare it with something like the following (assuming you changed arg0 to v)
if (v.getId() == R.id.start) // Assuming start is the id in your xml of your Button
// this will also allow you to use a switch statement
Your variables in onClick() (min, max, and answer) should be initialized outside of onClick() or they will be reset to the default values with each click which I'm pretty sure you don't want (thanks to 323go for pointing that out).
This is for my Android App. I have two EditText fields set to a maximum of one character that can be input in each field. Once the user inputs the first character I would like the focus to automatically jump to the second EditText field. I set up an addTextChangedListener on the first Edit Text and had it listen for when the text string is > 1. Then I call request focus on the second Edit Text. However, I keep receiving a force close when I input the character in the first Edit Text box. From what I've looked on StackOverflow, this should work. Anyone have any idea why it is not? I posted the relevant code below. Thanks.
LinearLayout llview = new LinearLayout(this);
llview.setOrientation(LinearLayout.VERTICAL);
EditText character1 = new EditText(this);
EditText character2 = new EditText(this);
character2.setId(2);
character1.addTextChangedListener(new TextWatcher(){
EditText character2 = (EditText) findViewById(2);
#Override
public void afterTextChanged(Editable s) {
if(s.length()>0)
{
character2.requestFocus();
}
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count,int after){}
#Override
public void onTextChanged(CharSequence s, int start, int before,int count) {}
});
llview.addView(character1);
llview.addView(character2);
this.setContentView(llview);
I'm not sure that you've posted all of the relevant code. It would seem that you are building your view hierarchy programmatically (rather than using a layout file), yet there is no code showing where you insert character1 and character2 into the view hierarchy. It would be helpful if you posted the code for building your view hierarchy.
Also, what debugging steps have you attempted so far? Have you set breakpoints to confirm that your variables are not null pointers when you invoke methods on them? Have you done a stack trace?
If I was to guess just based on this code segment you posted, then I would have to guess that you haven't inserted character1 and character2 into your activity's view hierarchy and findViewById(2) is simply returning null. Have you checked to see that findViewById(2) is in fact returning something other than null?