Saturday, October 29, 2011

Displaying an integer in a TextView on Android

This one took me for a ride for a few hours.  I went all over the internet with little help.  Here is the problem.  I have a layout created with TextView, but I want to display an integer value in that field. 

 

I declare my integer as:

int score = 1;

I also have a string value called scoreText.

I have a TextView with an ID of score.  Here is how I display the integer in the TextView

TextView text = (TextView)findViewById(R.id.score);

text.setText(scoreText.valueOf(score));

Setting up a button click on Android

In this example, I have a button in my layout XML file that I called bulb1_button.  I want to set up a click event for it.  Below is my code to do this inside the OnCreate method.

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
     
      // Button 1 clicked event. 
      final Button button1 = (Button)findViewById(R.id.bulb1_button);
      button1.setOnClickListener(new View.OnClickListener()
      {
       
        @Override
        public void onClick(View v)
        {
            // TODO: Actions to take when the button is clicked.           
        }
      });
      // End of Button 1 clicked event.

 

We first define a variable the will hold the button.  I call this one button1 which is of type Button.  Next I set up an event listener to handle the click.  Inside the listener, I create the code to handle the click and place inside of it what happens when the button is clicked.

How to change the text in a TextView

First off, you need a TextView defined in your XML file.  The one that I am going to be using is called status. Below is the relevant portion of the XML code.

 

<TextView
    android:id="@+id/status"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Large Text"
    android:textAppearance="?android:attr/textAppearanceLarge" />

below is the code that I used to change the text.

 

        // Update the status field.           
        TextView text = (TextView)findViewById(R.id.status);
        text.setText("Button1 clicked");

In this example, I declared a variable text of type TextView.  I then used the setText method to define the text that should appear in the TextView.

Friday, October 28, 2011

lining up ImageViews horizontally

I had an app that I was developing that required me to have three images lined up horizontally.  The problem is that I could not get them to line up in the center.  I could use margins and padding, but on devices of different screen sizes and pixel densities, this would not look good.  Below is the XML code that allowed me to do this and a screen shot.

 

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

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Large Text"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:paddingLeft="4.0dip"
        android:paddingRight="4.0dip"
        android:paddingTop="5.0dip"
        android:paddingBottom="1.0dip">


    </LinearLayout>

    <LinearLayout
        android:id="@+id/linearLayout2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >

        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="0.0dip"
            android:layout_height="fill_parent"
            android:src="@drawable/ql" android:layout_weight="1.0"/>

        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="0.0dip"
            android:layout_height="fill_parent"
            android:src="@drawable/ql" android:layout_weight="1.0"/>
               
        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="0.0dip"
            android:layout_height="fill_parent"
            android:src="@drawable/ql" android:layout_weight="1.0"/>               

    </LinearLayout>

</LinearLayout>

 

image

 

The key to making this work is the android:layout_weight.  The layout_weight takes any unused space in the layout and evenly distributes it among the widgets in the layout.

Monday, October 10, 2011

An Internal error occurred during: “Launching New-Configuration (1)”.

This error occurs when you are attempting to run a Droid app in Eclipse.  The details of the error are:

 

An internal error occurred during: "Launching New_configuration (1)".
Path for project must have only one segment.

 

 

This time when you run the app, give it a Project Name.

image

You can see that I left mine blank above.