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.
Related
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 have a question . Is it possible to set a specific background color for specific item? . For example i would like to set "yellow" for item "DLUGOSC". Below is my strings.xml and spinner layout.
Thanks
**strings.xml**
<resources>
<string name="app_name">Konwerter Jednostek</string>
<string name="action_settings">Settings</string>
<string-array name="units">
<item><b>DLUGOSC</b></item>
<item>Cal</item>
<item>Centymetr</item>
<item>Stopa</item>
<item>Jard</item>
<item>Metr</item>
<item>Mila</item>
<item>Kilometr</item>
<item>Decymetr</item>
<item>Milimetr</item>
</string-array>
Spinner layout
<Spinner
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/spinner_from"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:layout_alignParentEnd="true"
/>
Yes it is possible. Here I am highlighting "Cal".
List<String> units = Arrays.asList(getResources().getStringArray(R.array.units));
Spinner spinner = (Spinner) findViewById(R.id.spinner_from);
spinner.setAdapter(new TestAdapter(units , this);
public class TestAdapter extends BaseAdapter {
List<String> strings;
Context context;
public TestAdapter(List<String> stringList, Context context) {
strings = stringList;
this.context = context;
}
#Override
public int getCount() {
return strings.size();
}
#Override
public String getItem(int position) {
return strings.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
TextView textView = new TextView(context);
textView.setText(strings.get(position));
//here you can use position or string
if(position == 1 || strings.get(position).equals("Cal")) {
textView.setBackgroundColor(Color.RED);
}
return textView;
}
I am developing an Android application which has a seating plan. The seats are ImageViews placed in the GridView. On click, the imageview will be set to a different image. The problem is no matter which seat I click on, only the first seat's image is changed. Hope to be able to get some help as to why. Following is my code:
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" android:orientation="vertical">
<TextView
android:id="#+id/grid_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="15dp"
android:text="#string/screen"
android:textSize="20sp"
android:gravity="center" >
</TextView>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="50dp"
android:paddingRight="50dp"
android:paddingTop="10dp">
<GridView
android:id="#+id/gridView"
android:layout_width="240dp"
android:layout_height="match_parent"
android:paddingTop="15dp"
android:listSelector="#android:color/transparent"
android:numColumns="8"
android:columnWidth="30dp"
android:gravity="center"
android:verticalSpacing="0dp"
android:stretchMode="none"
android:layout_centerInParent ="true">
</GridView>
</RelativeLayout>
</LinearLayout>
seat_item.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<ImageView
android:id="#+id/seatItem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:adjustViewBounds="true"/>
</LinearLayout>
MainActivity.java
public class MainActivity extends Activity implements OnItemClickListener {
GridView gridview;
ImageAdapter imageAdapter;
ArrayList<Item> data = new ArrayList<Item>();
ImageView image;
boolean showingFirst = true;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView(); // Initialize the GUI Components
fillData(); // Insert The Data
setDataAdapter(); // Set the Data Adapter
}
// Initialize the GUI Components
private void initView() {
gridview = (GridView) findViewById(R.id.gridView);
gridview.setOnItemClickListener(this);
}
// Insert The Data
private void fillData() {
for (int i = 1; i < 17; i++) {
data.add(new Item(i, getResources().getDrawable(R.drawable.seat)));
}
}
// Set the Data Adapter
private void setDataAdapter() {
imageAdapter = new ImageAdapter(getApplicationContext(),
R.layout.seat_item, data);
gridview.setAdapter(imageAdapter);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
#Override
public void onItemClick(final AdapterView<?> arg0, final View view,
final int position, final long id) {
image= (ImageView) findViewById(R.id.seatItem);
if(showingFirst == true){
String message = "Clicked: " + position;
Toast.makeText(getApplicationContext(), message, Toast.LENGTH_SHORT)
.show();
image.setImageResource(R.drawable.seat_blue);
showingFirst = false;
}else{
image.setImageResource(R.drawable.seat);
showingFirst = true;
}
}
}
ImageAdapter.java
public class ImageAdapter extends ArrayAdapter<Item> {
private Context mContext;
int resourceId;
ArrayList<Item> data = new ArrayList<Item>();
ImageView image;
// Constructor
public ImageAdapter(Context c,int layoutResourceId, ArrayList<Item> data) {
super(c, layoutResourceId, data);
this.mContext = c;
this.resourceId = layoutResourceId;
this.data = data;
}
#Override
public Item getItem(int position) {
System.out.println("Get: "+position);
return data.get(position);
}
#Override
public long getItemId(int arg0)
{
return arg0;
}
// create a new ImageView for each item referenced by the Adapter
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if (convertView == null)
{
final LayoutInflater layoutInflater =
(LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(resourceId, parent, false);
holder = new ViewHolder();
holder.imgItem = (ImageView) convertView.findViewById(R.id.seatItem);
convertView.setTag(holder);
}
else
{
holder = (ViewHolder) convertView.getTag();
}
Item item = getItem(position);
System.out.println(position);
holder.imgItem.setImageDrawable(item.getImage());
return convertView;
}
static class ViewHolder
{
ImageView imgItem;
}
}
Use onItemClick second parameter view to access ImageView from ListView clicked row layout:
image= (ImageView)view.findViewById(R.id.seatItem);
At first glance I did not find anything wrong with your code. I would recommend you to try to delegate the click action to the clickable view itself instead of using an onItemClick, so in the getView method use a setonCLickListener to the image view, or the whole view if that is what suits your solution best.
In a side note I would also recommend that you do not use the activity as a callback since in most cases it will lead to an activity leak, create a class or use an anonimous class.
Hope this helps you.
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...
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;
}
}