0

I'm trying to put an horizontal LinearLayout (with code) into a vertical LinearLayout(which is actually on the xml file). Well, when I do that, it seems like it doesn't create the horizontal Linear, I post my code and an image:

MyCameraActivity

    package com.example.android.navigationdrawer;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Surface;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

public class MyCameraActivity extends AppCompatActivity {


    private static final int CAMERA_REQUEST = 1888;



    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.content_my_camera);
        ImageView photoButton = (ImageView) this.findViewById(R.id.button1);


        //OnClick camera button
        photoButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                startActivityForResult(cameraIntent, CAMERA_REQUEST);
            }
        });
    }

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) {

            //Cast LinearLayout of .xml
            LinearLayout CamaraLayout = (LinearLayout) findViewById(R.id.CameraLayout);

            //Get rotation
            int rotation =  getWindowManager().getDefaultDisplay().getRotation();

            //Number of photos

            int PhotoNumber = 0;

            //CREATE LINEAR LAYOUT
            LinearLayout LL = new LinearLayout(this);
            LL.setOrientation(LinearLayout.HORIZONTAL);
            LinearLayout.LayoutParams LLParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            LL.setLayoutParams(LLParams);


            int angle = 0;
            switch (rotation) {
                case Surface.ROTATION_90:
                    angle = -90;

                    PhotoNumber = PhotoNumber+1;

                    if (PhotoNumber == 6) {

                        //Set LinearLayout params
                        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
                        lp.setMargins(0, 5, 7, 5);


                        //Create imageView to put Bitmap
                        ImageView imageView = new ImageView(this);

                        //Put LayoutParams
                        imageView.setLayoutParams(lp);

                        //Adding the Bitmap
                        Bitmap photo = (Bitmap) data.getExtras().get("data");
                        imageView.setImageBitmap(photo);


                        //Add image to Linear Layout
                        LL.addView(imageView);

                        //Add the Linear Layout  to "CamaraLayout"
                        CamaraLayout.addView(LL);

                    } else {

                        //Set LinearLayout params
                        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
                        lp.setMargins(7, 5, 7, 5);


                        //Create imageView to put Bitmap
                        ImageView imageView = new ImageView(this);

                        //Put LayoutParams
                        imageView.setLayoutParams(lp);

                        //Adding the Bitmap
                        Bitmap photo = (Bitmap) data.getExtras().get("data");
                        imageView.setImageBitmap(photo);


                        //Add image to Linear Layout
                        CamaraLayout.addView(imageView);
                    }
                    break;


                case Surface.ROTATION_180:
                    angle = 180;

                    PhotoNumber = PhotoNumber+1;

                    if (PhotoNumber == 4) {

                        //Set LinearLayout params
                        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
                        lp.setMargins(0, 5, 7, 5);


                        //Create imageView to put Bitmap
                        ImageView imageView = new ImageView(this);

                        //Put LayoutParams
                        imageView.setLayoutParams(lp);

                        //Adding the Bitmap
                        Bitmap photo = (Bitmap) data.getExtras().get("data");
                        imageView.setImageBitmap(photo);


                        //Add image to Linear Layout
                        LL.addView(imageView);

                        //Add the Linear Layout  to "CamaraLayout"
                        CamaraLayout.addView(LL);

                    } else {

                        //Set LinearLayout params
                        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
                        lp.setMargins(7, 5, 7, 5);


                        //Create imageView to put Bitmap
                        ImageView imageView = new ImageView(this);

                        //Put LayoutParams
                        imageView.setLayoutParams(lp);

                        //Adding the Bitmap
                        Bitmap photo = (Bitmap) data.getExtras().get("data");
                        imageView.setImageBitmap(photo);


                        //Add image to Linear Layout
                        CamaraLayout.addView(imageView);
                    }
                    break;




                case Surface.ROTATION_270:
                    angle = 90;

                    PhotoNumber = PhotoNumber+1;

                    if (PhotoNumber == 6) {

                        //Set LinearLayout params
                        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
                        lp.setMargins(0, 5, 7, 5);


                        //Create imageView to put Bitmap
                        ImageView imageView = new ImageView(this);

                        //Put LayoutParams
                        imageView.setLayoutParams(lp);

                        //Adding the Bitmap
                        Bitmap photo = (Bitmap) data.getExtras().get("data");
                        imageView.setImageBitmap(photo);


                        //Add image to Linear Layout
                        LL.addView(imageView);

                        //Add the Linear Layout  to "CamaraLayout"
                        CamaraLayout.addView(LL);

                    } else {

                        //Set LinearLayout params
                        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
                        lp.setMargins(7, 5, 7, 5);


                        //Create imageView to put Bitmap
                        ImageView imageView = new ImageView(this);

                        //Put LayoutParams
                        imageView.setLayoutParams(lp);

                        //Adding the Bitmap
                        Bitmap photo = (Bitmap) data.getExtras().get("data");
                        imageView.setImageBitmap(photo);


                        //Add image to Linear Layout
                        CamaraLayout.addView(imageView);
                    }
                    break;

                default:
                    angle = 0;
                    PhotoNumber = PhotoNumber+1;

                    if (PhotoNumber == 4) {

                        //Set LinearLayout params
                        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
                        lp.setMargins(0, 5, 7, 5);


                        //Create imageView to put Bitmap
                        ImageView imageView = new ImageView(this);

                        //Put LayoutParams
                        imageView.setLayoutParams(lp);

                        //Adding the Bitmap
                        Bitmap photo = (Bitmap) data.getExtras().get("data");
                        imageView.setImageBitmap(photo);


                        //Add image to Linear Layout
                        LL.addView(imageView);

                        //Add the Linear Layout  to "CamaraLayout"
                        CamaraLayout.addView(LL);

                    } else {

                        //Set LinearLayout params
                        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
                        lp.setMargins(7, 5, 7, 5);


                        //Create imageView to put Bitmap
                        ImageView imageView = new ImageView(this);

                        //Put LayoutParams
                        imageView.setLayoutParams(lp);

                        //Adding the Bitmap
                        Bitmap photo = (Bitmap) data.getExtras().get("data");
                        imageView.setImageBitmap(photo);


                        //Add image to Linear Layout
                        CamaraLayout.addView(imageView);
                    }
                    break;
            }











        }
    }

}

I don't post the .xml, because it's only a LinearLayout and an ImageView.

What it does

And I want the images to be displayed one next to each other.

Some of you may have thought the idea of directly making an horizontal LinearLayout, and that works, but I'm working on a program that puts different "lines" of photos.

Any idea? thanks!

Adir
  • 11
  • 1
  • 4

1 Answers1

0

I'm not sure if your code is inside a loop or not. From what I'm guessing. Your code is inside a loop and you are adding the whole horizontal layout with 1 image 1 by 1 on your vertical layout. You should build first your horizontal layout by adding all images on it before eventually adding it on your vertical layout. That being said. Using your code. You may change it to:

LinearLayout CamaraLayout = (LinearLayout) findViewById(R.id.CameraLayout);

//CREATE LINEAR LAYOUT
LinearLayout LL = new LinearLayout(this);
LL.setOrientation(LinearLayout.HORIZONTAL);
LinearLayout.LayoutParams LLParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
LL.setLayoutParams(LLParams);

foreach()//or some loop
{
    //Number of photos
    int PhotoNumber = 0;

    //Set LinearLayout params
    LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        lp.setMargins(0, 5, 7, 5);


    //Create imageView to put Bitmap
    ImageView imageView = new ImageView(this);

    //Put LayoutParams
    imageView.setLayoutParams(lp);

    //Adding the Bitmap
    Bitmap photo = (Bitmap) data.getExtras().get("data");
    imageView.setImageBitmap(photo);


    //Add image to Linear Layout
    LL.addView(imageView);
}
//Add the Linear Layout  to "CamaraLayout"
CamaraLayout.addView(LL);
Luke Villanueva
  • 2,030
  • 8
  • 44
  • 94
  • Still having the error. Edited question with the full activity code – Adir May 23 '16 at 16:37
  • Please show what you really want to accomplish. Because on your code, you're only adding 1 view on your layout. So practically if you want to make the images display horizontally, you just need a single horizontal layout, make its height and width to "match_parent" and let it auto adjust as you add more views into it. – Luke Villanueva May 24 '16 at 00:50
  • I don't have an error, I just don't know how to do what I want to; simple gallery. My idea is: A vertical linear layout, wich includes an horizontal linear layout wich includes as much photos as possible. When it can't have more photos, it creates another horizontal linear layout that contains photos, etc... @ljpv14 – Adir May 24 '16 at 05:42
  • Then the only solution is make your outer most layout as horizontal linear layout and add views to it. Set its width and height as match_parent. – Luke Villanueva May 24 '16 at 06:04
  • Yup, tried this, but I can only take a number of photos, and I wanna insert a ilimitate number of photos @ljpv14 any suggestion? I've thought on setting 1rst linear to horizontal and when it's full, creating another. But it would be set next to the last image, so it's not worth it – Adir May 24 '16 at 06:26
  • Check this link http://stackoverflow.com/questions/2961777/android-linearlayout-horizontal-with-wrapping-children – Luke Villanueva May 24 '16 at 06:45
  • didn't help @ljpv14... Do you know how could I do what I want? – Adir May 24 '16 at 15:18