tags:

views:

831

answers:

2

How can I display a button on screen? I have defined it as

final Button nappi = (Button) findViewById(R.id.soita);

and

<Button 
  android:layout_width="100px"
  android:layout_height="wrap_content"
  android:text="@+string/Soita"
  android:id="@+id/soita" 
/>
+4  A: 

You need to create the layout of the view in which you want the button to appear. Even something like

Let's say you're storing the definition in a file called main.xml in your res/layout directory.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >

<Button android:text="Button01" android:id="@+id/Button01" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
</LinearLayout>

Then within your activity, you need to do the following:

public class Main extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Replace this with the name of your xml file dictating layout
        setContentView(R.layout.main);
    }
}

See the Hello Views tutorial; very useful. http://developer.android.com/guide/tutorials/views/index.html

If you're asking something different, like programmatically adding a button to an existing view, I'm not sure about that. In that case you might want to have the button start off as hidden and then reveal it when you need it.

I82Much
A: 

I don't see why this displays nothing. I tried to make my own simple user interface.

main:

package com.xyz;
import android.app.Activity;
import android.os.Bundle;

public class NewHomeScreen extends Activity {
/** Called when the activity is first created. */
@Override public void onCreate(Bundle state) {
    super.onCreate(state);
    setContentView(R.layout.main);
}
}

XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
>
<TextView  
   android:layout_width="fill_parent" 
   android:layout_height="fill_parent" 
   android:text="@string/hello" 
>
</TextView>
<Button android:text="Soita"
    android:id="@+id/soita" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"
>
</Button>
</LinearLayout>

Manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.xyz"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
  <activity android:name="NewHomeScreen">
    <intent-filter>
      <action android:name="android.intent.action.MAIN" />
      <category android:name="android.intent.category.HOME"/>
      <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>    
  </activity>
</application>
<uses-sdk android:minSdkVersion="3" />

</manifest>
Jaska
It looks like he didn't have a layout around his button and/or wasn't setting the content view.
fiXedd