8

I am looking to make a TextView invisible until the button is pressed. Specifically they are the answers to the question and only visible once the button below it is pressed.

What I have so far >>

public class AndroidAssignment2_1 extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_android_assignment2_1);

        Button next = (Button) findViewById(R.id.QButton);
        next.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                Intent intent = new Intent();
                setResult(RESULT_OK, intent);
                finish();

            }
        }); 
            Button next1 = (Button) findViewById(R.id.QButton);
            next1.setOnClickListener(new View.OnClickListener() {
                public void onClick(View view) {
                    Intent myIntent = new Intent(view.getContext(), AndroidAssignment2_2.class);
                    startActivityForResult(myIntent, 0);
                }
            }); 


    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.android_assignment2_1, menu);
        return true;
    }

}

The xml for this class

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

<TextView android:id="@+id/Questions"
    android:layout_weight="1"
    android:layout_width="wrap_content"
    android:layout_height="0dip"
    android:text="@string/Q2"   />

<Button android:id="@+id/QButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/button_question"  />

<Button android:id="@+id/AButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/button_send" />

<TextView android:id="@+id/Answers"
    android:layout_weight="1"
    android:layout_width="wrap_content"
    android:layout_height="0dip"
    android:hint="@string/A2" />

<Button android:id="@+id/QuitButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/button_quit" />


</LinearLayout>

I dont think more is needed, I am just looking for the code that I would apply to "AButton" to make the TextView "Answers" only visible once clicked by user.

hasan
  • 23,815
  • 10
  • 63
  • 101
Learning2Code
  • 191
  • 1
  • 3
  • 13

3 Answers3

16

XML:

<TextView android:id="@+id/Answers"
    android:layout_weight="1"
    android:layout_width="wrap_content"
    android:layout_height="0dip"
    android:hint="@string/A2"
    android:visibility="invisible"/>

Code:

Button button = (Button) findViewById(R.id.AButton);
button.setOnClickListener(new View.OnClickListener() {
    public void onClick(View view) {
        TextView tv = AndroidAssignment2_1.this.findViewById(R.id.Answers);
        tv.setVisibility(View.VISIBLE);
    }
}); 
hasan
  • 23,815
  • 10
  • 63
  • 101
  • What can I do for it getting invisible again when pressing OK in the TextView? – Marcos Sartorato Jun 09 '16 at 17:39
  • what do you mean pressing OK in the TextView? can you explain more? do you mean pressing the textView itself or pressing the same button or another button? – hasan Jun 09 '16 at 19:23
  • > I press the button >Text view appears > I finish typing my text > I press OK -- What I want is that when I press OK the text view disappears again! Understood now? – Marcos Sartorato Jun 09 '16 at 19:27
  • is the ok button is another button? what I mean is it the same button that let the textview appear in the first place or another button? – hasan Jun 09 '16 at 19:30
  • You press the OK in the keyboard to send your text. – Marcos Sartorato Jun 09 '16 at 19:31
  • Hey, but this way it only disappears when I press the back button – Marcos Sartorato Jun 09 '16 at 19:47
  • you need to do some changes. google it man. thats how you learn :) you can't always ask for the exact answer! programming doesn't work this way – hasan Jun 09 '16 at 22:49
  • I apologise you are right that was a wrong link. maybe this one helps more: http://stackoverflow.com/questions/5077425/android-detect-done-key-press-for-onscreen-keyboard – hasan Jun 09 '16 at 22:52
3

For this thing to work, first we need to make a TextView, write our message in it and make the TextView invisible using visibility property if Textview like this :

<TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world"
        **android:visibility="invisible"**/>

and then make a method in your main activity class to show invisbile test view in our Layout using setVisibility() method like this:

  public void onLoveButtonClicked(View view){
        TextView textView = (TextView) findViewById(R.id.haikuTextView);
        textView.setVisibility(View.VISIBLE);

  }
Taryn
  • 242,637
  • 56
  • 362
  • 405
Mayank Tripathi
  • 285
  • 4
  • 11
0

Set the Answers TextView visibility to invisible or gone by default.

Find the AButton Button in the same way you find the other Button views.

Set an onClickListener on the AButton Button like the other buttons' listeners that does two things:

Find the answers TextView in the same way you find the Button views.

Calls setVisibility on the the answers TextView.

blahdiblah
  • 33,069
  • 21
  • 98
  • 152
  • do I place the setVisibility under layout or call it in the onCreate method? – Learning2Code Oct 30 '13 at 22:33
  • 1
    @Learning2Code The usual way would be to set the visibility to "`gone`" in the layout with `android:visibility` and then use `setVisibility` in the `onClickHandler` to make it visible. – blahdiblah Oct 30 '13 at 22:37