How to show only the button

Refresh

November 2018

Views

733 time

1

Think that I have many activities,and all I want is this:

I have a button defined in a file called test.xml.

I want to show that button,in one of my activities,BUT only that button, not the whole test.xml.

Here I have a problem,because i made a new button like this:

final Button button = (Button) findViewById(R.id.aboutbutton);

But I just don't know how to display only that button,not the hole test.xml. I searched the android website,but from what I've seen,it displays the hole xml file,not only 1 button from it...so it's not what I need...

Also what is wrong with this code? errors: The method onCreate(Bundle) is undefined for the type View Gameview.java
The method onCreate(Bundle) of type Gameview must override a superclass method Gameview.java

    public class Gameview extends View{

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

private final Game game;
public Gameview(Context context) {
    super(context);

    this.game = (Game) context;
    setFocusable(true);
    setFocusableInTouchMode(true);
    // TODO Auto-generated constructor stub
}

@Override
   protected void onDraw(Canvas canvas) {
    // Draw the background...
      Paint background = new Paint();
      background.setColor(getResources().getColor(
            R.color.background));

      canvas.drawRect(0, 0, getWidth(), getHeight(), background);

      final Button button = (Button) findViewById(R.id.aboutbutton);




}

    }

**I fixed the code like you said but now everything gets errored. Most of them can be fixed but the ondraw ...:

   aboutbutton cannot be resolved or is not a field Gameview.java   
   The constructor Activity(Context) is undefined   Gameview.java   
   The method getHeight() is undefined for the type Gameview    
   The method getWidth() is undefined for the type Gameview 
   The method onDraw(Canvas) of type Gameview must override a superclass method                   Gameview.java 
   The method requestFocus() is undefined for the type Gameview Game.java   m
   The method setContentView(int) in the type Activity is not applicable for the   arguments (Gameview) Game.java   
   The method setFocusable(boolean) is undefined for the type Gameview  
   The method setFocusableInTouchMode(boolean) is undefined for the type Gameview   Gameview.java** 

2 answers

1

If you want to add just button then either make it programatically or just define only one button in button.xml file and set as setContentView(R.layout.button);

3

Programatically:

    @Override
    protected void onCreate(Bundle arg0) {
        super.onCreate(arg0);
        Button b=new Button(this);
        b.setText("some useful name");//because is empty by default 
        setContentView(b);
}

in xml:

in layout mulayout.xml

in the code

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

here is the complete code

package com.package.name;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.TextView;

public class TestProbaActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Button b=new Button(this);
                setContentView(b);
          }
     }