I have a basic TabActivity. On android 2.1 (and possibly older versions), it looks like a drop shadow is added below the tab widget. On 2.3, this shadow is not present. Is there a way to turn that shadow off completely? Maybe something like "android:fadingEdgeLength=0" ?
Thanks
Are you talking about the white strip? I've hacked past this by calling this method inside onTabChanged
Let you class implement OnTabChangeListener
private static TabHost mTabHost;
#Override
protected void onCreate(Bundle savedInstanceState) {
// Instantiate your tab host normally
}
#Override
public void onTabChanged(String tabId) {
removeWhiteStrip(mTabHost);
}
/**
* Hack
* #param tabHost
*/
private static void removeWhiteStrip(TabHost tabHost) {
TabWidget tw = (TabWidget) tabHost.getChildAt(1);
Field mBottomLeftStrip;
Field mBottomRightStrip;
try {
mBottomLeftStrip = tw.getClass().getDeclaredField("mBottomLeftStrip");
mBottomRightStrip = tw.getClass().getDeclaredField("mBottomRightStrip");
if (!mBottomLeftStrip.isAccessible()) {
mBottomLeftStrip.setAccessible(true);
}
if (!mBottomRightStrip.isAccessible()) {
mBottomRightStrip.setAccessible(true);
}
// This is a blank drawable basically a 1x1 png with 100% alpha
mBottomLeftStrip.set(tw, MyApp.getInstance().getResources().getDrawable(R.drawable.blank));
mBottomRightStrip.set(tw, MyApp.getInstance().getResources().getDrawable(R.drawable.blank));
}
catch (java.lang.NoSuchFieldException e) {
// possibly 2.2
try {
Method stripEnabled = tw.getClass().getDeclaredMethod("setStripEnabled", boolean.class);
stripEnabled.invoke(tw, false);
}
catch (Exception e1) {
e1.printStackTrace();
}
}
catch (Exception e) {
// tut tut shouldn't catch generic exception and ignore it
// but we do because this is a hack
}
}
Enjoy
Related
i'm trying to make an app by using calimero library for KNX. In my app, i use some buttons, switches, togglebuttons, etc to switch on/off the lights.
With 'Button', everything works well. I can switch on/off a light or open/close curtains.
private class button9OnClickListener implements View.OnClickListener {
public void onClick(View view){
try {
falseTask dt = new falseTask();
String adr = "5/1/0";
dt.execute(adr);
} catch (Exception e) {
}
}
}
But when i use 'Switch' with either OnClickListener or OnCheckedChangeListenser, it crash.
private class switch1OnCheckedChangeListener implements CompoundButton.OnCheckedChangeListener {
public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
if(compoundButton.isChecked()) {
try {
trueTask dt = new trueTask();
String adr = "5/4/6";
dt.execute(adr);
} catch (Exception e) {
e.printStackTrace();
}
} else {
try {
falseTask dt = new falseTask();
String adr = "5/4/6";
dt.execute(adr);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
the problem is below:
uncaughtException java.lang.RuntimeException: An error occured while executing doInBackground()
and here's my doInBackground
protected String doInBackground(String...strings){
String adr = strings[0];
try {
final InetSocketAddress localEP = new InetSocketAddress(InetAddress.getByName(hostip), 0);
final InetSocketAddress remoteEP = new InetSocketAddress(remoteip, knxServerPort);
knxLink = new KNXNetworkLinkIP(KNXNetworkLinkIP.TUNNEL, localEP, remoteEP, true, TPSettings.TP1);
pc = new ProcessCommunicatorImpl(knxLink);
pc.write(new GroupAddress(adr), true);
knxLink.close();
}
catch (final KNXException e) {
}
catch (final UnknownHostException e) {
}
return null;
}
i'm new to java and android programming. Can you help with this problem? i can't understand why this work for 'button' but not for 'switch'.
Thank you in advance
UPDATE
I copy my code and try with eclipse. Everything work fine. I can switch on/off the light i want and there is no error.
With android studio, it did not work. Someone know the reason?
I tried with ToggleButton and Switch in Eclipse. All work good. I put exactly what I used in android studio. I copy all the code without any change. Can't understand the problem.
Finally I find the problem. I use 2 Bundle in the MainActivity and I use these 2 Bundle in doInBackground for my AsyncTask in the SecondActivity. But I just pass on the first Bundle to the SecondActivity and I forget to pass on the second Bundle. So I got error while executing doInBackground() What foolish mistake I have made...
I use ActionBar tab navigation mode and a ViewPager. This works fine in newer versions of Android, but on Android 4.0.3, tabs is in the top of the ActionBar instead of below.
If I use ActionBar.SetDisplayShowHomeEnabled(true), the problem appears to be fixed. However, up navigation then will not work.
My code looks as follows
ActionBar.SetTitle(Resource.String.DocumentDetails);
ActionBar.SetDisplayHomeAsUpEnabled(true);
ActionBar.NavigationMode = ActionBarNavigationMode.Tabs;
SetContentView(Resource.Layout.Main);
ActionBar.Tab tab = ActionBar.NewTab();
tab.SetText("tab1");
tab.TabSelected += (sender, args) => {
// Do something when tab is selected
};
ActionBar.AddTab(tab);
tab = ActionBar.NewTab();
tab.SetText("tab2");
tab.TabSelected += (sender, args) => {
// Do something when tab is selected
};
ActionBar.AddTab(tab);
Is this on an actual device? I have tested your code in an Emulator and can't reproduce. However, it does seem that several other people reported this to Google: https://code.google.com/p/android/issues/detail?id=36191
So a couple of ideas to fix this. Instead of using SetDisplayHomeAsUpEnabled try using the DisplayOptions property instead:
ActionBar.DisplayOptions = ActionBarDisplayOptions.ShowHome |
ActionBarDisplayOptions.HomeAsUp |
ActionBarDisplayOptions.ShowTitle;
The important one is the ActionBarDisplayOptions.ShowHome in this case, which forces the appearance of a home icon and ActionBarDisplayOptions.HomeAsUp does what SetDisplayHomeAsUpEnabled(true) does. The last one I suppose you can guess.
If that does not help. There was one answer in that issue thread, which suggested to use an extension method to set the tab items as embedded into the ActionBar. However, that will look as if you are running on a tablet. The code is as follows:
using Java.Lang;
using Java.Lang.Reflect;
namespace SomeNamespace
{
public class ActionBarUtils
{
public static void SetHasEmbeddedTabs(Object inActionBar, bool hasEmbeddedTabs)
{
var actionBarClass = inActionBar.Class;
if ("android.support.v7.app.ActionBarImplJBMR2" == actionBarClass.Name)
{
actionBarClass = actionBarClass.Superclass.Superclass;
}
else if ("android.support.v7.app.ActionBarImplJB" == actionBarClass.Name)
{
actionBarClass = actionBarClass.Superclass;
}
try
{
var actionBarField = actionBarClass.GetDeclaredField("mActionBar");
actionBarField.Accessible = true;
inActionBar = actionBarField.Get(inActionBar);
actionBarClass = inActionBar.Class;
}
catch (IllegalAccessException) { }
catch (IllegalArgumentException) { }
catch (NoSuchFieldException) { }
try
{
var method = actionBarClass.GetDeclaredMethod("setHasEmbeddedTabs", new[] { Boolean.Type });
method.Accessible = true;
method.Invoke(inActionBar, new Boolean(hasEmbeddedTabs));
}
catch (NoSuchMethodException) { }
catch (InvocationTargetException) { }
catch (IllegalAccessException) { }
catch (IllegalArgumentException) { }
}
}
}
Then can be used like so in your Activity:
ActionBarUtils.SetHasEmbeddedTabs(ActionBar, true);
I'm trying the my Azure Mobile Service. Below is the code to make a new ToDo item entry. The sample Android code shows how to create a mobile client. I added it to the onCreate method as mentioned in the example.
But the insert always fails. I always get an exception which says com.microsoft.windowsazure.mobileservices.MobileServiceException: Error while processing request.
mClient does get initialized. But, mClient.mCurrentUser is null. Not sure if this is a problem.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
try {
mClient = new MobileServiceClient("https://myservice.azure-mobile.net/",
"slkerjasfi234eSomePrivateKey", this);
Item item = new Item();
item.setText("Awesome item");
item.setComplete(false);
mClient.getTable(Item.class).insert(item,
new TableOperationCallback<Item>() {
public void onCompleted(Item entity,
Exception exception,
ServiceFilterResponse response) {
if (exception == null) {
ShowMessage("Success");
} else {
ShowMessage("Failed");
}
}
});
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
In my case, it looked like the key (parameter 2 in MobileServiceClient constructor) was incorrect. That is how I had downloaded it though. It happened again today and I manually fixed the key and it worked for another service. Believe it was the same issue here. To view your key, you go to your service in Azure and click Manage Keys at the bottom.
I wrote an android game for android 4+. the game works fine on different genymotion emulators and on my device, which is Samsung Galaxy Young GT-6312 os 4.1.2. when i install it on a Galaxy S3 Mini GT-I8190T os 4.1.2 the function getHitsRect returns weird results. every time i call the function from the same view i get different size of rectangle, which makes my game unplayable. I have set all of my views padding to (0,0,0,0) and used wrap_content params for width and height, for no avail. I've tried to check the view hierarchy dump in ddms but i got an error, although i doubt it can show me something i don't already know. I have absolutely no idea what to do, any help would be highly appreciated.
public class CheckShotsHitThread implements Runnable {
private volatile ArrayList<ShotController> m_Shots;
private volatile RainbowDashController m_RD;
public CheckShotsHitThread(GameController[] controllers, RainbowDashController rainbowDashController) {
m_Shots = new ArrayList<ShotController>();
for (GameController controller : controllers) {
if (controller instanceof ShotController) m_Shots.add((ShotController) controller);
}
m_RD = rainbowDashController;
}
#Override
public void run() {
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
while (GameModel.isRunning) {
m_RD.getView().getHitRect(m_RD.mHitRect);
for (ShotController shot : m_Shots) {
if(shot.isOutOfGame()||shot.getModel().isDead()) continue;
shot.getView().getHitRect(shot.mHitRect);
if (shot.mHitRect.intersect(m_RD.mHitRect)) kill(shot, m_RD);
}
try {
Thread.sleep(GameModel.ITERATION_PAUSE_TIME);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
Log.d("test", "shots thread dead");
}
private void kill(ShotController shot, RainbowDashController rd) {
Log.d("test","shot width: "+shot.getView().getWidth()+" RD width:"+rd.getView().getWidth()+" hit rect: left: "+m_RD.mHitRect.left+", right: "+m_RD.mHitRect.right);
shot.getModel().setDead(true);
rd.getModel().setCaptured(true);
}
}
solved by overriding getHitRect(Rect outRect) method
#Override
public void getHitRect(Rect outRect) {
outRect.top=(int)getY()-getHeight()/2+getPaddingBottom()/2;;
outRect.bottom=(int)getY()+getHeight()/2-getPaddingBottom()/2;;
outRect.left=(int)getX();
outRect.right=(int)getX()+getWidth();
}
I've seen this question:
Changing the ActionBar hide animation?
But it doesn't say whether it's possible to disable animation altogether.
You can now do this,
getSupportActionBar().setShowHideAnimationEnabled(false);
I fixed using the below method:
public static void disableShowHideAnimation(ActionBar actionBar) {
try
{
actionBar.getClass().getDeclaredMethod("setShowHideAnimationEnabled", boolean.class).invoke(actionBar, false);
}
catch (Exception exception)
{
try {
Field mActionBarField = actionBar.getClass().getSuperclass().getDeclaredField("mActionBar");
mActionBarField.setAccessible(true);
Object icsActionBar = mActionBarField.get(actionBar);
Field mShowHideAnimationEnabledField = icsActionBar.getClass().getDeclaredField("mShowHideAnimationEnabled");
mShowHideAnimationEnabledField.setAccessible(true);
mShowHideAnimationEnabledField.set(icsActionBar,false);
Field mCurrentShowAnimField = icsActionBar.getClass().getDeclaredField("mCurrentShowAnim");
mCurrentShowAnimField.setAccessible(true);
mCurrentShowAnimField.set(icsActionBar,null);
}catch (Exception e){
//....
}
}
}
If you use ActionBarSherlock then you can do it. See ActionBarImpl class, it has setShowHideAnimationEnabled(boolean enabled) method.