Here's the working code,
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button
android:id="@+id/btn1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/sd"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="70dp"
android:onClick="onClick"/>
<Button
android:id="@+id/btn2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/sd2"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="140dp"
android:visibility="gone"
android:onClick="onClick"/>
</RelativeLayout>
MainActivity.java
package com.example.test;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
public class MainActivity extends Activity {
Button b1;
Button b2;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1 = (Button) findViewById(R.id.btn1);
b2 = (Button) findViewById(R.id.btn2);
}
public void onClick(View v) {
switch(v.getId()) {
case R.id.btn1:
b2.setVisibility(View.VISIBLE);
b1.setVisibility(View.INVISIBLE);
break;
case R.id.btn2:
b2.setVisibility(View.INVISIBLE);
b1.setVisibility(View.VISIBLE);
break;
}
}
}
Output (Compiled & ran in Emulator):
When the app starts
Button2
will be invisible since it was set as invisible inxml layout
.
- Once the user click
button1
above,button2
will be visible &button1
will be invisible.
- Once the user click