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


Last Updated:

  1. sambocrockett

    sambocrockett Member This Topic's Starter

    Joined:
    Jun 8, 2010
    Messages:
    22
    Likes Received:
    1
    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.
     

    Advertisement
  2. markb

    markb Well-Known Member

    Joined:
    Sep 14, 2009
    Messages:
    148
    Likes Received:
    26
    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:

    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
     
    sambocrockett likes this.
  3. sambocrockett

    sambocrockett Member This Topic's Starter

    Joined:
    Jun 8, 2010
    Messages:
    22
    Likes Received:
    1
    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
     
  4. markb

    markb Well-Known Member

    Joined:
    Sep 14, 2009
    Messages:
    148
    Likes Received:
    26
  5. sambocrockett

    sambocrockett Member This Topic's Starter

    Joined:
    Jun 8, 2010
    Messages:
    22
    Likes Received:
    1
    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.
     
  6. markb

    markb Well-Known Member

    Joined:
    Sep 14, 2009
    Messages:
    148
    Likes Received:
    26
    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 (Text):
    1.  
    2. <?xml version="1.0" encoding="utf-8"?>
    3. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    4.    android:orientation="vertical"
    5.    android:layout_width="fill_parent"
    6.    android:layout_height="fill_parent"
    7.    android:keepScreenOn="true"
    8.    android:background="#000000"
    9.    >
    10.  
    11.  
    12. <Gallery xmlns:android="http://schemas.android.com/apk/res/android"
    13.     android:id="@+id/gallery"
    14.     android:layout_width="fill_parent"
    15.     android:layout_height="wrap_content"
    16. />
    17.  
    18. <ImageView xmlns:android="http://schemas.android.com/apk/res/android"
    19.     android:id="@+id/image"
    20.     android:layout_width="fill_parent"
    21.     android:layout_height="wrap_content"
    22.     android:src="@drawable/screenshot"
    23. />
    24.  
    25. </LinearLayout>
    26.  

    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
     
  7. sambocrockett

    sambocrockett Member This Topic's Starter

    Joined:
    Jun 8, 2010
    Messages:
    22
    Likes Received:
    1
    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.
     
  8. jonbonazza

    jonbonazza Well-Known Member

    Joined:
    Jul 13, 2010
    Messages:
    1,934
    Likes Received:
    458
  9. sambocrockett

    sambocrockett Member This Topic's Starter

    Joined:
    Jun 8, 2010
    Messages:
    22
    Likes Received:
    1
    here is my (not so clean and clear) java code again.


    Code (Text):
    1.  
    2. package test.button;
    3.  
    4. import android.app.Activity;
    5. import android.os.Bundle;
    6. import android.text.InputType;
    7. import android.view.View;
    8. import android.view.View.OnClickListener;
    9. import android.view.ViewGroup.LayoutParams;
    10. import android.widget.Button;
    11. import android.widget.EditText;
    12. import android.widget.Gallery;
    13. import android.widget.ImageView;
    14. import android.widget.LinearLayout;
    15.  
    16.  
    17. public class button extends Activity {
    18. /** Called when the activity is first created. */
    19.  
    20. Button myButton;
    21. EditText myText;
    22. protected int count;
    23. int i = 0;
    24. long pictures[] = {R.drawable.hot_girls, R.drawable.hillarious_post, };
    25. int numGirls = 2;
    26.  
    27. @Override
    28. LinearLayout mLinearLayout;
    29.  
    30. protected void onCreate(Bundle savedInstanceState) {
    31.     super.onCreate(savedInstanceState);
    32.     setContentView(R.layout.main);
    33.     // Create a LinearLayout in which to add the ImageView
    34.     mLinearLayout = new LinearLayout(this);
    35.  
    36.     // Instantiate an ImageView and define its properties
    37.     ImageView i = new ImageView(this);
    38.     i.setImageResource(R.drawable.icon);
    39.     i.setAdjustViewBounds(true); // set the ImageView bounds to match the Drawable's dimensions
    40.     i.setLayoutParams(new Gallery.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
    41.  
    42.     // Add the ImageView to the layout and set the layout as the content view
    43.     mLinearLayout.addView(i);
    44.     setContentView(mLinearLayout);
    45. }
    46. //public void onCreate(Bundle savedInstanceState) {
    47. //super.onCreate(savedInstanceState);
    48.  
    49. //getWindow().setBackgroundDrawableResource(R.drawable.icon) ;
    50.  
    51. myButton = (Button) findViewById (R.id.my_button);
    52. myText = (EditText) findViewById (R.id.my_text);
    53.  
    54. myText.setSingleLine();
    55. myText.setInputType(InputType.TYPE_NULL);
    56.  
    57. myButton.setOnClickListener(new OnClickListener(){
    58.  
    59. @Override
    60. public void onClick(View arg0) {
    61. myText.setText("youve touched the screen"+ ++count +" times");
    62. if (i < (numGirls - 1))
    63. {
    64.    i++;
    65.    getWindow().setBackgroundDrawableResource((int) pictures[i]) ;
    66. }
    67. else
    68.  
    69. {
    70. i = 0;
    71. getWindow().setBackgroundDrawableResource((int) pictures[i]) ;
    72. }
    73.  
    74.  
    75. }
    76.  
    77.  
    78.  
    79. });
    80.  
    81.  
    82. }
    83. }
    84.  
    Im getting errors at

    @Override

    and also

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



    and heres my main xml code

    Code (Text):
    1.  
    2. <?xml version="1.0" encoding="utf-8"?>
    3. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    4.     android:orientation="vertical"
    5.     android:layout_width="fill_parent"
    6.     android:layout_height="fill_parent"
    7.     android:padding="4px">
    8.  
    9.    
    10. <EditText
    11. android:id="@+id/my_text"
    12. android:layout_width="fill_parent"
    13. android:layout_height="wrap_content">
    14.  
    15. </EditText>
    16.  
    17. <Button android:text="@string/button_text"
    18. android:id="@+id/my_button"
    19. android:layout_width="fill_parent"
    20. android:layout_height="wrap_content">
    21.  
    22. </Button>
    23.  
    24.  
    25. </LinearLayout>
    26.  
    and finally here is the android manifest
    Code (Text):
    1.  
    2. <?xml version="1.0" encoding="utf-8"?>
    3. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
    4.       package="test.button"
    5.       android:versionCode="1"
    6.       android:versionName="1.0">
    7.     <application android:icon="@drawable/icon" android:label="@string/app_name">
    8.         <activity android:name=".button"
    9.                   android:label="@string/app_name">
    10.             <intent-filter>
    11.                 <action android:name="android.intent.action.MAIN" />
    12.                 <category android:name="android.intent.category.LAUNCHER" />
    13.             </intent-filter>
    14.         </activity>
    15.  
    16.     </application>
    17.     <uses-sdk android:minSdkVersion="3" />
    18.  
    19. </manifest>
    20.  
     
  10. markb

    markb Well-Known Member

    Joined:
    Sep 14, 2009
    Messages:
    148
    Likes Received:
    26
    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
     
  11. markb

    markb Well-Known Member

    Joined:
    Sep 14, 2009
    Messages:
    148
    Likes Received:
    26
    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
     
  12. sambocrockett

    sambocrockett Member This Topic's Starter

    Joined:
    Jun 8, 2010
    Messages:
    22
    Likes Received:
    1
    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 ;)
     
  13. cp1

    cp1 Well-Known Member

    Joined:
    Apr 30, 2010
    Messages:
    375
    Likes Received:
    35
    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.
     
  14. markb

    markb Well-Known Member

    Joined:
    Sep 14, 2009
    Messages:
    148
    Likes Received:
    26
    The compilation errors are a little more fundamental.

    If you look at these lines:
    Code (Text):
    1.  
    2. myButton = (Button) findViewById (R.id.my_button);
    3. myText = (EditText) findViewById (R.id.my_text);
    4.  
    5. myText.setSingleLine();
    6. myText.setInputType(InputType.TYPE_NULL);
    7.  
    8. myButton.setOnClickListener(new OnClickListener(){
    9.  
    You'll see that they're outside of any method.

    The start of the method declaration has been commented out.

    Code (Text):
    1.  
    2. //public void onCreate(Bundle savedInstanceState) {
    3. //super.onCreate(savedInstanceState);
    4.  
    5. //getWindow().setBackgroundDrawableResource(R.drawable.icon) ;
    6.  
    (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
     
  15. cp1

    cp1 Well-Known Member

    Joined:
    Apr 30, 2010
    Messages:
    375
    Likes Received:
    35
    Oh I missed the end bracket of the oncreate method -- take that out
     
  16. sambocrockett

    sambocrockett Member This Topic's Starter

    Joined:
    Jun 8, 2010
    Messages:
    22
    Likes Received:
    1
    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.
     
  17. cp1

    cp1 Well-Known Member

    Joined:
    Apr 30, 2010
    Messages:
    375
    Likes Received:
    35
    I haven't used any android/java books -- I'm pretty much all self taught from web resources and just adapting what I know from C/C++ a long time ago. I did use some source code from this site: CommonsWare that worked really well, but I don't know how good the actual books are.
     

Share This Page

Loading...