GreenDroid Actionbar Text - android

I use greendroid to implement to actionbar under api 11.
Now i have a strange behaviour in my MainView (the startview works well)
public class MainView extends GDActivity {
private boolean ison = false;
private FinderThread finder;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
//this.getActionBar().removeViewAt(0);
setActionBarContentView(R.layout.mainlayout);
getActionBar().setBackgroundColor(Color.parseColor("#bf27c3"));
setTitle("TEST");
setTitleColor(Color.WHITE);
addActionBarItem(getActionBar().newActionBarItem(NormalActionBarItem.class).setDrawable(R.drawable.person));
Looking:
The Actionbar text is empty and no home button. please help

You can try to set the Title and the Color of the Title with
getActionBar().setTitle("Title")
The same for the color. Try also to set the color via an RGB value.
I can't promise that this will work, because i don't use greendroid for my actionbars. But i understand that you don't use actionbarsherlock + greendroid because they are only compatible with a trick (a bit hacking xD)

Related

Dynamically recolor activity background before showing it

I need to set custom color both to actionbar and to client area. With the following code my app get succesfully colored, however, I still see the default theme for like 0.5 seconds when the activity starts. How do I remove this gap?
The color is set dynamically, so I guess I cannot use theme definition here. (I will later change my code to get color from an Intent)
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_note_screen);
setNoteColor(0xFFFFF8DC);
}
public void setNoteColor(int color) {
getWindow().getDecorView().setBackground(new ColorDrawable(color));
assert getActionBar() != null;
getActionBar().setBackgroundDrawable(new ColorDrawable(color));
}
Funny thing I just read about that yesterday... =)
Here is a great post regarding you issue

Changing color of action bar doesn't work in ICS

I am changing the colour of action bar using the following code.
actionBar= getSupportActionBar();
actionBar.setBackgroundDrawable(new ColorDrawable(backgroundColor));
This works perfectly fine in onCreate() method(i.e when my activity starts).
But when I am using the same code in Android 4.1 to change color of action bar in onClick() method(i.e when user clicks say a button) then it changes the color of action bar to 'white' ONLY whatsoever be the value of backgroundColor.
In Android 2.3 it works perfectly fine in both cases, whether called from onCreate or onClick().
It seems to be dead end for me.
It seems like for Android 4.0 up to Android 4.1 (4.2 is ok) you cannot change the action bar background once the activity has started.
So I guess a solution in those cases is to restart the activity with a different background color.
Not very elegant but thats the best I can come up with.
Something like this perhaps:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn = (Button) findViewById(R.id.btn);
if (getIntent().hasExtra("color")) {
getSupportActionBar().setBackgroundDrawable(
new ColorDrawable(getIntent().getIntExtra("color", 0)));
}
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent it = getIntent();
it.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
it.putExtra("color", Color.BLUE);
startActivity(it);
}
});
}
A workaround I used to solve this problem was to maintain a reference to the ColorDrawable that was used to set the background initially. Then, to change its colour, modify the colour of the Drawable, and then tell the activity to invalidate its options, which causes the ActionBar to be redrawn. Here's my code to set the action bar color:
ColorDrawable sBackgroundDrawable;
...
#TargetApi(Build.VERSION_CODES.HONEYCOMB)
protected void setActionBarColor(int color){
if(getSherlockActivity() != null && !mCallback.isNavDrawerShowing()){
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH && Build.VERSION.SDK_INT <= Build.VERSION_CODES.JELLY_BEAN){
if(sBackgroundDrawable == null){
sBackgroundDrawable = new ColorDrawable(color);
getSherlockActivity().getSupportActionBar().setBackgroundDrawable(sBackgroundDrawable);
}else{
sBackgroundDrawable.setColor(color);
getSherlockActivity().invalidateOptionsMenu();
}
}else{
getSherlockActivity().getSupportActionBar().setBackgroundDrawable(new ColorDrawable(color));
}
}
}
Based off of what steve_the_kid mentioned I was able to solve this by saving the color drawable.
ColorDrawable actionBarBackground = new ColorDrawable();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getActionBar().setBackgroundDrawable(actionBarBackground());
actionBarBackground.setColor(someColor);
}
void onSomethingClick() {
actionBarBackground.setColor(someOtherColor);
}
Sorry for such a late reply,But to help others I am replying to this question after so long.
Just set the Title of the action bar or make change in the icons or menu along with changing the colorDrawables.A simple change will recreate the action bar with new color.Even if you need same title or icon every time set same title or same icon.
I hope this will help others

Preference Background Color Blinking

I managed to change my preference activity's background color, but when I focus/scroll it, it blinks. Please help me solve this. This is how i change my background...
public class ConfigActivity extends PreferenceActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preference);
findViewById(android.R.id.list).setBackgroundColor(getResources().getColor(R.color.gray));
}
}
Use android:cacheColorHint="#00000000" in your layout.
See also http://android-developers.blogspot.com/2009/01/why-is-my-list-black-android.html

adding click listener to titlebar image

Merry Christmas and Happy Holidays everyone!
I'm trying to setup a listener on the image icon that appears on the left side of the default title bar, but so far not having any luck.
Here's my Activity's onCreate:
#Override public void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_LEFT_ICON);
super.onCreate(savedInstanceState);
findViewById(Window.FEATURE_LEFT_ICON).setOnClickListener(new OnClickListener() {
#Override public void onClick(View v) {
System.out.println("It works!");
}
});
}
Any suggestions? I'm hoping to not see the answer "it's not possible" :)
There doesn't seem to be an id for the left icon, however for the classic title bar, there is an id available: android.R.id.title Here is a sample Activity using this id. The requestWindowFeature(Window.FEATURE_LEFT_ICON); should force the classic title bar regardless of theme.
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_LEFT_ICON);
setContentView(R.layout.activity_main);
getWindow().setFeatureDrawableResource(Window.FEATURE_LEFT_ICON,R.drawable.ic_launcher);
View v = findViewById (android.R.id.title);
v.setClickable(true);
v.setOnClickListener(new OnClickListener() {
#Override public void onClick(View v) {
Toast.makeText(MainActivity.this, "Works!", Toast.LENGTH_SHORT).show();
}
});
}
}
Basically, what this does, is it finds the id of the title bar (android.R.id.title) then assigns an onClickListener to it.
This will not work with ActionBars, only classic window title bars.
You should use the ActionBar. Use ActionBarSherlock to have it also for Android versions less than 3.0. To make the Icon clickable, see the ActionBar API Docs (see link below). It's very easy, you just activate the behaviour and then it works like a menu-item with a special item-id.
http://developer.android.com/guide/topics/ui/actionbar.html#Home

How can I add an image to the title bar in android 3.0?

I am learning android 3.0.
Can any one tell How can add Image in Title bar in android 3.0?
In developer site they are telling by default it will come.but for me it is not coming.
Thanks in advance.
You will have to create Custom Title Bar Layout to add your own image in titlebar.And then you can use following code to assign the titlebar to your view
boolean customTitleSupported;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
customTitleSupported = requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
if (customTitleSupported) {
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,R.layout.custom_tittlebar_layout);
}
setContentView(R.layout.main);
}
here custom_tittlebar_layout refers to your custom titlebar layout with image.

Categories

Resources