Customize spinner that is in the menu or Action Bar - java

I know how to customize the spinner. In my case it is a little bit different. I cannot modify my code more than I have now. Normally, I am able to make spinner that is reachable from all activities in my app.
I am trying to put icon along the textview in the spinner drop down.
Here the code I have;
Spinner.xml
<?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="fill_parent" >
<Spinner
android:id="#+id/spinner"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:drawSelectorOnTop="true"
android:spinnerMode="dialog"
android:prompt="#string/language_prompt" />
spinner_row.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/TextView01"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:addStatesFromChildren="true"
android:gravity="center_horizontal"
android:padding="5dp"
android:textColor="#FFFFFF"
android:textSize="17sp" >
</TextView>
<!-- Here I cannot use Relative layout. If I use it gives error like [java.lang.ClassCastException: android.widget.ImageView cannot be cast to android.widget.TextView]. Without layout I cannot implement ImageView. I am stuck here.
My activity class;
public class Base_Activity extends Activity {
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
//int flags = R.array.flags;
final Spinner spinner = (Spinner) menu.getItem(0).getActionView()
.findViewById(R.id.spinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this, R.array.languages, R.layout.spinner_row);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
What I want is to make is custom Adapter so that I can put image along the textview in spinner list. I am not able to make normal way that I checked in many tutorials. I think it's because I am creating spinner in onCreateOptionsMenu() constructor.

u can take a look at this tutorial on AndroidHive
http://www.androidhive.info/2013/11/android-working-with-action-bar/
code snippet
// Spinner title navigation data
navSpinner = new ArrayList<SpinnerNavItem>();
navSpinner.add(new SpinnerNavItem("Local", R.drawable.ic_location));
navSpinner
.add(new SpinnerNavItem("My Places", R.drawable.ic_my_places));
navSpinner.add(new SpinnerNavItem("Checkins", R.drawable.ic_checkin));
navSpinner.add(new SpinnerNavItem("Latitude", R.drawable.ic_latitude));
// title drop down adapter
adapter = new TitleNavigationAdapter(getApplicationContext(),
navSpinner);
getView() and getDropDownView() from TitleNavigationAdapter
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater mInflater = (LayoutInflater)
context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
convertView = mInflater.inflate(R.layout.list_item_title_navigation, null);
}
imgIcon = (ImageView) convertView.findViewById(R.id.imgIcon);
txtTitle = (TextView) convertView.findViewById(R.id.txtTitle);
imgIcon.setImageResource(spinnerNavItem.get(position).getIcon());
imgIcon.setVisibility(View.GONE);
txtTitle.setText(spinnerNavItem.get(position).getTitle());
return convertView;
}
#Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater mInflater = (LayoutInflater)
context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
convertView = mInflater.inflate(R.layout.list_item_title_navigation, null);
}
imgIcon = (ImageView) convertView.findViewById(R.id.imgIcon);
txtTitle = (TextView) convertView.findViewById(R.id.txtTitle);
imgIcon.setImageResource(spinnerNavItem.get(position).getIcon());
txtTitle.setText(spinnerNavItem.get(position).getTitle());
return convertView;
}
Download that code and u r good to go

Related

How can i add favourite (CheckBox/ToggleButton/ImageView) to my ListView?

Please help me.
How can i add favourite (CheckBox/ToggleButton/ImageView) to my ListView? ... I have never added a favourite button before so I would like to add one in listView with CheckBox or ToggleButton or ImageView .
Hope everyone understand my question
My code :
This is my ListAdapter
static class ListAdapter extends BaseAdapter {
private LayoutInflater inflater;
private Context ctx;
public ListAdapter(Context context) {
inflater = LayoutInflater.from(context);
ctx = context;
}
public int getCount() {
return RecipeName.length;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(final int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
if(convertView == null){
convertView = inflater.inflate(R.layout.row, null);
holder = new ViewHolder();
holder.txtRecipeName = (TextView) convertView.findViewById(R.id.txtRecipeName);
convertView.setTag(holder);
}else{
holder = (ViewHolder) convertView.getTag();
}
holder.txtRecipeName.setText(RecipeName[position]);
return convertView;
}
static class ViewHolder {
TextView txtRecipeName;
}
}
This is my row xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/LinearLayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:weightSum="1"
android:gravity="center_horizontal"
android:background="#drawable/row">
<TextView
android:id="#+id/txtRecipeName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:textColor="#color/text"
android:textSize="16sp"
android:textStyle="bold"
android:layout_weight="0.04"
android:typeface="normal"
android:background="#drawable/button2"/>
</LinearLayout>
For adding image u can use ImageView same as TextView in the .xml file..
Just add the src of the image after it "android:src="#drawable/beach" like this.
Here "#drawable/beach" is the path of image.
`<ImageView
android:id="#+id/photo_image_view" //for id
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop" //this will display image in center
android:src="#drawable/beach" /> //path of image`
For Toggle Button..use the following code in .xml file
<Switch
android:id="#+id/toggle_switch" //give id so can use this in java code later
android:layout_width="wrap_content" //set width
android:layout_height="wrap_content" //set height
android:text="Toggle Button" // string to be displayed
android:textAppearance="?android:textAppearanceSmall" />
How it will look
Toggle Button

change text color in spinner

I want to change a text color in text spinner in dropView. I tried to override the method getDropDownView and change a text color but it doesn't work.
SimpleCursorAdapter adapter = new SimpleCursorAdapter(
this,
android.R.layout.simple_spinner_item,
extendedCursor, from, to, CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER) {
#Override
public View getDropDownView(int position, View convertView,android.view.ViewGroup parent){
View v = convertView;
if (v == null) {
Context mContext = AddEditLoadActivity.this;
LayoutInflater vi = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// Androids orginal spinner view item
v = vi.inflate(android.R.layout.simple_spinner_dropdown_item, null);
}
// The text view of the spinner list view
TextView tv = (TextView) v.findViewById(android.R.id.text1);
boolean disabled = !isEnabled(position);
if(disabled){tv.setTextColor(Color.WHITE);}
else{tv.setTextColor(Color.WHITE);}
return v;
}
#Override
public long getItemId(int position) {
extendedCursor.moveToPosition(position);
return extendedCursor.getLong(extendedCursor.getColumnIndex(DatabaseContract.DictionaryTable.ITEM_ID));
}
};
android.R.layout.simple_spinner_item this line in your spinner adapter use default behavior, try using custom layout to change text color.
<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/text1"
style="?attr/spinnerDropDownItemStyle"
android:layout_width="match_parent"
android:layout_height="?attr/dropdownListPreferredItemHeight"
android:ellipsize="marquee"
android:singleLine="true"
android:textColor="#color/your_color" />
make sure its id will be #android:id/text1 and don't be changed.
use own custom layout
Create a layout named spinner_row.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#316FA2"
android:textSize="12sp"
android:gravity="left"
android:singleLine="true"
android:padding="6dip"
android:textColor="#color/white" />///you can add your color
/>
replace android.R.layout with R.layout.spinner_row.xml

some problems in EditText in ListView

I am very new in android and currently working on a simple andriod apps. I'm writing a listview and I want to put some EditText and textViews into it. My idea is to put one textview then followed by one edittext in every items on listview. Then I can get the values from edittext to make some calculations and get the result.
I can now make a listview and also handle the calculations. But I really cannot make the listview by my own.
I have been searched google for any instructions to make this but I cannot find any good resources. Can anyone please give me some sample codes so that I can have a guide to follow...
Thank you for that
Here are my partial code
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.infmain);
ListView lstView;
lstView = (ListView) findViewById(R.id.listView1);
ArrayAdapter<String> listadapter = new ArrayAdapter<String> (this, android.R.layout.simple_spinner_item,list);
lstView.setAdapter(listadapter);
Spinner spinner = (Spinner) findViewById(R.id.planets_spinner);
Integer[] items = new Integer[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,
12, 13, 14, 15, 16, 17, 18 };
ArrayAdapter<Integer> adapter = new ArrayAdapter<Integer>(this,
android.R.layout.simple_spinner_item, items);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new Spinner.OnItemSelectedListener() {
public void onItemSelected(AdapterView adapterView, View view,
int position, long id) {
}
public void onNothingSelected(AdapterView arg0) {
}
});
}
I am looking some sample codes that did similar things to me.
So I can read and learn from the codes.
thanks
Default adapter and view wont do as you as you have used in your code.
You need to have a separate xml for your rows. And inflate that row with a custom adapter.
In your rowview just add a an edittext below textview and that should work.
ok make a new layout named img_row_layout and copy this code to it
?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" >
<ImageView
android:id="#+id/img"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"/>
<TextView
android:id="#+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/img"
android:layout_toRightOf="#+id/img"
android:textStyle="bold" />
<TextView
android:id="#+id/dist"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/name"
android:layout_gravity="center"
android:layout_marginTop="2dip"
android:layout_toRightOf="#id/img"
android:gravity="right"
android:textSize="8dp"
android:textStyle="italic" />
</RelativeLayout>
The layout shown above is quite trivial and we don’t need to explain it. Then we have to modify the custom adapter in order to handle the images and the holder class. The holder class becomes:
public View getView(int position, View convertView, ViewGroup parent) {
    View v = convertView;
     
    PlanetHolder holder = new PlanetHolder();
     
    // First let's verify the convertView is not null
    if (convertView == null) {
        // This a new view we inflate the new layout
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        v = inflater.inflate(R.layout.img_row_layout, null);
        // Now we can fill the layout with the right values
        TextView tv = (TextView) v.findViewById(R.id.name);
        TextView distView = (TextView) v.findViewById(R.id.dist);
        ImageView img = (ImageView) v.findViewById(R.id.img);
         
        holder.planetNameView = tv;
        holder.distView = distView;
        holder.img = img;
         
        v.setTag(holder);
    }
    else
        holder = (PlanetHolder) v.getTag();
     
    System.out.println("Position ["+position+"]");
    Planet p = planetList.get(position);
    holder.planetNameView.setText(p.getName());
    holder.distView.setText("" + p.getDistance());
     
    holder.img.setImageResource(p.getIdImg());
     
    return v;
}
similar example 1
Similat Example 2

BaseAdapter ResourceNotFound Exception

In my application I am experiencing the same sort of error as found here or here however the suggested solution of cleaning the Eclipse project isn't working for me. Specifically I have a custom ListView adapter that is attempting to load two TextViews from its layout file, the first TextView is found no problem but the second throws the ResourceNotFound exception and crashes the application.
Layout File
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="#+id/roomNameText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="16dp"
android:text="Room name"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/roomNumText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/roomNameText"
android:layout_below="#+id/roomNameText"
android:text="Room number"
android:textAppearance="?android:attr/textAppearanceMedium" />
</RelativeLayout>
Adapter
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
m = new ViewHolder();
convertView = mInflater.inflate(R.layout.room_row, null);
convertView.setTag(m);
} else {
m = (ViewHolder) convertView.getTag();
}
m.name = (TextView) convertView.findViewById(R.id.roomNameText);
m.name.setText(rooms.get(position).getName());
m.roomNumber = (TextView) convertView.findViewById(R.id.roomNumText);
m.roomNumber.setText(rooms.get(position).getRoomNumber());
return convertView;
}
I have also tried editing R.java just to force Eclipse to regenerate the file but that also doesn't work. Really not sure what else to try since I have another adapter that is basically the same but for different data that works perfectly. Also ViewHolder is a static class that is just a container class for any views being accessed (have to use it according to coding style) in case anyone was wondering.
change
m.roomNumber.setText(rooms.get(position).getRoomNumber());
with
m.roomNumber.setText(String.valueOf(rooms.get(position).getRoomNumber()));
in your code you are using the setText(int stringId) which looks up inside R.string to retrive the stringId parameter . Also I would change your getView this way:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
m = new ViewHolder();
convertView = mInflater.inflate(R.layout.room_row, null);
m.name = (TextView) convertView.findViewById(R.id.roomNameText);
m.roomNumber = (TextView) convertView.findViewById(R.id.roomNumText);
convertView.setTag(m);
} else {
m = (ViewHolder) convertView.getTag();
}
m.name.setText(rooms.get(position).getName());
m.roomNumber.setText(String.valueOf(rooms.get(position).getRoomNumber()));
return convertView;
}

change the spinner title bar style

How can i change the spinner title bar style.
I wish to change the title bar for the following items:
icon
title textsize,textColor and
background color
How can i do this?
please help me...i have searched google and more sites.am not getting any solution for this.so please let me know its possible.if possible means how can i develop this????please explain me.
EDIT:
This is my code:
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
R.layout.row, R.id.country, list);
spinner.setPrompt("Choose a Status");
// spinner.setTextColor("#FF0000");
spinner.setAdapter(adapter);
adapter.notifyDataSetChanged();
spinner.setOnItemSelectedListener(new MyOnItemSelectedListener());
}
You have to create CustomSpinner,
To do so try this following way, it works well for me
Step 1: Create Custom Spinner Class
class CustomSpinnerAdapter extends CursorAdapter {
LayoutInflater mInflater;
private int cocktailname;
CustomSpinnerAdapter(Context context, Cursor cursor)
{
super(context, cursor);
mInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
cocktailname = cursor.getColumnIndex(YourDatabase.CK_NAME);
}
#Override
public View newDropDownView (Context context, Cursor cursor, ViewGroup parent)
{
return mInflater.inflate(R.layout.dropdownspinnertext, parent, false);
}
#Override
public View newView(Context context, Cursor cursor, ViewGroup parent)
{
return mInflater.inflate(R.layout.spinnertext, parent, false);
}
#Override
public void bindView(View row, Context context, Cursor cursor)
{
//Setting the Value here
TextView paymentname = (TextView) row.findViewById(R.id.text1);
paymentname.setTypeface(textFont);
String cocktail = cursor.getString(cocktailname);
paymentname.setText(cocktail);
}
}
Step 2 : Call this Adapter
CustomSpinnerAdapter custom_spinneradapter = new CustomSpinnerAdapter(this,youcursor);
spnListCocktails.setAdapter(custom_spinneradapter);
spnListCocktails.setOnItemSelectedListener(this);
Xml for dropdownspinnertext
I am using Checked Dropdown Spinner
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/text1"
style="?android:attr/spinnerDropDownItemStyle"
android:singleLine="true"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:textColor="#FFFFFF"
android:textSize="18sp"
android:background="#drawable/background_image"
/>
For spinnertext.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/text1"
style="?android:attr/spinnerItemStyle"
android:singleLine="true"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#FFFFFF"
android:gravity="center"
android:padding="4dip"
android:layout_marginLeft="10dip"
android:textSize="20sp"/>
Hope it helps
Using Java (Code):
spinner.setPrompt("Title")
OR
From XML:
android:prompt="#string/spinner_title
See this to change style

Categories

Resources