My listview's onItemClick method is not being called when on API version 23. On version prior to that it's working great.
I've read about this issue Issue and it seems like it might relate. Any ideas?
The code is pretty straightforward, nothing fancy. It works on all version below API 23.
List item layout:
<TextView
android:id="#+id/history_member_A"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:textSize="#dimen/history_item_text_size" />
<TextView
android:id="#+id/operator"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:textSize="#dimen/history_item_text_size" />
<TextView
android:id="#+id/history_member_B"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:textSize="#dimen/history_item_text_size" />
<TextView
android:id="#+id/equal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:text=" = "
android:textSize="#dimen/history_item_text_size" />
<TextView
android:id="#+id/history_result"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:textSize="#dimen/history_item_text_size" />
The layout the list sits in:
<?xml version="1.0" encoding="utf-8"?>
<keyboard.HistoryLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/history_layout"
android:layout_width="match_parent"
android:layout_height="#dimen/history_height"
android:animateLayoutChanges="true"
android:orientation="vertical">
<keyboard.HistoryView
android:id="#+id/history"
android:layout_width="match_parent"
android:layout_height="#dimen/history_height"
android:background="#android:color/white" />
</keyboard.HistoryLayout>
public class HistoryView extends ListView implements AdapterView.OnItemClickListener {
private HistoryAdapter mAdapter;
public HistoryView(Context context) {
super(context);
init();
}
public HistoryView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public HistoryView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
private void init() {
mAdapter = new HistoryAdapter();
setOnItemClickListener(this);
setAdapter(mAdapter);
}
#Override
protected void onFinishInflate() {
super.onFinishInflate();
}
public void loadHistory() {
ArrayList<CalculationItem> items = CalcManager.getInstance().getHistoryItems();
mAdapter.updateData(items);
}
public void update() {
mAdapter.notifyDataSetChanged();
smoothScrollToPosition(mAdapter.getCount() - 1);
}
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(getContext(), "Clicked", Toast.LENGTH_SHORT).show();
}
}
Related
using Android studio, I cannot find anything of my customized gridview in my design view.
Android Studio Arctic Fox | 2020.3.1 Patch 3
Build #AI-203.7717.56.2031.7784292, built on October 1, 2021
Runtime version: 11.0.10+0-b96-7249189 amd64
VM: OpenJDK 64-Bit Server VM by Oracle Corporation
Windows 10 10.0
GC: G1 Young Generation, G1 Old Generation
Memory: 1280M
Cores: 8
Registry: external.system.auto.import.disabled=true, debugger.watches.in.variables=false, compiler.automake.allow.when.app.running=true, ide.balloon.shadow.size=0
First of all, one of my customized gridview which can be seen is showing below:
public class PickWaveListGridVIew extends GridView {
public PickWaveListGridVIew(Context context, AttributeSet attrs) {
super(context, attrs);
initialize();
}
private void initialize() {
List<WaveListItemDTO> dataList = requestForWaveList();
setAdapter(new PickWaveListAdapter(dataList, getContext()));
}
private List<WaveListItemDTO> requestForWaveList() {
//todo demo data
return Lists.newArrayList(new WaveListItemDTO("S202205250001", "S202205250001", "2022-05-25 10:42:01"),
new WaveListItemDTO("S202205250002", "S202205250002", "2022-05-25 10:42:02"),
new WaveListItemDTO("S202205250003", "S202205250003", "2022-05-25 10:42:03")
);
}
/**
* dto class of demo
*/
private static class WaveListItemDTO {
private String waveId;
private String waveCode;
private String createTime;
public WaveListItemDTO() {
}
public WaveListItemDTO(String waveId, String waveCode, String createTime) {
this.waveId = waveId;
this.waveCode = waveCode;
this.createTime = createTime;
}
}
private class PickWaveListAdapter extends BaseAdapter {
private final List<WaveListItemDTO> dataList;
private LayoutInflater menuInflater;
public PickWaveListAdapter(List<WaveListItemDTO> dataList, Context context) {
this.dataList = dataList;
this.menuInflater = LayoutInflater.from(context);
}
#Override
public int getCount() {
return this.dataList.size();
}
#Override
public Object getItem(int position) {
return this.dataList.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
//load layout
View layout = this.menuInflater.inflate(R.layout.item_clickable_pick_wave_list_button, parent, false);
TextView waveId = layout.findViewById(R.id.item_clickable_pick_wave_list_button_wave_id);
TextView datetime = layout.findViewById(R.id.item_clickable_pick_wave_list_button_datetime);
WaveListItemDTO dto = this.dataList.get(position);
//set data to views
waveId.setText(dto.waveCode == null ? dto.waveId : dto.waveCode);
datetime.setText(dto.createTime);
return layout;
}
private <T extends BaseFunctionPageActivity> void openDetailOperationActivity(
BaseActivity currentActivity,
Class<T> clz,
String title,
String waveId) {
ActivityUtils.openNewActivity(currentActivity,
clz,
true,
null,
new JSONObjectBuilder()
.put(Constants.INTENT_PASSING_DATA_JSON_TITLE_KEY, title)
.put(Constants.INTENT_PASSING_DATA_JSON_WAVE_ID_KEY, waveId)
.build()
);
}
}
}
customized gridview R.layout.item_clickable_pick_wave_list_button
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="90dp"
android:background="#drawable/item_text_view_underline">
<!--拣货波次列表Item-->
<androidx.constraintlayout.widget.Guideline
android:id="#+id/item_clickable_pick_wave_list_button_splitter_mid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintGuide_percent="0.45"
android:orientation="vertical" />
<androidx.constraintlayout.widget.Guideline
android:id="#+id/item_clickable_pick_wave_list_button_splitter_left"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintGuide_percent="0.03"
android:orientation="vertical" />
<TextView
android:id="#+id/item_clickable_pick_wave_list_button_wave_id"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="S202205250001"
android:textSize="18sp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toRightOf="#+id/item_clickable_pick_wave_list_button_splitter_left"
app:layout_constraintRight_toLeftOf="#+id/item_clickable_pick_wave_list_button_splitter_mid" />
<ImageButton
android:id="#+id/item_clickable_pick_wave_list_button_img_btn"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginEnd="10dp"
android:src="#drawable/right"
android:background="#color/transparent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintRight_toRightOf="parent" />
<TextView
android:id="#+id/item_clickable_pick_wave_list_button_datetime"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="2022-05-25 10:42:01"
android:textSize="18sp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toRightOf="#+id/item_clickable_pick_wave_list_button_splitter_mid" />
</androidx.constraintlayout.widget.ConstraintLayout>
layout xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/activity_mws_pick_wave_list_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.originlinks.wmspda.activities.wms.pick.PickWaveListGridVIew
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintTop_toTopOf="#+id/activity_mws_pick_wave_list_layout"
app:layout_constraintBottom_toBottomOf="#+id/activity_mws_pick_wave_list_layout"
android:numColumns="1"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
it is normal to see my demo data in design view like this.
So I decided to develop another customized gridview in exactly the same way, I found it failed to show in design view.
Here below is the code which has problems:
my customize gridview
public class ProductListGridView extends GridView {
private List<ProductListDTO> productList;
private List<String> itemIdList;
public ProductListGridView(Context context) {
super(context);
initialize(context);
}
public ProductListGridView(Context context, AttributeSet attrs) {
super(context, attrs);
initialize(context);
}
public ProductListGridView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initialize(context);
}
public ProductListGridView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
initialize(context);
}
private void initialize(Context context) {
this.productList = getProductListOfSpecifiedWaveId();
this.itemIdList = this.productList.stream().map(e -> e.shelveCode + e.sku).collect(Collectors.toList());
setAdapter(new ProductItemAdapter(productList, getContext()));
}
private List<ProductListDTO> getProductListOfSpecifiedWaveId() {
//todo demo data
return Lists.newArrayList(
new ProductListDTO("F2-A01-001", "020111", "demo product name", 20, null),
new ProductListDTO("F2-A01-002", "020112", "demo product name", 20, null),
new ProductListDTO("F2-A01-003", "020113", "demo product name", 20, null),
new ProductListDTO("F2-A01-004", "020114", "demo product name", 20, null),
new ProductListDTO("F2-A01-005", "020115", "demo product nameK", 20, null)
);
}
private static class ProductListDTO {
private String shelveCode;
private String sku;
private String name;
private Integer number2Pick;
private Integer numberPicked;
private String imgUrl;
public ProductListDTO() {
}
public ProductListDTO(String shelveCode, String sku, String name, Integer number2Pick, String imgUrl) {
this.shelveCode = shelveCode;
this.sku = sku;
this.name = name;
this.number2Pick = number2Pick;
this.imgUrl = imgUrl;
this.numberPicked = 0;
}
}
private static class ProductItemAdapter extends BaseAdapter {
private final List<ProductListDTO> productList;
private final LayoutInflater menuInflater;
private final Context context;
public ProductItemAdapter(List<ProductListDTO> productList, Context context) {
this.productList = productList;
this.menuInflater = LayoutInflater.from(context);
this.context = context;
}
#Override
public int getCount() {
return this.productList.size();
}
#Override
public Object getItem(int position) {
return this.productList.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
//inflate the gridview and get the needed controllers
View layout = this.menuInflater.inflate(R.layout.item_clickable_pick_product_list_button, parent, false);
TextView shelveCode = layout.findViewById(R.id.item_clickable_pick_product_list_button_shelve_code);
TextView sku = layout.findViewById(R.id.item_clickable_pick_product_list_button_sku);
TextView name = layout.findViewById(R.id.item_clickable_pick_product_list_button_name);
TextView processNum = layout.findViewById(R.id.item_clickable_pick_product_list_button_process);
ImageView img = layout.findViewById(R.id.item_clickable_pick_product_list_button_pic);
//set the controllers' value by specified dto data
ProductListDTO dto = this.productList.get(position);
shelveCode.setText(dto.shelveCode);
sku.setText(dto.sku);
name.setText(dto.name);
String initialPickNum = dto.numberPicked.toString() + "/" + dto.number2Pick.toString();
processNum.setText(initialPickNum);
//async download images from url
ImageLoader imageLoader = new ImageLoader(this.context);
imageLoader.disPlayImage(dto.imgUrl, img);
return layout;
}
}
}
customized gridview R.layout.item_clickable_pick_product_list_button
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="150dp"
android:background="#drawable/item_text_view_underline">
<!--拣货商品明细列表的图文按钮组件-->
<androidx.constraintlayout.widget.Guideline
android:id="#+id/item_clickable_pick_product_list_button_splitter_left"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintGuide_begin="10dp"
android:orientation="vertical" />
<androidx.constraintlayout.widget.Guideline
android:id="#+id/item_clickable_pick_product_list_button_splitter_right"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintGuide_end="10dp"
android:orientation="vertical" />
<androidx.constraintlayout.widget.Guideline
android:id="#+id/item_clickable_pick_product_list_button_splitter_top"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintGuide_begin="10dp"
android:orientation="horizontal" />
<androidx.constraintlayout.widget.Guideline
android:id="#+id/item_clickable_pick_product_list_button_splitter_bottom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintGuide_end="10dp"
android:orientation="horizontal" />
<androidx.constraintlayout.widget.Guideline
android:id="#+id/item_clickable_pick_product_list_button_splitter_pic"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintGuide_begin="150dp"
android:orientation="vertical" />
<ImageView
android:id="#+id/item_clickable_pick_product_list_button_pic"
android:layout_width="130dp"
android:layout_height="130dp"
app:layout_constraintTop_toBottomOf="#id/item_clickable_pick_product_list_button_splitter_top"
app:layout_constraintLeft_toRightOf="#id/item_clickable_pick_product_list_button_splitter_left"
app:layout_constraintBottom_toTopOf="#id/item_clickable_pick_product_list_button_splitter_bottom"
android:scaleType="centerInside"
android:src="#drawable/empty_pic"
android:background="#drawable/item_text_view_surround_border" />
<!--文本框标题-->
<TextView
android:id="#+id/item_clickable_pick_product_list_button_shelve_code_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="#id/item_clickable_pick_product_list_button_splitter_top"
app:layout_constraintLeft_toRightOf="#id/item_clickable_pick_product_list_button_splitter_pic"
app:layout_constraintRight_toLeftOf="#id/item_clickable_pick_product_list_button_shelve_code"
android:textColor="#color/black"
android:textSize="16sp"
android:text="库位号:" />
<TextView
android:id="#+id/item_clickable_pick_product_list_button_sku_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="#id/item_clickable_pick_product_list_button_shelve_code_title"
app:layout_constraintLeft_toRightOf="#id/item_clickable_pick_product_list_button_splitter_pic"
app:layout_constraintRight_toLeftOf="#id/item_clickable_pick_product_list_button_sku"
android:layout_marginTop="8dp"
android:textColor="#color/black"
android:textSize="16sp"
android:text="ERP-SKU:" />
<TextView
android:id="#+id/item_clickable_pick_product_list_button_name_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="#id/item_clickable_pick_product_list_button_sku_title"
app:layout_constraintLeft_toRightOf="#id/item_clickable_pick_product_list_button_splitter_pic"
app:layout_constraintRight_toLeftOf="#id/item_clickable_pick_product_list_button_name"
android:layout_marginTop="8dp"
android:textColor="#color/black"
android:textSize="16sp"
android:lines="2"
android:ellipsize="end"
android:text="名称:" />
<TextView
android:id="#+id/item_clickable_pick_product_list_button_process_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="#id/item_clickable_pick_product_list_button_name_title"
app:layout_constraintLeft_toRightOf="#id/item_clickable_pick_product_list_button_splitter_pic"
app:layout_constraintRight_toLeftOf="#id/item_clickable_pick_product_list_button_process"
android:layout_marginTop="8dp"
android:textColor="#color/red"
android:textSize="16sp"
android:text="应拣/已拣:" />
<!--具体内容-->
<TextView
android:id="#+id/item_clickable_pick_product_list_button_shelve_code"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="#id/item_clickable_pick_product_list_button_splitter_top"
app:layout_constraintLeft_toRightOf="#id/item_clickable_pick_product_list_button_shelve_code_title"
app:layout_constraintRight_toLeftOf="#id/item_clickable_pick_product_list_button_splitter_right"
android:textColor="#color/black"
android:textSize="16sp"
android:text="F2-A01-001" />
<TextView
android:id="#+id/item_clickable_pick_product_list_button_sku"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="#id/item_clickable_pick_product_list_button_shelve_code_title"
app:layout_constraintLeft_toRightOf="#id/item_clickable_pick_product_list_button_sku_title"
app:layout_constraintRight_toLeftOf="#id/item_clickable_pick_product_list_button_splitter_right"
android:layout_marginTop="8dp"
android:textColor="#color/black"
android:textSize="16sp"
android:lines="1"
android:ellipsize="marquee"
android:text="05-06-001-020111" />
<TextView
android:id="#+id/item_clickable_pick_product_list_button_name"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="#id/item_clickable_pick_product_list_button_sku_title"
app:layout_constraintLeft_toRightOf="#id/item_clickable_pick_product_list_button_name_title"
app:layout_constraintRight_toLeftOf="#id/item_clickable_pick_product_list_button_splitter_right"
android:layout_marginTop="8dp"
android:textColor="#color/black"
android:textSize="16sp"
android:lines="2"
android:ellipsize="end"
android:text="这是一个两行的名称挺长的1231231231234511111111" />
<TextView
android:id="#+id/item_clickable_pick_product_list_button_process"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="#id/item_clickable_pick_product_list_button_name_title"
app:layout_constraintLeft_toRightOf="#id/item_clickable_pick_product_list_button_process_title"
app:layout_constraintRight_toLeftOf="#id/item_clickable_pick_product_list_button_splitter_right"
android:layout_marginTop="8dp"
android:textColor="#color/red"
android:textSize="16sp"
android:text="0/20" />
</androidx.constraintlayout.widget.ConstraintLayout>
layout xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/activity_wms_multi_pick_dashboard_layout"
xmlns:app="http://schemas.android.com/apk/res-auto">
<com.originlinks.wmspda.activities.wms.pick.ProductListGridView
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintTop_toTopOf="#+id/activity_wms_multi_pick_dashboard_layout"
app:layout_constraintBottom_toBottomOf="#+id/activity_wms_multi_pick_dashboard_layout" />
</androidx.constraintlayout.widget.ConstraintLayout>
activity
public class DemoActivity extends ComponentActivity {
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ActivityWmsMultiPickDashboardBinding binding = ActivityWmsMultiPickDashboardBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
}
}
I can see nothing showing in the design view but when I open my virtual device, the grid view is correcty showed.
How can I make the gridview visible in my design view? Is there any thing wrong with my code?
I am having problem with "GridView".If I use 6 items then it is not creating problem while scrolling but as I increase no. of items its behavior is unpredictable.
Please check the following video link for better understanding my problem:
GridView Issues video link
I am attaching my codes here:
activity_achievements.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:id="#+id/header"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_alignParentTop="true"
android:background="#color/green_color">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp"
android:layout_toRightOf="#+id/back_iv"
android:text="Acheivements"
android:textColor="#color/white_color"
android:textSize="24sp"
android:textStyle="bold" />
<ImageView
android:id="#+id/back_iv"
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:src="#drawable/back" />
<TextView
android:id="#+id/total_achievements_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="10dp"
android:text="0/15"
android:textColor="#color/white_color"
android:textSize="24sp"
android:textStyle="bold" />
</RelativeLayout>
<GridView
android:id="#+id/acheivements_gv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/header"
android:columnWidth="90dp"
android:gravity="center"
android:horizontalSpacing="5dp"
android:numColumns="2"
android:stretchMode="columnWidth"
android:verticalSpacing="5dp" />
</RelativeLayout>
achievements_row.xml
<?xml version="1.0" encoding="utf-8"?>
<com.xyz.quitsmoking.view.AchievementsView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center">
<android.support.v7.widget.CardView
android:id="#+id/card_view2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/card_view1"
android:layout_gravity="center"
android:layout_marginBottom="5dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="5dp"
card_view:cardCornerRadius="2dp"
android:background="#color/white_color"
card_view:contentPadding="2dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:id="#+id/image_iv"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_gravity="center"
android:layout_margin="10dp"
android:src="#drawable/achievement" />
<TextView
android:id="#+id/title_tv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:gravity="center"
android:textSize="20sp"
android:padding="10dp"
android:text="Himalyi"
android:textColor="#color/green_color" />
<TextView
android:id="#+id/desc_tv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:gravity="center"
android:textSize="16sp"
android:padding="10dp"
android:text="5 cigarettes non-smoked"
android:textColor="#color/teal_color" />
</LinearLayout>
</android.support.v7.widget.CardView>
</pairroxz.com.quitsmoking.view.AchievementsView>
AchievementsAdapter.java
public class AchievementsAdapter extends BaseAdapter {
private Context context;
private ArrayList<Achievements> list;
private LayoutInflater inflater;
public AchievementsAdapter(Context context,ArrayList<Achievements> list){
this.context = context;
this.list = list;
initialize();
}
private void initialize() {
inflater = LayoutInflater.from(context);
}
#Override
public int getCount() {
return list.size();
}
#Override
public Object getItem(int position) {
return position;
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
AchievementsView view = null;
if(view==null) {
view = (AchievementsView) inflater.inflate(R.layout.achievements_row,null);
}else{
view = (AchievementsView) convertView;
}
view.setContents(list.get(position));
notifyDataSetChanged();
return view;
}
}
AchievementsView.java
public class AchievementsView extends RelativeLayout {
private TextView title_tv,desc_tv;
private ImageView image_iv;
public AchievementsView(Context context) {
super(context);
}
public AchievementsView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public AchievementsView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
#TargetApi(Build.VERSION_CODES.LOLLIPOP)
public AchievementsView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
#Override
protected void onFinishInflate() {
super.onFinishInflate();
initialize();
}
private void initialize() {
image_iv = (ImageView)findViewById(R.id.image_iv);
title_tv = (TextView)findViewById(R.id.title_tv);
desc_tv = (TextView) findViewById(R.id.desc_tv);
}
public void setContents(Achievements achievements){
image_iv.setImageDrawable(getContext().getResources().getDrawable(achievements.getImg()));
title_tv.setText(achievements.getTitle());
desc_tv.setText(achievements.getDesc());
}
}
AchievementsActivity.java
public class AchievementsActivity extends AppCompatActivity implements View.OnClickListener {
private GridView achievements_gv;
private AchievementsAdapter adapter;
private ArrayList<Achievements> list;
DatabaseHelper helper;
private ImageView back_iv;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_achievements);
initialize();
}
private void initialize() {
helper = DatabaseHelper.getInstance(this);
back_iv = (ImageView) findViewById(R.id.back_iv);
achievements_gv = (GridView) findViewById(R.id.acheivements_gv);
list = new ArrayList<Achievements>();
getData();
setListener();
}
private void setListener() {
back_iv.setOnClickListener(this);
}
private void getData() {
list = helper.getAchievementsList();
//list =
adapter = new AchievementsAdapter(this, list);
achievements_gv.setAdapter(adapter);
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.back_iv:
onBackPressed();
break;
}
}
}
if(view == null)
view.setTag(viewHolder);
viewHolder = (ViewHolder) view.getTag();
don't use this. or send your code
how to solve this issue for ListView inside cardview.In the below image i have listview but the problem is i m not getting the full data shown.only first row is visible:
// this is my code
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginBottom="#dimen/card_margin"
android:layout_marginLeft="#dimen/card_margin"
android:layout_marginRight="#dimen/card_margin">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/qualification"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:background="#E0E0E0"
android:gravity="center"
android:text="Qualifications"
android:textAppearance="#style/TextAppearance.AppCompat.Title"
android:textColor="#4A148C" />
<TextView
android:id="#+id/addqualification"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/qualification"
android:layout_marginTop="15dp"
android:background="?attr/selectableItemBackground"
android:clickable="true"
android:drawableLeft="#drawable/ic_add"
android:drawablePadding="20dp"
android:drawableStart="#drawable/ic_add"
android:paddingLeft="30dp"
android:text="Add Qualifications"
android:textColor="#0e89d0" />
<ListView
android:id="#+id/listQualification"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/addqualification">
</ListView>
</RelativeLayout>
</android.support.v7.widget.CardView>
//this is my list_row
<?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="wrap_content"
>
<TextView
android:id="#+id/course"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="10sp"
android:textStyle="bold"
android:layout_marginLeft="10dp"
android:text="Course:"
android:visibility="gone"
/>
<TextView
android:id="#+id/courseName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/course"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:text="Course Name"
android:maxLength="255"
android:maxLines="4"
android:textSize="10sp"
android:ellipsize="middle"
/>
<TextView
android:id="#+id/university"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/courseName"
android:text="University:"
android:layout_marginLeft="10dp"
android:textSize="10sp"
android:textStyle="bold"
android:visibility="gone"
android:layout_marginTop="3dp"/>
<TextView
android:id="#+id/universityName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/courseName"
android:layout_toRightOf="#+id/university"
android:layout_marginLeft="10dp"
android:layout_marginTop="3dp"
android:textSize="10sp"
android:text="University Name"
android:ellipsize="middle"
android:maxLines="4"
/>
<TextView
android:id="#+id/year"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/universityName"
android:layout_marginLeft="10dp"
android:layout_marginBottom="5dp"
android:layout_marginTop="3dp"
android:textSize="10sp"
android:textStyle="bold"
android:text="Year:"
android:visibility="gone"/>
<TextView
android:id="#+id/year1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/universityName"
android:layout_toRightOf="#+id/year"
android:layout_marginTop="3dp"
android:layout_marginLeft="10dp"
android:textSize="10sp"
android:ellipsize="middle"
android:maxLines="4"
android:text="Year" />
</RelativeLayout>
// this is my adapter class
public class QualificationAdapter extends BaseAdapter{
public ArrayList<Qualification> mQualification;
Context context;
public QualificationAdapter(ArrayList<Qualification> mQualification, Context
context) {
this.context = context;
this.mQualification = mQualification;
}
#Override
public int getCount() {
return mQualification.size();
}
#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) {
View v = null;
Qualification qualification = mQualification.get(position);
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater)
context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = inflater.inflate(R.layout.list_qualification_row, parent,
false);
} else {
v = convertView;
}
TextView course = (TextView) v.findViewById(R.id.course);
TextView university = (TextView) v.findViewById(R.id.university);
TextView year = (TextView) v.findViewById(R.id.year);
TextView courseName = (TextView) v.findViewById(R.id.courseName);
courseName.setText(qualification.getCourse());
TextView universityName = (TextView)
v.findViewById(R.id.universityName);
universityName.setText(qualification.getUniversity());
TextView year1 = (TextView) v.findViewById(R.id.year1);
year1.setText(qualification.getYear());
course.setVisibility(View.VISIBLE);
university.setVisibility(View.VISIBLE);
year.setVisibility(View.VISIBLE);
return v;
}
}
you need to provide custom height of the listView.
<ListView
.....
android:layout_height="150dp"/>
Just use your custom ListView for this.
Create java file MyListView
public class MyListView extends ListView {
public MyListView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MyListView(Context context) {
super(context);
}
public MyListView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
#Override
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,
MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, expandSpec);
}
}
and in your layout replace your listview for this:
<yourpackagename.MyListView
...
android:layout_width="match_parent"
android:layout_height="wrap_content"
... />
Custom LinearLayout component view not displaying content
Hi i have created a custom component view that extends LinearLayout and when i use this view on my xml, it doesnt become visible on the screen.
Here is the main view that is using this custom component
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/contentLinearLayout"
android:layout_width="#dimen/width"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:id="#+id/image"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ABB0B6" />
<com.jon.ui.EnableView
android:id="#+id/enableContainter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
Here is the EnableView custom component code:
package com.jon.ui;
/**
*/
public class EnableView extends LinearLayout {
private static View enableView;
private static Button btnContactlessInfoAction;
private static LinearLayout btnMakeContactlessAction;
private static View makeContactlessView;
private static View.OnClickListener enableContaclessBtnClick = new View.OnClickListener() {
#Override
public void onClick(View v) {
}
};
private static View.OnClickListener contactlessHelpBtnClick = new View.OnClickListener() {
#Override
public void onClick(View v) {
}
};
private static ViewGroup viewContainer;
private final Context mContext;
public EnableContactlessView(Context context) {
super(context);
mContext = context;
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.custom_component, this);
setOrientation(LinearLayout.VERTICAL);
}
public EnableContactlessView(Context context, AttributeSet attrs) {
super(context, attrs);
mContext = context;
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.custom_component, this);
setOrientation(LinearLayout.VERTICAL);
}
#Override
protected void onAttachedToWindow() {
super.onAttachedToWindow();
}
public void initiate() {
btnMakeContactlessAction = (LinearLayout) this.findViewById(R.id.btn);
btnContactlessInfoAction = (Button) this.findViewById(R.id.info_btn);
btnContactlessInfoAction.setOnClickListener(contactlessHelpBtnClick);
}
#Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
}
}
Below is the
View's layout xml i use to inflate from EnableView class called custom_component.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/top_margin"
android:gravity="center_horizontal"
android:orientation="horizontal">
<LinearLayout
android:id="#+id/btn"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginRight="9dp"
android:layout_weight="3"
android:background="#color/button_colour"
android:gravity="center"
android:paddingBottom="9dp"
android:paddingTop="9dp">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="15dp"
android:src="#drawable/icon_symbol" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="activate and enable"
android:textColor="#android:color/white" />
</LinearLayout>
<Button
android:id="#+id/info_btn"
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_gravity="center_vertical|end"
android:background="#drawable/icon" />
</LinearLayout>
Do not override onLayout if you're leaving it empty, remove that function. onLayout is used to measure the parent and its children, if you leave it empty nothing is getting measured.
I use Spinner in my app, with keyboard opened.
This Spinner has 9 items (from 1 to 9).
However if keyboard is opened, spinner cannot be scrolled!
Thanks to it, some of items are out of screen, and I cannot select them.
Dialog Layout here:
<?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">
<com.material.widget.FloatingEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/fet_productName"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginLeft="#dimen/floating_edittext_margin"
android:layout_marginRight="#dimen/floating_edittext_margin"
android:hint="#string/product_name"
android:inputType="text" />
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/fet_productName"
android:layout_alignLeft="#+id/fet_productName"
android:layout_alignStart="#+id/fet_productName"
android:layout_alignRight="#+id/fet_productName"
android:layout_alignEnd="#+id/fet_productName"
android:layout_marginTop="#dimen/space_20dp"
android:id="#+id/linearLayout2">
<com.material.widget.FloatingEditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/fet_productUnit"
android:layout_weight="1"
android:hint="#string/product_unit"
android:inputType="text"
android:layout_marginRight="#dimen/space_6dp" />
<Spinner
android:layout_width="80dp"
android:layout_height="wrap_content"
android:id="#+id/sP_dialog_productNumber"
android:entries="#array/spinner_cart_item_number"
android:layout_marginLeft="#dimen/space_6dp" />
</LinearLayout>
</RelativeLayout>
Java code here:
public class CartFragment extends Fragment {
private Spinner spNum;
private MaterialDialog dialog;
private static String[] msITEMS;
private ArrayList<CartItemData> itemDatas;
private ArrayAdapter<String> strAdapter;
public CartFragment(){}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_placeholder_cart, container, false);
msITEMS = rootView.getContext().getResources().getStringArray(R.array.spinner_dialog_item_number);
strAdapter = new ArrayAdapter<String>(rootView.getContext(), R.layout.support_simple_spinner_dropdown_item, msITEMS);
strAdapter.setDropDownViewResource(R.layout.support_simple_spinner_dropdown_item);
final FloatingActionButton fabAdd = (FloatingActionButton)rootView.findViewById(R.id.fabAdd);
fabAdd.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
dialog = new MaterialDialog.Builder(getActivity())
.title(R.string.product_title)
.customView(R.layout.dialog_add_cartitem, false)
.positiveText(R.string.dialog_positive_add_cartitem)
.negativeText(R.string.dialog_negative_add_cartitem)
.show();
View view = dialog.getCustomView();
dialog.getActionButton(DialogAction.POSITIVE).setEnabled(false);
spNum = (Spinner)view.findViewById(R.id.sP_dialog_productNumber);
fetName = (FloatingEditText)view.findViewById(R.id.fet_productName);
fetUnit = (FloatingEditText)view.findViewById(R.id.fet_productUnit);
spNum.setAdapter(strAdapter);
dialog.getActionButton(DialogAction.POSITIVE).setOnClickListener(onPositiveClick());
}
});
return rootView;
}
public void onActivityCreated (Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
}
Create a custom scroll:
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import org.holoeverywhere.widget.ListPopupWindow;
import org.holoeverywhere.widget.ListView;
import org.holoeverywhere.widget.Spinner;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.View;
public class CustomSpinner extends Spinner
{
public CustomSpinner(Context context)
{
super(context);
}
public CustomSpinner(Context context, AttributeSet attrs)
{
super(context, attrs);
}
public CustomSpinner(Context context, AttributeSet attrs, int defStyle)
{
super(context, attrs, defStyle);
}
public CustomSpinner(Context context, AttributeSet attrs, int defStyle, int mode)
{
super(context, attrs, defStyle, mode);
}
public CustomSpinner(Context context, int mode)
{
super(context, mode);
}
#Override
public boolean performClick()
{
boolean bClicked = super.performClick();
try
{
Field mPopupField = Spinner.class.getDeclaredField("mPopup");
mPopupField.setAccessible(true);
ListPopupWindow pop = (ListPopupWindow) mPopupField.get(this);
ListView listview = pop.getListView();
Field mScrollCacheField = View.class.getDeclaredField("mScrollCache");
mScrollCacheField.setAccessible(true);
Object mScrollCache = mScrollCacheField.get(listview);
Field scrollBarField = mScrollCache.getClass().getDeclaredField("scrollBar");
scrollBarField.setAccessible(true);
Object scrollBar = scrollBarField.get(mScrollCache);
Method method = scrollBar.getClass().getDeclaredMethod("setVerticalThumbDrawable", Drawable.class);
method.setAccessible(true);
method.invoke(scrollBar, getResources().getDrawable(R.drawable.scrollbar_style));
if(VERSION.SDK_INT >= VERSION_CODES.HONEYCOMB)
{
Field mVerticalScrollbarPositionField = View.class.getDeclaredField("mVerticalScrollbarPosition");
mVerticalScrollbarPositionField.setAccessible(true);
mVerticalScrollbarPositionField.set(listview, SCROLLBAR_POSITION_LEFT);
}
}
catch(Exception e)
{
e.printStackTrace();
}
return bClicked;
}
}
As shown on this other post by End.Fouad
I found one solution but it makes UX worse.
When the activity is appeared I focus a view which isn't a input view.
<?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"
android:focusable="true"
android:focusableInTouchMode="true" >
<com.material.widget.FloatingEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/fet_productName"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginLeft="#dimen/floating_edittext_margin"
android:layout_marginRight="#dimen/floating_edittext_margin"
android:hint="#string/product_name"
android:inputType="text" />
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/fet_productName"
android:layout_alignLeft="#+id/fet_productName"
android:layout_alignStart="#+id/fet_productName"
android:layout_alignRight="#+id/fet_productName"
android:layout_alignEnd="#+id/fet_productName"
android:layout_marginTop="#dimen/space_20dp"
android:id="#+id/linearLayout2">
<com.material.widget.FloatingEditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/fet_productUnit"
android:layout_weight="1"
android:hint="#string/product_unit"
android:inputType="text"
android:layout_marginRight="#dimen/space_6dp" />
<Spinner
android:layout_width="80dp"
android:layout_height="wrap_content"
android:id="#+id/sP_dialog_productNumber"
android:entries="#array/spinner_cart_item_number"
android:layout_marginLeft="#dimen/space_6dp" >
</Spinner>
</LinearLayout>
<requestFocus/>
</RelativeLayout>
android:focusable="true"
android:focusableInTouchMode="true"
<requestFocus/>
Delete the following line in your 'Activity':
getWindow().setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS,
WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
or if you are using any such flags...