2

I have two activities; the first activity starts the second one with some data passed through the intent.

Intent i = new Intent(this,BActivity.class);
i.putExtra("identify", "c2f");
startActivityForResult(i, 1);

In the second activity, I want to make some TextViews/EditTexts visible (which are initially set to invisible) based on the information passed from the first activity.

Here's the code for that:

tv1 = (TextView)findViewById(R.id.textView2);
tv2 = (TextView)findViewById(R.id.textView3);
et1 = (EditText)findViewById(R.id.editText1);
et2 = (EditText)findViewById(R.id.editText2);
button = (Button)findViewById(R.id.send_result);

Bundle extras = getIntent().getExtras();
String identifier = extras.getString("identify");

if(identifier == "c2f")
{
    tv1.setVisibility(0);
    tv1.setText("Celcius");
    et1.setVisibility(0);       
}

else if(identifier == "f2c")
{
    tv1.setVisibility(0);
    tv1.setText("Fahrenheit");
    et1.setVisibility(0);
}

else if(identifier == "currency")
{
    tv1.setVisibility(0);
    tv1.setText("Amount");
    tv2.setVisibility(0);
    tv2.setText("Conv. Rate");
    et1.setVisibility(0);
    et2.setVisibility(0);
}

Now when the second activity starts, none of these TextViews or EditTexts seem to get visible!
identifier (string) holds the correct value passed from first activity and it even goes into the if conditions, but it doesn't make any view visible.
Am I making any mistake in trying to make these views visible?

Ben Pearson
  • 7,532
  • 4
  • 30
  • 50
Abdul Jabbar
  • 2,573
  • 5
  • 23
  • 43

3 Answers3

5

Use .equals instead of == to string comparison. You can also use the variable after the quoted string to avoid nullpointer. And you can use TextView.VISIBLE, it's a constant to get it visible.

    if("c2f".equals(identifier))
    {
        tv1.setVisibility(TextView.VISIBLE);
        tv1.setText("Celcius");
        et1.setVisibility(TextView.VISIBLE);       
    }
Giacomoni
  • 1,468
  • 13
  • 18
  • Waow... it worked :S can you please explain why '==' didn't work there? – Abdul Jabbar Feb 11 '14 at 17:53
  • TextView.VISIBLE has constant value = 0 so that should't be problem. Although it's correct to use named constants instead of magic numbers. Comparing string objects with == is a problem here. – Michal Feb 11 '14 at 17:56
  • 2
    You can take a look at that post about string comparison: http://stackoverflow.com/questions/767372/java-string-equals-versus – Giacomoni Feb 11 '14 at 17:58
  • == does not work because in Java strings are immutable objects, not arrays of characters as in other languages. To compare two objects you need to use the equals method. When you're dealing with chars the == sign works fine. – Rarw Feb 11 '14 at 18:02
2

Simply use the View's constants for this.

your_view.setVisibility(View.VISIBLE);

This will make your View visible.

your_view.setVisibility(View.INVISIBLE);

This will make it invisible but still with the layout visible (basically, the space where it goes remains untouched)

your_view.setVisibility(View.GONE);

This will make your View disappear, like it never existed!

nKn
  • 13,691
  • 9
  • 45
  • 62
2

As pointed by giacomoni, please use equals for String comparison. Here is a link to explain why. http://javarevisited.blogspot.in/2012/03/how-to-compare-two-string-in-java.html

Also, try using the standard View.VISIBLE etc constants for showing and hiding views. They are much more easy to use and understand. Happy coding. :)

Atul O Holic
  • 6,692
  • 4
  • 39
  • 74