MapActivity in TabHost - android

I have a CUSTOM tab host the uses the addTab() method instead of tabspec. one of my tabs in a MapViewthat shows a specific location. The problem I am encountering is an exception on the line in my tabhost that sets up the mapview tab.
Tab Activity (removed package for confidential reasons):
import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TabHost;
import android.widget.TextView;
public class CustomTabHost extends TabActivity {
/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tabhost);
setTabs() ;
}
private void setTabs()
{
addTab("Services", R.drawable.servicesicon, services.class);
addTab("Our Work", R.drawable.ourworkicon, ourwork.class);
addTab("RSS", R.drawable.rssicon, RSSReader.class);
addTab("Locate IML", R.drawable.locateicon, Locate.class);
addTab("Contact IML", R.drawable.contacticon, ContactUs.class);
}
private void addTab(String labelId, int drawableId, Class<?> c)
{
TabHost tabHost = getTabHost();
Intent intent = new Intent(this, c);
TabHost.TabSpec spec = tabHost.newTabSpec("tab" + labelId);
View tabIndicator = LayoutInflater.from(this).inflate(R.layout.tabs_bg, getTabWidget(), false);
TextView title = (TextView) tabIndicator.findViewById(R.id.title1);
title.setText(labelId);
ImageView icon = (ImageView) tabIndicator.findViewById(R.id.icon1);
icon.setImageResource(drawableId);
spec.setIndicator(tabIndicator);
spec.setContent(intent);
tabHost.addTab(spec);
}
}
MapActivity:
import java.util.List;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;
public class Locate extends MapActivity {
#Override
protected void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.locateiml);
MapView mapview = (MapView) findViewById(R.id.locatemap);
mapview.setBuiltInZoomControls(true);
List<Overlay> mapOverlays = mapview.getOverlays();
Drawable drawable =this.getResources().getDrawable(R.drawable.locateicon);
Locateoverlay itemoverlay = new Locateoverlay(drawable, this);
GeoPoint point = new GeoPoint(382571066,-85751392);
OverlayItem overlayitem = new OverlayItem(point, "Interactive", null);
itemoverlay.addOverlay(overlayitem);
mapOverlays.add(itemoverlay);
}
protected boolean isRouteDisplayed()
{
return false;
}
}
There is also an overlay, but that is not needed for this question. My question is, I am pretty sure I need to create a parent activity the handles the map activity, but I do not know how to do this.

I know that the Activity in TabHost is not considered as Activity. I think it is managed as View.
In my case, main Activity was inherited Activity and TabHost was implemented through xml configuration. Then I added MapActivity -as a TabSpec- to TabHost, but MapView shows gray background only.So, I make main Activity inherit MapActivity, and it worked.
I don't know whether this is best solution, whether your main Activity must inherit TabActivity, but if there is no obvious reason, I think it is better to inherit MapActivity rather then TabActivity.Maybe my answer is not the best, I wish I could help you in some way.Good luck!

TabActivity caused me a lot of issues in the past. This might be another issue related to it.
Right now TabActivity is deprecated. The doc says:
New applications should use Fragments instead of this class; to
continue to run on older devices, you can use the v4 support library
which provides a version of the Fragment API that is compatible down
to DONUT.
To use a MapView inside a Fragment you will need to use: petedoyle's android-support-v4-googlemaps.

Related

Previous activity visible under the new fragment

When I call fragment from activity then fragment calling successfully but some part of previous activity is on the top of in fragment, anyone help to correct the code
package com.example.newproject;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import static android.view.View.GONE;
public class ThankYouPageActivity extends AppCompatActivity {
Button thank_continue_btn,thank_track_btn;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_thank_you_page);
getSupportActionBar().setTitle("Confirmation");
thank_continue_btn = findViewById(R.id.thank_continue_btn);
thank_track_btn = findViewById(R.id.thank_track_btn);
findViewById(R.id.thank_continue_btn).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
FragmentManager fm = getSupportFragmentManager();
HomeFragment fragment = new HomeFragment();
fm.beginTransaction().replace(R.id.thanku_container, fragment).addToBackStack(null).commit();
set background to white in the root tag of your fragment's xml
background = "#FFFFFF"
The container of your Fragment must be overlapped by your Activity layouts. Please attach the layouts of your Activity and Fragment so that I can help you find your issue.
I had same issues , i fixed it by removing previous fragment while setting new fragment in oncreateview
container.removeAllViews(); before replacing the fragment
R.id.thanku_container - I think it is FrameLayout?
Make it full match_parent. Or set background color like activity.

Having errors with R variables

I am getting errors on the R variables. The variables are R.id.drawer_layout, R.id.layout, and R.id.main. There is a red squiggly line under the words after id. These are all errors from R. Is there something wrong with my layout? I'm just beginning android programming and this is coming from github.
http://manishkpr.webheavens.com/android-navigation-drawer-example-using-fragments/
^This is where the tutorial and code is from.
package com.anshikka.datagushalpha;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.widget.DrawerLayout;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class MainActivity extends FragmentActivity {
final String[] data ={"one","two","three"};
final String[] fragments ={
"com.anshikka.datagushalpha.FragmentOne",
"com.anshikka.datagushalpha.FragmentTwo",
"com.anshikka.datagushalpha.FragmentThree"};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
#SuppressWarnings({ "unchecked", "rawtypes" })
ArrayAdapter adapter = new ArrayAdapter(getActionBar().getThemedContext(), android.R.layout.simple_list_item_1, data);
final DrawerLayout drawer = (DrawerLayout)findViewById(R.id.drawer_layout);
final ListView navList = (ListView) findViewById(R.id.drawer);
navList.setAdapter(adapter);
navList.setOnItemClickListener(new OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> parent, View view, final int pos,long id){
drawer.setDrawerListener( new DrawerLayout.SimpleDrawerListener(){
#Override
public void onDrawerClosed(View drawerView){
super.onDrawerClosed(drawerView);
FragmentTransaction tx = getSupportFragmentManager().beginTransaction();
tx.replace(R.id.main, Fragment.instantiate(MainActivity.this, fragments[pos]));
tx.commit();
}
});
drawer.closeDrawer(navList);
}
});
FragmentTransaction tx = getSupportFragmentManager().beginTransaction();
tx.replace(R.id.main,Fragment.instantiate(MainActivity.this, fragments[0]));
tx.commit();
}
}
Import your R,
is something like
import com.yourproject.R;
Look into you imports and if you find "android.R" delete it. Finally, press Ctrl + Shift + O to import your project's R (using Eclipse as IDE) or just manually add it:
import com.anshikka.R
After that you should see com.anshikka.R on your imports at the top of the file.
Check if you have two main.java classes. I had this problem a couple of days ago when I imported a project into a current project. I had two main classes and after I deleted one the R error disappeared. Also make sure you imported the right R class. If after you imported the R class you get a redline under .activity_main you imported the wrong R class
I suppose those variables don't exist, since the red line only appears under the words after "id".
If you haven't copied the file activity_main.xml provided in the link you mentioned, which defines "#+id/drawer_layout", "#+id/"main" and "#+id/drawer", into your project's res/layout folder, the corresponding variables weren't generated.

Adding extended fragment to Activity

I have been trying to build a Master-Detail f low in android but wanted to change one of the detail fragments to a different fragment. As this is one of my first Android applications, I was just trying to make a picture appear on this new fragment. For this, I built the following two classes
1) Fragment class ( displays the picture to be displayed )
package com.userName.miscapp;
import com.userName.miscapp.dummy.DummyContent;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class PictureFragment extends Fragment {
// Default Copy Constructor for the fragment
public PictureFragment() {
}
#Override
public void onCreate ( Bundle savedInstanceState )
{
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView ( LayoutInflater inflater , ViewGroup container , Bundle savedInstanceState )
{
View rootView = inflater.inflate(R.layout.fragment_simple_picture, container, false);
return rootView;
}
}
2) Activity to display the same
package com.userName.miscapp;
import android.os.Bundle;
import android.app.Activity;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.view.Menu;
public class SimplePicture extends FragmentActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getActionBar().setDisplayHomeAsUpEnabled(true);
if (savedInstanceState == null) {
// Create the detail fragment and add it to the activity
// using a fragment transaction.
Bundle arguments = new Bundle();
arguments.putString(ItemDetailFragment.ARG_ITEM_ID,
getIntent().getStringExtra(ItemDetailFragment.ARG_ITEM_ID));
PictureFragment frag = new PictureFragment();
frag.setArguments(arguments);
android.support.v4.app.FragmentManager fragmentManager = getSupportFragmentManager();
android.support.v4.app.FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.add(R.id.simple_picture_container,frag).commit();
//setContentView(R.layout.activity_simple_picture);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.simple_picture, menu);
return true;
}
}
On compilation, it does not recognize the PictureFragment to be an extension of Fragment class. This is inspite of it being clearly written in the first file. Searching for solutions on Stackoverflow said to extend FragmentActivity instead of Activity and trying to use getSupportFragmentManager() neither of which helped.
PS : Using 11 as the base for the current application.
Any help would be appreciated
Thanks
This is because you are using android.app.Fragment from new API in conjunction with android.support.v4.app.FragmentManager from support library, you should replace import in your PictureFragment from android.app.Fragment to android.support.v4.app.Fragment to make it work.

Android Tab icon for 4.0 and later

I want to set icon on a tab. My codes are right and I am sure.Is it a problem because of the version of android? I looked lots of codes but all of them are the same.The codes are;
package com.mesutemre.androidtabkullanimi;
import android.os.Bundle;
import android.app.Activity;
import android.app.ActivityGroup;
import android.app.TabActivity;
import android.content.Intent;
import android.view.Menu;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;
public class MainActivity extends TabActivity {
#SuppressWarnings("deprecation")
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TabHost tabhost = getTabHost();
tabhost.setup();
tabhost.addTab(tabhost
.newTabSpec("tabindis1")
.setIndicator("SEKME1",
getResources().getDrawable(R.drawable.document))
.setContent(new Intent(MainActivity.this, Sekme1Activity.class)));
tabhost.addTab(tabhost
.newTabSpec("tabindis2")
.setIndicator("SEKME2",
getResources().getDrawable(R.drawable.music))
.setContent(new Intent(MainActivity.this, Sekme1Activity.class)));
tabhost.addTab(tabhost
.newTabSpec("tabindis3")
.setIndicator("SEKME3",
getResources().getDrawable(R.drawable.delete))
.setContent(new Intent(MainActivity.this, Sekme1Activity.class)));
tabhost.addTab(tabhost
.newTabSpec("tabindis4")
.setIndicator("SEKME4",
getResources().getDrawable(R.drawable.close))
.setContent(new Intent(MainActivity.this, Sekme1Activity.class)));
}
#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;
}
}
I read from some links that this is a problem adroid 4.0 and later. What can i do for setting icon on a tab.
Pls try setView to set TextView instead of setIndicator.
Set icon and indicator text to TextView.

android public textbox

I have come across another prickly little problem.
I am writing an app with tabs, but I have a textbox (EditText) at the top of the screen that I want to be able to receive text data from any of the tabs. As each of the tabs has it's own activity and layout, this is proving difficult to achieve.
I want to be able to use:
editText1.setText("Hello World");//sample text
from any Tab/Activity.
Does anyone know how to make a textbox from one layout public and able to recieve text?
I am using TabActivity, yes I know it's deprecated but as this is my first app with tabs, it's easier to learn than Fragments. I will try them next time, unless they are the answer to my problem, in which case I have a lot of re-coding to do!!
ok, new part.
package com.epsilonitsystems.epecsandroid;
import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.TabHost;
import android.widget.TextView;
import android.widget.TabHost.TabSpec;
import android.view.Menu;
import android.speech.tts.TextToSpeech;
public class MainActivity extends TabActivity {
public EditText editText1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText1 = (EditText) findViewById(R.id.editText1);
TabHost tabHost = getTabHost();
// Tab for Core
TabSpec corespec = tabHost.newTabSpec("Core");
corespec.setIndicator("", getResources().getDrawable(R.drawable.ic_i));
Intent coreIntent = new Intent(this, CoreActivity.class);
corespec.setContent(coreIntent);
// Tab for Drink
TabSpec drinkspec = tabHost.newTabSpec("Drink");
drinkspec.setIndicator("", getResources().getDrawable(R.drawable.ic_drink));
Intent drinkIntent = new Intent(this, DrinkActivity.class);
drinkspec.setContent(drinkIntent);
// Adding all TabSpec to TabHost
tabHost.addTab(corespec); // Adding Core tab
tabHost.addTab(drinkspec); // Adding Drink tab
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
That's the Main Activity, I'll just show the Core Activity as they will all be the same when I get it working.
package com.epsilonitsystems.epecsandroid;
import android.app.Activity;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
import android.speech.tts.TextToSpeech;
public class CoreActivity extends Activity {
private TextToSpeech mTts;
// This code can be any value you want, its just a checksum.
private static final int MY_DATA_CHECK_CODE = 1234;
EditText editText1;
String Spch,Str;
ImageButton imageButton1,imageButton2;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.core_layout);
//button1 = (Button) findViewById(R.id.button1);
editText1 = (EditText) findViewById(R.id.editText1);
mTts = new TextToSpeech(this, null);
}
public void onimageButton1Click(View view) {
//mTts.speak("I",TextToSpeech.QUEUE_FLUSH,null);
//Spch=editText1.toString();
//Spch=Spch+"I ";
editText1.setText("Hello World");
}
}//Activity End
I can't post a screenshot as I'm still a new user, sorry.
Any ideas please?
Gary,
What you are looking for probably is how to share data between different Activities.
You can do this in a couple of ways, but the cleanest is using the Intent you start the new Activity with.
With this you can set extra data via the Intent.putXXXXX methods.
You could use something as Intent.putExtra("my_private_key", mTextVar); and fetch that out in your other Activity with this.getIntent().getStringExtra("my_private_key");
ps. As tempting as it might seem to start with the TabActivity, you're actually making it a lot more difficult for yourself by making a bad start and learning classes which you should not use anymore. I'd advise you to pick up some good fragments tutorials which will be just as easy once correctly explained. You should take a look at per example http://www.vogella.com/android.html

Categories

Resources