Monday, November 21, 2011

java.lang.ClassCastException: android.widget.Button problem

 

 

While trying to create a clickable ImageView, I received this error:

 

11-21 15:35:15.940: E/AndroidRuntime(7607): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mctexpert.android.ishunt/com.mctexpert.android.ishunt.MenuScreen}: java.lang.ClassCastException: android.widget.Button

 

The problem was it thought that I was trying to work with a button as opposed to the image.  I ended up deleting the ImageView from the layout and then recreating it.  That took care of this problem.

Thursday, November 10, 2011

Lock a screen in portrait mode without losing data

 

As I continue to progress in my Android education, I’m finding out how to make my apps work the way I want them to do.  In this case, I needed to prevent the screen from rotating.  I also forgot that Android calls the onCreatemethod every time you do this.  Here is how I fixed that,

 

I opened the AndroidManifest.xml file.  I added two lines of code:

android:screenOrientation="portrait"
android:configChanges="keyboardHidden|orientation">

The first tells Android to keep the screen in portrait mode. 

The second tells Android that in the event the device is rotated or the keyboard is pulled out, do not execute the onCreate method.  Instead, keep going with what you currently have open. 

Below is how the code in my AndroidManifest XML file looks.  Notice that these elements are called on a per activity bases.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="
http://schemas.android.com/apk/res/android"
    package="com.mctexpert.android.orientation"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="7" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:label="@string/app_name"
            android:name=".OrientationActivity"
            android:screenOrientation="portrait"
            android:configChanges="keyboardHidden|orientation">
            <intent-filter >
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

Wednesday, November 2, 2011

Generate a random number on Android

For this I have a TextView named mTextField3

 

Random r = new Random();

int i1=r.nextInt(100);

mTextView3.setText(String.valueOf(i1));

 

This will generate a random number between 0 and 99.

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.