Tuesday, 2 January 2018

Gridview with two texts in android

Gridview with two texts in android

Reply with comment




Manefest:

<?xml version="1.0" encoding="utf-8"?>
 <manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="com.example.admin.grid1"
<application 
 android:allowBackup="true" 
 android:icon="@mipmap/ic_launcher" 
 android:label="@string/app_name" 
 android:supportsRtl="true" 
 android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>
 

activity_main:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 
 xmlns:android="http://schemas.android.com/apk/res/android" 
 xmlns:tools="http://schemas.android.com/tools" 
 android:layout_width="match_parent" 
 android:layout_height="match_parent"
 <GridView xmlns:android="http://schemas.android.com/apk/res/android" 
 android:id="@+id/gridview" 
 android:layout_width="match_parent" 
 android:layout_height="wrap_content" 
 android:background="#cccccc" 
 android:horizontalSpacing="1dp" 
 android:numColumns="3" 
 android:stretchMode="columnWidth" 
 android:verticalSpacing="1dp" />
</RelativeLayout>
   

grid_layout:

<?xml version="1.0" encoding="utf-8"?> 
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
 android:layout_width="match_parent" 
 android:layout_height="match_parent" 
 android:orientation="vertical" 
 android:gravity="center" 
 android:background="#fff" 
 android:padding="15dp" 
 
<TextView 
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content" 
 android:text="Medium Text" 
 android:id="@+id/textView" 
 android:layout_gravity="center_horizontal" /> 
<TextView 
 android:id="@+id/grid_item_label" 
 android:layout_marginTop="5dp" 
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content" 
 android:text="Medium Text" 
 android:textColor="#555" /> 
</LinearLayout>

MainActivity: 


public class MainActivity extends AppCompatActivity {

    String[] web = {
            "one",
            "one",
            "one",
            "one",
            "one",
            "one",
            "one",
            "one",
            "one",
            "one"

    } ;
    String[] web2 = {
            "two",
            "two",
            "two",
            "two",
            "two",
            "two",
            "two",
            "two",
            "two",
            "two"

    } ;




    @Override 
 protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Img1 adapter = new Img1(MainActivity.this,web,web2);

        GridView gridview = (GridView) findViewById(R.id.gridview);


        gridview.setAdapter(adapter);
        gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View v,
                                    int position, long id) {
                if(position==0)
                {
                    Toast.makeText(MainActivity.this, "add", Toast.LENGTH_SHORT).show();
                }
                if(position==1)
                {

                }
                if(position==2)
                {

                }
                if(position==3)
                {

                }
                if(position==4)
                {

                }
                if(position==5)
                {

                }
            }
        });
    }
}


Img1:


public class Img1 extends BaseAdapter
{
    private Context mContext;
    private final String[] web;
    private final String[] web2;
    public Img1(Context c, String[] web,String[] web2 )
    {
        mContext = c;
        this.web=web;
        this.web2=web2;
    }



    @Override    public int getCount()
    {

        return web.length;
    }
    @Override    public Object getItem(int position)
    {
        return position;
    }
    @Override    public long getItemId(int position)
    {
        return 0;
    }
    @Override    public View getView(int position, View convertView, ViewGroup
            parent)
    {
        LayoutInflater inflater = (LayoutInflater)
                mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View gridView;
        if (convertView == null)
        {
            gridView = new View(mContext);
            // get layout from mobile.xml 
 gridView = inflater.inflate(R.layout.grid_layout, null);
            // set value into textview 
 TextView textView = (TextView)gridView.findViewById(R.id.grid_item_label);
 TextView textView2 = (TextView)gridView.findViewById(R.id.textView);

            textView.setText(web[position]);
            textView2.setText(web2[position]);
            // set image based on selected text

        }
        else 
 {
            gridView = (View) convertView;
        }
        return gridView;
    }
} 

No comments:

Post a Comment