0

I'm totally beginner in Java and Android programming.

Now I wan to show up button when a method is run. But the button is already placed on the screen.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // set the listener for the button
    mConfrimButtton = (Button) findViewById(R.id.confirm_button);
}

public void aMethod {
    // show up the button when doing this method but the button is already shown up
}

How can I do it?

Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360

4 Answers4

1
  • First Issue

You forgot to call setContentView(R.layout.layout);

Read more here What is setContentView(R.layout.main)?

  • Now I wan to show up button when a method is run

You can use setVisibility

Set the visibility state of this view. EX. mConfrimButtton.setVisibility(View.VISIBLE);

  • But the button is already placed on the screen.

You can use android:visibility

Controls the initial visibility of the view. EX. android:visibility="gone"

You can read more here about Android : difference between invisible and gone?

AskNilesh
  • 67,701
  • 16
  • 123
  • 163
  • 1
    Thanks. It works but it works without `setContentView()`. I still don't understand what the first issue is. –  Oct 05 '18 at 04:50
  • @ani r u sure you code works without `setContentView()` because there no `setContentView()` method in your above code inside `onCreate()` method – AskNilesh Oct 05 '18 at 04:54
  • @ani check updated answer – AskNilesh Oct 05 '18 at 04:57
1

When the activity loads, set its visibility to INVISIBLE or GONE in your layout XML:

<Button android:text="Your Button"
    android:id="@+id/yourButton"
    android:visibility="invisible" />

Then, in your Java activity code, make that button visible:

mConfirmButtton = (Button) findViewById(R.id.yourButton);
mConfirmButtom.setVisibility(View.VISIBLE);

Note that adding an OnClick listener is not directly related to what you described in your question. A click listener is what would fire if the button, already visible, were clicked by a user in that activity. But, it wouldn't have anything to do with making that button visible when a certain method is run.

Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
0

Write mConfrimButtton.setVisibility(View.INVISIBLE) in onCreate() And then make button visible in aMethod() by using mConfrimButtton.setVisibility(View.VISIBLE).

AskNilesh
  • 67,701
  • 16
  • 123
  • 163
Parul
  • 387
  • 1
  • 5
0

make button visibility gone

in xml:-

android:visibility="gone"

you can also set button visibility programatically

mConfrimButtton.setVisibility(View.GONE);

wherever you want to show button set button visibility VISIBLE

mConfrimButtton.setVisibility(View.VISIBLE);
Mittal Varsani
  • 5,601
  • 2
  • 15
  • 27