Has anyone encountered such an issue before? The new value of the spinner overlaps the old one instead of replacing it.
This happens on application startup. Additionally, I've found that it happens on my tablet, but not on my phones.
Relevant code:
spinnerSearchField.setSelection(sharedPref.getInt(
getString(R.string.lastChosenSpinnerValue), 0), false);
spinnerSearchField.setOnItemSelectedListener(
new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
SharedPreferences.Editor editor = sharedPref.edit();
editor.putInt(getString(R.string.lastChosenSpinnerValue),
spinnerSearchField.getSelectedItemPosition());
editor.apply();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
// do nothing
}
});
My layout file:
<LinearLayout
android:id="#+id/mainLayout"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
>
<TextView
android:layout_marginTop="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:text="#string/textViewSearchField"
/>
<Spinner
android:id="#+id/spinnerSearchField"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="10dp"
android:layout_marginBottom="30dp"
android:entries="#array/arraySearchFields"
/>
...
</LinearLayout>
</ScrollView>
Related
I'm creating a list view in an android app and each item expands and collapses.
Here is each item's code:
<?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="wrap_content"
android:background="#drawable/alarme_layout"
android:backgroundTint="#color/colorPrimaryDark"
android:padding="20dp">
<TextView
android:id="#+id/textView_aHora"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:text="00:00"
android:textSize="26sp"
android:textColor="#color/colorOnPrimary"
android:layout_marginBottom="10dp"/>
<Switch
android:id="#+id/switch_aEstado"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true" />
<LinearLayout
android:id="#+id/layout_ultimaLinhaCollapsed"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/textView_aHora"
android:layout_alignParentStart="true">
<LinearLayout
android:id="#+id/layout_diasRepetir"
android:orientation="horizontal"
android:layout_width="0px"
android:layout_weight=".9"
android:layout_height="25dp">
</LinearLayout>
<ImageView
android:id="#+id/imageView_arrowDown"
android:layout_width="0px"
android:layout_weight=".1"
android:layout_height="wrap_content"
android:tint="#color/colorOnPrimary"
android:src="#drawable/ic_keyboard_arrow_down_24dp"
android:focusable="true"
android:clickable="true"/>
</LinearLayout>
<RelativeLayout
android:id="#+id/layout_detalhesExpanded"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/textView_aHora">
<CheckBox
android:id="#+id/checkbox_repetir"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:text="#string/repetir"
android:textColor="#color/colorOnPrimary"
android:foregroundTint="#color/colorSecondary"/>
<LinearLayout
android:id="#+id/layout_diasRepetirButtons"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/checkbox_repetir"
android:layout_alignStart="#+id/checkbox_repetir"
android:layout_marginTop="10dp"
android:visibility="invisible">
</LinearLayout>
<LinearLayout
android:id="#+id/ultimaLinhaExpanded"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/layout_diasRepetirButtons"
android:layout_marginTop="10dp">
<EditText
android:id="#+id/editText_mensagem"
android:layout_width="0px"
android:layout_weight=".9"
android:layout_height="wrap_content"
android:hint="#string/mensagemHint"
android:inputType="text"
android:backgroundTint="#color/colorOnPrimary"
android:foregroundTint="#color/colorSecondary"
android:textColor="#color/colorSecondary"
android:shadowColor="#color/colorSecondary"
android:textColorHighlight="#color/colorSecondaryLight" />
<ImageView
android:id="#+id/imageView_arrowUp"
android:layout_width="0px"
android:layout_weight=".1"
android:layout_height="wrap_content"
android:tint="#color/colorOnPrimary"
android:src="#drawable/ic_keyboard_arrow_up_24dp"
android:focusable="true"
android:clickable="true"/>
</LinearLayout>
</RelativeLayout>
</RelativeLayout>
And here is the "getView()" method of list view:
public View getView(final int position, View convertView, ViewGroup parent) {
convertView = getLayoutInflater().inflate(R.layout.alarme, null);
final Alarme alarme = alarmes.get(position);
//main view
TextView hora = convertView.findViewById(R.id.textView_aHora);
Switch estado = convertView.findViewById(R.id.switch_aEstado);
LinearLayout layoutDiasRepetir = convertView.findViewById(R.id.layout_diasRepetir);
final ImageView arrowDown = convertView.findViewById(R.id.imageView_arrowDown);
//detalhes
final RelativeLayout layoutDetalhesExpanded = convertView.findViewById(R.id.layout_detalhesExpanded);
CheckBox cbRepetir = convertView.findViewById(R.id.checkbox_repetir);
LinearLayout layoutDiasRepetirButtons = convertView.findViewById(R.id.layout_diasRepetirButtons);
EditText editText = convertView.findViewById(R.id.editText_mensagem);
final ImageView arrowUp = convertView.findViewById(R.id.imageView_arrowUp);
layoutDetalhesExpanded.setVisibility(View.INVISIBLE);
hora.setText(alarme.getHora());
estado.setChecked(alarme.isLigado());
cbRepetir.setChecked(alarme.isRepete());
arrowDown.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
arrowDown.setVisibility(View.INVISIBLE);
arrowUp.setVisibility(View.VISIBLE);
layoutDetalhesExpanded.setVisibility(View.VISIBLE);
alarme.setCollapsed(false);
}
});
arrowUp.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
arrowUp.setVisibility(View.INVISIBLE);
arrowDown.setVisibility(View.VISIBLE);
layoutDetalhesExpanded.setVisibility(View.INVISIBLE);
alarme.setCollapsed(true);
}
});
editText.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
#Override
public void afterTextChanged(Editable s) {
alarme.setMensagem(s.toString());
}
});
arrowDown.setVisibility(alarme.isCollapsed() ? View.VISIBLE : View.INVISIBLE);
arrowUp.setVisibility(alarme.isCollapsed() ? View.INVISIBLE : View.VISIBLE);
layoutDetalhesExpanded.setVisibility(alarme.isCollapsed() ? View.INVISIBLE : View.VISIBLE);
editText.setText(alarme.getMensagem());
return convertView;
}
I thought each item would have its size wrapped depending on the visible content, but I have this result instead:
This is the item when expanded:
https://imgur.com/gallery/oIv50dT
This is the item when collapsed:
https://imgur.com/gallery/dmzw5GU
I wanted the collapsed item to have its size wrapped to the content.
I am going to guess here that in your XML layout you have set a static height.
You need to set it to be wrap_content which allows for dynamic row heights.
I solved it by changing the visibility of the views from View.INVISIBLE to View.GONE.
I'm stumped. This one IS simple and I'm missing something simple but I'm just not getting anywhere.
I have a ListView that contains pretty simple items - TextView's that contain text of the form "N. name", where N is the position in the list and name is the name of the associated objects. I've created a ListAdapter that presents that information into the ListView.
The presentation stuff seems to work fine. It's the part where when I tap on a row that's not working. I just don't get a notification. If I set a onClickListener on the item (when it's generated in the Adapter) I do get a notification, but it's convoluted to make that work the way I'd like it to. I've tried a bunch of stuff - changing descendent focusability, enabling, disabling, setting itemsCanFocus to false, ... - but no luck on any of them.
I've included relevant code for configuring the view and the elements in the ListView.
In particular, what I'm trying to do is get that OnItemClickLister.onItemClick() to get invoked when I tap on a row. Any thoughts on what I'm doing wrong?
Activity:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
model = FZMModel.getInstance(this);
setContentView(R.layout.activity_deploy);
TextView viewTitle = (TextView) findViewById(R.id.deploy_header);
if (model.getProfiles().size() == 0) {
viewTitle.setText(R.string.deploy_header_create);
} else {
viewTitle.setText(R.string.deploy_header_choose);
}
profileListView = (ListView) findViewById(R.id.profileListView);
profileListAdapter = new ProfileListAdapter(this, model);
profileListView.setAdapter(profileListAdapter);
profileListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
Log.d(TAG, String.format("Item %d selected (with id=0x%x)", position, id));
}
});
}
Adapter:
... relevant code from getView():
#Override
public View getView(int position, View convertView, ViewGroup parent) {
final Profile profile = model.getProfileAtIndex(position);
final int rowNum = position;
if (convertView == null) {
LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(R.layout.profile_list_layout, parent, false);
}
TextView profileIndexView;
TextView profileNameView;
profileIndexView = (TextView) convertView.findViewById(R.id.profileIndexLabel);
profileNameView = (TextView) convertView.findViewById(R.id.profileNameLabel);
profileIndexView.setText(String.format("%d.", position));
profileNameView.setText(profile.getName());
// convertView.setOnClickListener(new View.OnClickListener() {
// #Override
// public void onClick(View view) {
// Toast.makeText(context, String.format("Did select row #%d (index=%d)",rowNum, profile.getPosition()), Toast.LENGTH_SHORT).show();
// }
// });
return convertView;
}
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"
tools:context="com.nsn.flexizonemobile.FZMConcreteActionDeploy"
tools:ignore="MergeRootFrame" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="#string/deploy_header_choose"
android:id="#+id/deploy_header"
android:layout_margin="10dp"
android:layout_centerHorizontal="true" />
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/profileListView"
android:layout_gravity="left|top"
android:layout_alignParentLeft="true"
android:layout_marginLeft="0dp"
android:choiceMode="none"
android:layout_below="#+id/deploy_header"
android:divider="#android:color/darker_gray"
android:dividerHeight="2dp"
android:clickable="true"
android:descendantFocusability="beforeDescendants" />
</RelativeLayout>
profile_list_layout layout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="#dimen/profile_index_width"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="A. "
android:id="#+id/profileIndexLabel"
android:gravity="center_vertical|right"
android:layout_centerVertical="true"
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:textSize="24sp"
android:textIsSelectable="true"
android:layout_alignParentStart="false"
android:layout_alignParentTop="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Profile Name"
android:id="#+id/profileNameLabel"
android:layout_alignParentLeft="false"
android:layout_alignParentTop="false"
android:gravity="center_vertical"
android:layout_toRightOf="#+id/profileIndexLabel"
android:layout_marginLeft="10dp"
android:layout_centerVertical="true"
android:textSize="24sp"
android:paddingTop="10dp"
android:paddingBottom="10dp" />
</RelativeLayout>
in the textview of your inflator layout just add:
android:focusable="false"
android:focusableInTouchMode="false"
and in parent layout of your inflator i.e in relative layout of inflator add:
android:descendantFocusability="blocksDescendants"
remove :
android:descendantFocusability="beforeDescendants"
from your listview. and set height of your listview to match_parent will solve your issue.
remove following attributes from your ListView:
android:choiceMode="none"
android:clickable="true"
android:descendantFocusability="beforeDescendants"
also
android:layout_width="wrap_content"
android:layout_height="wrap_content"
makes no sense in this context (especially height)
I have generated a swiping ListView using a SimpleAdapter ( I have used the com.fortysevendeg.swipelistview.SwipeListView to generate the ListView ). I have done the following in it. When i swipe the ListView the front layout gets highlighted as Black and the color of a ImageView changes to Orange. It works fine when I have only one row in the ListView. But I noticed that when there are more than one row, whichever row is clicked the lastrow gets highlighted to Black. Can anyone tell me step by step how to fix this. I want to proceed with SimpleAdapter itself,but any other solutions are also welcomed. My codes are as below.Please go through it.
public class mycontacts extends Fragment{
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final View view = inflater.inflate(R.layout.mycontacts_activity, container,
false);
final SwipeListView swipy=(SwipeListView)view.findViewById(R.id.swipy);
SimpleAdapter k=new SimpleAdapter(getActivity(),val,R.layout.contact2,new String[]{"name","path","id"},new int[]{R.id.example_itemname,R.id.path,R.id.contactid})
{
#Override
public View getView (int position, View convertView, ViewGroup parent)
{
final RelativeLayout l=(RelativeLayout)v.findViewById(R.id.front);
swipy.setSwipeListViewListener(new BaseSwipeListViewListener(){
#Override
public void onClickFrontView(int position) {
// TODO Auto-generated method stub
super.onClickFrontView(position);
l.setBackgroundColor(Color.BLACK);
}
#Override
public void onStartOpen(int position, int action, boolean right) {
// TODO Auto-generated method stub
super.onStartOpen(position, action, right);
}
});
return v;
}
};
swipy.setAdapter(k);
return view;
}
contact2.xml file
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:id="#+id/back"
android:layout_width="fill_parent"
android:layout_height="60dp"
android:background="#android:color/white"
android:tag="back" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="68dp"
android:layout_height="fill_parent"
android:adjustViewBounds="true"
android:background="#color/purple"
android:clickable="true"
android:scaleType="centerInside"
android:src="#android:drawable/btn_star" />
</LinearLayout>
<RelativeLayout
android:id="#+id/front"
android:layout_width="fill_parent"
android:layout_height="60dp"
android:background="#android:color/white"
android:orientation="vertical"
android:tag="front" >
<TextView
android:id="#+id/example_itemname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/path"
android:layout_marginLeft="20dp"
android:layout_toRightOf="#+id/example_image"
android:text="hiii"
android:textColor="#android:color/black"
android:textSize="18sp" />
<TextView
android:id="#+id/path"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/example_itemname"
android:layout_alignParentTop="true"
android:text="path"
android:textAppearance="?android:attr/textAppearanceSmall"
android:visibility="invisible" />
<TextView
android:id="#+id/contactid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/example_image"
android:layout_alignParentTop="true"
android:text="Id"
android:textAppearance="?android:attr/textAppearanceSmall"
android:visibility="gone" />
</RelativeLayout>
</FrameLayout>
mycontacts_activity.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:swipe="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:id="#+id/mycontacts_id"
android:layout_height="match_parent">
<com.fortysevendeg.swipelistview.SwipeListView
android:id="#+id/swipy"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
swipe:swipeFrontView="#+id/front"
swipe:swipeBackView="#+id/back"
swipe:swipeMode="right"
/>
</LinearLayout>
I have a Spinner in my class with certain items. I have to display an EditText whose visibility I have set to "Invisible". I need to show this EditText when "Others" item is selected in the spinner.
Here is my code:-
Activity class:-
protected void onCreate(Bundle savedInstanceState)
{
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.maintenance);
ETServiceStation = (EditText)findViewById(R.id.ETServiceStation);
ETServiceType = (EditText)findViewById(R.id.ETServiceType);
ETServiceCost= (EditText)findViewById(R.id.ETServiceCost);
spinServiceType = (Spinner)findViewById(R.id.spinServiceType);
btnMaintenenceSave= (ImageButton)findViewById(R.id.btnMaintenenceSave);
btnMaintenanceClear = (ImageButton)findViewById(R.id.btnMaintenenceClear);
spinServiceType.setOnItemSelectedListener(this);
btnMaintenanceClear.setOnClickListener(this);
btnMaintenenceSave.setOnClickListener(this);
ArrayAdapter<String> serviceTypeAdapter = new ArrayAdapter<String>(getApplicationContext(), R.array.service_type_array, android.R.layout.simple_spinner_item);
serviceTypeAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinServiceType.setAdapter(serviceTypeAdapter);
}
public void onItemSelected(AdapterView<?> parent, View view, int servicePos, long id)
{
// TODO Auto-generated method stub
spinnerServiceType = spinServiceType.getItemAtPosition(servicePos).toString();
if(spinnerServiceType.matches("Others"))
{
ETServiceType = (EditText)findViewById(R.id.ETServiceType);
ETServiceType.setVisibility(View.VISIBLE);
spinnerServiceType = ETServiceType.getText().toString();
}
serviceType = spinnerServiceType;
}
}
Resource:-
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="service_type_array">
<item>Full Service</item>
<item value="serviceother">Other</item>
</string-array>
</resources>
maintenance.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"
android:padding="30dp">
<TextView
android:id="#+id/txtMTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Maintenance"
android:layout_gravity="center_horizontal"
android:textSize="25sp"
android:textColor="#color/SteelBlue"
android:typeface="sans"/>
<TextView
android:layout_marginTop="60dp"
android:id="#+id/txtServiceStation"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Service station name:" />
<EditText
android:id="#+id/ETServiceStation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10" >
<requestFocus />
</EditText>
<TextView
android:id="#+id/txtServiceType"
android:layout_marginTop="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Service type: "/>
<Spinner
android:id="#+id/spinServiceType"
android:layout_marginTop="10dp"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<EditText
android:id="#+id/ETServiceType"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="numberDecimal"
android:visibility="invisible"
android:ems="10" />
<TextView
android:id="#+id/txtServiceCost"
android:layout_marginTop="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Servicing expence:" />
<EditText
android:id="#+id/ETServiceCost"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="numberDecimal"
android:ems="10" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="40dp"
android:layout_marginLeft="50dp"
android:orientation="horizontal">
<ImageButton
android:id="#+id/btnMaintenenceSave"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#color/white"
android:src="#drawable/fuelbtnsaveclick" />
<ImageButton
android:id="#+id/btnMaintenenceClear"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:background="#color/white"
android:src="#drawable/fuelbtnclearclick" />
</LinearLayout>
</LinearLayout>
Database:
public long createMainEntry(MaintenanceExpense me)
{
ContentValues mcv = new ContentValues();
mcv.put(KEY_SERVICE_STATION,me.getServiceStation());
mcv.put(KEY_SERVICE_TYPE,me.getServiceType());
mcv.put(KEY_SERVICE_COST,me.getServiceCost());
return otherInfoDatabase.insert(MAINTENANCE_DATABASE_TABLE, null, mcv);
}
I have to display the ETServiceType when "Others" item is clicked.
Thanks for any help.
public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
{
if(spinServiceType.getItemAtPosition(position).toString().equalsIgnorecase("Others")
{
editextObject.setVisibility(View.VISIBLE);
}
}
public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
{
// TODO Auto-generated method stub
spinnerServiceType = spinServiceType.getItemAtPosition(position).toString();
//if spinnerServiceType 's value is other
//then set visibility of textview visible put text watcher on it so u can get text when user finished ..if u dont want then u can use simple getText then change its visibility
//As u are setting your array value from resource you cant update it programmatically
//so first of all u have to take its array items in list . so u can update this list after user enter the value in edit text
}
I have used a Custom ListView and I am displaying some data using the same ListView. When I click on the List View item, the onClickListener is not getting called. I am not able to select any list item.
Layout Code:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_marginBottom="16dp"
android:background="#drawable/list_selector"
android:clickable="true"
android:orientation="horizontal"
android:padding="5dip" >
<LinearLayout
android:id="#+id/imgProperty"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginRight="5dip"
android:padding="3dip" >
<ImageView
android:id="#+id/list_image"
android:layout_width="50dp"
android:layout_height="50dp"
android:contentDescription="#string/app_name"
android:src="#drawable/ic_launcher"
android:focusable="false"/>
</LinearLayout>
<TextView
android:id="#+id/tvCity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="75dip"
android:layout_toRightOf="#+id/list_image"
android:paddingBottom="10dip"
android:text="property"
android:textColor="#040404"
android:textSize="15sp"
android:textStyle="bold"
android:typeface="sans" />
<TextView
android:id="#+id/tvprice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/imgProperty"
android:layout_alignLeft="#+id/tvCity"
android:text="Price"
android:focusable="false"/>
</RelativeLayout>
Adapter code:
public class CustomListAdapter extends BaseAdapter {
ArrayList<Propety> PropertiesArray;
private LayoutInflater Inflater;
public CustomListAdapter(ArrayList<Propety> PropertiesArray) {
this.PropertiesArray=PropertiesArray;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return PropertiesArray.size();
}
#Override
public Object getItem(int position) {
return PropertiesArray.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, final ViewGroup parent) {
if (convertView == null) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
convertView = inflater.inflate(R.layout.customlistview, parent, false);
}
final Propety ListArray = PropertiesArray.get(position);
TextView tvPropertyName = (TextView) convertView.findViewById(R.id.tvCity);
tvPropertyName.setText(ListArray.getName());
TextView tvPrice = (TextView) convertView.findViewById(R.id.tvprice);
tvPrice.setText(ListArray.getPrice());
ImageView imgProperty = (ImageView) convertView.findViewById(R.id.list_image);
imgProperty.setImageResource(R.drawable.ic_launcher);
convertView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(parent.getContext(), "view clicked: " + ListArray.getName(), Toast.LENGTH_SHORT).show();
}
});
return convertView;
}
}
ListView code:
ListView propertylistview = (ListView) findViewById(R.id.listview);
CustomListAdapter customlistview=new CustomListAdapter(PropertiesArray);
propertylistview.setAdapter(customlistview);
ListView XML:
<ListView
android:id="#+id/listview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/customview"
android:layout_marginBottom="10dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="24dp"
android:background="#drawable/list_selector"
android:textAlignment="center" >
custom.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<SurfaceView
android:id="#+id/surface"
android:layout_width="fill_parent"
android:layout_height="match_parent" />
<TextView
android:id="#+id/txtangle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_marginBottom="115dp"
android:layout_marginLeft="95dp"
android:text="" />
<ListView
android:id="#+id/listview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/customview"
android:layout_marginBottom="10dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="24dp"
android:background="#drawable/list_selector"
android:textAlignment="center" >
</ListView>
<view
android:id="#+id/customview"
android:layout_width="110dp"
android:layout_height="110dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
class="com.example.samplebuapp.CustomCompass" />
<view
android:id="#+id/view1"
android:layout_width="110dp"
android:layout_height="110dp"
android:layout_above="#+id/listview"
android:layout_alignParentRight="true"
android:layout_marginRight="18dp"
class="com.example.samplebuapp.CustomView" />
<LinearLayout
android:id="#+id/rl1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/listview"
android:orientation="vertical"
android:focusable="false">
</LinearLayout>
</RelativeLayout>
Even scrolling is not working.
I am unable to figure out why is this happening? Am I missing out something?
Any help in resolving this is appreciated.
The following will do the job in your case.
ListView propertylistview = (ListView) findViewById(R.id.listview);
propertylistview.setOnItemClickListener( myListViewClicked ):
OnItemClickListener myListViewClicked = new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(YourActivity.this, "Clicked at positon = " + position, Toast.LENGTH_SHORT).show();
}
};
Dont forget to remove the following from the CustomAdapter
convertView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(parent.getContext(), "view clicked: " + ListArray.getName(), Toast.LENGTH_SHORT).show();
}
});
If the touch events are getting intercepted inside the cell layout, in the layout for your custom cell, set
android:descendantFocusability="blocksDescendants" on the top level layout of your cell. For me, it was too much of a pain to add xml attributes to every individual view.
Note that this can also be set in code:
cell.setDescendantFocusability(FOCUS_BLOCK_DESCENDANTS);
I also tried this with a ListView inside a cell, and I had to override onTouchEvent to get it to work:
public class NoTouchListView extends ListView {
#Override
public boolean onTouchEvent(MotionEvent ev) {
return false;
}
}
the simple code that solved my problem, and method view.setOnClickListener be within my custom adapter
view.setFocusable(false)
Check your list row layout once :
layout or any view should not be `android:clickable="true" and
android:focusable="true"
if it is there just delete and run the application again.
Even I was having the same problem, I am having checkbox, did the following to masker itemClickListener work,
Added the following properties to the checkbox,
android:focusable="false"
android:focusableInTouchMode="false"
android:clickable="false"
and ItemClickListner started working.
For detailed example you can go through the link,
http://knowledge-cess.com/android-itemclicklistner-with-checkbox-or-radiobutton/
Hope it helps Cheers!!
For TextView you should also use
android:textIsSelectable="false"
https://developer.android.com/reference/android/widget/TextView.html#setTextIsSelectable