Android Studio If Statement Problems [duplicate] - android

This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 6 years ago.
Click Event
public void buttonOnClick(View v) {
Button button=(Button) v;
((Button) v).setText("clicked");
Changes BackGround to an image
RelativeLayout rl = (RelativeLayout) findViewById(R.id.BackGround);
rl.setBackgroundResource(R.drawable.mapwork);
Makes Certain Items Disapear
button.setVisibility(View.INVISIBLE);
EditText mText2 = (EditText) findViewById(R.id.input);
mText2.setVisibility(View.INVISIBLE);
Changes what is in the output if they put in a certain number
EditText mText = (EditText) findViewById(R.id.output);
if("8" == mText.getText().toString()){
mText.setText("That does not work!");
}
}
When I test the application the If statement never happens even when the requirements are met. I have tried look for an answer and have not found one. Thank you for helping.

Now it will work
if(mText.getText().toString().equals("8")){
mText.setText("That does not work!");}

Related

how to get the text from button and use it with if() [duplicate]

This question already has answers here:
How do I compare strings in Java?
(23 answers)
Get text from pressed button
(8 answers)
Closed 5 years ago.
It must display "Correct" when the button text = "de" but when I click the button it says wrong but my button text is "de". Why (if) statement does not work despite of the button text is "de"?
public void bot1(View v) {
Button choice1 = (Button) findViewById(R.id.button1);
if(choice1.getText() == "de")
Toast.makeText(this, "Correct", Toast.LENGTH_SHORT).show();
else
Toast.makeText(this, "Wrong", Toast.LENGTH_SHORT).show();
}
You have to use equals, not ==
public void bot1(View v){
Button choice1 = (Button) findViewById(R.id.button1);
if(choice1.getText().toString().equals("de"))
Toast.makeText(this,"Correct",Toast.LENGTH_SHORT).show();
else
Toast.makeText(this,"Wrong",Toast.LENGTH_SHORT).show();
}

How to detect links in programmatically created TextView [duplicate]

This question already has answers here:
Android active link of url in TextView
(10 answers)
Closed 6 years ago.
I've programmatically created TextView and then set data in it. But I'm not able to detect the links inside the data.
This is the code:
TextView dataView = new TextView(this);
dataView.setLayoutParams(dataParams);
dataView.setText("www.google.com");
I've tried with the : dataView.setMovementMethod(LinkMovementMethod.getInstance());
and
dataView.setLinksClickable(true);
but it doesn't works for me.
It will be great if anyone can help me here.
Thanks in advance.
Try this
Linkify.addLinks(dataView, Linkify.WEB_URLS);
dataView.setLinksClickable(true);
Try This:
dataView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
TextView tv = (TextView)v;
String link = tv.getText().toString();
}
};);
TextView dataView = new TextView(this);
dataView.setLayoutParams(dataParams);
dataView.setText( Html.fromHtml("<b><a style='text-color:white;' href='"+"http://www.google.com"+"'>Google</a></b>"));
dataView. setMovementMethod(LinkMovementMethod.getInstance());

Button to hide a TextView

I've been searching for a solution for this for a while but cannot seem to get one working. There are one or two on here about this subject but I can't seem to get them going. I'm also a novice in Android and while I've been on and off playing with it for a few years, I still understand next to nothing about what I'm writing.
Basically I've got a TextView and a button. Ideally I'd like to put some text in the TextView, press a button it's gone, press the button again and it's back.
I've narrowed it down to needing to understand what findViewById(R.id.button2) does but honestly I'm a bit lost.
I've added my button code but apologies that this is such a noob question
public void onClick(Button v){
TextView t1 = (TextView)findViewById(R.id.editText);
v.setVisibility(View.GONE);
Button button = (Button) findViewById(R.id.button2);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
TextView t1 = (TextView)findViewById(R.id.TextView);
v.setVisibility(View.GONE);
}
});
}
Your code has a couple of issues. I'm not going to give you the code because that won't really help you learn. Instead I'll explain things and let you try to figure it out or come back with more explicit questions.
You know that xml file you set using setContentView? Some of the tags in it had a property android:id="xxxx". That xxxx is the id of that view, its used so you can find that view in your code. The findViewById function walks through all the views on screen and finds a view with that id and returns it. That gives you a reference to the view so you can change it. For example, you can set its visibility, set its background color, or set an OnClickListener.
So to have a button toggle the visibility of another view, you need to be able to do the following things:
1)Find the view who's visibility you want to change
2)Figure out what its visibility currently is
3)Figure out what you want it to be (the opposite of what it currently is
4)Set that visibility
You need to write a function that does all that. Then you need to do this
1)Find the button you want to use to change the visibility
2)Tell it to call your function when its pressed.
Figure out how to do each of those steps individually, and you should be able to put it together. Good luck.
findViewById(R.id.button2) finds the view with the id button2.
You can check inside onClick whether t1 is visible or not (t1.setVisibility(View.GONE); not v.setVisibility(View.GONE);), and toggle between View.GONE and View.VISIBLE.
Remember that your findViewById() should have a real id. They are normally set on the activity_name.xml.
You are using a onClick inside a onClick. Personally I recommend setting the listener manually with setOnClickListener.
There's a lot of work for you, start with these tutorials. Keep trying and try to understand what you are doing.
Look like you need a toogle button feature, here is a piece of code.
Important: you must pay heed to #GabeSechan and #SkyDriver2500 answers.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.your_layout);
//your other code
Button button = (Button) findViewById(R.id.button2);
final TextView t1 = (TextView) findViewById(R.id.editText);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
t1.setVisibility(t1.getVisibility() == View.VISIBLE ? View.GONE : View.VISIBLE);
}
});
}
I'm not sure if the code will help you now. But just in case, here it is
final boolean[] isTvVisible = {false};
final TextView t1 = (TextView)findViewById(R.id.editText);
t1.setVisibility(View.GONE);
Button button = (Button) findViewById(R.id.button2);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (isTvVisible[0]) {
t1.setVisibility(View.GONE);
isTvVisible[0] = false;
} else {
t1.setVisibility(View.VISIBLE);
isTvVisible[0] = true;
}
}
});

How can I find out which view currently has focus? [duplicate]

This question already has answers here:
How to find out which view is focused?
(6 answers)
Closed 6 years ago.
My app uses keyboard control and when moving around I've noticed that occasionally you get what I'll refer to as a "Mystery View" - I say that because nothing visible seems to be selected, yet nothing visible is focussed either
Is there any way to find out what I'm focussed on at any given time? eg. a listener that fires on a view selection, etc.
Thanks
** edit - some code for ntc **
LinearLayout col1 = (LinearLayout)findViewById(R.id.col1);
Button btn = new Button(this);
btn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
LinearLayout col1 = (LinearLayout)findViewById(R.id.col1);
if(col1.getFocusedChild() != null) {
Log.d(TAG, col1.getFocusedChild().toString());
}
}
});
col1.addChild(btn);
getWindow().getCurrentFocus();
You can probably use this method for every view. Maybe this may gets bigger and hectic but is sure to work:
if(yourChildView.isFocused()) {
yourChildView.setFocusable(false);
}

How to get the text from the Button when clicked? [duplicate]

This question already has answers here:
Get text from pressed button
(8 answers)
Closed 9 years ago.
for( i=0; i<26; i++) {
btnAlpha[i] = new Button(this);
btnAlpha[i].setBackgroundColor(Color.TRANSPARENT);
btnAlpha[i].setTextColor(Color.GREEN);
btnAlpha[i].setText(Character.toString ((char)(j+i)));
btnAlpha[i].setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
//Want to get the text from the current button.
btnAlpha[i].getText();
//But it gives error that "i" cannot be accessed.
}});
I get the error "cannot refer to a non-final variable inside an inner class defined in a different method". But I need to get the text at that right time. How to do it? Is there any other way to do it?
Button is a subclass of View, so the argument to onClick, v, is the Button being clicked. Try
public void onClick(View v) {
((Button) v).getText();
}
create a new variable final int x = i; in the for loop and use x in place of i in the onClick method
edit: actually im not sure this will work correctly. oops. Kype P's answer looks pretty good.

Categories

Resources