(Android Studio) Problem 1 - Customize a content display with GridView Please wr
ID: 3915939 • Letter: #
Question
(Android Studio)
Problem 1 - Customize a content display with GridView
Please write the Java and XML codes.
We have gone through the process of creating a customized display with a ListView.
(Figure 1) For each list item, you have one ImageView and two TextView. We created a ListAdapter object to manage ListView content. Because we needed to present a complicated data, ArrayAdapter is extended.
In this lab, your job is to create an Android mobile app from scratch to display images as Figure 2. A few notes for your implementation:
• You will create a new adapter, named GridItemAdapter, to extend ArrayAdapter.
• Then, you set this GridItemAdapater object to the GridView object. (Figure 2)
• You also need to create a layout of grid item and update the layout of the main activity.
• The date shown next to the title should be the current date when running the application, not a
fixed String “2018-02-25”.
Explanation / Answer
Solution
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
tools:context=".MainActivity" >
<GridView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:numColumns="auto_fit"
android:gravity="center"
android:columnWidth="98dp"
android:stretchMode="columnWidth"
android:id="@+id/mygrid"
/>
</LinearLayout>
Single_grid.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:padding="6dp" >
<ImageView
android:layout_width="48dp"
android:id="@+id/image_grid"
android:layout_height="48dp">
</ImageView>
<TextView
android:id="@+id/text_grid"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_marginTop="17dp"
android:textSize="8sp" >
</TextView>
</LinearLayout>
Grid_custom.java
package com.customlist.customgrid;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class Grid_Custom extends BaseAdapter{
private Context context;
private final String[] today_date;
private final int[] id_image;
public CustomGrid(Context a,String[] today_date,int[]id_image) {
context = a;
this. id_image = id_image;
this.today_date = today_date;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return today_date.length;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(int position, View View_convert, ViewGroup parent) {
// TODO Auto-generated method stub
View grid1;
LayoutInflater li = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (View_convert == null) {
grid1 = new View(context);
grid1 = if.inflate(R.layout.single_grid, null);
TextView textView = (TextView) grid1.findViewById(R.id.text_grid);
ImageView imageView =(ImageView)grid1.findViewById(R.id. image_grid);
textView.setText(today_date[position]);
imageView.setImageResource(Imageid[position]);
} else {
grid1 = (View) View_convert;
}
return grid1;
}
}
MainActivity.java
package com.customlist.customgrid;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.Toast;
public class MainActivity extends Activity {
GridView grid1;
String date_current = new SimpleDateFormat("dd-MM-yyyy").format(new Date());
String[] today_date = {
date_current, date_current, date_current, date_current, date_current, date_current, date_current, date_current, date_current, date_current, date_current, date_current, date_current, date_current, date_current,
} ;
int[] id_image = {
R.drawable.your_image1,
R.drawable.your_image2,
R.drawable.your_image3,
R.drawable.your_image4,
R.drawable.your_image5,
R.drawable.your_image6,
R.drawable.your_image7,
R.drawable.your_image8,
R.drawable.your_image9,
R.drawable.your_image10,
R.drawable.your_image11,
R.drawable.your_image12,
R.drawable.your_image13,
R.drawable.your_image14,
R.drawable.your_image15
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
CustomGrid my_adapter = new CustomGrid(MainActivity.this, today_date, id_image);
grid1=(GridView)findViewById(R.id.grid1);
grid1.setAdapter(my_adapter);
grid1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Toast.makeText(MainActivity.this, “This is ”+ today_date [+ position], Toast.LENGTH_SHORT).show();
}
});
}
}
Manifest File
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.customlist.customgrid"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/your_app_name"
android:theme="@style/Your_AppTheme" >
<activity
android:name=" com.customlist.customgrid.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Feel free to reach out regarding any queries in the comment section .
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.