I have following code that should:
listView = (ListView) findViewById(R.id.listview_github_entries);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
}
});
This is what I load into the ListView:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/list_item"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="10dp"
android:orientation="horizontal"
android:clickable="true">
<ImageView
android:layout_width="100dp"
android:layout_height="100dp"
android:src="#mipmap/github_icon"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/github_name"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/github_url"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
Here is the Adapter :
public class GithubEntryAdapter extends ArrayAdapter<GithubEntry>{
public GithubEntryAdapter(Activity context, ArrayList<GithubEntry> githubEntries){
super(context, 0, githubEntries);
}
public View getView(int position, View convertView, ViewGroup parent){
View listItemView = convertView;
if (listItemView == null){
listItemView = LayoutInflater.from(getContext()).inflate(
R.layout.list_item, parent, false);
}
GithubEntry currentGithubEntry = getItem(position);
TextView github_url = (TextView) listItemView.findViewById(R.id.github_url);
github_url.setText(currentGithubEntry.getGithub_url());
TextView github_name = (TextView) listItemView.findViewById(R.id.github_name);
github_name.setText(currentGithubEntry.getGithub_name());
return listItemView;
}
}
This is not working for me. Im no quit sure where I should place this code. Can I place this in the onCreate? If not where should I move it? I completly new in Android and I have also not much experience in Java.
Here is what i did for you..
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
private ListView listView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView) findViewById(R.id.listview_github_entries);
listView.setAdapter(new GithubEntryAdapter(MainActivity.this, getList()));
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
TextView github_url_tv = view.findViewById(R.id.github_url);
String url_text= github_url_tv.getText().toString();
Toast.makeText(MainActivity.this, url_text", Toast.LENGTH_LONG).show();
}
});
}
private ArrayList<GithubEntry> getList() {
ArrayList<GithubEntry> githubEntries = new ArrayList<>();
GithubEntry githubEntry = new GithubEntry();
githubEntry.setGithub_name("Name");
githubEntry.setGithub_url("url");
GithubEntry githubEntry1 = new GithubEntry();
githubEntry1.setGithub_name("Name");
githubEntry1.setGithub_url("url");
GithubEntry githubEntry2 = new GithubEntry();
githubEntry2.setGithub_name("Name");
githubEntry2.setGithub_url("url");
githubEntries.add(githubEntry);
githubEntries.add(githubEntry1);
githubEntries.add(githubEntry2);
return githubEntries;
}
}
Here is adapter
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import java.util.ArrayList;
public class GithubEntryAdapter extends ArrayAdapter<GithubEntry> {
public GithubEntryAdapter(Activity context, ArrayList<GithubEntry>
githubEntries){
super(context, 0, githubEntries);
}
public View getView(int position, View convertView, ViewGroup parent){
View listItemView = convertView;
if (listItemView == null){
listItemView = LayoutInflater.from(getContext()).inflate(
R.layout.list_item, parent, false);
}
GithubEntry currentGithubEntry = getItem(position);
TextView github_url = (TextView) listItemView.findViewById(R.id.github_url);
github_url.setText(currentGithubEntry.getGithub_url());
TextView github_name = (TextView) listItemView.findViewById(R.id.github_name);
github_name.setText(currentGithubEntry.getGithub_name());
return listItemView;
}
}
here is POJO(plan java object) class
class GithubEntry {
private String Github_url;
private String Github_name;
public String getGithub_url() {
return Github_url;
}
public void setGithub_url(String github_url) {
Github_url = github_url;
}
public String getGithub_name() {
return Github_name;
}
public void setGithub_name(String github_name) {
Github_name = github_name;
}
}
and here is list_item
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/list_item"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="10dp"
android:orientation="horizontal"
android:clickable="false">
<ImageView
android:layout_width="100dp"
android:layout_height="100dp"
android:src="#mipmap/ic_launcher_round"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/github_name"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/github_url"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
and here is activity layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.kaimeramedia.githubentry.MainActivity">
<ListView
android:id="#+id/listview_github_entries"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
If mean that the OnItemClickListeneter not working, then you need to implement a custom adapter by extending ArrayAdater to serve you custom row, And in the custom adapter you can use a callback interface or implement a listener on the view it self See the example.
I think you want to display the list of values here. You should write it inside onCreate because onCreate is a method where you method starts and run. So, Put it inside onCreate. For more correct answer please explain everything.
Related
I'm trying to make custom adapter for my small app, I got stuck in filling my rows with data, I can't find a good tutorial for that. I know how to use Simple Adapter and Array Adapter, but custom ones have some trouble.
I don't know how to implement it, and the online articles confused me, Please help me with the logic and what steps should I do implement.
package com.example.administrator.healthyfood;
import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.ListView;
public class MainActivity extends AppCompatActivity {
ListView list;
String[] food = {"Dog","Cat","Cow","Fish","Frog","Bird","Rabbit","Horse","Chikcen"};
#Override
protected void onCreate(Bundle savedInstanceState) {
list = (ListView)findViewById(R.id.listView);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public class MyListViewAdapter extends BaseAdapter {
#Override
public int getCount() {
return food.lenght();
}
#Override
public long getItemId(int i) {
return 0;
}
#Override
public Object getItem(int i) {
return null;
}
#Override
public View getView(int i, View view, ViewGroup viewGroup) {
return null;
}
}
}
activity_main.xml
<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"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<ListView
android:id="#+id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >
</ListView>
</RelativeLayout>
custom.xml
<?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" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_width="255dp"
android:layout_height="wrap_content"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Video1"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#339966"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="#+id/detail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="video1"
android:textColor="#606060" />
</LinearLayout>
</LinearLayout>
<ImageView
android:id="#+id/img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher" />
</LinearLayout>
</LinearLayout>
Your MainActivity.java -
package com.example.sample;
import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
public class MainActivity extends Activity {
ListView l1;
String[] t1={"video1","video2"};
String[] d1={"lesson1","lesson2"};
int[] i1 ={R.drawable.ic_launcher,R.drawable.ic_launcher};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
l1=(ListView)findViewById(R.id.list);
l1.setAdapter(new dataListAdapter(t1,d1,i1));
}
class dataListAdapter extends BaseAdapter {
String[] Title, Detail;
int[] imge;
dataListAdapter() {
Title = null;
Detail = null;
imge=null;
}
public dataListAdapter(String[] text, String[] text1,int[] text3) {
Title = text;
Detail = text1;
imge = text3;
}
public int getCount() {
// TODO Auto-generated method stub
return Title.length;
}
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return null;
}
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = getLayoutInflater();
View row;
row = inflater.inflate(R.layout.custom, parent, false);
TextView title, detail;
ImageView i1;
title = (TextView) row.findViewById(R.id.title);
detail = (TextView) row.findViewById(R.id.detail);
i1=(ImageView)row.findViewById(R.id.img);
title.setText(Title[position]);
detail.setText(Detail[position]);
i1.setImageResource(imge[position]);
return (row);
}
}
}
Hope this is enough for you.
You can create a simple Adapter class using these simple steps
make a java class
public class Wallet_Adapter extends RecyclerView.Adapter<Wallet_Adapter.ViewHolder>
{
LayoutInflater inflater;
List<Wallet_Model> modelclasslists;
public Wallet_Adapter(Context ctx, List<Wallet_Model> modelclasslists)
{
this.inflater=LayoutInflater.from(ctx);
this.modelclasslists=modelclasslists;
}
#NonNull
#Override
public Wallet_Adapter.ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view=inflater.inflate(R.layout.layout_wallet,parent,false);
return new Wallet_Adapter.ViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull Wallet_Adapter.ViewHolder holder, int position) {
holder.date.setText(modelclasslists.get(position).getDate());
holder.amount.setText(modelclasslists.get(position).getAmount());
holder.description.setText(modelclasslists.get(position).getDescription());
holder.type.setText(modelclasslists.get(position).getType());
}
#Override
public int getItemCount() {return modelclasslists.size(); }
public class ViewHolder extends RecyclerView.ViewHolder
{
TextView date,amount,description,type;
public ViewHolder(#NonNull View itemView)
{
super(itemView);
date=itemView.findViewById(R.id.dateans);
amount=itemView.findViewById(R.id.amountans);
description=itemView.findViewById(R.id.descriptionans);
type=itemView.findViewById(R.id.typeans);
}
}
}
I am new to android. I have created material design Grid View. Now what I want to do, when I click on each grid view it should take to me each new activity. When i click on ALPHABETS it will go to ListAlphabet.java. Thank you for help.
My activity_main.xml is :
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/android_coordinator_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:id="#+id/appbar_layout"
android:layout_height="#dimen/app_bar_height"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.design.widget.CollapsingToolbarLayout
android:id="#+id/collapsing_toolbar_android_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:contentScrim="?attr/colorPrimary"
app:expandedTitleMarginStart="#dimen/expanded_toolbar_title_margin_start"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<ImageView
android:id="#+id/image_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerInside"
android:src="#drawable/code"
app:layout_collapseMode="parallax"
app:layout_collapseParallaxMultiplier="0.7" />
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_collapseMode="pin"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"
app:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar" />
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:id="#+id/nestedscrollview"
android:layout_height="match_parent"
android:fillViewport="true"
app:layout_behavior="#string/appbar_scrolling_view_behavior">
<LinearLayout
android:id="#+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<GridView
android:id="#+id/grid"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:columnWidth="100dp"
android:gravity="center"
android:listSelector="#00000000"
android:numColumns="auto_fit"
android:stretchMode="columnWidth" />
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
</android.support.design.widget.CoordinatorLayout>
gridview_custom_layout.xml is :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/android_gridview_custom_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
android:padding="10dp">
<com.andexert.library.RippleView
android:id="#+id/more"
rv_centered="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
app:rv_color="#fff"
app:rv_rippleDuration="200">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/linearLayout"
android:orientation="vertical">
<ImageView
android:id="#+id/gridview_image"
android:layout_width="80dp"
android:layout_height="80dp"
android:src="#mipmap/ic_launcher" />
<TextView
android:id="#+id/gridview_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/grid_image"
android:layout_marginTop="10dp"
android:gravity="center"
android:text="Grid View Item"
android:textColor="#444"
android:textSize="12sp"
android:textStyle="bold" />
</LinearLayout>
</com.andexert.library.RippleView>
</LinearLayout>
MainActivity.java is :
package com.affinityapp.sj.alphabet;
import android.content.Context;
import android.os.Bundle;
import android.support.design.widget.CollapsingToolbarLayout;
import android.support.design.widget.CoordinatorLayout;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.widget.GridView;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
Toolbar toolbar;
CollapsingToolbarLayout collapsingToolbarLayoutAndroid;
CoordinatorLayout rootLayoutAndroid;
GridView gridView;
Context context;
ArrayList arrayList;
public static String[] gridViewStrings = {
"ALPHABETS",
"NUMBERS",
"MONTH",
"DAYS",
"ANIMALS",
"CALL US",
};
public static int[] gridViewImages = {
R.drawable.icon_alphabet,
R.drawable.icon_number,
R.drawable.icon_calendar,
R.drawable.icon_days,
R.drawable.icon_animal,
R.drawable.icon_call
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
gridView = (GridView) findViewById(R.id.grid);
gridView.setAdapter(new CustomAndroidGridViewAdapter(this, gridViewStrings, gridViewImages));
initInstances();
}
private void initInstances() {
rootLayoutAndroid = (CoordinatorLayout) findViewById(R.id.android_coordinator_layout);
collapsingToolbarLayoutAndroid = (CollapsingToolbarLayout) findViewById(R.id.collapsing_toolbar_android_layout);
collapsingToolbarLayoutAndroid.setTitle("e-Learning");
}
}
CustomAndroidGridViewAdapter.java is :
package com.affinityapp.sj.alphabet;
/**
* Created by SJ on 23-01-2017.
*/
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;
/**
* Created by HP on 5/11/2016.
*/
public class CustomAndroidGridViewAdapter extends BaseAdapter {
private Context mContext;
private final String[] string;
private final int[] Imageid;
public CustomAndroidGridViewAdapter(Context c,String[] string,int[] Imageid ) {
mContext = c;
this.Imageid = Imageid;
this.string = string;
}
#Override
public int getCount() {
return string.length;
}
#Override
public Object getItem(int p) {
return null;
}
#Override
public long getItemId(int p) {
return 0;
}
#Override
public View getView(int p, View convertView, ViewGroup parent) {
View grid;
LayoutInflater inflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
grid = new View(mContext);
grid = inflater.inflate(R.layout.gridview_custom_layout, null);
TextView textView = (TextView) grid.findViewById(R.id.gridview_text);
ImageView imageView = (ImageView)grid.findViewById(R.id.gridview_image);
textView.setText(string[p]);
imageView.setImageResource(Imageid[p]);
} else {
grid = (View) convertView;
}
return grid;
}
}
So, you can use this way to click grid view:
public class MainActivity extends AppCompatActivity {
Toolbar toolbar;
CollapsingToolbarLayout collapsingToolbarLayoutAndroid;
CoordinatorLayout rootLayoutAndroid;
GridView gridView;
Context context;
ArrayList arrayList;
public static String[] gridViewStrings = {
"ALPHABETS",
"NUMBERS",
"MONTH",
"DAYS",
"ANIMALS",
"CALL US",
};
public static int[] gridViewImages = {
R.drawable.icon_alphabet,
R.drawable.icon_number,
R.drawable.icon_calendar,
R.drawable.icon_days,
R.drawable.icon_animal,
R.drawable.icon_call
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
gridView = (GridView) findViewById(R.id.grid);
gridView.setAdapter(new CustomAndroidGridViewAdapter(this, gridViewStrings, gridViewImages));
initInstances();
gridView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
// Do whatever you want, like start new Activity or display new view or display dialog box or display just message
Toast.makeText(MainActivity.this, "" + position, Toast.LENGTH_SHORT).show();
}
});
}
private void initInstances() {
rootLayoutAndroid = (CoordinatorLayout) findViewById(R.id.android_coordinator_layout);
collapsingToolbarLayoutAndroid = (CollapsingToolbarLayout) findViewById(R.id.collapsing_toolbar_android_layout);
collapsingToolbarLayoutAndroid.setTitle("e-Learning");
}
}
Hope, this will help you.
Implement onItemClickListener method in you activity:
gridview.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
final Intent intent;
switch(position)
{
case 0:
intent = new Intent(context, FirstActivity.class);
break;
case 1:
intent = new Intent(context, SecondActivity.class);
break;
...
default:
intent = new Intent(context, DefaultActivity.class);
break;
}
startActivity(intent);
}
});
Possibly duplicate question...
Research thoroughly before posting a question please.
Answer can be found in this thread: How can I give an imageview click effect like a button on Android?
Edit:
In your current activity, you need to create a variable ImageButton.
ImageButton myButton = (ImageButton) findViewById(R.id.the_button_you_created_on_the_layout.xml);
Then you need to set a click listener to this button
myButton.setOnClickListener(new View.OnClickListner(){
// When the button is pressed/clicked, it will run the code below
#Override
public void onClick()
// Intent is what you use to start another activity
Intent myIntent = new Intent(this, YourActivity.class);
startActivity(intent);
}
});
So i want to make my CustomArrayAdapter class generic.
CustomDialogFragmentNotGeneric:
public class CustomDialogFragment extends DialogFragment
{
TextView listViewItemTextView;
ArrayAdapter<String> arrayAdapter;
ListView dialogListView;
String[] items = {"Hello","Hello there","Hi","Hi there"};
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState)
{
View rootView = inflater.inflate(R.layout.dialog, container,false);
getDialog().setTitle("Choose an option"); // Set dialog title
listViewItemTextView = (TextView) rootView.findViewById(R.id.list_view_item_text_view_id);
dialogListView = (ListView) rootView.findViewById(R.id.dialog_list_view_id);
getDialog().setTitle("Opening Words"); // Setting dialog title
CustomArrayAdapter customArrayAdapter = new CustomArrayAdapter(getActivity(), items);
dialogListView.setAdapter(customArrayAdapter);
dialogListView.setOnItemClickListener(new OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> parent, View view,int position, long id)
{
Toast.makeText(getActivity(), items[position], Toast.LENGTH_SHORT).show();
}
});
return rootView;
}
}
It's not generic because of the String[] itemm = line.
So instead of building a lot of classes that in each there will be a different String[] item, how do i make that generic?
dialog.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ListView
android:id="#+id/dialog_list_view_id"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
MyCustomArrayAdapter class:
package com.example.predesignedmails;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
public class CustomArrayAdapter extends ArrayAdapter<String>
{
Context context;
String[] items;
LayoutInflater layoutInflater;
public CustomArrayAdapter(Context context, String[] items)
{
super(context, R.layout.list_view_row,items);
this.context = context;
this.items = items;
}
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
if (convertView == null)
{
this.layoutInflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = this.layoutInflater.inflate(R.layout.list_view_row,parent,false);
}
TextView rowTextView = (TextView) convertView.findViewById(R.id.row_text_view_id);
rowTextView.setText(this.items[position]);
return convertView;
}
}
list_view_row.xml:
<?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" >
<TextView
android:id="#+id/row_text_view_id"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
I have put a lot of effort into this post. Please help me guys. Thanks.
EDIT
my activity layout:
<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"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.predesignedmails.LoveMailsActivity" >
<TextView
android:id="#+id/love_email_emai_to_send_to_text_view_id"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/emai_to_send_to_text_view_text"
android:textSize="16sp"
android:textStyle="bold"
android:textColor="#color/opening_words_list_view_header_color"
/>
<EditText
android:id="#+id/love_email_email_to_send_to_edit_text_id"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="#string/emai_to_send_to_edit_text_hint"
android:inputType="textEmailAddress"
android:textSize="18sp"
android:textColor="#color/selection_text_color"
android:layout_below="#id/love_email_emai_to_send_to_text_view_id"
/>
<TextView
android:id="#+id/love_email_opening_words_header_text_view_id"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/love_email_opening_words_text_view_text"
android:textSize="16sp"
android:textStyle="bold"
android:textColor="#color/opening_words_list_view_header_color"
android:layout_below="#id/love_email_email_to_send_to_edit_text_id"
/>
<TextView
android:id="#+id/love_email_opening_words_text_view_id"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/initial_text"
android:textSize="18sp"
android:textColor="#color/selection_text_color"
android:layout_below="#id/love_email_opening_words_header_text_view_id"
/>
</RelativeLayout>
ArrayAdapter is already generic. Based on your current code, you can get rid of your entire CustomArrayAdapter class and simply use this constructor (replacing String with whatever type your items are):
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),
R.layout.list_view_row, R.id.row_text_view_id, items);
If you plan to keep CustomArrayAdapter, you can declare it like this to keep the generics:
public class CustomArrayAdapter<T> extends ArrayAdapter<T> {
...
}
Naturally you would use T instead of String wherever necessary.
I am trying to add a checkbox in a customized listview but don't know how.
I have the following codes:
ListObject.java
package br.com.eduvm.xurrascalc;
public class ListObject {
private String texto;
private int iconeRid;
public ListObject() {
}
public ListObject(String texto, int iconeRid) {
this.texto = texto;
this.iconeRid = iconeRid;
}
public int getIconeRid() {
return iconeRid;
}
public void setIconeRid(int iconeRid) {
this.iconeRid = iconeRid;
}
public String getTexto() {
return texto;
}
public void setTexto(String texto) {
this.texto = texto;
}
}
ListAdapter.java
package br.com.eduvm.xurrascalc;
import java.util.ArrayList;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;
public class ListAdapter extends BaseAdapter {
private LayoutInflater mInflater;
private ArrayList<ListObject> itens;
public ListAdapter(Context context, ArrayList<ListObject> itens) {
// Itens que preencheram o listview
this.itens = itens;
// responsavel por pegar o Layout do item.
mInflater = LayoutInflater.from(context);
}
public int getCount() {
return itens.size();
}
public ListObject getItem(int position) {
return itens.get(position);
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View view, ViewGroup parent) {
ListObject item = itens.get(position);
view = mInflater.inflate(R.layout.itens_lista, null);
((TextView) view.findViewById(R.id.text)).setText(item.getTexto());
((ImageView) view.findViewById(R.id.imagemview)).setImageResource(item.getIconeRid());
return view;
}
}
List.xml
<?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="horizontal" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:padding="5sp" >
<ImageView
android:id="#+id/imagemview"
android:layout_width="wrap_content"
android:layout_height="match_parent" />
<TextView
android:id="#+id/text"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="5sp"
android:gravity="center_vertical"
android:textColor="#FFF" />
</LinearLayout>
</LinearLayout>
How could I do to insert a checkbox in the items in this list?
This would be the layout for each individual row in your list
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<CheckBox
android:id="#+id/lstChkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/lstText"
android:textSize="30px"
android:layout_weight="1" />
<ImageView
android:id="#+id/listImage"
android:layout_height="wrap_content"
android:layout_width="wrap_content" />
You main list activity layout will look something like this:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:drawSelectorOnTop="false"
android:choiceMode="multipleChoice"
/>
Your class which extends BaseAdapter getView method will somethings like this:
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder vHolder = null;
if (convertView != null)
vHolder = (ViewHolder) convertView.getTag(); // convertView is been recycled
else {
convertView = (View) mInflater.inflate(R.layout.list_item, null); // Set content of new View with list_item.xml
vHolder = new ViewHolder();
vHolder.checkBox = ((CheckBox) convertView.findViewById(R.id.lstChkbox)); // Getting pointers
vHolder.textView = ((TextView) convertView.findViewById(R.id.lstText));
vHolder.imageView = ((ImageView) convertView.findViewById(R.id.listImage));
vHolder.checkBox.setOnCheckedChangeListener(this); // Setting Listeners
convertView.setTag(vHolder);
}
vHolder.checkBox.setId(position); // This is part of the Adapter APi
vHolder.textView.setId(position); // Do not delete !!!
vHolder.imageView.setId(position);
if (mItems.get(position).getChecked()) { // Setting parameters for the View from our mItems list
vHolder.checkBox.setChecked(true);
} else {
vHolder.checkBox.setChecked(false);
}
vHolder.textView.setText(mItems.get(position).getText());
vHolder.imageView.setImageDrawable(mItems.get(position).getmImage());
return convertView;
}
public static class ViewHolder {
CheckBox checkBox;
TextView textView;
ImageView imageView;
}
/*
* Ok for this test but Toast are going to show every time the row comes into View
*/
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Log.d(TAG, "Checked");
int position = buttonView.getId();
if (isChecked) {
mItems.get(position).setChecked(true);
Toast.makeText(context, mItems.get(position).getText(), Toast.LENGTH_LONG).show();
} else {
mItems.get(buttonView.getId()).setChecked(false);
}
}
Just add a checkbox to the layout itens_lista.xml.
Just adding a checkbox is easy. Do you want to do anything more to ti.
Add a CheckBox View to your custom row layout.
Hello dear programmers,
I am very new to android application development and for practice I tried to develope an application with following code
package com.android.practice;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.TextView;
public class CalculatorAdapter extends BaseAdapter {
private Context mContext;
private String[] texts={"1","2","3","+","4","5","6","-","7","8","9","*",".","0","=","/"};
public CalculatorAdapter(Context context) {
mContext=context;
}
#Override
public int getCount() {
return texts.length;
}
#Override
public Object getItem(int position) {
return null;
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
TextView tv;
if(convertView==null){
tv=new TextView(mContext);
tv.setLayoutParams(new GridView.LayoutParams(85,85));
}else{
tv=(TextView)convertView;
}
tv.setText(texts[position]);
tv.setTextSize(30);
return tv;
}
}
package com.android.practice;
import android.app.Activity;
import android.os.Bundle;
import android.widget.GridView;
public class MyCalculatorActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
GridView gv=(GridView)findViewById(R.id.gridView);
//gv.setGravity(0x11);
gv.setAdapter(new CalculatorAdapter(this));
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:weightSum="1">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="0"
android:textStyle="bold"
android:textSize="30px"
android:gravity="center_horizontal"
android:background="#aa0000"
/>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/gridView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:clickable="true"
android:numColumns="4"
android:columnWidth="90dp"
android:horizontalSpacing="10dp"
android:verticalSpacing="10dp"
android:stretchMode="columnWidth"
/>
</LinearLayout>
My problem is though I have given the gridview gravity attribute as center, I am not getting the texts at the center of the cells of my gridview.
Please help me, If somebody finds where the problem lies.
Thanks in advance.
Use this
<TextView
android:gravity="center_vertical|center_horizontal" />
You can try infalting a custom layout in the getview section of the adaptor
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null){
convertView = LayoutInflater.from(context).inflate(R.layout.tab_button, null);
}
LinearLayout ll=(LinearLayout) convertView.findViewById(R.id.linear_topbar);
TextView tv= (TextView) convertView.findViewById(R.id.title);
tv.setText(texts[position]);
tv.setTextSize(30);
return convertView;
}
this is the xml files
<TextView android:id="#+id/title" android:text="title"
android:layout_width="wrap_content" android:textColor="#fff"
android:textStyle="bold" android:textSize="30dp" android:layout_height="wrap_content"></TextView>