• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Creating a simple application

craigtb

Lurker
Jan 25, 2010
6
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
 
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.
 
Upvote 0
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?

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.:)
 
Upvote 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:
package com.androidcore.hello;

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


public class button extends Activity {
	   /** Called when the activity is first created. */
	   @Override
	  
	   public void onCreate(Bundle savedInstanceState) {
		   
	       super.onCreate(savedInstanceState);
	        TextView tv = new TextView(this);
	        final Button btn = new Button(this);
	    
	       tv.setText("Test");
	       btn.setText("Hey");
	       setContentView(tv);
	       setContentView(btn);
	        int inc = 0;
	       btn.setOnClickListener(new View.OnClickListener() {
	             

				public void onClick(View v) {
	                 // Perform action on click
					
	            	 btn.setText("Pressed");
	            	 

	             }
	       });
	   }
}

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
 
Upvote 0
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
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:
<?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:id="@+id/sampleTextView"  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="your text view"
    />
<Button
    android:id="@+id/sampleButton"  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="your button"
    />
</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.
 
Upvote 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:
<ImageView id="@+id/picview"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   />
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.
 
Upvote 0
Ok so this is my code to start off
Code:
public class button extends Activity {
	   /** Called when the activity is first created. */
	   @Override
	  
	   public void onCreate(Bundle savedInstanceState) {
		   
	       super.onCreate(savedInstanceState);
	       setContentView(R.layout.main);
	      final Button t = (Button)findViewById(R.id.ok);
	     
	       
	       
	       t.setOnClickListener(new View.OnClickListener() {
	             public void onClick(View v) {
	                 // Perform action on click
	            	 t.setText("It worked");
	            	
	             }
	         });
	   }
	   
	   
}

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!!!
 
Upvote 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
 
Upvote 0
Ok so i put
Code:
<ImageView
    android:src="@drawable/f4"
/>
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?
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.
 
Upvote 0

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones