I have managed to code to have images in each row of spinner but the images are all the same.I want to change each image. how do you do that. here is my xml and the code
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:orientation="horizontal">
<ImageView
android:id="#+id/icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/calendar"/>
<TextView
android:id="#+id/weekofday"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
here is java
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, R.layout.row, R.id.weekofday, list);
dataAdapter.setDropDownViewResource(R.layout.row);
spinner.setAdapter(dataAdapter);
it displays the same image because it is set in the row.xml file. how do I make it dynamic?
Why don't you create Custom adapter and write the code accordingly. As far as I understand that , its showing same image because you have set it in row.xml. But how would adapter will know that the image should be changed on each row.
In Custom Adapter you can set the image as per the position (i.e. index) of Spinner Item
public class SavedSearchAdapter extends BaseAdapter {
private Context mContext;
private ArrayList<SavedSearch> mData;
private boolean isEditMode = false;
public SavedSearchAdapter(Context context , ArrayList<SavedSearch> data){
mContext = context;
mData = data;
}
#Override
public int getCount() {
return mData.size();
}
#Override
public Object getItem(int position) {
return mData.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view;
TextView tvTitle;
ImageView ivDelete;
if(convertView == null){
view = View.inflate(mContext, R.layout.screen_saved_search_item_row, null);
}
else{
view = convertView;
}
tvTitle = (TextView) view.findViewById(R.id.screen_saved_search_item_row_tv);
tvTitle.setText(mData.get(position).name);
ivDelete = (ImageView) view.findViewById(R.id.screen_saved_search_item_row_iv_delete);
view.setTag(mData.get(position));
return view;
}
}
Related
I defined a spinner like this, with its own adapter.
How can I change the style of only one item of the spinner?
In particular I would like to change the color of the last string inserted in the spinner.
Thanks
Spinner spinner = findViewById(R.id.spinner);
ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, "List<String>");
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
You can create custom adapter for spinner, in adapter inside getView add a condition when a position is the last one, change a view(TextView) background color.
UPDATE :
create customAdapter
public class CustomAdapter extends BaseAdapter {
private final Context context;
private final List<String> list;
public CustomAdapter(#NonNull Context context, #NonNull List<String> list) {
this.context = context;
this.list = list;
}
#Override
public int getCount() {
return list.size();
}
#Override
public Object getItem(int i) {
return null;
}
#Override
public long getItemId(int i) {
return 0;
}
#NonNull
#Override
public View getView(int position, #Nullable View convertView, #NonNull ViewGroup parent) {
View view;
if (convertView == null) {
view = LayoutInflater.from(context).inflate(R.layout.list_item, parent, false);
} else {
view = convertView;
}
TextView textView = view.findViewById(R.id.textView_name);
textView.setText(list.get(position));
if (position == list.size() - 1) {
textView.setBackgroundResource(android.R.color.holo_blue_light);
}
return view;
}
}
list_item.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"
xmlns:app="http://schemas.android.com/apk/res-auto">
<TextView
android:id="#+id/textView_name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:textSize="20sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:gravity="center"
android:textStyle="bold"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
then set customAdpater to spinner
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
List<String> list = new ArrayList<String>();
list.add("first");
list.add("second");
list.add("third");
list.add("forth");
Spinner spinner = findViewById(R.id.spinner);
CustomAdapter adapter = new CustomAdapter(this, list);
spinner.setAdapter(adapter);
}
result will be as the photo
When my spinner positioned at the actionbar is clicked, it's supposed to present a dropdown with an array of items. Every viewholder for an item has a name and a photo.
However, I want the spinner's title to be another layout (text-only).
In short: How can I set a layout for the title spinner that differs from the dropdown layout?
Main.java
SpinnerAdapter spinnerAdapter = new SpinnerAdapter(this, dummyLists);
mMyListsSpinner.setAdapter(spinnerAdapter);
SpinnerAdapter.java
private class SpinnerAdapter extends BaseAdapter {
private String[] mListNames;
SpinnerAdapter(Context context, String[] listNames) {
this.mListNames = listNames;
}
#Override
public int getCount() {
if(this.mListNames == null) {
return 0;
}
return this.mListNames.length;
}
#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) {
Context context = parent.getContext();
int spinnerItemViewLayout = R.layout.layout_spinner_dropdown_item;
LayoutInflater inflater = LayoutInflater.from(context);
View view = inflater.inflate(spinnerItemViewLayout, parent, false);
TextView nameTextView = (TextView) view.findViewById(R.id.tv_spinner_list_name);
nameTextView.setText(this.mListNames[position]);
return view;
}
}
layout_spinner_dropdown_item.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/linearlayout_spinner_dropdown_item"
android:layout_width="222dp"
android:layout_height="#dimen/toolbar_actionbar_height"
android:orientation="horizontal">
<TextView
android:id="#+id/tv_spinner_list_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="LIST"
android:textSize="#dimen/text_size_subheading"
android:textColor="#color/color_green_dark"/>
<ImageView
android:layout_width="42dp"
android:layout_height="42dp"
android:layout_centerVertical="true"
android:layout_alignParentRight="true"
android:src="#color/color_green_dark"/>
</RelativeLayout>
This may be helpful: Android Spinner with different layouts for “drop down state” and “closed state”?
Basic points:
Rewrite getView() and getDropDownView() in your Adapter class [SpinnerAdapter]
In getView(), you should inflate normal layout for title bar, in your case it's a TextView only. In getDropDownView(), you should inflate your layout_spinner_dropdown_item.xml.
Java code
list view layout #1
How can i insert change the graphics of my ListView adding this layout file to my list view? What should i write in my java code for making each row of my ListView just like my layout file. Thank you all guys !!
First of all you need to create the xml for the custom row you want(custom_row.xml):
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="8dp"
android:id="#+id/text" /> </LinearLayout>
Then you need to create your custom adapter:
public class CustomAdapter extends BaseAdapter {
Context context;
List<String> textArray;
LayoutInflater inflater;
public RutinaAdapter(Context context, List<String> textarray) {
this.context = context;
inflater = LayoutInflater.from(context);
this.textArray = textarray;
}
#Override
public int getCount() {
return textArray.size();
}
#Override
public Object getItem(int position) {
return textArray.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewGroup vg;
if (convertView != null) {
vg = (ViewGroup) convertView;
} else {
vg = (ViewGroup) inflater.inflate(R.layout.custom_row, null);
}
String text = textArray.get(position);
final TextView text = ((TextView) vg.findViewById(R.id.text));
return vg;
} }
Then you need to add the adapter to the ListView:
list = (ListView) view.findViewById(R.id.list);
CustomAdapter adapter = new CustomAdapter(getContext(),textArray);
list.setAdapter(adapter1);
Add info to you textArray, and notify the adapter data changed, and thats it.
I am working on a college project to build an android based mobile learning app. I'm using Parse for backend services. There is a class, namely 'Course' which contains name of the courses to be offered along with an icon for each course. I have written code for custom adapter to display a list of all the courses with icons. The project is executing but the list is not appearing. I cannot figure out what is going wrong.
Here is my SelectCourse.java code
final List<Item> items=new ArrayList<Item>();
ParseQuery<ParseObject> query = ParseQuery.getQuery("Course");
query.orderByAscending("name");
query.findInBackground(new FindCallback<ParseObject>() {
#Override
public void done(List<ParseObject> list, ParseException e) {
if (e == null) {
if (list.size() > 0)
for (int i = 0; i < list.size(); i++) {
final String course = list.get(i).getString("name");
ParseFile image = list.get(i).getParseFile("image");
//adapter.add(course.getString("name"));
image.getDataInBackground(new GetDataCallback() {
public void done(byte[] data, ParseException e) {
if (e == null) {
Bitmap icon = BitmapFactory.decodeByteArray(
data, 0, data.length);
Item item = new Item(icon, course);
items.add(item);
} else {
Toast.makeText(getApplicationContext(),
e.getMessage(),
Toast.LENGTH_LONG).show();
}
}
});
}
} else {
Toast.makeText(getApplicationContext(),
e.getMessage(),
Toast.LENGTH_LONG).show();
}
}
});
CustomAdapter adapter=new CustomAdapter(this,items);
ListView listView = (ListView) findViewById(R.id.course_list);
listView.setAdapter(adapter);
This is my CustomAdapter.java code
public class CustomAdapter extends BaseAdapter {
private Context context;
private List<Item> list;
CustomAdapter(Context context, List<Item> list){
this.context = context;
this.list = list;
}
#Override
public int getCount() {
return list.size();
}
#Override
public Object getItem(int position) {
return list.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View rowView=convertView;
if(rowView==null) {
ViewHolder viewHolder = new ViewHolder();
LayoutInflater layoutInflater = (LayoutInflater) context.
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
rowView = layoutInflater.inflate(R.layout.list_select_course, parent, false);
viewHolder.icon = (ImageView) rowView.findViewById(R.id.rowImageView);
viewHolder.text = (TextView) rowView.findViewById(R.id.rowTextView);
rowView.setTag(viewHolder);
}
ViewHolder viewHolder = (ViewHolder) rowView.getTag();
viewHolder.icon.setImageBitmap(list.get(position).image);
viewHolder.text.setText(list.get(position).text);
return rowView;
}}
Here are ViewHolder and Item
public class ViewHolder {
ImageView icon;
TextView text;}
public class Item {
Bitmap image;
String text;
Item(Bitmap image, String text){
this.image=image;
this.text=text;
}}
This is content_select_course.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android: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.rsa.minerva.SelectCourseActivity"
tools:showIn="#layout/app_bar_select_course">
<ListView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="#+id/course_list"
android:layout_weight="1" />
And finally list_select_course.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:padding="16dp">
<ImageView
android:id="#+id/rowImageView"
android:layout_width="48dp"
android:layout_height="48dp" />
<TextView
android:id="#+id/rowTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/colorButton"
android:text="Hello World"/>
This part of the code:
query.findInBackground(new FindCallback<ParseObject>()
Doesn't run synchronously, meaning that you are actually creating an adapter with no data:
Put this snippet before you call the query.findInBackground():
CustomAdapter adapter=new CustomAdapter(this,items);
ListView listView = (ListView) findViewById(R.id.course_list);
listView.setAdapter(adapter);
And then inside the public void done() callback, put:
adapter.notifyDataSetChanged();
After you add the items to the list with items.add(item).
That should do it.
I spent all day searching for an answer, found a lot, but all of them are not what I need.
So, I have a pretty simple menu with ListView and a file for items with TextView. I want to set Segoe-Print font for items in my menu, I know, that I need adapter, but I can't do it. Help!!!
menu.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout android:id="#+id/menu_top"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/menu_top">
</LinearLayout>
<ListView
android:id="#+id/ListView_Menu"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/fon_android"
android:divider="#drawable/menu_line"
android:textStyle="bold" >
</ListView>
</LinearLayout>
menu_item.xml:
<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/menu_prop"
android:layout_width="fill_parent"
android:textSize="19px"
android:text="test string"
android:layout_gravity="left"
android:layout_height="wrap_content"
android:shadowRadius="5"
android:textColor="#color/menu_color"
android:shadowDy="3"
android:shadowDx="3" />
A part of Main_Menu.java:
public class Main_menu extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.menu);
ListView menuList = (ListView) findViewById(R.id.ListView_Menu);
String[] items = {
getResources().getString(R.string.system),
getResources().getString(R.string.convert),
getResources().getString(R.string.forks),
getResources().getString(R.string.margin)};
ArrayAdapter<String> adapt = new ArrayAdapter<String>(this,R.layout.menu_item, items);
menuList.setAdapter(adapt);
}
CustomAdapter.java:
public class CustomAdapter extends ArrayAdapter<String> {
public CustomAdapter(Context context, int resource, String[] objects) {
super(context, resource, objects);
// TODO Auto-generated constructor stub
}
public View getView (int position, View convertView, ViewGroup parent) {
View view = super.getView(position, convertView, parent);
TextView textView = (TextView) view.findViewById(R.id.menu_prop);
Typeface font = Typeface.createFromAsset(getAssets(), "segoe_print.ttf");
textView.setTypeface(font);
return textView;
}
private AssetManager getAssets() {
// TODO Auto-generated method stub
return null;
}
}
The font doesn't change...
view.txtViewTitle.setTypeface(Typeface.createFromAsset(parent
.getContext().getAssets(), "fonts/DroidSerif.ttf"));
here is code for custom adapter:
private class CostumiseAdapter extends ArrayAdapter<item>{
public CostumiseAdapter(ArrayList<item> items) {
super(getActivity(), 0, items);
}
#Override
public View getView(int position, View convertView, ViewGroup parent){
if(convertView == null){
convertView = getActivity().getLayoutInflater().inflate(R.layout.scroll_fragment, null);
}
Item item = getItem(position);
TextView text = (TextView) convertView.findViewById(R.id.text_view);
text.setText(item.mString);
RadioButton b1 = (RadioButton) convertView.findViewById(R.id.radio_b1);
b1.setChecked(item.mB1);
RadioButton b2 = (RadioButton) convertView.findViewById(R.id.radio_b2);
b2.setChecked(item.mB2);
CheckBox cB = (CheckBox) convertView.findViewById(R.id.check_box);
cB.setChecked(item.mB2);
return convertView;
}
}
I've take it from http://androide-examples.blogspot.com/2013/11/android-scroll-list.html see for the full example.
In this example are some semantik mistakes. So you have to fix some items to Items (capitalise)...
You need to create a custom adapter for setting custom font. Here is the minimal code :
public class CustomAdapter extends ArrayAdapter<String> {
//Contructor here
public View getView (int position, View convertView, ViewGroup parent) {
View view = super.getView(position, convertView, parent);
TextView textView = (TextView) view.findViewById(R.id.myTextView);
// Here you set the typeface as you do for the TextView
textView.setTypeFace(....);
}
}
That should provide you the custom font for your Listview.
I found a desition!
Well, first of all I create another class for Adapter:
public class Typefaces {
private static final Hashtable<String, Typeface> cache = new Hashtable<String, Typeface>();
public static Typeface get(Context c, String name) {
synchronized (cache) {
if (!cache.containsKey(name)) {
String path = "fonts/"+name;
try {
Typeface t = Typeface.createFromAsset(c.getAssets(), path);
cache.put(name, t);
} catch (Exception e) {
e.printStackTrace();
}
}
return cache.get(name);
}
}
}
Then in the main Adapter I added two strings:
Typeface tf = Typefaces.get(getContext(), "tahoma.ttf");
holder.txtTitle.setTypeface(tf);
That's all! It's working! )) Thank you all for help and answers.
try in xml:
android:fontFamily="somefont.ttf"
From android 4.1+ the following font are available: http://robotofont.com/
android:fontFamily="sans-serif"
android:fontFamily="sans-serif-thin"
android:fontFamily="sans-serif-light"
android:fontFamily="sans-serif-condensed"
you can also set font programaticly:
TextView t = (TextView) findViewById(R.id.name);
Typeface font = Typeface.createFromAsset(getAssets(),"font/somefont.ttf");
t.setTypeface(font);
put the font file in a folder...