I need to override List of dropdown spinner items when Spinner id in dialog mode (android:spinnerMode="dialog"). I need this to define my own list divider.
I have found dropDownListViewStyle item in application Theme, which contains divider item. And it works, but only for android:spinnerMode="dropdown".
Can I have the same effect for "dialog" mode?
Try this:
<Spinner
android:id="#+id/spinnerAddToList"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginTop="10dp"
android:theme="#style/ThemeOverlay.AppCompat.Light"
android:spinnerMode="dialog"/>
Dropdown layout spinner_item_line_drop.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingLeft="3dp"
android:paddingRight="3dp">
<TextView
android:id="#+id/text_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:textSize="18sp" />
<LinearLayout
android:id="#+id/separator"
android:layout_height="1dp"
android:layout_width="match_parent"
android:background="#color/colorPrimary"
android:orientation="vertical"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"/>
</LinearLayout>
Main View layout: spinner_item_line_main.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="wrap_content"
android:orientation="vertical"
android:paddingBottom="10dp"
android:paddingLeft="3dp"
android:paddingRight="3dp"
android:paddingTop="10dp">
<TextView
android:id="#+id/text_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:textSize="18sp" />
</LinearLayout>
SpinnerAdapter:
public class SpinnerLineAdapter extends BaseAdapter {
private List<MyListEntity> values;
public SpinnerLineAdapter(List<MyListEntity> values)
{
this.values = values;
}
public void setValue(List<MyListEntity> list)
{
this.values = list;
}
#Override
public int getCount() {
return values.size();
}
#Override
public MyListEntity getItem(int position) {
return values.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View itemView = convertView;
ViewHolder viewHolder;
Context context = parent.getContext();
if (convertView == null) {
itemView = LayoutInflater.from(context).inflate(R.layout.spinner_item_line_main, parent, false);
viewHolder = new ViewHolder();
viewHolder.textView = itemView.findViewById(R.id.text_view);
itemView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) itemView.getTag();
}
viewHolder.textView.setText(values.get(position).getName());
return itemView;
}
public View getDropDownView(int position, View convertView,ViewGroup parent) {
View itemView = convertView;
ViewHolder viewHolder;
Context context = parent.getContext();
if (convertView == null) {
itemView = LayoutInflater.from(context).inflate(R.layout.spinner_item_line_drop, parent, false);
viewHolder = new ViewHolder();
viewHolder.textView = itemView.findViewById(R.id.text_view);
itemView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) itemView.getTag();
}
viewHolder.textView.setText(values.get(position).getName());
return itemView;
}
private static class ViewHolder {
TextView textView;
}
}
Activity:
SpinnerLineAdapter spinnerAddToListAdapter = new SpinnerLineAdapter(list);
spinnerAddToList.setAdapter(spinnerAddToListAdapter);
spinnerAddToList.setSelection(0, false);
spinnerAddToList.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> adapterView, View view,
int position, long id) {
MyListEntity list = spinnerAddToListAdapter.getItem(position);
viewModel.addList(list.id);
viewModel.update(myEntity);
}
#Override
public void onNothingSelected(AdapterView<?> adapter) {
}
});
Button click to show dialog:
mButtonAdd.setOnClickListener(view -> {
spinnerAddToList.performClick();
});
Related
I want to thanks zzhouj first of all, but I'm trying to use his Android-DraggableGridViewPager with some errors. this is his class:
Android-DraggableGridViewPager RAW
This is my layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/gridview_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="100">
<TextClock
android:gravity="center"
android:textColor="#color/colorBlack"
android:id="#+id/text_clock"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="15"
android:fontFamily="sans-serif-black"
android:text="20:15"
android:textSize="50sp"
android:textStyle="bold" />
<TextView
android:gravity="center"
android:id="#+id/text_date"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="10"
android:fontFamily="sans-serif-thin"
android:text="09/09/09"
android:textColor="#color/colorBlack"
android:textSize="20sp" />
<com.kangel.hybridlauncher.adapters.AppDraggableGridViewPager
android:foregroundGravity="center"
android:id="#+id/draggable_grid_view_pager"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_gravity="center"
android:layout_weight="75"
android:background="#color/colorAccent"
android:gravity="center" />
</LinearLayout>
I'm using it to create a simple gridview with draggable layout in, but if I reduce size of that ViewGroup, the element wont stay in center, because elements go on the left.
Do you have any ideas? Thanks in advance!
EDIT
public class AppArrayAdapter extends ArrayAdapter<AppObject> {
private Context context;
public AppArrayAdapter(#NonNull Context context, int resource) {
super(context, resource);
this.context = context;
}
#Override
public long getItemId(int position) {
return position;
}
#NonNull
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
View v;
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = inflater.inflate(R.layout.gridview_app_layout, parent, false);
} else {
v = convertView;
}
LinearLayout layout = v.findViewById(R.id.single_item_core);
ImageView icon = layout.findViewById(R.id.app_layout);
TextView label = layout.findViewById(R.id.app_label);
final AppObject appObject = this.getItem(position);
icon.setImageDrawable(appObject.getIcon());
label.setText(appObject.getName());
return v;
}
#Override
public void add(#Nullable AppObject object) {
super.add(object);
notifyDataSetChanged();
}
#Override
public void addAll(#NonNull Collection<? extends AppObject> collection) {
super.addAll(collection);
notifyDataSetChanged();
}
#Override
public void addAll(AppObject... items) {
super.addAll(items);
notifyDataSetChanged();
}
#Override
public void insert(#Nullable AppObject object, int index) {
super.insert(object, index);
notifyDataSetChanged();
}
#Override
public void remove(#Nullable AppObject object) {
super.remove(object);
notifyDataSetChanged();
}
#Override
public void clear() {
super.clear();
notifyDataSetChanged();
}
}
EDIT
I set adapter in MainActivity:
DraggableGridViewPager mDraggableGridViewPager = mViewAppsScroll.findViewById(R.id.draggable_grid_view_pager);
mAppAdapter = new AppArrayAdapter(this, 0);
mAppAdapter.addAll(getList());
mDraggableGridViewPager.setAdapter(mAppAdapter);
Please, the listview with just 3 textview (there is no image) it was working very fine when it was taking the full screen in height and width.
and after i have been change it to take the half of screen in height, it became scrolling very slow.
layout for rows in listView
<?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">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
android:layout_marginBottom="4dp"
android:layout_marginEnd="16dp"
android:layout_toStartOf="#id/timer_row">
<TextView
android:id="#+id/name_row"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textSize="#dimen/main_title_size"
android:textColor="#color/white"
/>
<TextView
android:id="#+id/author_row"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="#dimen/main_timer_size"
android:textColor="#color/white"
android:layout_below="#id/name_row"
android:layout_marginTop="16dp"
android:layout_alignParentStart="true"/>
</RelativeLayout>
<TextView
android:id="#+id/timer_row"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="#dimen/main_timer_size"
android:layout_margin="16dp"
android:padding="8dp"
android:textAlignment="center"
android:textColor="#color/white"
android:gravity="center"
android:layout_centerVertical="true"
android:layout_alignParentEnd="true"/>
</RelativeLayout>
and the adapter with the viewholder
public class MusicAdapter extends BaseAdapter {
private ArrayList<Music> list;
private LayoutInflater layoutInflater;
MusicAdapter(Context context, ArrayList<Music> list) {
this.list = list;
layoutInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount() {
return list.size();
}
#Override
public Object getItem(int position) {
return list.get(position);
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder;
if (convertView == null) {
convertView = layoutInflater.inflate(R.layout.music_row, parent, false);
viewHolder = new ViewHolder(convertView);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
Music music = (Music) getItem(position);
viewHolder.title.setText(music.getName());
viewHolder.author.setText(music.getAuthor());
viewHolder.timer.setText(music.getTimer());
return convertView;
}
static class ViewHolder{
TextView title,author,timer;
ViewHolder(View v)
{
this.title = v.findViewById(R.id.name_row);
this.author = v.findViewById(R.id.author_row);
this.timer = v.findViewById(R.id.timer_row);
}
}
}
You should try to use RecyclerView instead of ListView: https://developer.android.com/guide/topics/ui/layout/recyclerview
public class MyAdapter extends BaseAdapter implements ListAdapter {
private List<String> events = new ArrayList<String>();
private Context context;
public MyAdapter(List<String> events, Context context) {
this.events = events;
this.context = context;
String a = String.valueOf(events.size());
Toast.makeText(context,a,Toast.LENGTH_LONG).show();
}
#Override
public int getCount() {
return events.size();
}
#Override
public Object getItem(int position) {
return events.get(position);
}
#Override
public long getItemId(int position) {
//return events.get(position).getId();
return 0;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
View view = convertView;
if (view == null) {
LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = layoutInflater.inflate(R.layout.list_item_layout, null);
}
TextView listtv = (TextView) view.findViewById(R.id.label1);
listtv.setText(events.get(position));
Button delbutton = (Button) view.findViewById(R.id.del_button);
delbutton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(context,"You didn' code this feature yet",Toast.LENGTH_LONG).show();
}
});
return view;
}
}
I am creating an object of MyAdapter and calling it by passing a List and Activity.this
when i try to toast the size of the list passed i get the proper size
however only the first item of the list gets displayed on the screen
MY Xml`
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:layout_height="match_parent"
android:layout_width="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:background="#00000000">
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/label1"
android:padding="10dp"
android:textSize="15dp"
android:textStyle="bold"
android:gravity="center"
>
</TextView>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/del_button"
android:background="#00000000"
android:drawableTop="#drawable/ic_close_black_24dp"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:paddingTop="10dp"
>
</Button>
</RelativeLayout>`
and my main layout file is
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/content_cal_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="com.test123.CalView"
tools:showIn="#layout/activity_cal_view">
<ListView
android:id="#+id/list_id"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
</ListView>
</RelativeLayout>
public static class ViewHolder{
public TextView listTV;
public Button delButton;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
View view = convertView;
ViewHolder holder;
if (view == null) {
LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = layoutInflater.inflate(R.layout.list_item_layout, null);
//Edited this part
holder = new ViewHolder
holder.listTV = (TextView) view.findViewById(R.id.label1);
holder.delbutton = (Button) view.findViewById(R.id.del_button);
view.setTag(holder);
} else holder=(ViewHolder)view.getTag();
listtv.setText(events.get(position));
delbutton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(context,"You didn' code this feature yet",Toast.LENGTH_LONG).show();
}
});
return view;
}
I have Listview that contains a Textview, I populate data from sqlite inside them , now I want to change the textview background color for each row based on the value in each one.
I couldn't do it , can anybody help me for how to get each textview from the listview ??
Edit :
Here is my code to populate the listview:
sql = new SqlCommands(getApplicationContext());
Fields = new String[]{SqlCommands.Group, SqlCommands.Subject, SqlCommands.Body, SqlCommands.DateTime};
int [] Dview = new int []{R.id.grop, R.id.sbj,R.id.bdy,R.id.DT};
Cursor c = sql.GetData();
Adpt = new SimpleCursorAdapter(getApplicationContext(),R.layout.items, c , Fields, Dview ,0 );
NotiLst.setAdapter(Adpt);
this is the XML file for the items :
<?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:orientation="horizontal"
android:background="#drawable/grp_shap" >
<ImageView android:id="#+id/Prio"
android:layout_width="32dip"
android:layout_height="32dip"
/>
<TextView
android:id="#+id/grop"
android:layout_width="64sp"
android:layout_height="40sp"
android:layout_alignBottom="#+id/Prio"
android:layout_alignParentLeft="true"
android:text="S"
android:textSize="12sp" />
<TextView
android:id="#+id/DT"
android:layout_width="wrap_content"
android:layout_height="14sp"
android:layout_alignParentRight="true"
android:layout_alignTop="#id/title"
android:gravity="right"
android:text="Date Rec"
android:layout_marginRight="5dip"
android:textSize="12sp"
/>
<TextView
android:id="#+id/bdy"
android:layout_width="fill_parent"
android:layout_height="28sp"
android:layout_below="#+id/DT"
android:layout_toLeftOf="#+id/hi"
android:layout_toRightOf="#+id/Prio"
android:text="Body"
android:textSize="12sp" />
<TextView
android:id="#+id/sbj"
android:layout_width="fill_parent"
android:layout_height="12sp"
android:layout_alignLeft="#+id/bdy"
android:layout_alignParentTop="true"
android:layout_alignRight="#+id/bdy"
android:fontFamily="sans-serif"
android:text="Subject"
android:textSize="12sp" />
<ImageView
android:id="#+id/hi"
android:layout_width="20dip"
android:layout_height="26dip"
android:layout_alignLeft="#+id/DT"
android:layout_below="#+id/DT"
android:layout_marginLeft="15dp"
android:src="#drawable/highprio" />
</RelativeLayout>
and I have a listview on the activity_mail layout which populated whith this items.
I want to change the color for the textview with id grop based on the value inserted on it
You should use a custom adapter:
public class MyAdapter extends BaseAdapter {
private static class ViewHolder {
public final TextView mTv;
}
private final Context mContext;
private final List<String> mList; //you can change this to String array
public MyAdapter (Context context, List<String> list) {
this.mContext = context;
this.mList= list;
}
#Override
public int getCount() {
return mList.size();
}
#Override
public String getItem(int position) {
return mList.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder h;
final String string = mList.get(position);
if (convertView == null) {
convertView = View.inflate(mContext, R.layout.item_listview, null);
h = new ViewHolder();
h.mTv= (TextView) convertView .findViewById(R.id.mTvRes);
convertView.setTag(h);
} else {
h = (ViewHolder) convertView.getTag();
}
h.mTv.setText....
h.mTv.setTextColor....
h.mTv.setBackground....
return convertView;
}
}
in your activity:
List<String> list = Arrays.asList(Fields);
MyAdapter adapter = new MyAdapter(this, list);
NotiLst.setAdapter(adapter);
I didn't checked it and you maybe need to do some changes but it should work.
If you have something like this:
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder;
if(convertView == null) {
LayoutInflater inflater = LayoutInflater.from(getContext());
convertView = inflater.inflate(resource, parent, false);
viewHolder = new ViewHolder();
viewHolder.label = (TextView) convertView.findViewById(android.R.id.text1);
viewHolder.label.setBackground(getResources().getColor(color.white));
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.label.setText(activitiesList.get(position).getName());
return convertView;
}
I need to customize spinner in Android.
I have my simple Adapter:
public class MonthsSpinnerAdapter extends BaseAdapter {
private Activity context;
private String [] itemList;
public MonthsSpinnerAdapter(Activity context,String [] monthArray) {
this.context=context;
this.itemList=monthArray;
}
#Override
public int getCount() {
return 0;
}
#Override
public Object getItem(int position) {
return itemList[position];
}
#Override
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
View row = inflater.inflate(R.layout.spinner_item, parent,
false);
RobotoTextView make = (RobotoTextView) row.findViewById(R.id.spinner_title);
make.setText(itemList[position]);
Log.d("SpinnerA", "getView");
return row;
}
public View getDropDownView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
View row = inflater.inflate(R.layout.spinner_dropdown_item, parent,
false);
RobotoTextView month = (RobotoTextView) row.findViewById(R.id.spinner_month);
month.setText(itemList[position]);
RobotoTextView year = (RobotoTextView) row.findViewById(R.id.spinner_year);
year.setText(context.getString(R.string.year));
Log.d("SpinnerA", "getDropDownView");
return row;
}
}
But when I added it to my spinner and run the up, I don't see anything in spinner.
MonthsSpinnerAdapter adapter = new MonthsSpinnerAdapter(GeneralActivity.this
, getResources().getStringArray(R.array.month_list));
spinner.setAdapter(adapter);
I think it will be work nice, but I dont know where I have mistake. This is example what I see, when run app:
And at this picture, what I need:
And this my xml files if you need:
<com.devspark.robototextview.widget.RobotoTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
app:typeface="roboto_medium"
android:padding="5dp"
android:textColor="#fff"
android:id="#+id/spinner_title"
android:text="month"
android:background="#color/primary_color"
xmlns:android="http://schemas.android.com/apk/res/android" />
And DropDown layout:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" >
<com.devspark.robototextview.widget.RobotoTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:typeface="roboto_medium"
android:layout_marginTop="16dp"
android:layout_marginLeft="12dp"
android:id="#+id/spinner_month"
android:text="month"
android:textColor="#545454"
android:background="#fafafa" />
<com.devspark.robototextview.widget.RobotoTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:typeface="roboto_regular"
android:layout_marginTop="16dp"
android:layout_marginRight="12dp"
android:layout_alignParentRight="true"
android:text="year"
android:id="#+id/spinner_year"
/>
</RelativeLayout>
Help me please with my problem
Try to return itemList.length in getCount function of your adapter.
#Override
public int getCount() {
return itemList.length;
}