I need to use spinner as a menu. But problem is that when I click on an item it gets selected and shown at that place a behavior which I want to avoid. Secondly I need that the very first item should always be a heading no matter which of the items has been selected as in the following images:
spinner in normal condition
when user taps the spinner
Now if user taps any of the items heading should not be changed but item should be selected. I have achieved it using ListView but I think I must use proper Android components (if it is really possible).
Thanks in advance.
I have solved the above issue using the following code, but need to use spinner.
layout file
<RelativeLayout
android:id="#+id/header_main"
style="#style/layout_f_w"
android:layout_alignParentTop="true"
android:layout_centerInParent="true"
android:background="#color/heading_color" >
<TextView
android:id="#+id/headingText"
style="#style/layout_wrap"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="MATCH CENTER"
android:textColor="#color/text_color_white"
android:textSize="15sp"
android:textStyle="bold" />
<ImageButton
android:id="#+id/matchcenter_menu"
style="#style/layout_wrap"
android:layout_centerVertical="true"
android:layout_toRightOf="#id/headingText"
android:background="#android:color/transparent"
android:paddingTop="20dp"
android:src="#drawable/drp_down_menu" />
</RelativeLayout>
....
<ListView
android:id="#+id/mainscreen_menu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:background="#android:color/black"
android:cacheColorHint="#00000000"
android:divider="#android:color/white"
android:dividerHeight="1dp"
android:drawSelectorOnTop="false"
android:listSelector="#android:color/transparent" />
Setting the views.
TextView headingText = (TextView)findViewById(R.id.headingText);
headingText.setTypeface(Utils.getCustomFont(LiveScoreCrowdScreen.this));
headingText.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
showMatchCenterMenu(v);
}
});
....
matcheCenterMenu = (ImageButton)findViewById(R.id.matchcenter_menu);
matcheCenterMenu.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
showMatchCenterMenu(v);
}
});
....
mainscreenMenu = (ListView)findViewById(R.id.mainscreen_menu);
....
public void showMatchCenterMenu(View btn) {
ScreenMenuItemsAdapter adapter = null;
mainscreenMenu = (ListView)findViewById(R.id.mainscreen_menu);
adapter = new ScreenMenuItemsAdapter(LiveScoreCrowdScreen.this, getResources().getStringArray(R.array.livescorecard_menuitems));
mainscreenMenu.setAdapter(adapter);
mainscreenMenu.setVisibility(View.VISIBLE);
mainscreenMenu.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View row, int position, long id) {
CCApplication.isMenuOpened = false;
switch (position) {
case 0:// Refresh
//refresh screen
break;
case 1:// Highlights
//get highlights
break;
case 2:// Preferences
//get preferences
break;
case 3:// current time
// get current time
break;
}
mainscreenMenu.setVisibility(View.GONE);
}
});
}
just a hacking solution not proper one:
Again set the adapter with the spinner when you done all works in side onItemSelectedListener().
i.e
spinner.setAdapter(your_adapter);
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parentView,
View selectedItemView, int position, long id) {
//Do works here
//atlast again load the same adapter
spinner.setAdapter(your_adapter);
}
}
#Override
public void onNothingSelected(AdapterView<?> parentView) {
// your code here
}
});
It will load the same adapter after selecting an item and show you like before.
Related
I have created a spinner in my app which i want to be invisible when someone press the sos button then it should be visible for the user to select one option in it how can i solve it?
I have created a spinner in my app which i want to be invisible when
someone press the sos button
You can set a listener on the button that will set the visibility of the spinner.
Ex.
sosButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mySpinner.setVisibility(View.GONE);
}
});
it should be visible for the user to select one option in it how can i
solve it?
I'm not sure what this means. I thought you wanted the spinner to be invisible?
You can use the below code to hide and show the Spinner
//hide
spinner.setVisibility(View.GONE);
//show
spinner.setVisibility(View.VISIBLE);
Also,you can use the below code snippet to get the item selected by user;
spinner.setOnItemSelectedListener(this);
...
public void onItemSelected(AdapterView<?> parent, View view, int pos,long id) {
Toast.makeText(parent.getContext(),
"OnItemSelectedListener : " + parent.getItemAtPosition(pos).toString(),
Toast.LENGTH_SHORT).show();
}
//Hide
spinner.setVisibility(View.GONE);
//Show
spinner.setVisibility(View.VISIBLE);
Android: How to make a Spinner invisible and then visible again?
#HumanOidRoBo you can do it by this code..
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="Optional"
android:textSize="20sp" />
<Spinner
android:id="#+id/mySpinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"
android:layout_marginLeft="5dp">
</Spinner>
</LinearLayout>
and in class add this on click event of SOS
sosButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mySpinner.setVisibility(View.VISIBLE); // for Show
// or
mySpinner.setVisibility(View.GONE); // for Hide
}
});
I want to get SelectedItem index of ListView. I select item by tapping on item. OnItemClick event work fine, but getSelectedItemPosition() return -1.
What I do wrong?
xml:
<ListView
android:id="#+id/lvAddEdtList"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:choiceMode="singleChoice"
android:drawSelectorOnTop="true"
android:focusable="true"
android:focusableInTouchMode="true"
android:listSelector="?android:attr/listChoiceIndicatorSingle" />
In Activity:
lvAddEdtDel.setAdapter(namesList);
lvAddEdtDel.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
lvAddEdtDel.setSelection(position);
view.setSelected(true);
}
});
public void onClick(View v) {
if(lvAddEdtDel.getSelectedItemPosition() < 0 )
{
Toast.makeText(getApplicationContext(), getString(R.string.ItemNotSelected),Toast.LENGTH_LONG).show();
}
});
sorry for my English
I solved the problem by myself.
Layout of the adapter has chosen: android.R.layout.simple_list_item_1;
In my case, it was necessary to choose: android.R.layout.simple_list_item_single_choice;
In touch mode must yuse getCheckedItemPosition
I'm using this CoverFlow :
http://www.inter-fuser.com/2010/02/android-coverflow-widget-v2.html
I want to be able to change View when I click on a button, the button being a back/forward icon which will take you to the previous/next item in the coverflow.
I have modded the coverflow slightly so that I use an XML layout instead.
Here is my onCreate() method:
setContentView(R.layout.main);
CoverFlow coverFlow = (CoverFlow)findViewById(R.id.coverflow);
coverFlow.setAdapter(new ImageAdapter(this));
ImageAdapter coverImageAdapter = new ImageAdapter(this);
coverFlow.setAdapter(coverImageAdapter);
coverFlow.setSpacing(-20);
coverFlow.setSelection(0, true);
coverFlow.setAnimationDuration(1000);
tv = (TextView)findViewById(R.id.textView1);
coverFlow.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
Toast.makeText(getBaseContext(), String.valueOf(arg2), Toast.LENGTH_SHORT).show();
}
});
coverFlow.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
tv.setText(String.valueOf(arg2));
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// Do something
}
});
My XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/home_background"
android:orientation="vertical" >
<com.demo.app.coverflow.CoverFlow
android:id="#+id/coverflow"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_marginBottom="39dp"
android:layout_marginLeft="35dp"
android:background="#drawable/button_left" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginBottom="39dp"
android:layout_marginRight="35dp"
android:background="#drawable/button_right" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="55dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="hello"
android:textColor="#color/White"
android:textSize="16dp" />
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_marginRight="170dp"
android:layout_marginTop="15dp"
android:src="#drawable/unsa_logo" />
<ImageView
android:id="#+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginRight="14dp"
android:layout_marginTop="23dp"
android:src="#drawable/pnc_logo" />
</RelativeLayout>
Thanks David, I'm one of the Mike colleagues but I've never written a line of Java.
I work in the iOS department of our company.
I understood what you were talking about. Your answer was helpful and well written!
I just had to add :
if ( currentimageposition < coverFlow.getcount()-1) {
coverFlow.setSelection(currentImagePosition +1, true);
currentImagePosition = currentImagePosition +1;
}
Because your onItemSelected was not called (update the current marker), I don't know why.
However, coverFlow.selection( int , bool) is not animated. It is just a set method when the view is loaded. For example, coverFlow.selection(3,true) will set the fourth image on the center of the coverflow.
While I was trying to find out how to add the animation I came across this which does everything in just one line :
go left
coverFlow.onKeyDown(KeyEvent.KEYCODE_DPAD_LEFT, new KeyEvent(0, 0));
go right
coverFlow.onKeyDown(KeyEvent.KEYCODE_DPAD_RIGHT, new KeyEvent(0, 0));
(just add each line to their respective button onclick methods
Hope this can help anyone else who finds them self in the same situation as I was :)
I hope i understand your question. if so, here's an idea:
you need to store a private instance variable that tracks the current position of the image that is being displayed from the coverflow. then, you update it from the setOnItemSelectedListener() method, which is inherited from Gallery. On the "back" or "forward" button, you simply set your current selection +/- 1 depending on the button pressed.
so in your Activity, add an instance variable int that will keep track of position like...
public class MyActivity {
private int currentImagePosition = -1;
//add the rest of your onCreate code here...
}
Next, you need to be able to update the current position of the image that is currently being displayed in the coverflow. you can do so by overriding setOnItemSelectedListener like so (inside your onCreate method):
coverFlow.setOnItemSelectedListener(new OnItemSelectedListener(){
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
currentImagePosition = position; //this will update your current marker
}
};
Lastly, all you have to do is set each button's onclick listeners to change to the previous or next image by using setSelection( currentImagePosition+1 ) or setSelection( currentImagePosition-1 ). By the way, what's the true or false parameter of setSelection() do? i'm not sure what it does so i'm just going to use true like you did initially. your onCreate() method will now look something like:
#Override
public void onCreate(Bundle savedInstanceState){
//insert your other code in onCreate here in this spot....
//add the following lines to your code:
Button goLeft = (Button) findViewById(R.id.button1);
goLeft.setOnClickListener( new OnClickListener() {
#Override
public void onClick(View v) {
//go to previous image
if( currentImagePosition > 0 ){ //i'm assuming you dont want to go left anymore once at the leftmost image
coverFlow.setSelection(currentImagePosition - 1, true);
}
}
} ) ;
Button goRight = (Button) findViewById(R.id.button2);
goRight.setOnClickListener( new OnClickListener() {
#Override
public void onClick(View v) {
//go to previous image
if( currentImagePosition < coverFlow.getCount()-1 ){ //i'm assuming you dont want to go left anymore once at the leftmost image
coverFlow.setSelection(currentImagePosition + 1, true);
}
}
} ) ;
}
NOTE: the part about updating position is assumed to work from reading Get the position of the current image displayed in Gallery so i hope this works. if not, at least i tried :D
Good luck!!!
I have a list View and In the adaptor of this list View i have a method
public void onItemClick(AdapterView<?> AdapterView, View View,
int position, long id)
each row of list contain 3 view
xml for row is
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:background="#android:color/white" android:padding="5dp">
<TextView android:id="#+id/name" android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="20dp" android:textStyle="bold" android:textColor="#android:color/black"
android:layout_marginTop="10dp" android:focusable="false">
</TextView>
<TextView android:id="#+id/street"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_below="#id/name"
android:textColor="#android:color/black" android:focusable="false">
</TextView>
<ImageView android:id="#+id/pic" android:src="#drawable/pic"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_alignParentRight="true" android:layout_marginRight="10dp"
android:layout_below="#id/name" android:focusable="false">
</ImageView>
</RelativeLayout>
In OnItemClickMethod i want to find click event on these 3 view. how can i do this?
Note: if i am using view.getId() it is returning -1 for first row . so i am unable to use this one.
if(view.getId() == R.id.name){
dotask();
}
Update: I just call onClick method inside getView Method
setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Toast.makeText(context,position.toString(), Toast.LENGTH_LONG).show();
}
});
and my problem get solved.
Thanks FunkTheMonk and Vikky.
Here in onItemClick View view refers to RelativeLayout and not textviews according to your xml file.
public void onItemClick(AdapterView<?> AdapterView, View View, int position, long id) {
TextView name = View.findViewById ( R.id.name );
for other views .......
}
You can set an onClickListener on Views inside the row used in a ListView.
abstract class Clicker implements OnClickListener {
int mPosition;
public Clicker(int position) {
mPosition = position;
}
}
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView == null) {
//inflate
}
//do your stuff
convertView.findViewById(R.id.button1).setOnClickListener(new Clicker(position) {
public void onClick(View v)
{
int row = mPosition;
//button 1 clicked
}
});
convertView.findViewById(R.id.button2).setOnClickListener(new Clicker(position) {
public void onClick(View v)
{
int row = mPosition;
//button 2 clicked
}
});
}
onItemClick gives only the selected row, and provides no touch co-ordinates so you can't manually determine which child has been clicked
You can use view.findViewById on the view you get in onItemClick. It says here ( http://www.vogella.de/articles/AndroidListView/article.html ) that it's a relatively lengthy operation, and that the recommended way is using the view's tag to store its relevant child-views. It seems logical, but it might be premature optimization in your case - depending on the number of uses for your view.
I a new to Android development, so this is kind of a basic question.
I would like to implement the same behavior as in the Contacts app. You have a ListView with a series of Contacts | phone icons. There you have one behavior when you click on the contact name, and another behavior when you click on the phone icon.
Here is my code.
Any help is much appreciated.
In summary, what is wrong with the approach
switch (v.getId()) {
case R.id.imageButtonAction:
Activity Class
public class CompaniesActivity extends Activity {
MyApp app;
ListView listCompanies;
Cursor cursor;
// Adapter and its corresponding FROM and TO statements. The number and sequence of the arguments must match in FROM / TO arguments.
SimpleCursorAdapter adapter;
static final String[] FROM = { MenuNavigationData.C_COMPANY, MenuNavigationData.C_DESCRIPTION};
static final int[] TO = { R.id.textCompany, R.id.textDescription }; //
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.companies);
//Gets a reference to the application
app = (MyApp) getApplication();
// Find your views
listCompanies = (ListView) findViewById(R.id.listCompanies);
addButton = (Button) findViewById(R.id.buttonAdd);
// Add actions to user interaction
listCompanies.setOnItemClickListener(new OnItemClickListener() {
#Override
**public void onItemClick(AdapterView<?> arg0, View v, int position, long id) {
switch (v.getId()) {
case R.id.imageButtonAction:
startActivity(new Intent(app, InstructionsActivity.class));
break;
default:
int i = adapter.getItemViewType(position);
startActivity(new Intent(app, EditMenuNavigationActivity.class));
break;
}**
}
});
}
Activity xml
<!-- Companies ListView-->
<ListView android:id="#+id/listView1" android:layout_height="wrap_content" android:layout_width="match_parent"></ListView>
<ListView
android:id="#+id/listCompanies"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:layout_weight="1"
android:background="#5555"/>
</LinearLayout>
Row xml
android:background="#ffff"
android:padding="6dip">
<!-- Company TextView -->
<TextView
android:id="#+id/textCompany"
android:text="TIM"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:singleLine="true"
android:ellipsize="marquee"
android:textColor="#c000"
android:textStyle="bold"
android:textSize="25sp"/>
<!-- Description TextView -->
<TextView
android:id="#+id/textDescription"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_below="#id/textCompany"
android:layout_alignWithParentIfMissing="true"
android:layout_alignParentLeft="true"
android:singleLine="true"
android:ellipsize="marquee"
android:textColor="#c000"></TextView>
<!-- Action ImageView -->
<ImageView
android:id="#+id/imageButtonAction"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_gravity="right"
android:background="#drawable/icon"/>
</RelativeLayout>
Your onItemClick() callback receives both the specific view as well as its position (0-based), each of these can help you decide which view was clicked. The position is an index into the items you've added, and for more complicated scenarios you can view.setTag(Object o), and use getTag() to retrieve it from your callback.
New much better approach to solve this issue elegantly, and with less code!!!
With the following modifications, the User interface is much more responsive, no more double-clicking issues. :)
Much, much less code that simply works!
Modifications to Row xml
Insert a Linear layout to wrap both the
In this Linear layout, insert a tag named android:onClick="editCompanyClick"
This is the click handler that will be called in the Activity.
Insert a Linear layout to wrap the
In this Linear layout, insert a tag named android:onClick="dialClick"
This is the click handler that will be called in the Activity.
Modifications to Activity class
Remove the previous code
listCompanies.setOnItemClickListener(new OnItemClickListener() { #Override public void onItemClick(AdapterView arg0, View v, int position, long id) {
TextView company = (TextView) v.findViewById(R.id.textCompany);
ImageView dial = (ImageView) v.findViewById(R.id.imageButtonDTMFDial);
company.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(app, EditMenuNavigationActivity.class));
}
});
dial.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(app, InstructionsActivity.class));
}
});
}
Insert the code
public void dialClick(View v) {
startActivity(new Intent(app, InstructionsActivity.class));
}
public void editCompanyClick(View v) {
startActivity(new Intent(app, EditMenuNavigationActivity.class));
record
}
Row 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="?android:attr/listPreferredItemHeight"
android:padding="6dip" android:orientation="horizontal">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/linearLayout1"
android:orientation="vertical"
android:onClick="editCompanyClick"
android:layout_weight="1">
<!-- Company TextView -->
<TextView android:singleLine="true" android:text="TIM" android:id="#+id/textCompany" android:ellipsize="marquee" android:layout_height="wrap_content" style="#android:style/TextAppearance.Medium" android:layout_width="match_parent" android:gravity="top"></TextView>
<!-- Description TextView -->
<TextView android:singleLine="true" android:text="Chamar atendente" android:id="#+id/textDescription" android:ellipsize="marquee" android:layout_height="wrap_content" style="#android:style/TextAppearance.Small" android:layout_width="match_parent" android:gravity="bottom"></TextView>
</LinearLayout>
<LinearLayout
android:layout_height="match_parent"
android:id="#+id/linearLayout2"
android:onClick="dialClick"
android:layout_width="wrap_content" android:layout_gravity="right">
<!-- DTMFDial ImageView -->
<ImageView android:layout_height="wrap_content" android:background="#drawable/icon" android:id="#+id/imageButtonDTMFDial" android:layout_gravity="right" android:layout_width="wrap_content"></ImageView>
</LinearLayout>
</LinearLayout>
I finally found a solution.
This solves the issue. But adds another one. As expected, the ListView now behaves differently when the user clicks on different views(either TextView or ImageView).
But it seems unresponsive. I have to "double-click" in order to trigger either the company.setOnClick or dial.setOnClick. Any suggestions?
// Add actions to user interaction
listCompanies.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View v, int position, long id) {
TextView company = (TextView) v.findViewById(R.id.textCompany);
ImageView dial = (ImageView) v.findViewById(R.id.imageButtonDTMFDial);
company.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(app, EditMenuNavigationActivity.class));
}
});
dial.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(app, InstructionsActivity.class));
}
});
}