Nov 30, 2010

Display a icon/image on button

To display a icon/image of drawable type, the tab android:drawableRight (or drawableTop, drawableRight and drawableBottom) can be used.

<Button
android:id="@+id/button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="- I'm a Button -"
android:drawableRight="@drawable/icon"
/>


Display a icon/image on button


Related post:
- Display a icon/image on button using Java code

Nov 28, 2010

Button and Button.OnClickListener




<?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="wrap_content"
android:text="@string/hello"
/>
<Button
android:id="@+id/button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="- I'm a Button -"
/>
</LinearLayout>


package com.android.coding.HelloWorld;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

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

Button button = (Button)findViewById(R.id.button);
button.setOnClickListener(new Button.OnClickListener(){

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(HelloWorld.this, "I'm a Toast!", Toast.LENGTH_LONG).show();
}});
}
}

Toast: show a little message for the user

Toast
The simplest form to make a standard toast that just contains a text view, and show it for the specified duration.

Toast.makeText(HelloWorld.this, "I'm a Toast!", Toast.LENGTH_LONG).show();


where:
Context context - The context to use. Usually your Application or Activity object.
CharSequence text - The text to show. Can be formatted text.
int duration - How long to display the message. Either LENGTH_SHORT or LENGTH_LONG



Nov 27, 2010

HelloWorld on Android



/HelloWorld/res/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="wrap_content"
android:text="@string/hello"
/>
</LinearLayout>


/HelloWorld/src/com/android/coding/HelloWorld/HelloWorld.java
package com.android.coding.HelloWorld;

import android.app.Activity;
import android.os.Bundle;

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




Infolinks In Text Ads