Very simple example about a button in Android?

20,108

Solution 1

First, let's take a look at your test.java, it won't work as is. I hope you understand basic concepts of object-oriented programming and get rid of the static methods.

In Android there's a class called Activity which you need to extend. If you followed Lazy Ninja's answer, the Eclipse plugin might have already created that one for you (it may be called MainActivity.java). Simply put, an activity represents a view in your application. In your trivial example, most if not all, your code goes to that class.

In Android, you don't use public static void main(String[] args), but override the activity's onCreate method. In the AndroidManifest.xml you define your activities (at this point you only need one) as well as which is the one launched when your application starts, and whose onCreate is the starting point of the whole app (again see the structure created by the Eclipse).

In onCreate you should call setContentView with the layout you wish to use in that activity. The layout is given to the setContentView as an integer parameter from the generated R class, like R.layout.layout_file_name which maps to the XML file at res/layout/layout_file_name.xml.

The layout file is the one that contains the declaration of the desired layout. In your case, it might have a LinearLayout as the root element and two Buttons.

Solution 2

Go into the xml for the buttons and set an onClick attribute of "runClient".

<Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:onClick="runClient"
        android:text="=)" />

UPDATE:

You should see something like this in eclipse.

enter image description here

UPDATE 2:

This is an example android project file tree. Highlighted is where the layout.xml should be.

enter image description here

Solution 3

In your main activity use something like this:

//Part of onCreate
Button b1;
setContentView(whatever.it.is);
b1 = (Button)findViewById(R.id.buttonID);

b1.setOnClickListener(buttonAddOnClickListener);

//Outside of onCreate, on its own
Button.OnClickListener buttonAddOnClickListener  = new Button.OnClickListener(){
    @Override
    public void onClick(View arg0) {
        //Switch statement so you don't have to use a lot of click listeners
        switch (arg0.getId()) {
            case R.id.b1:
                doSomething();
            case R.id.b2:
                doSomethingElse();
        }
    }
};

In your XML when you implement the button make sure to add this:

android:id="@+id/anID"

Change anID to the id you want.

Also I prefer IntelliJ (http://www.jetbrains.com/idea/) for Android programming. Your XML should be in a folder titled 'res' then in a subdirectory 'layout'. If you don't see those folders your project might not be set up correctly.

Solution 4

Answer step by step to your question :

  1. Mostly, XML file store in the /res folder where you can define the interface of Android UI. For example, if you have 2 activities in your app, you can create 2 xml files ( layout of an activity ) represent of that 2 activities.
  2. In the /src directory is just for java file. XML file should be created in the /res/... directory.
  3. Each view of XML file has an id. When you first create an android project. the main activity already set a content view of your first app using method of Activity Class.

    setContentView(R.layout.main)

-layout - represent layout folder in the /res folder -main - represent an XML file in the /res/layout folder.

You can find out more with android doc

Solution 5

On eclipse

  1. Click File -> New -> Other, a dialog will pop up.
  2. From the dialog, select Android -> Android application project and hit next.
  3. A New Android Project dialog will popup, fill Application, Project and packages names Chose SDK version
  4. Follow the wizard until you click Finish

There you will have your android project file. And you layout xml file will be the res -> layout folder.
I think you should read Android Training to get you start.

Share:
20,108
hhh
Author by

hhh

Updated on July 09, 2022

Comments

  • hhh
    hhh almost 2 years

    I want a very simple button with some explanation/vizual-thing, perhaps onCreate/onLauncher to do what makes you smile! I have tried buttons here but errs here, probably easiest if someone could direct me to a ready working example about a button in Android. History shows problems with R and XML -files, probably easiest if someone could state it clearly how to do it: a very simple button. Its directory tree etc?

    Please, provide a simple example about a button in Android, nothing else.

    enter image description here