1

How can I add one TextView with the image in this.I want to add a TextView below the imageView.

   public View getView(int position, View convertView, ViewGroup parent) {
     ImageView imageView;

   if (convertView == null) {
        imageView = new ImageView(mContext);
       imageView.setLayoutParams(new GridView.LayoutParams(400, 400));
       imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        imageView.setPadding(8, 8, 8, 8);
        } else {
      imageView = (ImageView) convertView;
   }

     imageView.setImageResource(mThumbIds[position]);

     return imageView;
    }
5
  • 1
    why don't you use the xml editor and use the an inflater to retrive the java's object? Commented Nov 7, 2015 at 10:21
  • Just trying to do it other way.Exploring the possibilites. :) Commented Nov 7, 2015 at 10:26
  • Instead of creating an Image view first create a vertical linear layout, then add the image view and then the second view. This second will will be under the image view then. Commented Nov 7, 2015 at 10:27
  • We can use java code to make a layout and Java code needed when we want to create dynamic runtime view. Otherwise it would be better to use XML only Commented Nov 7, 2015 at 10:28
  • Thanks guys.Really nice of you. Commented Nov 7, 2015 at 10:30

2 Answers 2

1
     LinearLayout LL = new LinearLayout(this);
     LL.setOrientation(LinearLayout.VERTICAL);

     LayoutParams LLParams = new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT);

     LL.setLayoutParams(LLParams);

           ImageView imageView = new ImageView(mContext);

           //....your imageview code.......


           TextView textView = new TextView(mContext);

           //....your textview code.......


     LL.addView(imageview);
     LL.addView(textview);
Sign up to request clarification or add additional context in comments.

Comments

0

You should use the xml layout editor to create your Grid's row and use an inflater to retrive the View Java's object. Programmatically, something like

LinearLayout parent = new LinearLayout(context);

parent.setLayoutParams(new GridView.LayoutParams(400, 400));
parent.setOrientation(LinearLayout.VERTICAL);

TextView textView = new TextView(context);
textView.setId(1)
ImageView imageView = new ImageView(mContext);
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(8, 8, 8, 8);
imageView.setId(0);

parent.addView(imageView);
parent.addView(textView);

should do it.

parent is what your getView will return. Of course you will have to change the cast from ImageView to LinearLayout. you can use findViewById with 0 and 1, to retrieve respectively the ImageView and the TextView, or getChildAt(0) and getChildAt(1) to do the same thing

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.