I'd like to add an onclick listener to my launcer icon in the title bar of my app.
Since i'm also supporting API level 8 i do not have an Action bar.
The following code works great, however the menu is set back to default (white background, white text, small icon etc.)
The code:
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();
}
});
}
}
source: adding click listener to titlebar image
How can I keep my standard layout of my title bar (black background color, white text and large icon), while also implement this onclick listener?
Here are two shots of the different layouts:
good: http://gyazo.com/40d1cdd5302de3cd28b698b68164a556
bad: http://gyazo.com/3cee42524ec4167392baec6cc2369584
(Note that the good one is also has a greater height)
Related
I have created a float button in Android Studio
After I click the image, the add floating button should be disabled and the 3 images should be displayed. One images have to selected from the images displayed horizontally just like in the image below. If i have to select the image in the right then i have to the right to position the image in the center and select the image
And after choosing the appropriate image it should be back to displaying floating button like in the first image.
Here, I have provided an example outline of what you are trying to do. You will have to add a lot more code, but this will get you started.
public class MainActivity extends AppCompatActivity {
private boolean cupImageClicked = false;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.your_layout);
//initialize and set up imageViews and button.
//listener for button.
mFloatingButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mFloatingButton.setVisibility(View.GONE);
imageView_1.setVisibility(View.VISIBLE);
imageView_2.setVisibility(View.VISIBLE);
imageView_3.setVisibility(View.VISIBLE);
}
});
//example click listener for an image.
cupImageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (cupImageClicked) {
//this will execute when cupImage is already in the center
//gather whatever info you need from that clicked image.
imageView_1.setVisibility(View.INVISIBLE);
imageView_2.setVisibility(View.INVISIBLE);
imageView_3.setVisibility(View.INVISIBLE);
mFloatingButton.setVisibility(View.VISIBLE;
} else {
//here you will need to move the cupImage to the center and
// move the center imageView to the left.
cupImageClicked = true;
}
}
});
}
I am busy creating an app. I succeeded in creating the button with a custom font and all. Now what I'd want is that when I click the button, it must disapear, the background color of the view must randomely change and text must be loaded from an database.
How does one go about this?
Matthew
Well, the disappearing can be make like this:
public class MyActivity extends Activity {
protected void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.content_layout_id);
final Button button = (Button) findViewById(R.id.button_id);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click - Disappear in your case...
v.setVisibility(View.INVISIBLE); //can be View.GONE as well...
}
});
}
}
Then for the background I guess you can create an array with all the colors you want (or something that generates a random HEX code) and then do setBackground(X) where X is the HEX code that you just generated... You need to specify more about the database part though.
I'm trying put text in to bar, I only put (buttons), I don't know if I can put text 2 lines + image.
Here are references as changing the color:
https://developer.android.com/training/basics/actionbar/styling.html
in this other picture is displayed, as you can put more text in the top bar
http://www.whatsapp.com/img/v3/es/s1-chat.png
Simply create your own view and set it for your ActionBar: setCustomView().
You cannot achieve what you want with the standard layout. For plenty of examples look here:
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ActionBar actionBar = getActionBar();
// add the custom view to the action bar
actionBar.setCustomView(R.layout.actionbar_view);
EditText search = (EditText) actionBar.getCustomView().findViewById(R.id.searchfield);
search.setOnEditorActionListener(new OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId,
KeyEvent event) {
Toast.makeText(MainActivity.this, "Search triggered",
Toast.LENGTH_LONG).show();
return false;
}
});
actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM
| ActionBar.DISPLAY_SHOW_HOME);
}
}
Source: vogella.com
I don't think you can do it.
I'd advise you just to create a layout that would act like a top bar in your application.
This way you would be able to put there anything you want.
If you have more than one activity that needs to have this top bar, then use fragments instead of activities (to avoid your custom top bar from being animated when new activities are being opened).
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
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