I am trying to inflate a menu from OnItemClick. I looked at the android developer website, and was able to inflate a toast using the first code below. However, when I try to replace it with a Menu Inflater, it does not work. Can you help me solve the problem? The first set of code is the toast inflater that works, the second set of code is the Menu Inflater that doesn’t. The toast pops up when I click on an item, but the Menu will not. The program will no run, and says I have multiple syntax errors in the menu inflater. Though I cannot solve them.
package com.carouseldemo.main;
import com.carouseldemo.controls.Carousel;
import com.carouseldemo.controls.CarouselAdapter;
import com.carouseldemo.controls.CarouselItem;
import com.carouseldemo.controls.CarouselAdapter.OnItemClickListener;
import com.carouseldemo.controls.CarouselAdapter.OnItemSelectedListener;
import com.carouseldemo.main.R;
import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity implements OnClickListener {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
MediaPlayer mp = MediaPlayer.create(this, R.raw.radio);
mp.start();
Carousel carousel = (Carousel)findViewById(R.id.carousel);
carousel.setOnItemClickListener(new OnItemClickListener(){
public void onItemClick(CarouselAdapter<?> parent, View view,
int position, long id) {
LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.toast_layout,
(ViewGroup) findViewById(R.id.toast_layout_root));
TextView text = (TextView) layout.findViewById(R.id.text);
text.setText("Welcome to Power Source International");
Toast toast = new Toast(getApplicationContext());
toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
toast.setDuration(Toast.LENGTH_LONG);
toast.setView(layout);
}
});
carousel.setOnItemSelectedListener(new OnItemSelectedListener(){
public void onItemSelected(CarouselAdapter<?> parent, View view,
int position, long id) {
final TextView txt = (TextView)(findViewById(R.id.selected_item));
switch(position){
case 0:
txt.setText("Power Source International ");
break;
case 1:
txt.setText("Power Source International");
break;
case 2:
txt.setText("Power Source International");
break;
case 3:
txt.setText("Power Source International.");
break;
case 4:
txt.setText("Power Source International");
case 5:
txt.setText("Power Source InternationalPower Source InternationalPower Source InternationalPower Source InternationalPower Source InternationalPower Source InternationalPower Source InternationalPower Source InternationalPower Source InternationalPower Source InternationalPower Source InternationalPower Source InternationalPower Source InternationalPower Source International");
break;
}
}
public void onNothingSelected(CarouselAdapter<?> parent) {
}
}
);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
}
This works correctly. However, when I try to inflate a menu instead of a toast, it does not. This is the code that does not work, and has syntax errors in the menu inflater. Not matter what I try to do, I cannot correct the code.
package com.carouseldemo.main;
import com.carouseldemo.controls.Carousel;
import com.carouseldemo.controls.CarouselAdapter;
import com.carouseldemo.controls.CarouselItem;
import com.carouseldemo.controls.CarouselAdapter.OnItemClickListener;
import com.carouseldemo.controls.CarouselAdapter.OnItemSelectedListener;
import com.carouseldemo.main.R;
import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MenuInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity implements OnClickListener {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
MediaPlayer mp = MediaPlayer.create(this, R.raw.radio);
mp.start();
Carousel carousel = (Carousel)findViewById(R.id.carousel);
carousel.setOnItemClickListener(new OnItemClickListener(){
private Object v;
public void onItemClick(CarouselAdapter<?> parent, View view,
int position, long id) {
@Override
public void onCreateContextMenu (ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.context_menu, menu);
}
}
});
carousel.setOnItemSelectedListener(new OnItemSelectedListener(){
public void onItemSelected(CarouselAdapter<?> parent, View view,
int position, long id) {
final TextView txt = (TextView)(findViewById(R.id.selected_item));
switch(position){
case 0:
txt.setText("Power Source International ");
break;
case 1:
txt.setText("Power Source International");
break;
case 2:
txt.setText("Power Source International");
break;
case 3:
txt.setText("Power Source International.");
break;
case 4:
txt.setText("Power Source International");
case 5:
txt.setText("Power Source InternationalPower Source InternationalPower Source InternationalPower Source InternationalPower Source InternationalPower Source InternationalPower Source InternationalPower Source InternationalPower Source InternationalPower Source InternationalPower Source InternationalPower Source InternationalPower Source InternationalPower Source International");
break;
}
}
public void onNothingSelected(CarouselAdapter<?> parent) {
}
}
);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
}
Any advice on this? Thank you in advance.
Updated Code:
package com.carouseldemo.main;
import com.carouseldemo.controls.Carousel;
import com.carouseldemo.controls.CarouselAdapter;
import com.carouseldemo.controls.CarouselItem;
import com.carouseldemo.controls.CarouselAdapter.OnItemClickListener;
import com.carouseldemo.controls.CarouselAdapter.OnItemSelectedListener;
import com.carouseldemo.main.R;
import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MenuInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity implements OnClickListener {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
MediaPlayer mp = MediaPlayer.create(this, R.raw.radio);
mp.start();
Carousel carousel = (Carousel)findViewById(R.id.carousel);
carousel.setOnItemClickListener(new OnItemClickListener(){
public void onItemClick(CarouselAdapter<?> parent, View view,
int position, long id) {
openContextMenu(view);
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.context_menu, menu);
// TODO Auto-generated method stub
}
});
carousel.setOnItemSelectedListener(new OnItemSelectedListener(){
public void onItemSelected(CarouselAdapter<?> parent, View view,
int position, long id) {
final TextView txt = (TextView)(findViewById(R.id.selected_item));
switch(position){
case 0:
txt.setText("Power Source International ");
break;
case 1:
txt.setText("Power Source International");
break;
case 2:
txt.setText("Power Source International");
break;
case 3:
txt.setText("Power Source International.");
break;
case 4:
txt.setText("Power Source International");
case 5:
txt.setText("Power Source InternationalPower Source InternationalPower Source InternationalPower Source InternationalPower Source InternationalPower Source InternationalPower Source InternationalPower Source InternationalPower Source InternationalPower Source InternationalPower Source InternationalPower Source InternationalPower Source InternationalPower Source International");
break;
}
}
public void onNothingSelected(CarouselAdapter<?> parent) {
}
}
);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
}
Update 2:
package com.carouseldemo.main;
import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.MenuInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
import com.carouseldemo.controls.Carousel;
import com.carouseldemo.controls.CarouselAdapter;
import com.carouseldemo.controls.CarouselAdapter.OnItemClickListener;
import com.carouseldemo.controls.CarouselAdapter.OnItemSelectedListener;
public class MainActivity extends Activity implements OnClickListener {
@Override
public void onCreateContextMenu (ContextMenu menu, View v, ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.context_menu, menu);
}
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
MediaPlayer mp = MediaPlayer.create(this, R.raw.radio);
mp.start();
Carousel carousel = (Carousel)findViewById(R.id.carousel);
carousel.setOnItemClickListener(new OnItemClickListener(){
public void onItemClick(CarouselAdapter<?> parent, View view,
int position, long id) {
openContextMenu(view);
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
// TODO Auto-generated method stub
}
});
carousel.setOnItemSelectedListener(new OnItemSelectedListener(){
public void onItemSelected(CarouselAdapter<?> parent, View view,
int position, long id) {
final TextView txt = (TextView)(findViewById(R.id.selected_item));
switch(position){
case 0:
txt.setText("Power Source International ");
break;
case 1:
txt.setText("Power Source International");
break;
case 2:
txt.setText("Power Source International");
break;
case 3:
txt.setText("Power Source International.");
break;
case 4:
txt.setText("Power Source International");
case 5:
txt.setText("Power Source InternationalPower Source InternationalPower Source InternationalPower Source InternationalPower Source InternationalPower Source InternationalPower Source InternationalPower Source InternationalPower Source InternationalPower Source InternationalPower Source InternationalPower Source InternationalPower Source InternationalPower Source International");
break;
}
}
public void onNothingSelected(CarouselAdapter<?> parent) {
}
}
);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
}
You should try moving your
onCreateContextMenu()method to the root of your Activity so it looks like this:Then you are probably going to want something like this:
Note that this will pop up the Context menu as soon as the user taps on an item.