Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8689707
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T23:43:20+00:00 2026-06-12T23:43:20+00:00

Here is my code in my Activity: public class GridViewActivity extends Activity { GridView

  • 0

Here is my code in my Activity:

public class GridViewActivity extends Activity {

    GridView gridView;

    static final String[] MOBILE_OS = new String[] { "Android", "iOS",
            "Windows", "Blackberry" };

    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        gridView = (GridView) findViewById(R.id.gridView1);

        gridView.setAdapter(new ImageAdapter(this, MOBILE_OS));

        gridView.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View v,
                    int position, long id) {
                Toast.makeText(
                        getApplicationContext(),
                        ((TextView) v.findViewById(R.id.grid_item_label))
                                .getText(), Toast.LENGTH_SHORT).show();

            }
        });

    }

}

And in my Image Adapter:

public class ImageAdapter extends BaseAdapter {
    private Context context;
    private final String[] mobileValues;

    private TextView t;
    public ImageAdapter(Context context, String[] mobileValues) {
        this.context = context;
        this.mobileValues = mobileValues;
    }

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

        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        View gridView;

        if (convertView == null) {

            gridView = new View(context);

            // get layout from mobile.xml
            gridView = inflater.inflate(R.layout.mobile, null);




            // set value into textview
            TextView textView = (TextView) gridView
                    .findViewById(R.id.grid_item_label);
            textView.setText(mobileValues[position]);
            Button b = (Button) gridView.findViewById(R.id.grid_item_button);
            Button b2 = (Button) gridView.findViewById(R.id.grid_item_button2);
            t = (TextView) gridView
                    .findViewById(R.id.grid_item_number);
            b.setOnClickListener(new MyOnClickListener(t));
            b2.setOnClickListener(new MyOnClickListenerm(t));
            // set image based on selected text
            ImageView imageView = (ImageView) gridView
                    .findViewById(R.id.grid_item_image);

            String mobile = mobileValues[position];

            if (mobile.equals("Windows")) {
                imageView.setImageResource(R.drawable.windows_logo);
            } else if (mobile.equals("iOS")) {
                imageView.setImageResource(R.drawable.ios_logo);
            } else if (mobile.equals("Blackberry")) {
                imageView.setImageResource(R.drawable.blackberry_logo);
            } else {
                imageView.setImageResource(R.drawable.android_logo);
            }

        } else {
            gridView = (View) convertView;
        }

        return gridView;
    }

    private void clickedButton(TextView tv){

        int num = Integer.parseInt(tv.getText().toString());
        ++num;
        tv.setText(Integer.toString(num));

    }
    private void clickedButtonm(TextView tv){

        int num = Integer.parseInt(tv.getText().toString());
        if(num>0){
            --num;
            tv.setText(Integer.toString(num));
        }
    }
    @Override
    public int getCount() {
        return mobileValues.length;
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }
    class MyOnClickListener implements OnClickListener{

        public final TextView tv;

        public MyOnClickListener(TextView tv){
            this.tv=tv;
        }
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            clickedButton(tv);
        }

    }
    class MyOnClickListenerm implements OnClickListener{

        public final TextView tv;

        public MyOnClickListenerm(TextView tv){
            this.tv=tv;
        }
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            clickedButtonm(tv);
        }

    }

}

and my mobile.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:padding="5dp" >

    <ImageView
        android:id="@+id/grid_item_image"
        android:layout_width="wrap_content"
        android:layout_height="74dp"
        android:layout_marginRight="10px"
        android:layout_weight="1.44"
        android:src="@drawable/android_logo" >
    </ImageView>


    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:orientation="vertical" >

        <Button
            android:id="@+id/grid_item_button"
            android:layout_width="30dp"
            android:layout_height="23dp"
            android:text=" " />

        <TextView
            android:id="@+id/grid_item_number"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="0" />

          <Button
            android:id="@+id/grid_item_button2"
            android:layout_width="30dp"
            android:layout_height="23dp"
            android:text=" " />
    </LinearLayout>

    <TextView
        android:id="@+id/grid_item_label"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="5px"
        android:text="@+id/label"
        android:textSize="15px" >
    </TextView>

</LinearLayout>

I find it a bit troubling not to be able to pout 2 items per row. I would like to know how to format my GridView to be able to fulfill such idea

Here is my source code: http://www.mediafire.com/?25vae87fvahu4wp

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T23:43:22+00:00Added an answer on June 12, 2026 at 11:43 pm

    Use the android:numColumns="2" in your GridView widget

    <?xml version="1.0" encoding="utf-8"?>
    <GridView xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/grid"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:numColumns="2"
        android:verticalSpacing="10dp"
        android:horizontalSpacing="10dp"
        android:scrollbarStyle="outsideOverlay"
        android:verticalScrollbarPosition="right"
        android:scrollbars="vertical">
    </GridView>
    

    You can get familiar with Android UI in this link.Try to focus on the GridView Topic

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here is my code... package com.bmc; public class Details extends Activity { public void
I have the following Activity: public class StartActivity extends Activity { String str =
Here is the class i am passing to public class AxxessCCPAccountDetails extends Activity {
Here is my code import org.ksoap2.*; import org.ksoap2.serialization.*; import org.ksoap2.transport.*; import android.app.Activity; import android.os.Bundle;
Here is my intent-filter code... <activity android:name=IntentReceiver> <intent-filter> <action android:name=android.nfc.action.TAG_DISCOVERED/> <category android:name=android.intent.category.DEFAULT /> <data
Here a code to demonstrate an annoying problem: class A { public: A(): m_b(1),
I have this code here: public class clsDataLayer { // This function saves the
Here is the code Variable declared public String gpu2dcurent = 1234567; Asynctask, after finished
Here is my code: chartDialog = new Dialog(item_activity.this); chartDialog.setContentView(R.layout.customlayout); chartDialog.setTitle(Title ); TextView textItemDetailsDialog =
Here I'm gettimg memory allocation problem at activity indicator and my code is: -

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.