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

  • Home
  • SEARCH
  • 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 8938399
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T10:36:58+00:00 2026-06-15T10:36:58+00:00

My activity contains a textview and under that it has gridview elements. Stage 1:

  • 0

My activity contains a textview and under that it has gridview elements. Stage 1: When the activity starts, it adjusts the elements in the order that I have set(like item1, item2,..) stage1 picture. Stage 2: But when I click on edittext, the gridview elements get adjusted to allocate space for the softkeypad stage2 picture. Stage 3: So when I press back button, the soft keypad disappears(as usual), but the order of gridview elements are getting changed stage3 picture. The order of child items of gridview changes. Can anyone please suggest me how to avoid this uncertainty in the order? Below is my entire code and xml file, though I think these are not that much necessary to solve this issue.

public class PresentActivity extends Activity {

GridView gridView;

    static final String[] TEXT = new String[] { 
        "item1", "item2","item3", "item4", "item5","item6","item7","item8" };


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        setGridView();        
    }


    public void setGridView()
    {
        gridView = (GridView)findViewById(R.id.gridView1);

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

        gridView.setNumColumns(3);

        gridView.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View v,
                    int position, long id) {

                String currentText=(String) ((TextView)v.findViewById(R.id.grid_item_label)).getText();

    Toast.makeText(getApplicationContext(), currentText, Toast.LENGTH_SHORT).show();


            }
        });  

    }


}

Here is my custom adapter class.

public class ImageAdapter extends BaseAdapter{

    private Context context;
    private final String[] textValues;

    public ImageAdapter(Context context, String[] textValues) {
        this.context = context;
        this.textValues = textValues;
    }

    @Override
    public int getCount() {

        return textValues.length;
    }

    @Override
    public Object getItem(int arg0) {

        return null;
    }

    @Override
    public long getItemId(int position) {

        return 0;
    }

    @Override
    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 imagewithtext.xml
            gridView = inflater.inflate(R.layout.imagewithtext, null);

            // set value into textview
            TextView textView = (TextView) gridView
                    .findViewById(R.id.grid_item_label);
            textView.setText(textValues[position]);

            // set image based on selected text
            ImageView imageView = (ImageView) gridView
                    .findViewById(R.id.grid_item_image);

            String extractedText = textValues[position];

            System.out.println("the position value is>>>>>>"+position);

            if (extractedText.equals("item1")) {
                imageView.setImageResource(R.drawable.icon);
            }
            else if (extractedText.equals("item2")) {
                imageView.setImageResource(R.drawable.icon);
            } else if (extractedText.equals("item3")) {
                imageView.setImageResource(R.drawable.icon);
            }else if(extractedText.equals("item4")){
                imageView.setImageResource(R.drawable.icon);
            } else if (extractedText.equals("item5")) {
                imageView.setImageResource(R.drawable.icon);
            } else if (extractedText.equals("item6")) {
                imageView.setImageResource(R.drawable.pic1);
            }
            else if(extractedText.equals("item7")){
                imageView.setImageResource(R.drawable.pic1);
            }
            else{
                imageView.setImageResource(R.drawable.pic1);
            }

        } else {
            gridView = (View) convertView;
        }

        return gridView;    


    }

}

xml file which contains gridview:

<LinearLayout
   xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:orientation="vertical">

<RelativeLayout
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >


     <Button 
        android:id="@+id/button1"
        android:layout_width="200px"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:text="@string/button"
        android:onClick="button1"
        />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_alignParentRight="true"
        android:text="@string/present_location"
      />



</RelativeLayout>

<EditText 
    android:id="@+id/editText1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="@string/editText1_hint"
    />

<GridView
    android:id="@+id/gridView1"
    android:numColumns="auto_fit"
    android:verticalSpacing="10dp"
    android:horizontalSpacing="10dp"
    android:gravity="center"
    android:columnWidth="100dp"
    android:stretchMode="columnWidth"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

</GridView>


</LinearLayout>
  • 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-15T10:36:59+00:00Added an answer on June 15, 2026 at 10:36 am

    Let’s assume your items contain just 1 ImageView and 1 TextView (for the sake of simplicity).

    Your getView method in the Adapter should do this:

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder vh;
        if (convertView == null) {
            final LayoutInflater inflater = LayoutInflater.from(getContext());
            convertView = inflater.inflate(R.layout.imagewithtext,null);
            //Custom ViewHolder, you have to create it in the same class.
            vh = new ViewHolder();
            vh.imageView = convertView.findViewById(R.id.imageView1);
            vh.textView = convertView.findViewById(R.id.textView1);
            convertView.setTag(vh);
        } else {
            vh = (ViewHolder) convertView.getTag();
        }
        vh.imageView.setImageResource(R.drawable.whatever);
        vh.textView.setText("Whatever text you want to set here");
        return gridView;    
    }
    

    And in your adapter, just add this:

    private class ViewHolder{
    
        public ViewHolder(){}
    
        public ImageView imageView;
        public TextView textView;
        //Add any views you want to use in getView here
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Tab that has a custom layout which contains a TextView .
I have an activity that contains several user editable items (an EditText field, RatingBar,
I have a service that also contains an activity the problem is sometimes it
I have a workflow that contains a Pick activity. Each PickBranch is triggered by
I have an app that contains several activities...so lets say user is navigating activity
I currently have a class called clientActivity that contains a finger paintig activity that
I have an activity with a layout contains an ImageView and TextView . ImageView
I have textview which contains urls like www.google.com . for this textview a have
I have an Activity whose layout is in the file main.xml that contains a
I have a gallery which contains one textview and imageview. If i set OnClickListener

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.