Creating a simple application


Last Updated:

  1. craigtb

    craigtb Member This Topic's Starter

    Joined:
    Jan 25, 2010
    Messages:
    6
    Likes Received:
    0
    I am very new to programming for android, but have had 2 java classes so I have some experience. I am working ok creating an application where basically it just displays a picture, has a button below it and when you press it plays a sound clip. Very simple I'm sure. I could do most of this in the way I was taught, but not in android.

    So i guess the first thing I need help with is displaying a picture. will i need to import anything? I have placed the picture in res/drawable-hdpi. I read on a tutorial that you need to place it in the drawable folder but all i have is drawable-hdpi/ldpi/mdpi. Do I have it in the correct place?

    Also in messing around I was just trying to place a Textview and a button in the same window. They both work independently but when they are both there the button takes up the whole screen. IS there a way to size it and place it below the text view, or what I am hoping with be the image at some point?

    I'm sorry if this was very unclear. Any help regarding this would be just awesome!

    Thanks in advance,
    Craig
     

    Advertisement
  2. rock_on2109

    rock_on2109 New Member

    Joined:
    Jan 10, 2010
    Messages:
    4
    Likes Received:
    0
    For your button taking up the whole window problem:
    in your main layout XML:
    <Button
    android:id="@+id/Button"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Press Me!"
    />
    Having "fill_parent" in both the height and width will cause it to take up the whole screen.
    Hope that helps.
     
  3. htngon

    htngon Member

    Joined:
    Jan 26, 2010
    Messages:
    10
    Likes Received:
    0
    You are using firmware 2.0 and ADT 0.9.5. I has just working on firmware 1.6 and ADT 0.9.1. In this firmware, all pictures you need then put them into "drawable" folder. And I think it is similarly to your firmware.

    You can access it: "@drawable-hdpi/ldpi/mdpi/the_name_of_your_picture".

    I hope it is useful for you.:)
     
  4. craigtb

    craigtb Member This Topic's Starter

    Joined:
    Jan 25, 2010
    Messages:
    6
    Likes Received:
    0
    Ok the ADT platform I am using is 2.1, I think thats what you are asking. Also I am not sure what firmware I am using.

    Ok So in my XML the button height is wrap content but it is still filling the whole screen I may not be using that button though. Here is the code that I have:
    Code (Text):
    1. package com.androidcore.hello;
    2.  
    3. import android.app.Activity;
    4. import android.os.Bundle;
    5. import android.view.View;
    6. import android.view.View.OnClickListener;
    7. import android.widget.TextView;
    8. import android.widget.Button;
    9.  
    10.  
    11. public class button extends Activity {
    12.        /** Called when the activity is first created. */
    13.        @Override
    14.      
    15.        public void onCreate(Bundle savedInstanceState) {
    16.            
    17.            super.onCreate(savedInstanceState);
    18.             TextView tv = new TextView(this);
    19.             final Button btn = new Button(this);
    20.        
    21.            tv.setText("Test");
    22.            btn.setText("Hey");
    23.            setContentView(tv);
    24.            setContentView(btn);
    25.             int inc = 0;
    26.            btn.setOnClickListener(new View.OnClickListener() {
    27.                  
    28.  
    29.                 public void onClick(View v) {
    30.                      // Perform action on click
    31.                    
    32.                      btn.setText("Pressed");
    33.                      
    34.  
    35.                  }
    36.            });
    37.        }
    38. }
    The button does work because the text on it changes when I press it. From what I can tell this is probably not the best way to go about doing this because when I was looking at the examples on the android dev site it added buttons and text fields in the XML (like you were saying). The button I have in the XML isnt showing though.

    Is there a way to change the button size and place it somewhere relative to the screen, or should I delete that button and use the button in the XML, if so how do I use it?


    Thanks,
    Craig
     
  5. htngon

    htngon Member

    Joined:
    Jan 26, 2010
    Messages:
    10
    Likes Received:
    0
    Every android project has "layout" folder. It is the view of your application. So, when you want design view of your app, you should modify in XML. You can code to create your view, but that isn't the best way.

    You can refer the following example: your activity contain a textview and a button:

    filename: main.xml (it occupy "layout" folder)
    Code (Text):
    1. <?xml version="1.0" encoding="utf-8"?>
    2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    3.     android:orientation="vertical"
    4.     android:layout_width="fill_parent"
    5.     android:layout_height="fill_parent"
    6.     >
    7. <TextView
    8.     android:id="@+id/sampleTextView"  
    9.     android:layout_width="fill_parent"
    10.     android:layout_height="wrap_content"
    11.     android:text="your text view"
    12.     />
    13. <Button
    14.     android:id="@+id/sampleButton"  
    15.     android:layout_width="fill_parent"
    16.     android:layout_height="wrap_content"
    17.     android:text="your button"
    18.     />
    19. </LinearLayout>
    Then, you use setContentView(R.layout.main)
    Compile and run, and see the result.

    If any problem, don't hesitate to ask me.
    Good luck.
     
  6. craigtb

    craigtb Member This Topic's Starter

    Joined:
    Jan 25, 2010
    Messages:
    6
    Likes Received:
    0
    I have done that and it does work, but the question I have with that is how do I interact with that button?

    Also I have tried to add
    Code (Text):
    1. <ImageView id="@+id/picview"
    2.    android:layout_width="fill_parent"
    3.    android:layout_height="wrap_content"
    4.    />
    to my xml because i found thats how you start with adding an image but when i try and run it it says there are errors in my code. Also I am not sure how i declare what picture it is that i want displayed.
     
  7. htngon

    htngon Member

    Joined:
    Jan 26, 2010
    Messages:
    10
    Likes Received:
    0
    You can interact with button by setOnClickListen on it. In your activity, declare Button t = (Button)findViewbyId(R.id.sampleButton);
    t.set...

    try it

    You can refer developer.android.com
     
  8. craigtb

    craigtb Member This Topic's Starter

    Joined:
    Jan 25, 2010
    Messages:
    6
    Likes Received:
    0
    Ok so this is my code to start off
    Code (Text):
    1. public class button extends Activity {
    2.        /** Called when the activity is first created. */
    3.        @Override
    4.      
    5.        public void onCreate(Bundle savedInstanceState) {
    6.            
    7.            super.onCreate(savedInstanceState);
    8.            setContentView(R.layout.main);
    9.           final Button t = (Button)findViewById(R.id.ok);
    10.          
    11.            
    12.            
    13.            t.setOnClickListener(new View.OnClickListener() {
    14.                  public void onClick(View v) {
    15.                      // Perform action on click
    16.                      t.setText("It worked");
    17.                    
    18.                  }
    19.              });
    20.        }
    21.        
    22.        
    23. }
    Where I'm at now is trying to get an image to display. It looks like I need to add an image view in the XML but what i have gives me an error (see previous post). ANy idea on where to go from there?

    Also when i try to change the text on the button it says it has to be a variable declared as final. Why is that?

    Thanks again for all the help!!!
     
  9. htngon

    htngon Member

    Joined:
    Jan 26, 2010
    Messages:
    10
    Likes Received:
    0
    Button is a variable, so don't declare it as final (a constant). drop final.
    What do you want with image?. If you want an image in your program. You have to put it into your drawablexx folder first. Try it.
    Can I ask you a question? Where do you come from?
     
  10. craigtb

    craigtb Member This Topic's Starter

    Joined:
    Jan 25, 2010
    Messages:
    6
    Likes Received:
    0
    I have a picture in res/drawable-hdpi named f4.jpg.

    Also when I try to do anything with "t" in the onClick it says "t" must be final.

    I am in Auburn, AL
     
  11. htngon

    htngon Member

    Joined:
    Jan 26, 2010
    Messages:
    10
    Likes Received:
    0
    Assuming that you have layout xml with a button. And the code that you written in code like this:

    private Button btn;

    public void onCreate....{
    btn = (Button)findViewById(R.id.a_some_button);
    btn.setOnClickListener(...){
    }
    }

    That's enough.

    And you want to load picture. You can use @drawable/f4. Example
    you have an ImageView in xml
    android:src="@drawable/f4
     
  12. craigtb

    craigtb Member This Topic's Starter

    Joined:
    Jan 25, 2010
    Messages:
    6
    Likes Received:
    0
    Ok so i put
    Code (Text):
    1. <ImageView
    2.     android:src="@drawable/f4"
    3. />
    4.  
    In and when i run it it pops up with an error saying the "application button (process com.androidcore.hello) has stopped unexpectantly. please try again. force close"

    Any ideas?
     
  13. htngon

    htngon Member

    Joined:
    Jan 26, 2010
    Messages:
    10
    Likes Received:
    0
    You can refer this attachment
     

    Attached Files:

  14. htngon

    htngon Member

    Joined:
    Jan 26, 2010
    Messages:
    10
    Likes Received:
    0
    You should refer developer.android.com to get the basic knowledge about programming android. After you get it, it's very easy to create an simple app for you. Maybe, it's difficult but no more choice. Good luck.
     

Share This Page

Loading...