I have 4tab on every tab I have listview, on selecting row(any row) I want to generate activity with 6tab,.
#parkinson, Ok, so create an activity that has tabs. OnClick of a list item spawn a new intent and startActivity(new intent). BTW punctuation will help us understand what you are trying to say.
http://developer.android.com/guide/topics/fundamentals.html
Also read this, It will help with spawning new activities.
Related
I'm not so good at Android but I'm working on my project. So please forgive my ignorance.
I'm making a set of similar items with need prefenrences setting, so I made them preference fragments for each, and put those preference fragments in a viewpager, for easier use. I want to go to fragment No.1 if I clicked item No.1, then maybe fragment No.3 if I clicked on item No.3. Simply put, I want the items can link to its own preference fragment in the viewpager. The items are in MainActivity, the viewpager is in its own activity and this viewpager activity would not start as the app starts unless an item was clicked.
I learnt that viewPager.setCurrentItem(position); should be used when I want to get to a specific page of a viewpager. But my items are in MainActivity, not inside the viewpager activity. I set the viewpager as static, and put viewPager.setCurrentItem(position); inside onClick method in MainActivity, but when I click a item, the app crashes. It shows error: java.lang.NullPointerException. The app had worked fine before I dicided to link those items to their own settings, and now I really have no idea...
Could you please tell me whether it is possible to get to a specific page of a viewpager from other activity? If it is possible, how to do? Thank you so much!
Renew:
I tried to mess with the code, I changed the adapter of the viewpager into protected static MyAdapter adapter;. There was no crash then, but I always went to the first page of the viewpager, whichever item I clicked...
I think you need to pass the data to the activity.
When you call the ViewPager in the MainActivity, set a parameter.
Intent intent = new Intent(getBaseContext(), ViewPagerActivity.class);
intent.putExtra("EXTRA_PAGE", (String) position);
startActivity(intent);
Then in the start of the View Pager Activity, get the object and change the currentItem.
Bundle extras = getIntent().getExtras();
if (extras != null) {
String value = extras.getString("EXTRA_PAGE");
int position= Integer.parseInt(value );
viewPager.setCurrentItem(position);
}
Android can only have 1 visible activity at the same time, so i think it's impossible in your way.
Another approach will be the use of Fragment, but it depends on your app design and structure.
You can check more about Fragments here (Android documentation).
Hope it helps ;)
Best Regards
I am creating my first app. In this app, I have an expandable list with many items. When I select any of these items, I want several paragraphs to be displayed. Do I need to create an Activity for each of these items if text is the only thing I want displayed? I know that there has to be an easier way. I did create it like this at first and it seemed very bulky (30+ activities), so now I have it set up so that when an item is selected, the setContentView opens the corresponding layout with the text that needs to be displayed. This works but there is a catch, whenever I hit the back button, it takes me back to my main activity class and not my expandable list class. I want the user to be able to go back and select something else from the list. Any guidance as to what I need to do would be appreciated.
I would suggest creating string resources for each item you would like to display, then creating one activity with a TextView. Then, instead of creating new intents for each activity, create an intent that goes to the new activity, and add an extra that contains the text for the TextView. For example:
Activity1:
myButton.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
Intent intent = new Intent(this, ParagraphView.class);
intent.putExtra("textData", getResources().getString(R.string.myText));
getBaseContext().startActivity(intent);
}
});
In the onCreate of the viewer, add this to get your TextView:
Intent intent = getIntent();
String textData = intent.getStringExtra("text");
Now, we need to write the text into the TextView:
TextView tv = (TextView) findViewById(R.id.myTextView);
tv.setText(textData);
All you have to to is set up your string resources and button click listeners. You may consider this easier than having lots of activities (it's definitely easier to manage entries this way) but does require a bit of setup.
Edit: Thanks to #ianhanniballake for pointing out a much better way (I don't even know what I was thinking at the time...)
Edit2: Wow, I REALLY messed up my code. (Hopefully) Fixed now
This is my first week of Android development and I am having some troubles so please be patient with me.
This is really simple but all other answers weren't clear or detailed enough for me to apply it.
I am trying to switch from my "activity_main.xml" to a second .xml after a button click. I have already connected the button and put in setContentView(R.layout.view) and it works but I want it to animate. I want the view to come from the right and then the opposite when the user press back. I am doing this in eclipse if that helps.
Thanks in advance!
Assuming what you're after is a transition between two activities, here's what you're going to need to do:
Create a new Activity class. For this example, lets name it MySecondActivity.
In this new Activity class, make sure you're inflating the new layout xml.
In the original Activity class, open the new Activity with an Intent, then on the new activity, call the overridePendingTransition with the animation you want:
Code sample:
Intent intent = new Intent(this, MySecondActivity.class);
startActivity(intent);
getActivity().overridePendingTransition(android.R.anim.slide_in_left, android.R.anim.slide_out_right);
In this example, I'm using Android pre-defined animations. You can create your own too, but I feel this might be enough for your needs.
Hope this helps.
I have implemented a TabHost. In one tab I have Activity1, which calls Activity2 after a button click, which calls Activity3 after a button click, which calls Activity1 after a button click, etc.. No backstack functionality is required, just 1 --> 2 --> 3 --> 1, etc. All three activities have a separate layout file.
Everything works fine, except that after the first transition from 1 --> 2 the activities grab the entire screen and the tabs are invisble forever.
Question: how can I keep these three activities within the confinement of de tab area and the tabs visible? The problem has been recognized here many times before; the solution used to be ActivityGroups, but these are deprecated and Fragments are advised instead. I have seen many examples here, but nothing that could help me.
Can I keep my three activites (Activity1 extends Activity, etc)?
Should I add fragment tags to the layout files?
Do I need to work with transactions?
Should I work with one fragment class or three?
Can you please give me a few hints how I should go about? I woud already be helped if you tell which classes I need to use and of what type they are.
Thanks in advance.
It took me more than half a day, but finally found a solution that works. Unfortunately I am still stuck with deprecated issues (Activity Group and getLocalActivityManager().startActivity(..)).
Again I have a single tab under a TabHost and several activities, all operating within that tab. Navigation from one activity to the next occurs with a buttonclick. Solution:
all Activities operating within the tab need to extend ActivityGroup
All Activity classes need to have a button handler that links to the next activity like this:
public void onBtnClicked(View view) {
Intent intent = new Intent(view.getContext(), NextActivity.class);
replaceContentView("NextActivity", intent);
}
public void replaceContentView(String id, Intent newIntent) {
View view = getLocalActivityManager().startActivity(id, newIntent.
addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)).getDecorView();
this.setContentView(view);
}
By this the tabs remain visible all the time, as desired.
Hope this helps someone.
am not too experienced in android and i am just using the TabActivity, so please bear with me. i keep seeing post about not using activites in Tabhost but views. and am not sure which is which and if thats the reason of my latest headache? i have a code with 4 tabs like this:
// Category TabActivity class
tab.setContent(new Intent(this, Mylist.class));
tab1.setContent(new Intent(this, Mylist.class));
tab2.setContent(new Intent(this, Mylist.class));
and each tab is showing the result of a method in the Mylist Activity.
is that using an activity in a tab or displaying a view? whats the difference?.
i have a context menu on an item selected in the Mylist Activity which updates the list. how can i reflect the changes in the list back to the tab in other to display that particular method in the Mylist class, that was set as the tabs content. obviously this will change if i updated or deleted an item from the list when the tab is shown. i think its possible with onResume(), but don't know what to call there or is there any better way?
in the Mylist class, i have tried this little piece of code to restart the TabActivity:
myAdapter.deleteItem(id);
fillData(); //
Intent refereshCategory = new Intent(this, Category.class);
startActivity(refereshCategory);
its restarting the activity after the item have been deleted, but how can i only show the tab whose view was in focus when it restarts and i don't want fillData() method to be shown as it does not have to do with the tabs. i hope i made myself clear enough. Thanks
Any help will be greatly appreciated. Thanks for your time.
With tab.setContent(new Intent(this, Mylist.class)); You are telling the application to display a new MyList Activity in that tab.
I'm not sure of the activity lifecycle for tabbed activities but I think it is safe to assume they follow the same standards as regular activities. onResume is called every time the activity shown (which would include being switched from another tab's activity to this activity). So any kind of updating you want to do to the list every time it is shown should go here and not in onCreate(Bundle savedInstanceState)
To create any kind of Android application you should become familiar with the Component Lifecycle