How to get tabs in ICS and gingerbread to look the same? - java

Being new to android development, i have created a project using tabs using tutourials.
on Gingerbread it looks sort of like :
While on ICS(emulator) it looks like:
Because of the sort of "Holo" type teme i have going i cant have the tab on the gingerbread style. I need help please it is killing me. Please dont tell me i have to figure out how to design it myself for Gingerbread i'll cry.
here is what i did:
public class MainActivity extends FragmentActivity
{
private FragmentTabHost mTabHost;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
mTabHost.setup(this, getSupportFragmentManager(), R.id.tabframelayout);
mTabHost.addTab(mTabHost.newTabSpec("tab1").setIndicator("Tab 1"),
FragmentTab.class, null);
mTabHost.addTab(mTabHost.newTabSpec("tab2").setIndicator("Tab 2"),
FragmentTab.class, null);
mTabHost.addTab(mTabHost.newTabSpec("tab3").setIndicator("Tab 3"),
FragmentTab.class, null);
}

It is greatly suggested that you use Action Bar Tabs rather than using TabHost. While the Action Bar was added in Honeycomb, you can use compatibility libraries like ActionBarSherlock (which would give you consistent tabs across all devices and my personal recommendation) or follow the Creating Backward-Compatible UIs training (which is very similar to what you are doing now).

Related

Android O device blank screen after addView / setContentview

I recently decided to update my older apps and bring them a little more up to date. In doing so I noticed an odd problem when running them on my new handset (Android O / API Level 29).
My App is essentially a single Activity using OpenGL. This is basically what happens when the app is first loaded...
Create Layout, Create a Splashscreen View, create custom GLSurfaceView
Add GLSurfaceView to Layout
Add Splashscreen to Layout
Start AsyncTask to load resources and do setup ops
GLSurfaceView dismisses Splashscreen to reveal renderer...
So, it all works perfectly well on my older devices (3 x handsets, 1 x tablet all running various versions of Android no higher than Lollipop).
However, on my Android 10 handset, all I get is a blank screen while the app starts. Just to be clear, there are no errors, the app itself works perfectly fine. After the usual startup time, the blank screen is dismissed and the app continues, it's just that the 'splashscreen' has now become a "blankscreen".
But Android Studio Layout Inspector tells a different story
Indeed, if I open the Layout Inspector in Android Studio, oddly it shows exactly what I would expect... the splashscreen with its text/graphics, but not on the actual device...
Some test code
In the following test code I replaced the resource loading / setup with a simple Thread.sleep just to create a longer delay so I could more easily inspect the output in Android Studio. This does still exhibit the problem...
#Override
protected void onCreate(Bundle savedInstanceState) {
int[] deviceResolution = getDeviceResolution();
width = deviceResolution[0];
height = deviceResolution[1];
layout = new RelativeLayout(this);
splash = new Splash(getApplication(), width, height);
myGLView = new CustomGLSurfaceView(this.getApplication());
layout.addView(myGLView);
layout.addView(splash);
setContentView(layout);
loadResource = new LoadResources(newBundle);
loadResources.execute();
super.onCreate(savedInstanceState);
}
class LoadResources AsyncTask<Void, Void, Void> {
Bundle savedBundle;
LoadResources(Bundle savedBundle){
this.savedBundle=savedBundle;
}
#Override
protected void onPreExecute() {
super.onPreExecute();
}
#Override
protected Void doInBackground(Void... params) {
// Simulate some long-running task here. While this is happening, splashscreen should be visible. And on older devices, it is. Not on Android O handset (but OK in Android Studio Layout Inspector).
try {
Thread.sleep(30000);
} catch (InterruptedException e) {
e.printStackTrace();
}
// Would typically make my calls here to load resources, and complete other setup tasks here
return null;
}
#Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
}
}
** Other observations **
I know that Async is now deprecated and I'm also aware of other Splashscreen tecnhiques such as using a dedicated Activity. However due to the setup/complexity of the apps and given that it doesn't 'break' them per-se, I don't want to have to refactor the entire codebase (just yet anyway) any new apps going forward will use a different system for the splashscreen. What I'm really trying to understand here is why this now doesn't work as it used to. Is this a bug? Or a behavioural change that happened in an Android version somewhere down the line? Or maybe it's just me doing something wrong. If anyone has come across it, I'd be really interested to know of a fix/workaround.
** Other things I've tried **
In the above example, if I simply don't add 'myGLView' then the splashscreen shows, but of course the app then won't work. So I though about adding it in onPostExecute and then bringing the splashscreen view back to the front with splash.bringToFront(). This kind of works, but is messy. The splashscreen only shows for a brief second, and on devices that show it correctly there is a 'glitch' as the correctly displayed splashscreen is overlayed with the GL Renderer and then bought back to the front.
You are adding a view to an object which is not currently set in an Activity.
The better approach would be calling setContentView() first as well as the super method, then adding the multiple views in it.
So in your case, it'll be like:
super.onCreate(savedInstanceState);
setContentView(layout);
layout = new RelativeLayout(this);
int[] deviceResolution = getDeviceResolution();
width = deviceResolution[0];
height = deviceResolution[1];
layout = new RelativeLayout(this);
splash = new Splash(getApplication(), width, height);
myGLView = new CustomGLSurfaceView(this.getApplication());
layout.addView(myGLView);
layout.addView(splash);
loadResource = new LoadResources(newBundle);
loadResources.execute();

How to lock one activities orientation,then have another on top which is free to rotate?

I am developing an app for Android where I use the front camera as a mirror and have controls on top to do various functions. Now I was wondering how I can get the MainActivity to stay locked as 'portrait' and have another activity on top (which has all the buttons and has a transparent background) to have freedom to rotate?
Thanks
So you have two activites, one for which orientation is fixed and another one is free to rotate.
For the activity for which orientation is to be fixed, add this line in its activity tag in Manifest.xml :
<activity
android:name=".MyPreferencesActivity"
android:screenOrientation="landscape"/>
And by default the other activity is free to rotate.Mark the answer as verified if it works.
The above way is static (defined in xml).Ii it doesn't work, try this programmatical(can be changed during runtime) method also:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}
public void onButtonPressed(Uri uri) {getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);}

onCreate android os bundle is never used

I'm a noob in java android but I get this error when I hover onCreate method onCreate(android.os.Bundle) is never used. Should I worry about this error?
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
actionbar = getActionBar();
actionbar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
actionbar.addTab(actionbar.newTab().setText("Feed").setTabListener(this));
actionbar.addTab(actionbar.newTab().setText("Browse").setTabListener(this));
actionbar.addTab(actionbar.newTab().setText("Settings").setTabListener(this));
}
Thanks
thats not an error its a warning. the bundle is used for putting information in when the app changes state ie. orientation so you can continue where the user left off.
there is no harm in not using it however the activity will be shown just as it would if you were to launch it from the launcher.
Got it! I just changed extends FragmentActivity into extends Activity and imported android.app.Activity.

Getting GoogleAnalytics working in all activities

I want to track all activities and button clicks throughout my 7 activities in my app. Right now I am at the beginning of implementing GA. I have it working in my opening main menu screen and can view it online on google.com/analytics. My question is how to implement this into all activities?
Right now in my main menu class I have this:
private Tracker tracker;
private GoogleAnalytics ga;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.mainmenumain);
EasyTracker.getInstance().activityStart(this);
//Get the GA singleton.
ga = GoogleAnalytics.getInstance(this);
//ga.setDebug(true);
//Use the GA singleton to get a Tracker object.
tracker = ga.getTracker("UA-###-1");
Do I have to declare the 2 variables and then call EasyTracker.getInstance().activityStart(this);, ga = GoogleAnalytics.getInstance(this); and tracker = ga.getTracker("UA-####-1"); in every activity?
I know there are similar topics out there but I am asking this for the new version 2 of Google Analytics and not the legacy version 1.
Implement a new BaseActivity to do the work in it's onCreate method, then make all your 7 activities extends BaseActivity.
Remember to call super.onCreate in your activities.
For Analytcs V2, these are the only 2 lines you need in your Activities:
EasyTracker.getInstance().activityStart(this);
Tracker trackerV2 = EasyTracker.getTracker();
You are kind of mixing up V1 and V2 calls. The tracker instantiation (associating your API key) is set via a attribute in the manifest, so you no longer need the lines:
tracker = ga.getTracker("UA-####-1");

Java implementation of slidemenu library for Android

Im using this library for a slidemenu in Android. This is working fine but I'm wondering if somebody has an implementation for the menu. Im trying to switch activities or fragments when a listitem is clicked but its not working. I have declared a new xml layout which is set as menu layout with the code:
menu.setMenu(R.layout.activity_menu);
The layout shows up nice but i have no idea how to manipulate the mainactivity to show a new fragment or activity.
Does somebody have a good implementation of this library?
My Slidemenu implementation:
menu.setMode(SlidingMenu.LEFT_RIGHT);
menu.setTouchModeAbove(SlidingMenu.TOUCHMODE_NONE);
menu.setFadeDegree(0.35f);
menu.setShadowWidth(20);
menu.setShadowDrawable(R.drawable.shadow);
menu.setBehindOffset(150);
menu.attachToActivity(this, SlidingMenu.SLIDING_CONTENT);
menu.setMenu(R.layout.activity_menu);
menu.setSecondaryMenu(R.layout.secondary_menu);
menu.setSecondaryShadowDrawable(R.drawable.shadowright);
menuBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
menu.toggle();
}
});
Implemented this with succes. It was kind of complex but I'm willing to lend a hand. Feel free to comment on this question so I can provide you some help.

Categories

Resources