<FrameLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ProgressBar
android:id="@+id/progressBar1"
style="?android:attr/progressBarStyleLarge"
android:layout_gravity="center_horizontal"
android:layout_width="wrap_content"
android:visibility="gone"
android:layout_height="wrap_content" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_height="wrap_content"
android:text="Click Me" />
</FrameLayout>
then in your onclick listener of your button
ProgressBar p = (ProgressBar)findViewById(R.id.progressBar1);
if(p.getVisibility() != 0){ // check if it is visible
p.setVisibility(0); // if not set it to visible
arg0.setVisibility(1 or 2); // use 1 or 2 as parameters.. arg0 is the view(your button) from the onclick listener
}
for your second problem.. it happens because you have set the visibility of the button to gone or invisible, if you finished the activity, then when you call it again the os will re-create it, but if you didn't specify to close it then the os will store it in something called backstack, also an activity has lifecycle , more just copy and paste it
@Override
public void onResume() { // this is called by your activity before it gets visible to the user, when you leave this activity 2 and come back to
//activity 1, because activity 1 wasn't killed it is resumed from the backstack and this function is called
// TODO Auto-generated method stub
super.onResume();
// so you will check here, if your button is visible or not like the way you did in the onclick for the progressbar
ProgressBar p = (ProgressBar)findViewById(R.id.progressBar1);
Button b = (Button) findViewById(R.id.button1); // if you have already instantiated your button then nevermind
if(b.getVisibility() != View.VISIBLE){ // check if it is visible
//View.VISIBLE is an int variable which is 0
b.setVisibility(View.VISIBLE); // if not set it to visible
p.setVisibility(View.INVISIBLE or View.GONE); //View.INVISIBLE is 1, and 2 is View.GONE
// you can either use the ints or the static variables.. gone stands for when the view is invisible and is not accounted for with respect to space on the screen
// so if you use relativeLayout and you align a view in respect to another if you use gone your viewlayout will be squashed around,
// invisible is the when its invisible and is being accounted for
}