This is my activity code:
#Override
public void onCreate(Bundle icicle)
{
super.onCreate(icicle);
String[] names = new String[] { "Investor Relations", "Social Sharing", "Rate this app", "About Rathbones",
"Disclaimer", "Security", "Credits"};
// Create an ArrayAdapter, that will actually make the Strings above
// appear in the ListView
this.setListAdapter(new ArrayAdapter<String>(this, R.layout.moremenulist,
R.id.label, names));
}
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
// Get the item that was clicked
Object o = this.getListAdapter().getItem(position);
String keyword = o.toString();
Toast.makeText(this, "You selected: " + keyword, Toast.LENGTH_LONG).show();
}
And here is the xml file for this:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:id="#+id/LinearLayout1"
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:layout_width="wrap_content" android:textAppearance="?android:attr/textAppearanceLarge" android:layout_height="wrap_content" android:id="#+id/label"></TextView>
<LinearLayout android:id="#+id/linearLayout2" android:layout_width="wrap_content" android:orientation="vertical" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentRight="true" android:layout_alignParentBottom="true">
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageButton android:layout_width="wrap_content" android:id="#+id/imgbtn1" android:layout_alignParentLeft="true" android:layout_alignBottom="#+id/imgbtn3" android:layout_height="wrap_content" android:src="#drawable/topnews" android:visibility="visible"></ImageButton>
<ImageButton android:layout_width="wrap_content" android:layout_alignParentRight="true" android:id="#+id/imgbtn5" android:layout_alignBottom="#+id/imgbtn4" android:layout_height="wrap_content" android:src="#drawable/more"></ImageButton>
<ImageButton android:layout_width="wrap_content" android:id="#+id/imageButton1" android:layout_height="wrap_content" android:src="#drawable/contact_us" android:layout_alignParentTop="true" android:layout_toRightOf="#+id/imgbtn1"></ImageButton>
</RelativeLayout>
</LinearLayout>
</RelativeLayout>
Now, when the activity is loading it is appending the menu with each item in the list. I just want it to appear only in bottom not with each and every list item.
any suggestions?
Edit-1 :
I tried this but getting an error of nullpointer exception
View footerView =
((LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.footer, null, false);
ListView lv = new ListView(this);
lv.addFooterView(footerView);
What do you mean by menu? Do you mean your ImageButtons?
If that's the case then remove those create another layout for those ImageButtons and add them using ListView.addFooterView() instead.
You could create e.g. list_footer_layout.xml, which contains your ImageButtons. Then:
public void onCreate(Bundle bundle) {
...
ListView lv = getListView(); // If your activity is ListActivity
View foot = getLayoutInflater().inflate(R.layout.list_footer_layout, null);
lv.addFooterView(foot);
}
Related
I have a fragment that displays some items "images and buttons"
I want to scroll all the screen, not just the gridview, I made some search and used some codes but it is sold not working, so how can I do that.......
..................................................................................
...................................................................................
Here is the problem 4 sec
my codes
<ScrollView
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:background="#FFFFFF"
android:overScrollMode="never"
android:scrollbars="none"
tools:context=".Fragment.ImagesFragmentProject.HomeImagesFragment">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF">
<!-- editor's chooses text -->
<TextView
android:id="#+id/choose_text2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/scrollView_image"
android:paddingTop="20dp"
android:paddingRight="10dp"
android:paddingBottom="6dp"
android:text="إختيار المحررين"
android:textColor="#color/colorBlack"
android:textSize="18sp"
android:textStyle="bold" />
<GridView
android:id="#+id/gridview_image_choose"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_below="#id/choose_text2"
android:background="#fff"
android:columnWidth="100dp"
android:fastScrollEnabled="false"
android:gravity="center"
android:horizontalSpacing="0dp"
android:numColumns="auto_fit"
android:paddingTop="8dp"
android:scrollbars="none"
android:stretchMode="columnWidth"
android:verticalSpacing="0dp" />
</RelativeLayout>
Java class
public class HomeImagesFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
/* ArrayList for chooseImages */
final String[] chooseImages = {
"https://i1.wp.com/www.newphotodownload.info/wp-content/uploads/2018/12/%D8%A8%D9%88%D8%B3%D8%AA%D8%A7%D8%AA-%D8%AF%D9%8A%D9%86%D9%8A%D8%A9-61-1.jpg",
"https://quotess.cc/wp-content/uploads/2020/01/4688.jpg",
"https://whatt.cc/wp-content/uploads/2018/07/4443.jpg",
"https://i1.wp.com/www.newphotodownload.info/wp-content/uploads/2018/12/%D8%A8%D9%88%D8%B3%D8%AA%D8%A7%D8%AA-%D8%AF%D9%8A%D9%86%D9%8A%D8%A9-61-1.jpg",
"https://quotess.cc/wp-content/uploads/2020/01/4688.jpg",
"https://whatt.cc/wp-content/uploads/2018/07/4443.jpg",
"https://i1.wp.com/www.newphotodownload.info/wp-content/uploads/2018/12/%D8%A8%D9%88%D8%B3%D8%AA%D8%A7%D8%AA-%D8%AF%D9%8A%D9%86%D9%8A%D8%A9-61-1.jpg"
};
/* make new object and find the view "GridView" */
GridView gridview1 = rootView.findViewById(R.id.gridview_image_choose);
// display all the images from Array on it
gridview1.setAdapter(new PicassoImagesAdapter(getActivity(), chooseImages));
/* display the image when click on it */
// we made a class for this method called "PicassoDisplayImageAdapter"
gridview1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// get the image
String image = chooseImages[position];
Intent intent = new Intent(getActivity(), PicassoDisplayImageAdapter.class);
intent.putExtra("imageUrl", image);
getActivity().startActivity(intent);
}
});
return rootView;
}
}
I am developing an android app where a user will input text to an EditText, then it will be added to a ListView.
But once a button with onClick value "start" is clicked, it will display each item of the ListView in a TextView with id displayText, in the order of each item and it will loop(Repeat) once it is exhausted.
Here is my Code
Math.java
public class Math extends AppCompatActivity {
private ArrayList<String> items;
private ArrayAdapter<String> itemsAdapter;
private ListView lvItems;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_math);
lvItems = (ListView) findViewById(R.id.lvItems);
items = new ArrayList<String>();
itemsAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, items);
lvItems.setAdapter(itemsAdapter);
}
public void AddItem(View v) {
EditText etNewItem = (EditText) findViewById(R.id.etNewItem);
String itemText = etNewItem.getText().toString();
itemsAdapter.add(itemText);
etNewItem.setText("");
}
public void start(View v){
RelativeLayout lc = (RelativeLayout)findViewById(R.id.contentContainer);
lc.setVisibility(View.GONE);
RelativeLayout tc = (RelativeLayout)findViewById(R.id.tvContainer);
tc.setVisibility(View.VISIBLE);
}
}
activity_math.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"
tools:context=".Math">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/tvContainer"
android:visibility="gone">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:textSize="30sp"
android:id="#+id/displayText"/>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/contentContainer">
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/lvItems"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_above="#+id/btnAddItem" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/etNewItem"
android:layout_alignTop="#+id/btnAddItem"
android:hint="Enter a new item"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_toLeftOf="#+id/btnAddItem"
android:layout_toStartOf="#+id/btnAddItem"
android:layout_alignParentBottom="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add Item"
android:id="#+id/btnAddItem"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:onClick="AddItem"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start"
android:layout_alignParentBottom="true"
android:layout_toLeftOf="#id/btnAddItem"
android:onClick="start"/>
</RelativeLayout>
Okay, i think the issue is that you haven't given the TextView any thing to display. you have to convert the arrayList to a String array and use setText to display the Value.
add this part to the "start" method
String ls =System.getProperty("line.separator");// i used this to seperate the array items
String joint = TextUtils.join(ls, lvitems);
result.setText(joint);
That should work
I want to have a spinner which will contain a list of items.
The design I want is as follow:
I tried to put background for the spinner so now it only shows a background. I want white layout inside and with separator.
I tried to create a layout for the item and apply to the spinner but it gives error :
Process: com.kiranaapp, PID: 16697
java.lang.IllegalStateException: ArrayAdapter requires the resource ID to be a TextView
My design now looks like this:
And onclick of spinner I get layout like this:
But I want the list to be shown onClick of TextInputLayout.
Here is Xml file:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
android:background="#drawable/bg"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="com.kiranacustomerapp.Activities.SearchActivity"
tools:showIn="#layout/activity_search">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/white"
android:orientation="vertical">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:focusable="true"
android:focusableInTouchMode="true"
android:layout_height="wrap_content">
<android.support.design.widget.TextInputLayout
android:id="#+id/input_layout_item_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_marginTop="20dp"
android:paddingTop="05dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="150dp"
android:background="#drawable/bg">
<Spinner
android:id="#+id/items_spinner"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="visible"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:background="#ffffff"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp" />
</LinearLayout>
<android.support.design.widget.TextInputEditText
android:layout_width="240dp"
android:layout_height="45dp"
android:id="#+id/editTextItemName"
android:layout_gravity="center_horizontal"
android:hint="#string/item_name"
android:textSize="15sp"
android:padding="10dp" >
</android.support.design.widget.TextInputEditText>
</android.support.design.widget.TextInputLayout>
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:focusable="true"
android:focusableInTouchMode="true"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.design.widget.TextInputLayout
android:id="#+id/input_layout_item_unit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:paddingTop="05dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp">
<android.support.design.widget.TextInputEditText
android:layout_width="240dp"
android:layout_height="45dp"
android:id="#+id/editTextItemUnit"
android:layout_gravity="center_horizontal"
android:hint="#string/unit"
android:textSize="15sp"
android:padding="10dp" />
</android.support.design.widget.TextInputLayout>
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:focusable="true"
android:focusableInTouchMode="true"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.design.widget.TextInputLayout
android:id="#+id/input_layout_item_quantity"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:paddingTop="05dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp">
<android.support.design.widget.TextInputEditText
android:layout_width="240dp"
android:layout_height="45dp"
android:id="#+id/editTextItemQuantity"
android:layout_gravity="center_horizontal"
android:hint="#string/quantity"
android:textSize="14sp"
android:padding="10dp" />
</android.support.design.widget.TextInputLayout>
</LinearLayout>
<Button
android:layout_width="100dp"
android:layout_height="30dp"
android:text="Select"
style="?android:attr/borderlessButtonStyle"
android:id="#+id/buttonSelect"
android:layout_gravity="center_horizontal"
android:layout_marginTop="100dp"
android:layout_marginBottom="50dp"
android:background="#drawable/btn_hlf_blue"
android:textColor="#android:color/white"
android:textSize="12sp" />
</LinearLayout>
</RelativeLayout>
Activity:
public class SearchActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener {
private TextInputEditText edt_Item_Name,edt_Item_Unit,edt_Item_quantity;
private TextInputLayout textInput_Item_name,textInput_Item_Unit,textInput_Item_quantity;
private Spinner spinner;
private Button btnSelect;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
toolbar.setNavigationIcon(R.drawable.ic_back);
setSupportActionBar(toolbar);
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
finish();
}
});
setUpUI();
}
public void setUpUI(){
edt_Item_Name = (TextInputEditText) findViewById(R.id.editTextItemName);
edt_Item_quantity = (TextInputEditText)findViewById(R.id.editTextItemQuantity);
edt_Item_Unit = (TextInputEditText)findViewById(R.id.editTextItemUnit);
textInput_Item_name = (TextInputLayout)findViewById(R.id.input_layout_item_name);
textInput_Item_quantity = (TextInputLayout)findViewById(R.id.input_layout_item_quantity);
textInput_Item_Unit = (TextInputLayout)findViewById(R.id.input_layout_item_unit);
spinner = (Spinner)findViewById(R.id.items_spinner);
btnSelect = (Button)findViewById(R.id.buttonSelect);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
R.array.items_array, R.layout.order_item_layout);
// Specify the layout to use when the list of choices appears
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the spinner
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(this);
textInput_Item_name.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
spinner.setVisibility(View.VISIBLE);
edt_Item_Unit.setVisibility(View.GONE);
edt_Item_quantity.setVisibility(View.GONE);
btnSelect.setVisibility(View.GONE);
textInput_Item_name.setBackgroundResource(R.drawable.purple_bg);
}
});
}
public void onItemSelected(AdapterView<?> parent, View view,
int pos, long id) {
// An item was selected. You can retrieve the selected item using
// parent.getItemAtPosition(pos)
}
public void onNothingSelected(AdapterView<?> parent) {
// Another interface callback
}
}
How can I achieve this design? Can anyone help with this please? Thank you..
Use ListView or RecyclerView and put a spinner in a layout that will be use in your viewholder.class associated with getView method in adapter class, after-that set your data using ArrayList/Hashmap with the help of constructor of Adapter class.
Follow this
package com.example.spinner;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.Toast;
import android.widget.AdapterView.OnItemSelectedListener;
class AndroidSpinnerExampleActivity extends Activity implements OnItemSelectedListener{
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Spinner element
Spinner spinner = (Spinner) findViewById(R.id.spinner);
// Spinner click listener
spinner.setOnItemSelectedListener(this);
// Spinner Drop down elements
List<String> categories = new ArrayList<String>();
categories.add("Automobile");
categories.add("Business Services");
categories.add("Computers");
categories.add("Education");
categories.add("Personal");
categories.add("Travel");
// Creating adapter for spinner
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, categories);
// Drop down layout style - list view with radio button
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// attaching data adapter to spinner
spinner.setAdapter(dataAdapter);
}
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
// On selecting a spinner item
String item = parent.getItemAtPosition(position).toString();
// Showing selected spinner item
Toast.makeText(parent.getContext(), "Selected: " + item, Toast.LENGTH_LONG).show();
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
}
Use this layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:padding="10dip"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dip"
android:text="Category:"
android:layout_marginBottom="5dp"/>
<Spinner
android:id="#+id/spinner"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:prompt="#string/spinner_title"/>
</LinearLayout>
Draw your layout somthing like this:
<RelativeLayout
android:id="#+id/spinner_layout"
android:layout_width="match_parent"
android:layout_height="#dimen/dp_30"
android:layout_margin="#dimen/dp_20"
android:background="#color/fragment_bg"
android:orientation="horizontal">
<TextView
android:id="#+id/txt_city"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginStart="#dimen/dp_20"
android:hint="#string/select_city"
android:singleLine="tru
android:textSize="#dimen/sp_15"/>
<Spinner
android:id="#+id/spinner_city"
android:layout_width="match_parent"
android:layout_height="#dimen/dp_30"
android:layout_centerVertical="true"
android:entries="#array/StateName"
android:gravity="center"
android:padding="#dimen/sp_15"
app:binding="#{location.locations}"/>
</RelativeLayout>
and use your textinput layout instead of textview
Don't use Edittext use Textview to set clicked item text. "if you use edittext, then you have to tap two times to get call in onClickListener method because on first time of click it set to get focus and open soft keyboard"
I have a grid view with images in it. Once one is clicked it should go to PlayVideoActivity but onItemClick never get called.
I followed in the debugger and tried clicking the image but it never fired onItemClick.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_videos);
gridView = (GridView) findViewById(R.id.gridView);
customGridAdapter = new GridViewAdapter(this, R.layout.row_grid,
getData());
gridView.setAdapter(customGridAdapter);
gridView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> a, View v, int i, long l) {
Toast.makeText(videoChannelActivity.this, i + "#Selected",
Toast.LENGTH_SHORT).show();
Intent myIntent = new Intent(videoChannelActivity.this, PlayVideoActivity.class);
startActivity(myIntent);
}
});
}
row_grid.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginTop="5dp"
android:background="#drawable/grid_color_selector"
android:clickable="true"
android:focusable="true"
android:gravity="center"
android:orientation="vertical"
android:padding="5dp" >
<ImageView
android:id="#+id/image"
android:layout_width="280dp"
android:layout_height="158dp"
android:contentDescription="#string/desc"
android:scaleType="centerInside" />
<TextView
android:id="#+id/text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:gravity="center"
android:textSize="12sp" >
</TextView>
Add android:descendantFocusability="blocksDescendants" attribute in parent layout of row_grid.xml
You should remove
android:clickable="true"
android:focusable="true"
So I have simple listview with a list item that contains a textview, and two imageviews.
I want both of the imageviews to act as buttons. Unfortunately, I can't even get one of them to work. The list view loads but then the app crashes when either image is touched. Here is the xml for the listitem:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/bg_card">
<TextView
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:textColor="#color/Black"
android:id="#+id/name"
android:textSize="20sp"
android:padding="16dp">
</TextView>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_gravity="right"
android:gravity="right">
<ImageView
android:layout_width="40dp"
android:layout_height="fill_parent"
android:id="#+id/email"
android:clickable="true"
android:src="#drawable/ic_action_email"
android:layout_margin="8dp"/>
<View
android:layout_width="2dp"
android:layout_height="match_parent"
android:background="#color/LightGrey" />
<ImageView
android:layout_width="40dp"
android:layout_height="fill_parent"
android:id="#+id/internet"
android:clickable="true"
android:layout_margin="8dp"
android:src="#drawable/ic_action_web_site" />
</LinearLayout>
</LinearLayout>
Here is the layout with the listview:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="#+id/lvDepartments"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:divider="#android:color/transparent"
android:dividerHeight="8dp"
android:padding="8dp"
android:smoothScrollbar="true"
android:scrollbarStyle="insideOverlay"
android:listSelector="#color/Navy"/>
</LinearLayout>
Here is the related java code:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.staff_layout);
staffmemberlist = (ListView)findViewById(R.id.lvDepartments);
ArrayAdapter<String> arrayAdapter =new ArrayAdapter<String> (getApplicationContext(),R.layout.emailbrowsingcard,R.id.name, d);
staffmemberlist.setAdapter(arrayAdapter);
mailbutton=(ImageView)findViewById(R.id.email);
mailbutton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(),
"This line would appear on clicking this icon",
Toast.LENGTH_LONG).show();
}
});
}
this isnt going to work because the ImageView is in the item list so you should consider doing..
mailButton = (ImageView) row.findViewById(R.id.email);
where row is the item list. remember the ImageView isn't in staff_layout, it is in your item_layout.
to make it works you need to move this block:
mailbutton=(ImageView)findViewById(R.id.email);
mailbutton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(),
"This line would appear on clicking this icon",
Toast.LENGTH_LONG).show();
}
});
to the getView() in your adapter class. and let the View get findViewById or have an instance of the item_layout view in order to reach the buttons.
you should use a custom adapter for that and in custom adapter use findViewById for finding imageviews and setting listener
this is a good tutorial: http://www.vogella.com/articles/AndroidListView/article.html