0

I'm trying to make my first Android application, and here is the problem i met:

When i am tapping the Yes Button, the function onYesButtonClick() is not called. The function is supposed to change the visibility of the TextView with the id TextView1, but when i tap, nothing happens. Here is the activity_main.xml:

<TextView
    android:id="@+id/TextView01"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="19dp"
    android:text="@string/q1" />

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/button1"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="55dp"
    android:text="@string/hello_world"
    android:visibility="invisible" />

<Button
    android:id="@+id/button1"
    style="?android:attr/buttonStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/TextView01"
    android:layout_marginTop="22dp"
    android:layout_toLeftOf="@+id/textView1"
    android:onClick="onYesButtonClick"
    android:text="Yes" />

<Button
    android:id="@+id/button2"
    style="?android:attr/buttonStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/textView1"
    android:layout_toRightOf="@+id/textView1"
    android:onClick="onNoButtonClick"
    android:text="No" />

and here is the MainActivity.java:

 public class MainActivity extends ActionBarActivity {

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

public void onYesButtonClick(View view){

     TextView textView = (TextView) findViewById(R.id.textView1);
     textView.setVisibility(android.view.View.VISIBLE);

}
public void onNoButtonClick(View view){


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

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

}

VIOO66
  • 67
  • 8

3 Answers3

0

declare textView in onCreate method because findViewById available in context of activity methods. So Change your code as

    TextView textView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textView= (TextView) findViewById(R.id.textView1);

    }

    public void onYesButtonClick(View view){
       textView.setVisibility(android.view.View.VISIBLE);
  }
Giru Bhai
  • 14,370
  • 5
  • 46
  • 74
0
public class MainActivity extends Activity {

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

public void onYesButtonClick(View view){

    TextView textView = (TextView) findViewById(R.id.textView1);
    textView.setVisibility(View.VISIBLE);
}

public void onNoButtonClick(View view){
    TextView textView = (TextView) findViewById(R.id.textView1);
    textView.setVisibility(View.GONE);
}

}
Akash Jindal
  • 505
  • 3
  • 7
0
//this should be in your onCreate methord
TextView textView = (TextView) findViewById(R.id.textView1);

public void onYesButtonClick(View view){

     if(view.getID()==R.id.button1){
     textView.setVisibility(android.view.View.VISIBLE);
    }
}
codePG
  • 1,754
  • 1
  • 12
  • 19