Android Spinner is unable to scroll - android

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...

Related

android: Customized gridview is not showing in design view

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?

Show loader on Spinner (Dropdown) when it is fetching data from web service

In the image above, I have shown that when the user touches the drop-down spinner it will call the web api for getting data for the spinner. Then, that moment, I want to show the loader only on the spinner view on the left or right somewhere on the view itself like in the image, rather than on whole screen when it is getting data from the web service dynamically and hide that progress bar later when web service completely hit at the end (Ignore that search bar in image).
Just create an custom adapter for your spinner. Follow the instructions found here How to create Spinner-list using CustomAdapter in android .
Put the loading view in the layout inflated in the getView method in the adapter, and manipulate it via a callback from your async task used for fetching the result.
In this i am showing loader on start button and hiding loader when stop button is pressed so you can use according to your need.So , for this i have made three class CustomSpinner,Spinner_Custom_adapter and Activity class for using it
In main layout file
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:orientation="vertical">
<www.your_packagename.com.spinnerwithloaderex.CustomSpinner
android:id="#+id/custm_spnr"
android:layout_width="120dp"
android:layout_height="40dp"
android:layout_centerInParent="true"
android:layout_gravity="center"
android:background="#drawable/dropdown_create_sales"
android:paddingRight="15dp"
android:text="Hello World!" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp">
<Button
android:id="#+id/start_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start" />
<Button
android:id="#+id/stop_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Stop" />
</LinearLayout>
</LinearLayout>
CustomSpinner class
public class CustomSpinner extends android.support.v7.widget.AppCompatSpinner {
private Spinner_Custom_adapter spinner_custom_adapter;
public CustomSpinner(Context context) {
super(context);
}
public CustomSpinner(Context context, int mode) {
super(context, mode);
}
public CustomSpinner(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CustomSpinner(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
public CustomSpinner(Context context, AttributeSet attrs, int defStyleAttr, int mode) {
super(context, attrs, defStyleAttr, mode);
}
public CustomSpinner(Context context, AttributeSet attrs, int defStyleAttr, int mode, Resources.Theme popupTheme) {
super(context, attrs, defStyleAttr, mode, popupTheme);
}
public void setItems(Activity activity, ArrayList<String> spnr_Arr) {
spinner_custom_adapter = new Spinner_Custom_adapter(activity, spnr_Arr);
setAdapter(spinner_custom_adapter);
}
public Spinner_Custom_adapter getSpinner_custom_adapter() {
return spinner_custom_adapter;
}
public void showLoader() {
setEnabled(false);
spinner_custom_adapter.showLoader(true, true);
}
public void dismissLoader() {
setEnabled(true);
spinner_custom_adapter.showLoader(false, true);
}
}
Custom_Adapter class
public class Spinner_Custom_adapter<T> extends ArrayAdapter<T> {
private LayoutInflater flater;
private ProgressBar spinner_progress;
private TextView txtTitle;
private Boolean showOrNot = false;
Spinner_Custom_adapter(Activity context, ArrayList<T> list) {
super(context, R.layout.loader_spinner_lt, R.id.title, list);
flater = context.getLayoutInflater();
}
#NonNull
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = flater.inflate(R.layout.loader_spinner_lt, parent, false);
}
Object object = getItem(position);
String rowItem = null;
if (object instanceof String) {
rowItem = (String) object;
}
TextView txtTitle = (TextView) convertView.findViewById(R.id.title);
txtTitle.setText(rowItem);
ProgressBar spinner_progress = (ProgressBar) convertView.findViewById(R.id.spinner_progress);
this.txtTitle = txtTitle;
this.spinner_progress = spinner_progress;
showLoader(showOrNot, false);
return convertView;
}
void showLoader(Boolean showOrNot, boolean notifyListOrNot) {
if (txtTitle != null && spinner_progress != null) {
this.showOrNot = showOrNot;
spinner_progress.setVisibility(showOrNot ? View.VISIBLE : View.GONE);
if (notifyListOrNot) {
notifyDataSetChanged();
}
}
}
}
Spinner single view layout xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_vertical"
android:gravity="center_vertical"
android:orientation="horizontal">
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/title"
style="?android:attr/spinnerDropDownItemStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toLeftOf="#+id/spinner_progress"
android:ellipsize="end"
android:singleLine="true"
android:text="Strawberry"
android:textColor="#CC0033"
android:textSize="16dp" />
<ProgressBar
android:id="#+id/spinner_progress"
style="?android:attr/progressBarStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:visibility="gone" />
</RelativeLayout>
and for using it
custm_spnr = (CustomSpinner) findViewById(R.id.custm_spnr);
ArrayList<String> items = new ArrayList<>();
items.add("Abcdefg");
items.add("hijklm");
items.add("nopqr");
items.add("stu");
items.add("vwxyza1b1c1");
items.add("d1e1f11g1h1");
custm_spnr.setItems(MainActivity.this, items);
findViewById(R.id.start_btn).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
custm_spnr.showLoader();
}
});
findViewById(R.id.stop_btn).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
custm_spnr.dismissLoader();
}
});

GridView issue with large data while scrolling

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

ListView onItemClick() not called on API 23 only

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();
}
}

Custom component Android LinearLayout not visible

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.

Categories

Resources