[Q]How to hand different Strings to dynamically created Fragments using ViewPager? - Java for Android App Development

I can't seem to find a way to give different text to a dynamically created fragment for my pageviewer-supoorted application. I've came to a point of my codding where I got stuck. For my application I want to have 400-500 dynamically created fragments, where you can horizontally slide thru them and every content of the fragment to be the same (repeating the same fragment) and the only different thing to be the text on them.
Here's where I got stuck at my codding :
Code:
package com.example.testarearg;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class MainActivity extends FragmentActivity {
/**
* The {@link android.support.v4.view.PagerAdapter} that will provide
* fragments for each of the sections. We use a
* {@link android.support.v4.app.FragmentPagerAdapter} derivative, which
* will keep every loaded fragment in memory. If this becomes too memory
* intensive, it may be best to switch to a
* {@link android.support.v4.app.FragmentStatePagerAdapter}.
*/
SectionsPagerAdapter mSectionsPagerAdapter;
/**
* The {@link ViewPager} that will host the section contents.
*/
ViewPager mViewPager;
[user=439709]@override[/user]
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);
mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
[user=439709]@override[/user]
public void onPageSelected(int position) {
}
});
}
/**
* A {@link FragmentPagerAdapter} that returns a fragment corresponding to
* one of the sections/tabs/pages.
*/
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
[user=439709]@override[/user]
public Fragment getItem(int position) {
// getItem is called to instantiate the fragment for the given page.
// Return a DummySectionFragment (defined as a static inner class
// below) with the page number as its lone argument.
Fragment fragment = new DummySectionFragment();
Bundle args = new Bundle();
args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, position + 1);
fragment.setArguments(args);
return fragment;
}
[user=439709]@override[/user]
public int getCount() {
// Show 3 total pages.
return 3;
}
}
/**
* A dummy fragment representing a section of the app, but that simply
* displays dummy text.
*/
public static class DummySectionFragment extends Fragment {
/**
* The fragment argument representing the section number for this
* fragment.
*/ private String mText; // display this text in your fragment
public static Fragment getInstance(String text) {
Fragment f = new Fragment();
Bundle args = new Bundle();
args.putString("text", text);
f.setArguments(args);
return f;
}
public void onCreate(Bundle state) {
super.onCreate(state);
setmText(getArguments().getString("text"));
// rest of your code
}
public static final String ARG_SECTION_NUMBER = "section_number";
public DummySectionFragment() {
}
[user=439709]@override[/user]
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main_dummy, container, false);
TextView dummyTextView = (TextView) rootView.findViewById(R.id.section_label);
dummyTextView.setText(Integer.toString(getArguments().getInt(ARG_SECTION_NUMBER)));
return rootView;
}
public String getmText() {
return mText;
}
public void setmText(String mText) {
this.mText = mText;
}
}
}

featyhu James
I wanted to first off introduce you to a widget called Viewpagerindicator by Jake Wharton if you haven't already come across it. It is a very popular viewpager extension.
You can download a demo from google play (this I recommend), search for "viewpager indicator"
To give you a back of the envelope solution, I would suggest you try creating at least 2 fragments and then page between them updating the text in on resume(). Do these fragment words need to be remembered? For instance if the user swipes back and forth are the same words meant to reappear?

hgpb said:
I wanted to first off introduce you to a widget called Viewpagerindicator by Jake Wharton if you haven't already come across it. It is a very popular viewpager extension.
You can download a demo from google play (this I recommend), search for "viewpager indicator"
To give you a back of the envelope solution, I would suggest you try creating at least 2 fragments and then page between them updating the text in on resume(). Do these fragment words need to be remembered? For instance if the user swipes back and forth are the same words meant to reappear?
Click to expand...
Click to collapse
Ohh, I was not aware about this extension. I'm gonna try it and see what I get out of it. Yes, the words need to be remembered. Thank you for the heads-up about 'viewpager indicator' .

Feciuc said:
Ohh, I was not aware about this extension. I'm gonna try it and see what I get out of it. Yes, the words need to be remembered. Thank you for the heads-up about 'viewpager indicator' .
Click to expand...
Click to collapse
When you get the Viewpager indicator setup with a couple of fragments initialised you need to track the words you create. You could use a SparseArray/ArrayList or such like to track the position of frag and word perhaps. Then you should be able to move through the array depending on the swipe you receive. The swipe would move you up or down the array giving you access.
Code:
SparseArray<String> sa = new SparseArray<String>();
As I said this is a back of the envelope solution but it may just do the trick.

Related

How can I tell when an EditText object no longer has the focus?

I'm wanting to "do something" (actually play an mp3 sound) when someone has finished entering data in an EditText object in my activity, but I'm not sure how to tell when this has occurred. In VB, there is a event called lostfocus, but I don't know how to do it in java. I'm guessing that it has something to do with OnClickListener to see when the user is actually in the EditText object (let's call it et_01), but how do I know when they've gone to some other object?
I'd appreciate any specific help, as I'm pretty new to java.
How about OnFocusChangeListener?
http://developer.android.com/reference/android/view/View.OnFocusChangeListener.html
I did some searching and it seems like I need to use OnFocusChangeListener. I found this code snippet (below), but I'm too dumb to figure out how to plug it into my app. If my EditText that I'm watching is called et_01, can somebody please tell me 2 things...
1. How do I tie the OnFocusChangeListener to my et_01?
2. Where (specifically) does this code go in my activity? Inside my extends Activity...after the extends Activity...where oh where?
Code:
OnFocusChangeListener focusListener = new OnFocusChangeListener() {
public void onFocusChange(View v, boolean hasFocus) {
/* When focus is lost check that the text field
* has valid values.
*/
if (!hasFocus) {
validateInput(v);
}
}
You have to connect a TextView object to your actual textview, then override the OnFocusChangeListener of that textview object.
Here's a complete implementation using your snippit (You have to implement validateInput() yourself):
Code:
package com.myapp.mytest;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnFocusChangeListener;
import android.widget.TextView;
public class MyTest extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView tv=(TextView)findViewById(R.id.et_01);
tv.setOnFocusChangeListener(new OnFocusChangeListener(){
@Override
public void onFocusChange(View v,boolean hasFocus){
/* When focus is lost check that the text field
* has valid values.
*/
if (!hasFocus) {
validateInput(v);
}
}
});
}
}
Thank you, thank you, thank you! That is some nice help. I have one follow up question:
In my own activity, I should be able to put the code below in just under my own call: setContentView(R.layout.main);
In other words, I don't need to create a new class to do this. Correct? I apologize for my ignorance.
Code:
TextView tv=(TextView)findViewById(R.id.et_01);
tv.setOnFocusChangeListener(new OnFocusChangeListener(){
@Override
public void onFocusChange(View v,boolean hasFocus){
/* When focus is lost check that the text field
* has valid values.
*/
if (!hasFocus) {
validateInput(v);
}
}
});
Gene Poole said:
You have to connect a TextView object to your actual textview, then override the OnFocusChangeListener of that textview object.
Here's a complete implementation using your snippit (You have to implement validateInput() yourself):
Code:
package com.myapp.mytest;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnFocusChangeListener;
import android.widget.TextView;
public class MyTest extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView tv=(TextView)findViewById(R.id.et_01);
tv.setOnFocusChangeListener(new OnFocusChangeListener(){
@Override
public void onFocusChange(View v,boolean hasFocus){
/* When focus is lost check that the text field
* has valid values.
*/
if (!hasFocus) {
validateInput(v);
}
}
});
}
}
Click to expand...
Click to collapse
Yes.
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx (stupid forum says my response was too short)
OK, I'm having an issue that I don't understand. In the code below, I'm getting an error on the MediaPlayer mpWeight = MediaPlayer.create(this, R.raw.mppig);
Holding my cursor over .create says:
The method create(Context, int) in the type MediaPlayer is not applicable for the arguments (new View.OnFocusChangeListener(){}, int)
What does that mean, and more importantly, how do I resolve it?
Here's the whole routine:
Code:
TextView tv=(TextView)findViewById(R.id.weight);
tv.setOnFocusChangeListener(new OnFocusChangeListener(){
@Override
public void onFocusChange(View v,boolean hasFocus){
/* When focus is lost check that the text field
* has valid values.
*/
if (!hasFocus) {
float tempweight = Float.parseFloat(et_weight.getText().toString());
if(tempweight > 200){
MediaPlayer mpWeight = MediaPlayer.create(this, R.raw.mppig);
mpWeight.start();
}
}
}
});
It has to do with the "this" pointer you are passing to the create() method. Since you are creating this within the OnFocusChangeListener() class, that it the "this" pointer. OnFocusChangeListener() does not resolve to a type "context" whereas if you'd created your media player within your Activity, Activity does resolve to a context.
To resolve this, make a class member of your activity that keeps a copy of the context. Assign it in the activity's OnCreate:
Code:
public class MyTest extends Activity {
[COLOR="blue"]protected Context mContext=null;[/COLOR]
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
[COLOR="Blue"]mContext=this;[/COLOR]
TextView tv=(TextView)findViewById(R.id.et_01);
tv.setOnFocusChangeListener(new OnFocusChangeListener(){
@Override
public void onFocusChange(View v,boolean hasFocus){
/* When focus is lost check that the text field
* has valid values.
*/
if (!hasFocus) {
float tempweight = Float.parseFloat(et_weight.getText().toString());
if(tempweight > 200){
MediaPlayer mpWeight = MediaPlayer.create([COLOR="blue"]mContext[/COLOR], R.raw.mppig);
mpWeight.start();
}
}
}
});
Gene, you are awesome! Thanks again for the help.
As with many things in life, this has led me to a new problem, that I do believe will be the end of this subject, if I can get it resolved.
In my activity, I have a few EditText objects mixed in with a few Spinner objects. The new problem is that if you have your cursor in an EditText object and the next item in the activity is a Spinner, the focus doesn't leave the EditText when the Spinner is selected. The focus of any of my EditText objects only leaves those objects IF the next thing selected is another EditText.
I'm unsure how to resolve this. Is there possibly a way to force the focus out of the EditText once a Spinner (or any other object) is touched? I would have thought that would happen automatically, but it doesn't seem to be doing so.
Gene Poole said:
It has to do with the "this" pointer you are passing to the create() method. Since you are creating this within the OnFocusChangeListener() class, that it the "this" pointer. OnFocusChangeListener() does not resolve to a type "context" whereas if you'd created your media player within your Activity, Activity does resolve to a context.
To resolve this, make a class member of your activity that keeps a copy of the context. Assign it in the activity's OnCreate:
Click to expand...
Click to collapse

Switching to and from layouts using buttons

import android.app.*;
import android.os.*;
import android.view.*;
import android.widget.*;
public class MainActivity extends Activity
{
/** Called when the activity is first created. */
@override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button display = (Button) findViewById(R.id.button);
TextView tv = (TextView) findViewById(R.id.textView);
display.setOnClickListener( new View.OnClickListener(){
public void onClick(View p1)
{
setContentView(R.layout.main2);
// TODO: Implement this method
}
});
Button ba = (Button) findViewById(R.id.buttonBac);
ba.setOnClickListener(new View.OnClickListener(){
public void onClick(View p1)
{
setContentView(R.layout.main);
// TODO: Implement this method
}
});
}
}
Anyway, the first button works and when clicked, displays main2 makin 2 whoum d have a textview and a button that switches back 2 main but when the onclick listener whTever is aplyed to the code it wont even disllag the first layout and simply crashes... hitting the wall again
Focusedrelaxaation87 said:
import android.app.*;
import android.os.*;
import android.view.*;
import android.widget.*;
public class MainActivity extends Activity
{
/** Called when the activity is first created. */
@override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button display = (Button) findViewById(R.id.button);
TextView tv = (TextView) findViewById(R.id.textView);
display.setOnClickListener( new View.OnClickListener(){
public void onClick(View p1)
{
setContentView(R.layout.main2);
// TODO: Implement this method
}
});
Button ba = (Button) findViewById(R.id.buttonBac);
ba.setOnClickListener(new View.OnClickListener(){
public void onClick(View p1)
{
setContentView(R.layout.main);
// TODO: Implement this method
}
});
}
}
Anyway, the first button works and when clicked, displays main2 makin 2 whoum d have a textview and a button that switches back 2 main but when the onclick listener whTever is aplyed to the code it wont even disllag the first layout and simply crashes... hitting the wall again
Click to expand...
Click to collapse
You can't do a second setContentView call in an activity. What you can do is define two Layouts within a LinearLayout in the XML, set the first one to "android:visibility="gone"", the second one to "visible" and switch between them onClick
Code:
[user=439709]@override[/user]
public void onClick(View v) {
switch (v.getId()){
case R.id.btnOne:
mLayoutFirst.setVisibility(View.GONE);
mLayoutSecond.setVisibility(View.VISIBLE);
break;
case (R.id.btnTwo):
mLayoutFirst.setVisibility(View.VISIBLE);
mLayoutSecond.setVisibility(View.GONE);
break;
}
}
Zatta said:
You can't do a second setContentView call in an activity. What you can do is define two Layouts within a LinearLayout in the XML, set the first one to "android:visibility="gone"", the second one to "visible" and switch between them onClick
Click to expand...
Click to collapse
Alternatively you could use a ViewSwitcher and call it's .next() and .previous() methods. This would probably be necessary when you have more than 2 views.
octobclrnts said:
Alternatively you could use a ViewSwitcher and call it's .next() and .previous() methods. This would probably be necessary when you have more than 2 views.
Click to expand...
Click to collapse
Thanks, never heard of before. I use that method for hiding/unhiding complete ViewGroups, Buttons and what not but I'll look into that, might be more easy.
Try naming the activity other than the "main" attribute, that is only for the first or "parent" activity. You need it to be a child activity.
Sent from my HUAWEI-M835 using xda app-developers app
Cool, learned something new

[Q] Code not bringing desired results

good day,
i'm trying to create an app that will create options in a listview on an an activity based on the option a user selects in the previous activity
below is the code i came up with but it doesn't work.
please what am i doing wrong?
thanks in advance
package com.inveniotech.moneyventure;
/**
* Created by BolorunduroWB on 9/3/13.
*/
import android.os.Bundle;
import android.app.Activity;
import android.view.*;
import android.widget.*;
import java.util.*;
import android.content.Intent;
public class menu_options extends Activity {
SimpleAdapter simpleAdpt;
Intent intent = getIntent();
public String message = intent.getStringExtra(football.EXTRA_MESSAGE);
String[] menuList;
@override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.menuoptionsview);
initList();
// We get the ListView component from the layout
ListView lv = (ListView) findViewById(R.id.listView);
// This is a simple adapter that accepts as parameter
// Context
// Data list
// The row layout that is used during the row creation
// The keys used to retrieve the data
// The View id used to show the data. The key number and the view id must match
simpleAdpt = new SimpleAdapter(this, optionList, android.R.layout.simple_list_item_1, new String[] {"options"}, new int[] {android.R.id.text1});
lv.setAdapter(simpleAdpt);
// React to user clicks on item
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parentAdapter, View view, int position, long id) {
// We know the View is a TextView so we can cast it
TextView clickedView = (TextView) view;
Toast.makeText(menu_options.this, "Item with id ["+id+"] - Position ["+position+"] - Planet ["+clickedView.getText()+"]", Toast.LENGTH_SHORT).show();
}
});
}
@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;
}
// The data to show
List<Map<String, String>> optionList = new ArrayList<Map<String,String>>();
private void initList() {
// We populate the planets
if (message.equals("5")){
menuList = new String[]{"News", "Fixtures","Results","Standings"," "};
}
else if (message.equals("6")){
menuList = new String[]{"News", "Tables"," "," "," "};
}
else if (message.equals("7")){
menuList = new String[]{"Done Deals", "Rumours","Latest News","Live","Transfer Centre"};
}
else {
menuList = new String[] {"News","Teams","Fixtures","Results","Table"};
}
optionList.add(createOptions("options", menuList[0]));
optionList.add(createOptions("options", menuList[1]));
optionList.add(createOptions("options", menuList[2]));
optionList.add(createOptions("options", menuList[3]));
optionList.add(createOptions("options", menuList[4]));
}
private HashMap<String, String> createOptions(String key, String name) {
HashMap<String, String> options = new HashMap<String, String>();
options.put(key, name);
return options;
}
}
Read This guide first, then it's easier to help you.
What I'm seeing is that you should set your message=getIntent ().... ; in the onCreate since the Intent data is probably not available before.
SimplicityApks said:
Read This guide first, then it's easier to help you.
What I'm seeing is that you should set your message=getIntent ().... ; in the onCreate since the Intent data is probably not available before.
Click to expand...
Click to collapse
Thank you. Wanted to post the link, too. :laugh:

How to use Navigation Drawer inside Tabs menu.

Hi guys.
I've been developing an app which has Swipeable menu in bottom and one of the pages , should have a Navigation Drawer to change its own Fragments.
MainActivity.java
Code:
package com.example.elizehprotowithshlkandvpis;
import com.example.elizehprotowithshlkandvpis.adapter.MainMenuAdapter;
import com.viewpagerindicator.TabPageIndicator;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.Menu;
public class MainActivity extends FragmentActivity {
private static final String[] CONTENT = { "Home", "AboutUs", "Maps", "Resturants", "Tours", "CustomerClub", "Neccesseries", "UrgentCall" };
private ViewPager mViewPager;
private FragmentPagerAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
adapter = new MainMenuAdapter(getSupportFragmentManager());
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(adapter);
TabPageIndicator indicator = (TabPageIndicator) findViewById(R.id.indicator);
indicator.setViewPager(mViewPager);
}
@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;
}
public static String[] getCONTENT() {
return CONTENT;
}
}
MainMenuAdapter.java
Code:
package com.example.elizehprotowithshlkandvpis.adapter;
import com.example.elizehprotowithshlkandvpis.MainActivity;
import com.example.menuclasses.AboutUs;
import com.example.menuclasses.CustomerClub;
import com.example.menuclasses.Maps;
import com.example.menuclasses.Neccesseries;
import com.example.menuclasses.Tours;
import com.example.menuclasses.UrgentCall;
import com.example.menuclasses.Resturants;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
public class MainMenuAdapter extends FragmentPagerAdapter {
private static final String[] CONTENT = { "Home", "AboutUs", "Maps", "Resturants", "Tours", "CustomerClub", "Neccesseries", "UrgentCall" };
private final int HOME_INDEX = 0;
private final int ABOUTUS_INDEX = 1;
private final int MAPS_INDEX = 2;
private final int RESTURANTS_INDEX = 3;
private final int TOURS_INDEX = 4;
private final int CUSTOMERCLUB_INDEX = 5;
private final int NECCESSERIES_INDEX = 6;
private final int URGENTCALL_INDEX = 7;
private final int NUMBEROFMENUS = CONTENT.length;
public MainMenuAdapter(FragmentManager fm) {
super(fm);
// TODO Auto-generated constructor stub
}
@Override
public CharSequence getPageTitle(int position) {
String[] CONTENT = MainActivity.getCONTENT();
return CONTENT[position % CONTENT.length].toUpperCase();
}
@Override
public Fragment getItem(int index) {
switch(index) {
case HOME_INDEX:
return new AboutUs();
case ABOUTUS_INDEX:
return new AboutUs();
case MAPS_INDEX:
return new Maps();
case RESTURANTS_INDEX:
return new Resturants();
case TOURS_INDEX:
return new Tours();
case CUSTOMERCLUB_INDEX:
return new CustomerClub();
case NECCESSERIES_INDEX:
return new Neccesseries();
case URGENTCALL_INDEX:
return new UrgentCall();
}
return null;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return NUMBEROFMENUS;
}
}
The Fragment that should contain a Navigation Drawer is Resturants , also drawer's mainactivity is implemented with Sherlock Library(SherlockFragmentActivity).
I'm puzzled how to call the SherlockFragmentActivity from the SherlockActivity
Thanks
Pouya_am said:
Hi guys.
I've been developing an app which has Swipeable menu in bottom and one of the pages , should have a Navigation Drawer to change its own Fragments.
MainActivity.java
Code:
package com.example.elizehprotowithshlkandvpis;
import com.example.elizehprotowithshlkandvpis.adapter.MainMenuAdapter;
import com.viewpagerindicator.TabPageIndicator;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.Menu;
public class MainActivity extends FragmentActivity {
private static final String[] CONTENT = { "Home", "AboutUs", "Maps", "Resturants", "Tours", "CustomerClub", "Neccesseries", "UrgentCall" };
private ViewPager mViewPager;
private FragmentPagerAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
adapter = new MainMenuAdapter(getSupportFragmentManager());
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(adapter);
TabPageIndicator indicator = (TabPageIndicator) findViewById(R.id.indicator);
indicator.setViewPager(mViewPager);
}
@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;
}
public static String[] getCONTENT() {
return CONTENT;
}
}
MainMenuAdapter.java
Code:
package com.example.elizehprotowithshlkandvpis.adapter;
import com.example.elizehprotowithshlkandvpis.MainActivity;
import com.example.menuclasses.AboutUs;
import com.example.menuclasses.CustomerClub;
import com.example.menuclasses.Maps;
import com.example.menuclasses.Neccesseries;
import com.example.menuclasses.Tours;
import com.example.menuclasses.UrgentCall;
import com.example.menuclasses.Resturants;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
public class MainMenuAdapter extends FragmentPagerAdapter {
private static final String[] CONTENT = { "Home", "AboutUs", "Maps", "Resturants", "Tours", "CustomerClub", "Neccesseries", "UrgentCall" };
private final int HOME_INDEX = 0;
private final int ABOUTUS_INDEX = 1;
private final int MAPS_INDEX = 2;
private final int RESTURANTS_INDEX = 3;
private final int TOURS_INDEX = 4;
private final int CUSTOMERCLUB_INDEX = 5;
private final int NECCESSERIES_INDEX = 6;
private final int URGENTCALL_INDEX = 7;
private final int NUMBEROFMENUS = CONTENT.length;
public MainMenuAdapter(FragmentManager fm) {
super(fm);
// TODO Auto-generated constructor stub
}
@Override
public CharSequence getPageTitle(int position) {
String[] CONTENT = MainActivity.getCONTENT();
return CONTENT[position % CONTENT.length].toUpperCase();
}
@Override
public Fragment getItem(int index) {
switch(index) {
case HOME_INDEX:
return new AboutUs();
case ABOUTUS_INDEX:
return new AboutUs();
case MAPS_INDEX:
return new Maps();
case RESTURANTS_INDEX:
return new Resturants();
case TOURS_INDEX:
return new Tours();
case CUSTOMERCLUB_INDEX:
return new CustomerClub();
case NECCESSERIES_INDEX:
return new Neccesseries();
case URGENTCALL_INDEX:
return new UrgentCall();
}
return null;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return NUMBEROFMENUS;
}
}
The Fragment that should contain a Navigation Drawer is Resturants , also drawer's mainactivity is implemented with Sherlock Library(SherlockFragmentActivity).
I'm puzzled how to call the SherlockFragmentActivity from the SherlockActivity
Thanks
Click to expand...
Click to collapse
That last sentence does not make sense I assume you mean from the Fragment?! Well to access the holding activity you need to add an interface to the Fragment which the activity will implement.
Am I understanding you correctly that have a horizontal ViewPager and one of the Fragments should have a Navigation drawer? To accomplish that you would still need the whole activity to hold the drawer and figure out a way to disable it when the user is on some other Fragment. But my question is why you want to use a navigation drawer in that case, because the drawer is meant for navigating on the same hierarchical level, but the different items are more independent than with the ViewPager. It doesn't really make sense to have both a drawer and horizontal swiping, even the YouTube app really bothers me with its swiping between suggestions and feed. I think what you need to use here is a spinner in the ActionBar, replacing the title of the Fragment.
SimplicityApks said:
That last sentence does not make sense I assume you mean from the Fragment?! Well to access the holding activity you need to add an interface to the Fragment which the activity will implement.
Am I understanding you correctly that have a horizontal ViewPager and one of the Fragments should have a Navigation drawer? To accomplish that you would still need the whole activity to hold the drawer and figure out a way to disable it when the user is on some other Fragment. But my question is why you want to use a navigation drawer in that case, because the drawer is meant for navigating on the same hierarchical level, but the different items are more independent than with the ViewPager. It doesn't really make sense to have both a drawer and horizontal swiping, even the YouTube app really bothers me with its swiping between suggestions and feed. I think what you need to use here is a spinner in the ActionBar, replacing the title of the Fragment.
Click to expand...
Click to collapse
Yeah you're right , I just want to use the drawer in order to change contents.
So as you suggest , I just have to use a simple tricks to make the drawer enable in desire Fragment.
I don't want to , I am asked to make it possible....

[Q] Open another screen on button click

Hello, I'm extremely new to both android application developing, and this forum.
I am trying to open another screen ( activity ) when a button ( readyButton ) on the splash screen is pressed. I've tried at least ten different times with different tutorials to no avail, this is my current code which didn't work, and instead forces the app to crash.
Code:
public class MainActivity extends Activity {
// Called when the activity is first created.
[user=439709]@override[/user]
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
OnClickListener listener = new OnClickListener() {
[user=439709]@override[/user]
public void onClick(View v) {
Intent intent = new Intent("SecondActivity");
startActivity(intent);
}
};
Button btn = (Button) findViewById(R.id.readybutton);
btn.setOnClickListener(listener);
}
}
Please help.
The button's name is 'readybutton'
the second activity name is 'SecondActivity'
also, where am I supposed to put this code into the java class? Here is how it is currently set up:
Code:
package com.unchat;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.view.View.OnClickListener;
// Default Items
public class FirstActivity extends Activity {
[user=439709]@override[/user]
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
}
/** New button code start */
public class MainActivity extends Activity {
// Called when the activity is first created.
[user=439709]@override[/user]
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
OnClickListener listener = new OnClickListener() {
[user=439709]@override[/user]
public void onClick(View v) {
Intent intent = new Intent("SecondActivity");
startActivity(intent);
}
};
Button btn = (Button) findViewById(R.id.readybutton);
btn.setOnClickListener(listener);
}
}
/** new button code end */
[user=439709]@override[/user]
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.my, menu);
return true;
}
[user=439709]@override[/user]
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
// End of Default Items
incorrectly announces intent
Try like this.
Code:
Intent intent = new Intent(this, SecondActivity.class) ;
startActivity(intent);
and check whether your Listener
1. you need to use the full name of your activity, including the package name.
2. you need to declare the activity in your AndroidManifest.xml file before calling it.
rhmsoft said:
2. you need to declare the activity in your AndroidManifest.xml file before calling it.
Click to expand...
Click to collapse
Unless he want's to run an activity that's not his, like opening the contact list, but I think you're right in assuming he's looking to launch a second one of his own activities.
bornander said:
Unless he want's to run an activity that's not his, like opening the contact list, but I think you're right in assuming he's looking to launch a second one of his own activities.
Click to expand...
Click to collapse
I thought you had to declare all your activities in the manifest?
Log
Post the error log please

Categories

Resources