I am really lost here.
I have an application which uses 3 XML files which each xml file is for each tabs ( i have 3 tabs with my app), which works fine. Under one of the tab there is a button, which when I click on it , it meant to pick up the name and number from the contact application and print it in that tab's screen, which is done by connecting to database and picking up names and number after being selected from the contact app. they all work fine.
But always after the process and picking up data and setting the textview values under the tb3, the main 3 tabs are disapeared and also all the 3 diffrent XML fileas are combined together Does anyone knows why?
Does anyone knows what to do ?any tutorial ?
someone suggested Fragmentmanager but I have no idea how to use that?
Pleas, please someone help me with this.
Here is the code for main activity which shows the tabs :
public class MainActivity extends TabActivity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Resources res = getResources(); // Resource object to get Drawables
TabHost tabHost = getTabHost(); // The activity TabHost
Button addbutton = (Button) findViewById(R.id.addButton);
tabHost.addTab(tabHost.newTabSpec("tab1").setIndicator("tab1",
res.getDrawable(R.drawable.tab1)).setContent(R.id.tab1Layout));
tabHost.addTab(tabHost.newTabSpec("tab2").setIndicator("tab2",
res.getDrawable(R.drawable.tab2)).setContent(R.id.tab2Layout));
tabHost.addTab(tabHost.newTabSpec("tab3").setIndicator("tab3",
res.getDrawable(R.drawable.tab3)).setContent(R.id.tab3));
tabHost.setCurrentTab(2);
addbutton .setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(MyActivity.this,ContactsDemo.class);
MainActivity.this.startActivityForResult(intent,1);
}
});
}
}
This suppose to open myActivity and then goes back to the MainAcitiviy. But after it loads the other activity, all three tabs disappears and all the three xml files combines.
Given your information I can only assume that addButton is a Button on one of your Tabs and that you want to return to the originating tab or one of the other two. I've built a similar app and I think you have to place the startActivityForResult()-call in one of the tab-activities, since you mentioned tb3 this would be a good start. When I do a startActivity() or startActivityForResult()-call and this new activity is closed or the user hits the back-button I'm back on my tab with the tabs-ribbon visible.
Hope this helps.
Related
When I first ran the sample HelloWorld app, it displays the hello world text on the emulator. I decided then to delete that and make a button. What I wanted is that when I click the button, it will show a text "This is the second activity". I made another XML file and another class to handle the second activity to display the text. But when I ran again, I cannot see the changes on the UI for the emulator. The text "This is the second activity" does not show after I clicked the button. I saved everything. How would I automatically update the UI of the emulator after some of the changes made on the design? I am new to android development. Please help me. Btw I cannot post images so it requires 10 reputation that's why I used online image viewing. Sorry for that.
Here is my Graphical layout on eclipse: activity_main.xml
http://s16.postimg.org/wusm4qrp1/image.png
second.xml
http://s29.postimg.org/qft17p5on/image.png
Running the emulator:
http://s28.postimg.org/f9dn32ku5/image.png
After clicking the button (in which case the text I edit does not show):
http://s16.postimg.org/75r62dodx/image.png
Because you didn't post your code, I'll try to explain it from the beginning.
I don't know if you can do it in an other way, but the following answer assumes we aim the good programming practice.
Both of the activities have their distinct layouts set in the following way, right?
public class MyFirstActivity extends Activity{
...
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_first);
...
public class MySecondActivity extends Activity{
...
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
...
Then, in your first activity, define onClickListener of your button
...
setContentView(R.layout.activity_first);
Button myButton = (Button)findViewById(R.id.my_button);
myButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(MyFirstActivity.this, MySecondActivity.class);
startActivity(intent);
}
});
That being said, I don't recommend you to have two activities for this functionality. You should have different activities when you need different layouts. Putting the button and the textview in the same layout and updating textview inside the button's onclicklistener is a better solution.
I have been trying to figure this thing out with no luck in eclipse. I have created 3 screens. One being the main menu which a button leading to another button which then leads to an Activity. I can get the button from the main menu to lead onto the button onto the second screen but i can't get the second button to lead onto the third screen.
Can anybody help me?
appproject
You should really post some code so we can help you...but it seems like you need to look at your onClick() method for the second button.
Providing you have three separate Activities for each of these three screens (I'll call them ActivityA, ActivityB, and ActivityC), you'll also probably need an XML layout for each. There are ways of doing it without an XML layout, but for now just stick with that.
The next thing is you want to make sure your Button is initialized properly. ActivityB should look like:
public class ActivityB extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_b);
Button b = new (Button) findViewById(R.id.button2);
b.setOnClickListener(new OnClickListener() {
#Override
protected void onClick() {
startActivity(new Intent(this, ActivityC.class);
}
});
}
The first thing you should do is check to make sure your button functions in a way similar to how I've described here. If that doesn't change anything, make sure you are initializing your Button in accordance to how it is defined in the layout XML. You must use an id for a Button that is in the same XML layout as the one you set in setContentView(). If not, it will do nothing, no matter what you put in the onClick() method.
I hope that helps!!
Firstly, Im new to android but have years of various other programming experience on unix, windows,but not with Java or android.
Im wanting to display a tab with 3 tabs, each having a different layout file (which works).
Im working on displaying a "blank" template and then the data is retrieved from an XML file once the user points a setting to one (i.e. via shared preferences).
My problem is that the function populateXMLCharacter is never called (using breakpoints). The tab activity always displays "", which is a strig the string.xml.
Actually putting a breakpoint in the oncreate function never gets called either. Ive tried using a call to populateXMLCharacter in onResume, but it too never gets called.
Im thinking its because of the call to the tab:
Resources res = getResources(); // Resource object to get Drawables
TabHost tabHost = getTabHost(); // The activity TabHost
TabHost.TabSpec spec; // Resusable TabSpec for each tab
// Do this and the same for all tabs
spec = tabHost.newTabSpec("Description").setIndicator("Description",
res.getDrawable(R.drawable.android)).setContent(R.layout.tab_harp_description);
tabHost.addTab(spec);
Should I be using an intent? I thought the layout was created in a seperate function (in this case harpCSDescription.java and then this allows that activity to be inserted into the tabhost?
other functions:
public class harpcsDescription extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView (R.layout.tab_harp_description);
}
//#Override
protected void onResume()
{
// NOW WE ADD DATA TO THE TEMPLATE
populateXMLCharacter();
}
public void populateXMLCharacter()
{
Ive tried using intents in the following manner but it simply crashes before any breakpoints are reached.
http://developer.android.com/resources/tutorials/views/hello-tabwidget.html
Can some one please guide me to what I should be doing to create an activity with data that is retrieved AFTER the tab is created?
I think you need an intent. To attach an activity to a tab, write the following in the tab host:
Intent intent = new Intent(this, MyActivity.class);
and then in the tab spec write:
tabSpec = tabHost.newTabSpec("name").setIndicator(this.getString(R.string.name)).setContent(intent)
Then in the MyActivity class, you can specify the layout and load the XML data to populate the tab in the onCreate() method. You can use a different activity for each tab.
I have an Android application which has four tabs (I use a main TabActivity with TabHost and TabSpecs).
In one of my sub activity (activity opened in a tab), i need to open a tab not by clicking on the tab title and i don't know how to do this.
For example, i have a button in my activity and when i click on it, it opens a different tab.
For the moment, it is what i do:
Intent intent = new Intent(myActivity.this, myTabActivity.class);
intent.putExtra("ComeFrom", true);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
Then in the TabActivity, if i get true reading the "ComeFrom" extra i open the wished tab but the problem is that it kills all the other activies. So, if someone knows a better (cleaner) way to do that trick, please tell me...
Found an easier (I think) answer:
on the TabActivity declare a public, static and self variable and populate it on the onCreate method. F.e.:
public class TheActivity extends TabActivity {
public static TheActivity self;
...
#Override
public void onCreate(Bundle savedInstanceState) {
self=this;
on any Activity running in a tab, when you want to change the one shown on your app. you can do this:
TabHost tabHost = TheActivity.self.getTabHost();
tabHost.setCurrentTab(0);
Worked ok for me, hope serves someone else!
You have to use TabHost's "setCurrentTab(...)" for that. In one of my projects, I created a static method in the main Activity (the one with the TabHost), named "swtichToTab(int tab)". In my subactivites (those inside the tabs) could then just call "MainActivity.switchToTab()" to trigger switching.
It may not be the cleanest method, I'm sure you can achieve this using broadcast intents too.
You can create a BroadcastReceiver and send a broadcast with the index of the tab as extra
You can use views instead of activities for the content of the tabs. This way, the code is simpler and doesn't use as much memory. Plus, you then can use the setCurrentTab(tabIndex) method to easily switch between views.
I have a simple tutorial here. It has a tab activity with a list and map view. When you you click on an item in the list, the activity dynamically goes to the map view (using the setCurrentTab(tabIndex) method). You can easily modify this to have a button switch views.
I have created two tabs, say TAB1 and TAB2. For TAB1, i have loaded one Activity, say ActivityOne, into TAB1 as
Intent intent = new Intent(this,ActivityOne.class);
TabHost.TabSpec spec = getTabHost().newTabSpec("ActivityOne")
.setIndicator("Activity One",getResources().getDrawable(R.drawable.artists)).setContent(intent);
getTabHost().addTab(spec);
This ActivityOne has extended the ActivityGroup and i added one button in this activity. By clicking on this button it will call another activity, say ActivityOne_One, as
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(ActivityOne.this,ActivityOne_One.class);
replaceContentView("ActivityOne_One",intent);
}
public void replaceContentView(String id, Intent intent){
View view = this.getLocalActivityManager().startActivity(id, intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)).getDecorView();
this.setContentView(view);
}
When we click on that button ActivityOne_One will be launched under same TAB1. In this application i have two problems:
1) If i want to go back to ActivityOne under same TAB1 by using traditional BACK button on emulator it is not working..
2)ActivityOne_One is launching with no animation(like sliding from right to left) effect.
If anyone know about any one of these, give your advice..
Thanks,
venu
I have found the solution for my question. The following blog gave me a route for this
http://ericharlow.blogspot.com/2010/09/experience-multiple-android-activities.html
see this link which may help you out with this. http://blog.henriklarsentoft.com/2010/07/android-tabactivity-nested-activities/
i have implemented what is advised in the link i provided. it works OK, but it is quirky and does not always redirect you BACK like you would expect. i have found that you must implement your own custom tabs to really get this desired effect.