Android opOptionsItemSelected menu doesn't work - android

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;
}

Related

RadioGroup listener dont work

I have a Fragment that displays a Dialog.
Inside that Dialog there is a RadioGroup.
I tried to create a listener to this RadioGroup, but i didn't successed.
Fragment --> Dialog --> RadioGroup
I tried four ways:
OnCheckedChangeListener on RadioGroup.
OnClickListener on RadioGroup.
OnCheckedChangeListener on RadioButton.
OnClickListener on RadioButton
No one worked.
Here is my code:
The Dialog - layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<RadioGroup
android:id="#+id/bS_rg"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:orientation="vertical">
<RadioButton
android:id="#+id/r_id"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="ID"
android:textSize="24sp" />
<RadioButton
android:id="#+id/r_title"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="Title"
android:textSize="24sp" />
<RadioButton
android:id="#+id/r_genre"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="Genre"
android:textSize="24sp" />
<RadioButton
android:id="#+id/r_author"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="Author"
android:textSize="24sp" />
</RadioGroup>
<AutoCompleteTextView
android:id="#+id/c_bSearch"
android:layout_width="278dp"
android:layout_height="72dp"
android:singleLine="true"/>
</LinearLayout>
The Fragment - layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<de.codecrafters.tableview.TableView
android:id="#+id/tableView"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
And the Fragment - code: (I marked where is the problem, the other code is not important)
package com.example.adiel.library_app_30;
import .....;
public class ShowAllBooks extends Fragment {
View v;
TableLayout table;
TableRow row;
String[] spaceProbeHeaders = {"ID", "Title"};
String[][] spaceProbes;
AutoCompleteTextView c_search;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, Bundle savedInstanceState) {
v = inflater.inflate(R.layout.show_all_books, container, false);
setHasOptionsMenu(true);
/*
------The problem is here----
*/
LayoutInflater li = LayoutInflater.from(v.getContext());
View promptsView = li.inflate(R.layout.b_search_dialog, null);
c_search = (AutoCompleteTextView) promptsView.findViewById(R.id.c_bSearch);
RadioGroup rg = (RadioGroup) promptsView.findViewById(R.id.bS_rg);
rg.check(R.id.r_id);
rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup group, int checkedId)
{
Log.d("Log", "Clicked");
}
});
rg.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.d("Log", "Clicked");
}
});
RadioButton r_id = (RadioButton) promptsView.findViewById(R.id.r_id);
r_id.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Log.d("Log", "Clicked");
}
});
r_id.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.d("Log", "Clicked");
}
});
/*
------Until here----
*/
final TableView<String[]> tb = (TableView<String[]>) v.findViewById(R.id.tableView);
tb.setColumnCount(2);
tb.setHeaderBackgroundColor(Color.parseColor("#2ecc71"));
Ask a = new Ask("books", "getAllCopiesOfBooks", null);
Client client = (Client) new Client(a, a.getIp(), v.getContext(), new Client.ClientReply() {
#Override
public void processFinish(Object output) {
if (output != null) {
ArrayList<CopyId> data = (ArrayList<CopyId>) output;
spaceProbes = new String[data.size()][2];
for (int i = 0; i < data.size(); i++) {
CopyId c = data.get(i);
spaceProbes[i][0] = String.valueOf(c.getId());
spaceProbes[i][1] = c.getTitle();
}
tb.setHeaderAdapter(new SimpleTableHeaderAdapter(v.getContext(), spaceProbeHeaders));
tb.setDataAdapter(new SimpleTableDataAdapter(v.getContext(), spaceProbes));
}
}
}).execute();
tb.addDataClickListener(new TableDataClickListener() {
#Override
public void onDataClicked(int rowIndex, Object clickedData) {
Toast.makeText(v.getContext(), ((String[]) clickedData)[0], Toast.LENGTH_SHORT).show();
Intent i = new Intent(v.getContext(), ShowBook.class);
i.putExtra("id", ((String[]) clickedData)[0]);
startActivity(i);
}
});
return v;
}
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.showallbooks_menu, menu);
super.onCreateOptionsMenu(menu, inflater);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case R.id.action_add:
return true;
case R.id.action_search:
search_func();
return true;
}
return super.onOptionsItemSelected(item);
}
public void search_func(){
final Dialog dialog = new Dialog(v.getContext());
dialog.setContentView(R.layout.b_search_dialog);
dialog.setTitle("Search");
dialog.show();
//RadioButton r_id = (RadioButton) promptsView.findViewById(R.id.r_id);
/*RadioButton r_title = (RadioButton) v.findViewById(R.id.r_title);
RadioButton r_genre = (RadioButton) v.findViewById(R.id.r_genre);
RadioButton r_author = (RadioButton) v.findViewById(R.id.r_author);*/
}
}

Showing Toast when clicking on textview

In this example, I am trying to click on each of the 2 text views (mLayout1 and mLayout2),
and a toast will show up. The code compiles with no error, but when running there is no toast showing up.
Here is my code:
public class MainActivity extends ActionBarActivity {
private TextView mLayout1, mLayout2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View view = inflater.inflate(R.layout.activity_main,
mLayout1 = (TextView) view.findViewById(R.id.item_1a);
mLayout1.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v) {
Toast.makeText(v.getContext(),"mLayout1 clicked",Toast.LENGTH_SHORT).show();
}
});
mLayout2 = (TextView) view.findViewById(R.id.item_1b);
mLayout2.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
Toast.makeText(v.getContext(),"mLayout2 clicked",Toast.LENGTH_SHORT).show();
}
});
return view;
}
}
and here is my 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: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="com.example.transitionexample.MainActivity" >
<TextView
android:id="#+id/item_1a"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Item 1a"
android:background="#color/green"
android:gravity="center"/>
<TextView
android:id="#+id/item_1b"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="74dp"
android:gravity="center"
android:background="#color/opaque_red"
android:text="Item 1b" />
</RelativeLayout>
your onCreateView is never called - this is not a fragment - add the #Override annotation and you will see ...
Try this:
android:clickable="true"
:
<TextView
android:clickable="true"
android:id="#+id/item_1a"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Item 1a"
android:background="#color/green"
android:gravity="center"/>

Android: Drop Down Menu - who have simple example?

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>

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){
}
});
}

Android ViewPager

I wanna make the event. when I push the button 3, I want to move ViewPager1(position 0->1) also ViewPager2(position 0->1) at the same time.
here is my code
main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/LinearLayout2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="vertical" >
<android.support.v4.view.ViewPager
android:id="#+id/viewpager_a"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</android.support.v4.view.ViewPager>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="vertical" >
<android.support.v4.view.ViewPager
android:id="#+id/viewpager_b"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</android.support.v4.view.ViewPager>
</LinearLayout>
</LinearLayout>
viewpager_a1.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/textView1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="center"
android:text="ViewPager1(Position 0)" />
<Button
android:id="#+id/button1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:text="Button1" />
</LinearLayout>
viewpager_a2.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/textView1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="center"
android:text="ViewPager1(Position 1)" />
<Button
android:id="#+id/button2"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:text="Button2" />
</LinearLayout>
viewpager_b1.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/textView1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="center"
android:text="ViewPager2(Position 0)" />
<Button
android:id="#+id/button3"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:text="Button3" />
</LinearLayout>
viewpager_b2.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/textView1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="center"
android:text="ViewPager2(Position 1)" />
<Button
android:id="#+id/button4"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:text="Button4" />
</LinearLayout>
main.java
public class MainActivity extends Activity {
private ViewPager viewpagerA;
private ViewPager viewpagerB;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewpagerA =(ViewPager)findViewById(R.id.viewpager_a);
viewpagerA.setAdapter(new AdpaterA(getApplicationContext(),viewpagerA));
viewpagerB =(ViewPager)findViewById(R.id.viewpager_b);
viewpagerB.setAdapter(new AdpaterB(getApplicationContext(),viewpagerB));
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
ViewPagerApdaterA.java
public class AdpaterA extends PagerAdapter {
public LayoutInflater mInflater;
public Context mContext;
public ViewPager mViewPager;
public AdpaterA(Context c, ViewPager pager) {
super();
mContext = c;
mInflater = LayoutInflater.from(c);
mViewPager=pager;
mViewPager.setAdapter(this);
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return 2;
}
#Override
public boolean isViewFromObject(View pager, Object obj) {
return pager == obj;
}
#Override
public Object instantiateItem(View pager, int position) {
View v = null;
if (position == 0) {
v = mInflater.inflate(R.layout.viewpager_a1, null);
Button button1 = (Button) v.findViewById(R.id.button1);
button1.setOnClickListener(mPagerClickListener);
} else if (position == 1) {
v = mInflater.inflate(R.layout.viewpager_a2, null);
Button button2 = (Button) v.findViewById(R.id.button2);
button2.setOnClickListener(mPagerClickListener);
}
((ViewPager) pager).addView(v, 0);
return v;
}
private View.OnClickListener mPagerClickListener = new View.OnClickListener() {
#Override
public void onClick(View v) {
switch (v.getId()){
case R.id.button1:
changeviewpager(1);
Toast.makeText(mContext, "Button 1", Toast.LENGTH_SHORT).show();
break;
case R.id.button2:
changeviewpager(2);
Toast.makeText(mContext, "Button 2", Toast.LENGTH_SHORT).show();
break;
}
}
};
public void changeviewpager(int type) {
if(type==1){
mViewPager.setCurrentItem(1);
}else if(type==2){
mViewPager.setCurrentItem(0);
}
}
#Override
public void destroyItem(View pager, int position, Object view) {
((ViewPager) pager).removeView((View) view);
}
#Override
public void restoreState(Parcelable arg0, ClassLoader arg1) {
}
#Override
public Parcelable saveState() {
return null;
}
#Override
public void startUpdate(View arg0) {
}
#Override
public void finishUpdate(View arg0) {
}
}
ViewPagerAdpaterB.java
public class AdpaterB extends PagerAdapter {
public LayoutInflater mInflater;
public Context mContext;
public ViewPager mViewPager;
public AdpaterB(Context c, ViewPager pager) {
super();
mContext = c;
mInflater = LayoutInflater.from(c);
mViewPager=pager;
mViewPager.setAdapter(this);
}
#Override
public int getCount() {
return 2;
}
#Override
public boolean isViewFromObject(View pager, Object obj) {
return pager == obj;
}
#Override
public Object instantiateItem(View pager, int position) {
View v = null;
if (position == 0) {
v = mInflater.inflate(R.layout.viewpager_b1, null);
Button button3 = (Button) v.findViewById(R.id.button3);
button3.setOnClickListener(mPagerClickListener);
} else if (position == 1) {
v = mInflater.inflate(R.layout.viewpager_b2, null);
Button button4 = (Button) v.findViewById(R.id.button4);
button4.setOnClickListener(mPagerClickListener);
}
((ViewPager) pager).addView(v, 0);
return v;
}
private View.OnClickListener mPagerClickListener = new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()){
case R.id.button3:
changeviewpager(1);
Toast.makeText(mContext, "Button 3", Toast.LENGTH_SHORT).show();
break;
case R.id.button4:
changeviewpager(2);
Toast.makeText(mContext, "Button 4", Toast.LENGTH_SHORT).show();
break;
}
}
};
public void changeviewpager(int type) {
if(type==1){
mViewPager.setCurrentItem(1);
}else if(type==2){
mViewPager.setCurrentItem(0);
}
}
#Override
public void destroyItem(View pager, int position, Object view) {
((ViewPager) pager).removeView((View) view);
}
#Override
public void restoreState(Parcelable arg0, ClassLoader arg1) {
}
#Override
public Parcelable saveState() {
return null;
}
#Override
public void startUpdate(View arg0) {
}
#Override
public void finishUpdate(View arg0) {
}
}
from here, there is no error. but i don't know how to add the code....
when push the button3, viewpager2 is moved from position '0' to '1'
and also viewpager1 is moved from position '0' to '1'.
help me~
You can use
//change 1 to whatever page you want
viewPager.setCurrentItem(1, true);
to change to any arbitrary page. The second parameter is smoothScroll, if you set it to true the pager will smoothly scroll to the new position if set to false it will just jump from one to the other.
See the docs for ViewPager to learn more.

Categories

Resources