3

I am a beginner in android.

My Scenario

I have a screen A which has 2 buttons Button A Button B.

When I open my application screen A opens up with the above 2 buttons, when I click on Button B a textview and Edittext is displayed.

What I want ?

When the back button is pressed the textview and edittext should hide and when I press back again, I should exit out of Screen A.

What have I tried so far ?

Is my below code correct for what I want ?

Main Activity.xml

import android.support.v7.app.ActionBarActivity;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends Activity implements OnClickListener {

    TextView title;
    EditText userinput;
    Button buttonA,buttonB;

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

    private void initialize() {
        userinput = (EditText)findViewById(R.id.userinput);
        title = (TextView)findViewById(R.id.title);
        buttonA = (Button)findViewById(R.id.buttonA);
        buttonB = (Button)findViewById(R.id.buttonB);
        buttonA.setOnClickListener(this);
        buttonB.setOnClickListener(this);
    }



@Override
public void onBackPressed() {
  title.setVisibility(View.INVISIBLE);
 userinput.setVisibility(View.INVISIBLE); 


    }

    @Override
    public void onClick(View v) {

        switch(v.getId())
        {

        case R.id.buttonA:
            break;

        case R.id.buttonB:

            title.setVisibility(View.VISIBLE);
            userinput.setVisibility(View.VISIBLE);
            break;
        }


    }


}

I referred this and this link but did not understand. If some one could help me in achieving me want I want 1

Community
  • 1
  • 1
  • 1
    Let me know if you need my xml Code –  Dec 08 '14 at 14:37
  • 1
    cant you just add a bool? fieldshidden=false; when you hide the fields, make it true then in onBackPressed do if(fieldshidden){bla bla} – Bart Hofma Dec 08 '14 at 14:40
  • @Bart Hofma :- yes, your solution is the easiest way to solve this – Rahul Patidar Dec 08 '14 at 14:55
  • @OP: If you are using [Fragments](developer.android.com/guide/components/fragments.html), then you can add `title` and `userinput` to a Fragment. When you make that fragment visible, add that transaction to backstack, and what you are trying to achieve will work like a magic! – Shishir Gupta Dec 08 '14 at 14:56

3 Answers3

3

When the back button is pressed the textview and edittext should hide and when I press back again,

@Override
public void onBackPressed() {
   if (title.getVisibility() != View.VISIBLE && 
             userInput.getVisibility() != View.VISIBLE) {
      super.onBackPressed();
      return;
   }
   title.setText(null);
   userinput.setText(null);
   title.setVisibility(View.INVISIBLE);
   userinput.setVisibility(View.INVISIBLE);        
 }
Blackbelt
  • 156,034
  • 29
  • 297
  • 305
0

Do like this.

@Override
 public void onBackPressed() {
if(title.getVisibility()==View.VISIBLE)
 {
  title.setVisibility(View.INVISIBLE);
  userinput.setVisibility(View.INVISIBLE); 
 }
  else
  {
   finish();
  }
}

Hop it will do what you want.

Nitesh
  • 3,868
  • 1
  • 20
  • 26
0

Change the code to below

@Override
public void onBackPressed() {
if (title.getVisibility() != View.VISIBLE && 
             userInput.getVisibility() != View.VISIBLE){
title.setVisibility(View.INVISIBLE);
userinput.setVisibility(View.INVISIBLE); 
}
super.onBackPressed();
}
Farooq Arshed
  • 1,984
  • 2
  • 17
  • 26