0

I am creating an application where there is a homescreen. I am trying to use a gridview to populate 5 images and a textview under each images. Imageviews height and width should be equal. When in portrait mode there should be 2 columns and in landscape mode there should be 3 columns. In both the modes, maximum of the screen space should be utilised and the user shouldn't have to scroll the screen to see the contents. I tried like,

//adapter

    public View getView(final int position, final View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    int resource = context.getResources().getIdentifier("status_bar_height", "dimen", "android");
    int statusBarHeight = 0;
    if (resource > 0) {
        statusBarHeight = context.getResources().getDimensionPixelSize(resource);
    }

    Holder holder=new Holder();
    View rowView;

    rowView = inflater.inflate(R.layout.homescreenitems, null);
    holder.tv=(TextView) rowView.findViewById(R.id.textView1);
    holder.img=(ImageView) rowView.findViewById(R.id.imageView1);

    holder.tv.setText(result[position]);
    holder.img.setImageResource(imageId[position]);

    DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
    int ImageWidth = displayMetrics.widthPixels;
    int ImageHeight = displayMetrics.heightPixels;
    android.view.ViewGroup.LayoutParams layoutParamsImage = holder.img.getLayoutParams();
    android.view.ViewGroup.LayoutParams layoutParamsTextView = holder.tv.getLayoutParams();
    if (ImageHeight > ImageWidth) {
        int ImageWidthHeight = (ImageHeight / 3) - 20 - statusBarHeight - 40;
        layoutParamsImage.width = ImageWidthHeight;
        layoutParamsImage.height = ImageWidthHeight;
        holder.img.setLayoutParams(layoutParamsImage);

        layoutParamsTextView.width = ImageWidth / 2;
        holder.tv.setLayoutParams(layoutParamsTextView);
    }else  {
        int ImageWidthHeight = (ImageHeight / 2) - 20 - statusBarHeight - 60;
        layoutParamsImage.width = ImageWidthHeight;
        layoutParamsImage.height = ImageWidthHeight;
        holder.img.setLayoutParams(layoutParamsImage);

        layoutParamsTextView.width = ImageWidth / 3;
        holder.tv.setLayoutParams(layoutParamsTextView);
    }




    rowView.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

        }
    });

    return rowView;
}

//Activity

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_homescreen);

    getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
    getSupportActionBar().setCustomView(R.layout.abs_layout);
    TextView textViewTitle = (TextView) findViewById(R.id.mytext);
    textViewTitle.setText("Home");
    gv=(GridView) findViewById(R.id.gridView1);
    DisplayMetrics displayMetrics = this.getResources().getDisplayMetrics();
    int ImageWidth = displayMetrics.widthPixels;
    int ImageHeight = displayMetrics.heightPixels;
    if (ImageHeight > ImageWidth) {
        gv.setNumColumns(2);
    }else  {
        gv.setNumColumns(3);
    }
    gv.setAdapter(new homescreencustomadapter(this, iconNames,iconImages,this));
}

The problem is, some of the content at the bottom is visible only if user scrolls the screen. Please help me.

iOSMAn
  • 46
  • 1
  • 14
  • Possible duplicate of [How can I force a GridView to use the whole screen (regardless of display size)?](http://stackoverflow.com/questions/5690144/how-can-i-force-a-gridview-to-use-the-whole-screen-regardless-of-display-size) – Devendra Singh Sep 07 '16 at 11:00
  • @DevendraSingh, may be this is a duplicate question, but the solutions given in that question are not "solutions". – iOSMAn Sep 07 '16 at 11:07

0 Answers0