Android: Drop Down Menu - who have simple example? - android

Who have simple example for Drop Down Menu by Button Click?
Need make list for installed programs and select for starting.
Menu listMenu = null;
listMenu.add("quasatron"); listMenu.add("magnetron"); listMenu.add("atarrilix");
onCreateOptionsMenu(listMenu);
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.popup_menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.food:
makeToast("food","","","");
return true;
case R.id.other:
makeToast("other","","","");
return true;
default:
return super.onOptionsItemSelected(item);
}
}
And XML file of popup_menu:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/food" android:title="Food" />
<item android:id="#+id/other" android:title="Other" />
</menu>

This example is create myself to when you select any item in dropdown (spinner) list at a moment image display rightside on base on select item. so this example help to you.
MainActivity.java
public class MainActivity extends Activity implements OnClickListener, OnItemSelectedListener{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String[] technology = {"PHP", "Ruby", "Java", "SQL"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, technology);
final Spinner spinnertech = (Spinner) findViewById(R.id.spinnertech);
spinnertech.setAdapter(adapter);
spinnertech.setOnItemSelectedListener(this);
// Spinner Start....
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
int position = arg0.getSelectedItemPosition();
ImageView ivtech = (ImageView) findViewById(R.id.imgtech);
if(position == 0) {
ivtech.setImageResource(R.drawable.php);
} else if(position == 1) {
ivtech.setImageResource(R.drawable.ruby);
} else if(position == 2) {
ivtech.setImageResource(R.drawable.java);
} else if(position == 3) {
ivtech.setImageResource(R.drawable.sql);
}
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
}
// Spinner End....
}
main.xml
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/TableLayout1"
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"
tools:context=".MainActivity" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<ImageView
android:id="#+id/imgtech"
android:layout_width="80dp"
android:layout_height="60dp"
android:layout_marginRight="10dp" />
<Spinner
android:id="#+id/spinnertech"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_weight="1" />
</LinearLayout>
</TableLayout>

Related

highlighting selected item in custom list view using context menu?

I've been trying to set my background colour on this list view and with following multiple tutorials it just doesn't come up, yet no errors are shown.
What I've got is an xml file called colours in the values folder:
<resources>
<drawable name="red_colour">#6D0E0E</drawable>
</resources>
Which links to an xml in the drawable folder:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_pressed="true"
android:state_activated="true"
android:drawable="#drawable/red_colour">
</item>
</selector>
I've tried placing the following code android:background:#id"colourhighlight.xml"
literally everywhere based on various tutorials yet no luck?
Any advice? Please help...
I've got one main class with a context menu:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user_contacts);
// The contacts from the contacts content provider is stored in this cursor
mMatrixCursor = new MatrixCursor(new String[] { "_id","name","photo","details"} );
// Adapter to set data in the listview
mAdapter = new SimpleCursorAdapter(getBaseContext(),
R.layout.lv_layout,
null,
new String[] { "name","photo","details"},
new int[] { R.id.tv_name,R.id.iv_photo,R.id.tv_details}, 0);
// Getting reference to listview
final ListView lstContacts = (ListView) findViewById(R.id.lst_contacts);
// Setting the adapter to listview
lstContacts.setAdapter(mAdapter);
// Creating an AsyncTask object to retrieve and load listview with contacts
ListViewContactsLoader listViewContactsLoader = new ListViewContactsLoader();
// Starting the AsyncTask process to retrieve and load listview with contacts
listViewContactsLoader.execute();
//Selecting and highlighting the elements in the listview
lstContacts.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
view.setActivated(true);
}
});
//Creating the context menu and the options for it
listview = (ListView) findViewById(R.id.lst_contacts);
listview.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
listview.setMultiChoiceModeListener(new AbsListView.MultiChoiceModeListener() {
#Override
public void onItemCheckedStateChanged(ActionMode mode, int position, long id, boolean checked) {
count = count+1;
mode.setTitle(count + " Contacts Selected");
}
#Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
MenuInflater inflater = mode.getMenuInflater();
inflater.inflate(R.menu.contact_context_menu, menu);
return true;
}
#Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
return false;
}
#Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
switch(item.getItemId()){
case R.id.delete_id:
Toast.makeText(getBaseContext(), count + " Contacts Deselected", Toast.LENGTH_SHORT).show();
count = 0;
mode.finish();
return true;
//break;
default:
return false; //break;
}
//return false;
}
#Override
public void onDestroyActionMode(ActionMode mode) {
}
});
}
lv_layout.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="match_parent"
>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/iv_photo"
android:maxHeight="80dp"
android:maxWidth="80dp"
android:adjustViewBounds="true"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:id="#+id/tv_name"
android:layout_alignParentTop="true"
android:layout_toRightOf="#+id/iv_photo"
android:layout_toEndOf="#+id/iv_photo" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:id="#+id/tv_details"
android:layout_below="#+id/tv_name"
android:layout_toRightOf="#+id/iv_photo"
android:layout_toEndOf="#+id/iv_photo" />
</RelativeLayout>
Main layout.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context="com.example.ankhit.saveme.UserContacts"
android:background="#drawable/colourhighlight"
>
<ListView
android:id="#+id/lst_contacts"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/button"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/instructions"
android:id="#+id/button"
android:onClick="showInstructions"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
#Override
public void onItemCheckedStateChanged(ActionMode mode, int position, long id, boolean checked)
{
try
{
final int checkedCount = listView.getCheckedItemCount();
mode.setTitle(""+checkedCount);
if (checked)
{
adapter.setNewSelection(position, checked);
}
else
{
adapter.removeSelection(position);
}
adapter.notifyDataSetChanged();
}
catch (Exception e)
{
e.printStackTrace();
}
}
add these methods in your adapter class
private HashMap<Integer, Boolean> mSelection = new HashMap<Integer, Boolean>();
public void setNewSelection(int position, boolean value)
{
mSelection.put(position, value);
}
public boolean isPositionChecked(int position)
{
Boolean result = mSelection.get(position);
return result == null ? false : result;
}
public Set<Integer> getCurrentCheckedPosition() {
return mSelection.keySet();
}
public void removeSelection(int position) {
mSelection.remove(position);
}
public void clearSelection() {
mSelection = new HashMap<Integer, Boolean>();
}
call this method inside getView() method of adapter it works properly
convertView.setBackgroundColor(mContext.getResources().getColor(android.R.color.transparent));
if (mSelection.get(position) != null)
{
convertView.setBackgroundColor(mContext.getResources().getColor(R.color.message_selector_holo_blue));
}
If you want highlighting selected row completely then change the background color of the convertview
lstContacts.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
view.setBackgroundColor(mContext.getResources().getColor(android.R.color.transparent));
}
});

Android opOptionsItemSelected menu doesn't work

I have a problem with a menu in my Android app: it displays when I click the button, but when I try to click the option which will start another activity, it does nothing. It doesn't crash, either.
Here's the code for the menu:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.bookmark:
this.item.setBookmark(!this.item.getBookmarks()) ;
if(this.item.getBookmarks()){
bookmark.setImageResource(R.drawable.favorito1);
}
else{
bookmark.setImageResource(R.drawable.favorito);
}
return true;
case R.id.settings:
Intent settings = new Intent(this,Settings.class);
startActivity(settings);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
And here's the full code of my activity:
public class EventList extends Activity implements OnClickListener {
ImageView bookmark;
ImageView palau;
ImageView noimage;
TextView nom;
TextView cognom;
TextView data;
TextView descripcio;
Button map;
Button join;
Context ct;
ItemEvent item;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_event_list);
item = (ItemEvent) getIntent().getSerializableExtra("item");
ct = this;
Toast.makeText(this, item.getNom(), Toast.LENGTH_LONG).show();
element_confi(); //line 63!
}
public void element_confi() {
bookmark = (ImageView) findViewById(R.id.bookmark);
nom = (TextView) findViewById(R.id.nom);
data= (TextView) findViewById(R.id.data);
descripcio = (TextView) findViewById(R.id.descripcio);
map = (Button) findViewById(R.id.mapa);
join = (Button) findViewById(R.id.join);
nom.setText(item.getNom());
data.setText(item.getDate());
descripcio.setText(item.getDescription());
if( item.getBookmarks()){
bookmark.setImageResource(R.drawable.favorito1); //line 80!
}
else{
bookmark.setImageResource(R.drawable.favorito);
}
map.setOnClickListener(this);
join.setOnClickListener(this);
ViewPager viewPager= (ViewPager) findViewById(R.id.view_pager);
ImageAdapter adapter = new ImageAdapter(item.getRutaFoto1(),item.getRutaFoto2());
viewPager.setAdapter(adapter);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.event, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.bookmark:
this.item.setBookmark(!this.item.getBookmarks()) ;
if(this.item.getBookmarks()){
bookmark.setImageResource(R.drawable.favorito1);
}
else{
bookmark.setImageResource(R.drawable.favorito);
}
return true;
case R.id.settings:
Intent settings = new Intent(this,Settings.class);
startActivity(settings);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.mapa:
Toast.makeText(ct, "mostrar mapa", Toast.LENGTH_LONG).show();
break;
case R.id.join:
Intent apuntar = new Intent(ct, JoinEvents.class);
startActivity(apuntar);
break;
}
}
private class ImageAdapter extends PagerAdapter {
private int[] mImages;
public ImageAdapter(int image1Recurso, int image2Recurso){
mImages = new int[] { image1Recurso,
image2Recurso,
};
}
#Override
public int getCount() {
return mImages.length;
}
#Override
public boolean isViewFromObject(View view, Object object) {
return view == ((ImageView) object);
}
#Override
public Object instantiateItem(ViewGroup container, int position) {
Context context = EventList.this;
ImageView imageView = new ImageView(context);
int padding = context.getResources().getDimensionPixelSize(R.dimen.padding_medium);
imageView.setPadding(padding, padding, padding, padding);
imageView.setScaleType(ImageView.ScaleType.FIT_START);
imageView.setImageResource(mImages[position]);
((ViewPager) container).addView(imageView, 0);
return imageView;
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
((ViewPager) container).removeView((ImageView) object);
}
}
}
Thank you very much for your help.
Yours sincerely,
Mauro.
EDIT:
Settings.java
(Some spots are pending polishing: don't mind them)
public class Settings extends Activity implements OnItemSelectedListener, OnClickListener {
CheckBox view;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);
//onCheckBoxClicked();
//onRadioButtonClicked();
Spinner_Menu();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.settings, menu);
return true;
}
//checkbox
public void onCheckBoxClicked (View v)
{
view = (CheckBox)findViewById(R.id.Setting1);
boolean checking = ((CheckBox) v).isChecked();
switch (view.getId())
{
case R.id.Setting1:
if (checking)
{
// //checkbox checked!
}
else
{
//not checked!
}
break;
}
}
//Menú desplegable
public void Spinner_Menu()
{
Spinner spinner = (Spinner) findViewById(R.id.Settings2);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.menu, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(this);
}
public void onRadioButtonClicked(View v)
{
boolean checked = ((RadioButton) v).isChecked();
switch (view.getId())
{
case R.id.Setting3_1:
if (checked)
{
//Option 1
}
break;
case R.id.Setting3_2:
if (checked)
{
//Option 2
}
break;
}
}
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
}
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
}
And the layout for EventList:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
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"
tools:context=".EventList" >
<LinearLayout
android:id="#+id/linear"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center">
<ListView
android:id="#+id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center"
>
</ListView>
</LinearLayout>
<TextView
android:id="#+id/nom"
android:layout_centerHorizontal="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Nom Event"
android:layout_below="#+id/linear"
/>
<ImageView
android:id="#+id/bookmark"
android:layout_width="30dp"
android:layout_height="30dp"
android:src="#drawable/favorito"
android:layout_below="#+id/nom"
/>
<android.support.v4.view.ViewPager
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/view_pager"
android:layout_below="#id/bookmark"
android:layout_width="match_parent"
android:layout_height="200dp"
/>
<TextView
android:id="#+id/data"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/view_pager"
android:text="TextView" />
<Button
android:id="#+id/mapa"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/data"
android:text="Com arribar" />
<Button
android:id="#+id/join"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/mapa"
android:text="Join event" />
<TextView
android:id="#+id/descripcio"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/join"
android:text="Descripció event" />
</RelativeLayout>
Just in case, the Settings.xml file too:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
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"
tools:context=".Settings" >
<LinearLayout
android:id="#+id/linear1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="#+id/tv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="setting1"/>
<CheckBox
android:id="#+id/Setting1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Checkbox1"
android:layout_below="#+id/linear1"/>
</LinearLayout>
<LinearLayout
android:id="#+id/linear2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_below="#+id/linear1">
<TextView
android:id="#+id/tv2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="setting2"/>
<Spinner
android:id="#+id/Settings2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Desplegable"
android:layout_below="#+id/linear2"/>
</LinearLayout>
<LinearLayout
android:id="#+id/linear3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_below="#+id/linear2">
<TextView
android:id="#+id/tv3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="setting3"/>
<RadioButton
android:id="#+id/Setting3_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Opció1"
android:layout_below="#+id/linear3"/>
<RadioButton
android:id="#+id/Setting3_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Opció2"
android:layout_below="#+id/Setting3_1"/>
</LinearLayout>
</RelativeLayout>
Thank you very much.
Try adding this to EventList.java:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()) {
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
return true;
case R.id.settings:
Intent settingsIntent = new Intent(this, Settings.class);
startActivity(settingsIntent);
return true;
}
}
can you please paste your event.xml as well as is Settings.class extending PreferenceActivity ?
Also this is happening sometimes if your are using eclipse. Just clean try to clean and rebuild your project.
if it wont work, then try:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
super.createOnOptionsMenu(menu);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.event, menu);
return true;
}

How to implement click listener for a button in android Page Viewer?

I want to create a simple Page Control in android.... I want to move from one page to another page scrolling horizontally, like the home screen in android device.
I have multiple layout in xml like main.xml, layout_first.xml, layout_second.xml and layout_third.xml
Now I have a simple button in my layout_first.xml, I want to implement a click listener for the button like
button.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
}
});
No I don't know where to put the above code
Here is my main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Main Layout"
android:textAppearance="?android:attr/textAppearanceLarge" />
<android.support.v4.view.ViewPager
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/myfivepanelpager"/>
</LinearLayout>
Here is my layout_first.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="match_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/myTextView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="First Layout"
android:textAppearance="?android:attr/textAppearanceLarge" />
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
Here is my layout_second.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="match_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/myTextView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Second Layout"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
Here is my layout_third.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="match_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/myTextView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Third Layout"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
Here is my java code
public class MainActivity extends Activity
{
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MyPagerAdapter adapter = new MyPagerAdapter();
ViewPager myPager = (ViewPager) findViewById(R.id.myfivepanelpager);
myPager.setAdapter(adapter);
myPager.setCurrentItem(0);
}
#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;
}
private class MyPagerAdapter extends PagerAdapter
{
#Override
public int getCount()
{
// TODO Auto-generated method stub
return 3;
}
public Object instantiateItem(View collection, int position)
{
LayoutInflater inflater = (LayoutInflater) collection.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
int resId = 0;
switch (position)
{
case 0:
resId = R.layout.layout_first;
break;
case 1:
resId = R.layout.layout_second;
break;
case 2:
resId = R.layout.layout_third;
break;
}
View view = inflater.inflate(resId, null);
((ViewPager) collection).addView(view, 0);
return view;
}
#Override
public void destroyItem(View arg0, int arg1, Object arg2) {
((ViewPager) arg0).removeView((View) arg2);
}
#Override
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0 == ((View) arg1);
}
#Override
public Parcelable saveState() {
return null;
}
}
}
After View view = inflater.inflate(resId, null); add the following:
if(position == 0){
view.findViewById(R.id.button).setOnClickListener(new OnClickListener() {
public void onClick(View v){
}
});
}

hightlight multiple selected item(s)

i am populating my list view with cursor adapter, and i want to allow user to long press on any item(s) and perform available actions using action mode. i am using support library as my minimum sdk version =10
Problem: when i long click on the item action mode is displayed but the item is not highlighted as selected.
here is my activity_layout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
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"
tools:context=".MainActivity" >
<ListView
android:id="#+id/listView1"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</ListView>
</LinearLayout>
and the layout for list row :
<?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:paddingTop="10dp"
android:paddingBottom="10dp"
android:paddingRight="5dp"
android:paddingLeft="5dp"
android:layout_marginTop="0dp"
android:layout_marginBottom="10dp"
android:orientation="horizontal"
android:background="#drawable/activated_background"
>
<TextView
android:id="#+id/item_id"
android:layout_width="40dp"
android:layout_height="wrap_content"
/>
<TextView
android:id="#+id/item_title"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:paddingLeft="5dp"
android:paddingRight="5dp"
/>
<TextView
android:id="#+id/item_amount"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:gravity="right"
/>
</LinearLayout>
i have used android:background="#drawable/activated_background" for row layout and it is defined in drawable folder as:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_activated="true" android:drawable="#drawable/list_activated_holo" />
<item android:drawable="#android:color/transparent" />
</selector>
when i long click on the item, it doesn't get highlighted.
here is my activity class:
public class MainActivity extends ActionBarActivity {
ActionMode _actionMode =null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ListView listView = (ListView)findViewById(R.id.listView1);
SQLiteDatabase db = new ItemDbContract(getBaseContext()).getReadableDatabase();
String selection[] = {ProductTable._ID,ProductTable.TITLE,ProductTable.Price};
Cursor cursor = db.query(ProductTable.TABLE_NAME, selection, null, null, null, null, null);
listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
listView.setAdapter(new ItemsListAdapter(getBaseContext(), cursor));
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
if(_actionMode!=null)
{
listView.setItemChecked(arg2, true);
System.out.println("item position checked="+arg2);
}
}
});
listView.setOnItemLongClickListener(new OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
System.out.println("long clicked on "+arg2);
listView.setItemChecked(arg2, true);
((ActionBarActivity)MainActivity.this).startSupportActionMode(actionModeCallback);
return true;
}
});
}
ActionMode.Callback actionModeCallback = new ActionMode.Callback() {
#Override
public boolean onPrepareActionMode(ActionMode arg0, Menu arg1) {
// TODO Auto-generated method stub
return false;
}
#Override
public void onDestroyActionMode(ActionMode arg0) {
// TODO Auto-generated method stub
}
#Override
public boolean onCreateActionMode(ActionMode arg0, Menu menu) {
// TODO Auto-generated method stub
_actionMode = arg0;
_actionMode.getMenuInflater().inflate(R.menu.item_journal_context_menu, menu);
return true;
}
#Override
public boolean onActionItemClicked(ActionMode arg0, MenuItem arg1) {
// TODO Auto-generated method stub
return false;
}
};
}
thanks in advance
finally solved my problem by maintaining a list of selected item in custom adapter and setting different background for the selected item.

Set TextView not changing

In the following code, I have a options menu and when I select the menu option, I want to update the textview. Setting the text initially works fine and no errors are generated. Here is the code where the settext isn't working. I have put in debug statements and the code is getting hit, but text not updating. I put arrows next to the code that isn't updating. Review other textview examples, but can't figure it out.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_item_Bflat:
if (VERBOSE) Log.v(TAG, "+++ menu_item_Bflat +++");
// BFLAT = 1
mKeySelected = BFLAT;
>>>> mChapterSelected.setText("B Flat");
return true;
case R.id.menu_item_Eflat:
// EFLAT = 2
mKeySelected = EFLAT;
>>>> mChapterSelected.setText("E Flat");
return true;
default:
return super.onOptionsItemSelected(item);
} // switch
} // onOptionsItemSelected
Here is the rest of my code, if needed:
public class ChapterListFragment extends ListFragment {
private static final boolean VERBOSE = true;
private ArrayList<Chapters> mChapters;
private static final String TAG = "ChapterListFragment";
private static final int BFLAT = 1;
private static final int EFLAT = 2;
private TextView mChapterSelected;
private View mView;
private int mKeySelected;
#Override
public void onCreate(Bundle savedInstanceState) {
if (VERBOSE) Log.v(TAG, "+++ onCreate +++");
super.onCreate(savedInstanceState);
// Must explicitly tell Fragment Manager that the fragment should receive a call to
// onCreateOptionsMenu().
setHasOptionsMenu(true);
// Get the view for the Fields
if (mView == null) {
mView = getActivity().getLayoutInflater()
.inflate(R.layout.activity_improvisation, null);
}
getActivity().setTitle(R.string.chapters_title);
mChapters = ChapterList.get(getActivity()).getChapters();
ChapterAdapter adapter = new ChapterAdapter(mChapters);
setListAdapter(adapter);
mChapterSelected = (TextView) mView.findViewById(R.id.chapter_selected);
mChapterSelected.setText("Chapter Selected Here");
}
#Override
public void onListItemClick(ListView l, View v, int position, long id) {
// Get the chapter from the adapter
Chapters c = ((ChapterAdapter)getListAdapter()).getItem(position);
//Start ImprovisationActivity
Intent i = new Intent(getActivity(), ImprovisationActivity.class);
i.putExtra(ChapterFragment.EXTRA_CHAPTER_ID, c.getId());
i.putExtra(ChapterFragment.EXTRA_KEYSELECTED, mKeySelected);
startActivity(i);
}
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.fragment_chapters_list, menu);
}
Here is the XML file
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_weight="2"
android:layout_height="fill_parent"
android:layout_width="match_parent"
>
<VideoView
android:id="#+id/video_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
/>
</LinearLayout>
<LinearLayout
android:layout_weight="1"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:orientation="horizontal" >
<TextView
android:id="#+id/chapter_selected"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity = "center"
android:textStyle="bold"
android:paddingLeft="4dp"
android:paddingRight="4dp"
android:text="Chapter Selection"
/>
</LinearLayout>
<FrameLayout
android:id="#+id/fragmentContainer"
android:layout_height="match_parent"
android:layout_width="match_parent"
/>
</LinearLayout>
I didn't find out why I couldn't setText, but displaying subtitles after the user selects a menu item works well for my purposes.

Categories

Resources