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

Apps Im still stuck making this picture gallery.....HELP!!!!!

I am a total novice at java/android and I just cant figure out whats going wrong or what I have to do in order to get pictures to draw to screen when I click the buttons I have set up. Ive tried replacing the classes but that does no good because I dont know how to use the methods or functions to get stuff to work right.....Ill just post all the code I have so you guys can see the mess I have so far.....can some one please help because I have 30 java books and 10 android books that Ive been skimming through and I even have looked over the android developers page but it doesnt help at all.....I feel like Im trying to read chinese or something.

Heres my main java file:

package test.button;

import android.app.Activity;
import android.os.Bundle;
import android.text.InputType;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.LinearLayout;


public class button extends Activity {
/** Called when the activity is first created. */

Button myButton;
EditText myText;
protected int count;
int i = 0;
long pictures[] = {R.drawable.hot_girls, R.drawable.hillarious_post, };
int numGirls = 2;

@Override
LinearLayout mLinearLayout;

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Create a LinearLayout in which to add the ImageView
mLinearLayout = new LinearLayout(this);

// Instantiate an ImageView and define its properties
ImageView i = new ImageView(this);
i.setImageResource(R.drawable.icon);
i.setAdjustViewBounds(true); // set the ImageView bounds to match the Drawable's dimensions
i.setLayoutParams(new Gallery.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

// Add the ImageView to the layout and set the layout as the content view
mLinearLayout.addView(i);
setContentView(mLinearLayout);
}
//public void onCreate(Bundle savedInstanceState) {
//super.onCreate(savedInstanceState);

//getWindow().setBackgroundDrawableResource(R.drawable.icon) ;

myButton = (Button) findViewById (R.id.my_button);
myText = (EditText) findViewById (R.id.my_text);

myText.setSingleLine();
myText.setInputType(InputType.TYPE_NULL);

myButton.setOnClickListener(new OnClickListener(){

@Override
public void onClick(View arg0) {
myText.setText("youve touched the screen"+ ++count +" times");
if (i < (numGirls - 1))
{
i++;
getWindow().setBackgroundDrawableResource((int) pictures) ;
}
else

{
i = 0;
getWindow().setBackgroundDrawableResource((int) pictures) ;
}


}



});


}
}




and now heres what I have in the main xml

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


<EditText
android:id="@+id/my_text"
android:layout_width="fill_parent"
android:layout_height="wrap_content">

</EditText>

<Button android:text="@string/button_text"
android:id="@+id/my_button"
android:layout_width="fill_parent"
android:layout_height="wrap_content">

</Button>


</LinearLayout>


some one help me fix this its been driving me crazy for 2 months or so now.
 
Hi sambocrockett

Please use CODE tags when posting code on the forum. (The # button in the toolbar)
That will preserve the layout & indentation of your code and make it easier for others to read it.

Apart from a general sense of frustration with Java and android development, I couldn't see what your problem was from your post. Could you expand on it a bit please?

What are you trying to achieve?
What does your code actually do? (I haven't tried to read it yet, I'm hoping a reformatted version will appear soon)
Does it crash? If so please supply a traceback?
Does it do something, but not what you want? If so, please elaborate.

Is there a specific fault/bug you need help with?

Nothing personal, and I really don't mean to cause any offence, but I think the problem is here:

Ive tried replacing the classes but that does no good because I dont know how to use the methods or functions to get stuff to work right.....

I feel like Im trying to read chinese or something.

If you're using classes and methods that you don't understand, then I really think you're approaching this the wrong way. You need to build on a better foundation of understanding.

You say you have 30 java books and 10 android books? Have you tried working through them doing the simple stuff first? If none of those books are helping, and none of it makes sense, then maybe Android development isn't for you? I can't really suggest any alternative books, as you'll probably have them already.

Sorry to be negative. :(

If you have specific problems, I'll be happy to try to help.
But if none of it makes any sense to you at all, then I can only suggest going back to the books. Make sure you understand the simpler stuff first. If you have trouble with those too, then ask questions here, to make sure you're happy before you move on to the next steps.

Mark
 
  • Like
Reactions: sambocrockett
Upvote 0
No worries man I dont take any offense. Most of my experience in programming is using c++ and some limited experience using c# and some others but Im really new to java and android.

I do understand there is a learning curve but I am totally impatient :p and I know that by looking at other examples and peoples code will help over time but Im kind of in a rush to make my first app before I graduate in oct. so I have something to show in android that I developed.

What I want from my app is to have it open up and display pictures that I have stored in an array. I want two buttons on the bottom of the screen, one for next and one for previous. In the future I want a button to take the user to a web page related to the picture that they are looking at and eventually to have a small widget that displays a twitter feed related to each picture. Im sure if you look at my code youll see that I plan on having pictures of girls to look at ;)

Im viewing this on my phone so once I get home and have some time I will repost my code like you asked but in the mean time do you have any suggestions on which class I should be looking at to perform such an action? I will do any reading and research i need to do in order to learn because I really want to be in mobile application development eventually.

Much thanks
 
Upvote 0
Yeah i actually tried that tutorial and it wouldnt work at first and then i found a forum where some guys where talking about how since 1.5 something in the java code didnt work anymore.

heres a link to a post that shows what Im talking about

Solution for Android Tutorial Hello Gallery problem

But they did post something that did work and I tried that but I cant figure out how to enable a onClickListener to work with that code so that when you click on one of the tiles the picture opens up on the screen in full size. Any ideas on that would be greatly appreciated.
 
Upvote 0
I just tried that gallery tutorial.
They really ought to provide all the source code as it's a bit confusing, and is bound to cause frustration for people who are trying to learn. (Which must be their target audience.)

I got it working though.

It gives you a horizontal scrolling list of images across the top of the screen.
You could add another ImageView underneath to show a larger version of the selected picture. From that it would be fairly straightforward to change the bigger image from inside the OnItemClickListener handler.

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"
   android:keepScreenOn="true"
   android:background="#000000"
   >


<Gallery xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/gallery"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
/>

<ImageView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/image"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:src="@drawable/screenshot"
/>

</LinearLayout>


If you want the selected image to take over the whole screen then you might have to start another activity. That activity will have a separate layout xml file with just a single ImageView.

Mark
 
Upvote 0
do you think that its possible to create the whole app from just xml and avoid the java. Lately Ive been reading this book


Amazon.com: Professional Android 2 Application Development (Wrox&#133;


and it looks like I can accomplish most of what I want to do in xml. In your experience do you know if that is possible. I would love to be able to use both as Im really trying to learn all things android :) but its really kicking my but lately.
 
Upvote 0
here is my (not so clean and clear) java code again.


Code:
package test.button;

import android.app.Activity;
import android.os.Bundle;
import android.text.InputType;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.LinearLayout;


public class button extends Activity {
/** Called when the activity is first created. */

Button myButton;
EditText myText;
protected int count;
int i = 0;
long pictures[] = {R.drawable.hot_girls, R.drawable.hillarious_post, };
int numGirls = 2;

@Override
LinearLayout mLinearLayout;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    // Create a LinearLayout in which to add the ImageView
    mLinearLayout = new LinearLayout(this);

    // Instantiate an ImageView and define its properties
    ImageView i = new ImageView(this);
    i.setImageResource(R.drawable.icon);
    i.setAdjustViewBounds(true); // set the ImageView bounds to match the Drawable's dimensions
    i.setLayoutParams(new Gallery.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

    // Add the ImageView to the layout and set the layout as the content view
    mLinearLayout.addView(i);
    setContentView(mLinearLayout);
}
//public void onCreate(Bundle savedInstanceState) {
//super.onCreate(savedInstanceState);

//getWindow().setBackgroundDrawableResource(R.drawable.icon) ;

myButton = (Button) findViewById (R.id.my_button);
myText = (EditText) findViewById (R.id.my_text);

myText.setSingleLine();
myText.setInputType(InputType.TYPE_NULL);

myButton.setOnClickListener(new OnClickListener(){

@Override
public void onClick(View arg0) {
myText.setText("youve touched the screen"+ ++count +" times");
if (i < (numGirls - 1))
{
   i++;
   getWindow().setBackgroundDrawableResource((int) pictures[i]) ;
}
else

{
i = 0;
getWindow().setBackgroundDrawableResource((int) pictures[i]) ;
}


}



});


}
}

Im getting errors at

@Override

and also

myButton = (Button) findViewById (R.id.my_button);



and heres my main xml code

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"
    android:padding="4px">

    
<EditText 
android:id="@+id/my_text" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content">

</EditText>

<Button android:text="@string/button_text" 
android:id="@+id/my_button" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content">

</Button>


</LinearLayout>

and finally here is the android manifest
Code:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="test.button"
      android:versionCode="1"
      android:versionName="1.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".button"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

    </application>
    <uses-sdk android:minSdkVersion="3" />

</manifest>
 
Upvote 0
do you think that its possible to create the whole app from just xml and avoid the java.

Using the standard SDK you will have to use Java. Although you can do layouts in XML, you need to be able to react to events (button presses, etc), and that requires Java.

If you want to avoid Java you could try the Android App Inventor.
App Inventor for Android

It's early days, and they seem to be rolling it out slowly.
You can apply to use it, but it seems to take a while for your application to be processed, from what I've seen.

I found a discussion here on alternatives to Java
Alternatives to Java for Android development? - Stack Overflow

That was with a quick google search. You'll probably be able to find others.

Another alternative would be to stick with Java, but try learning it away from Android.
Just get the basics under your belt first. Language basics, Classes, some Object Oriented basics, Files, I/O, etc...

Mark
 
Upvote 0
here is my (not so clean and clear) java code again.

Sorry to be blunt, but you really do need to go back to the Java books. :-(
The compilation errors you're getting show that you need to concentrate on learning Java. Get to grips with the basics, such as the basic syntax, how to write methods, etc.

A forum is a good place to ask questions about specific problems, but I personally don't think it's going to be better than a book when it comes to learning the language fundamentals.

It will be a very long and slow process to try to learn Java on this forum, if people have to help you to fix compilation errors. Once you've got past that stage there's still a lot to learn, and you could still be trying to get this app working in several weeks time. :-(

If you're trying to learn Java and Android development, then there really is no substitute for sitting down with a book and doing it properly. I understand that you're impatient to get started. But it'll be quicker in the end to work through the books, rather than keep struggling without the right foundations.

If you're not trying to learn, but just want the app written, you could just ask someone. Maybe someone will write it for you?

Again, sorry to be negative. I'm sure you'll get there in the end, but I really think you need a different approach.

Mark
 
Upvote 0
Well I already understand the basics of OOP so what I feel I need is just to keep seeing app source code so I understand how it all works in Java. I really do want to learn but I know that from my experience I learn faster from seeing actual practical code so I can see what is going on in real world use and not half the simple stuff in books. I will keep reading and researching but if I can get some one to just show me what Im missing that would be even better. I feel like why should I try to climb a rope when some one can let me into the elevator.....know what I mean ;)
 
Upvote 0
It's probably that @Override needs to go right before the function that you want to override, without the linear layout declaration in between.

You use setcontentview(mLinearLayout) which changes your main view to the linear layout you created. It unloads the main.xml which has the button you're trying to assign to mybutton. I think you should add mLinearLayout to the main view instead.
 
Upvote 0
The compilation errors are a little more fundamental.

If you look at these lines:
Code:
myButton = (Button) findViewById (R.id.my_button);
myText = (EditText) findViewById (R.id.my_text);

myText.setSingleLine();
myText.setInputType(InputType.TYPE_NULL);

myButton.setOnClickListener(new OnClickListener(){

You'll see that they're outside of any method.

The start of the method declaration has been commented out.

Code:
//public void onCreate(Bundle savedInstanceState) {
//super.onCreate(savedInstanceState);

//getWindow().setBackgroundDrawableResource(R.drawable.icon) ;

(I did copy/paste the code into Eclipse to check I wasn't mis-reading the layout & braces.)

That's why I suggested that it might be a good idea to become more familiar with the basic Java syntax, as they're really pretty basic errors. And I think this stuff needs to be learned properly before moving on to more complex stuff.

Mark
 
Upvote 0
Hey thanks for the help guys. I havent got to my computer where I have my eclipse set up but when I do Ill try and take a look at your suggestions and post back to let you know if I figured it out or not. I really do appreciate any help and suggestions.

Hey mark,

Do you have any suggestions as to any android/java books for beginners. I have so many but I noticed that most of them except for 4 were written previous of the 1.6 release so Im having trouble finding current examples that dont give me compile errors. I know that knowing java in and out would probably help but I litterally just started picking it up about 3 months ago when I started making this app and I usually dont have the time I would like to dedicate to my Java/Android skills hence the forum posting.
 
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