How to detect Action bar "Sub-Title" click event in Android Fragment..? - android

In my Android app I want to detect action bar "Sub-Title" click event but I don't know how to do it. I have successfully done Action bar "Title" click as shown in following code---
// Get action bar title ID
final int abTitleId = getResources().getIdentifier("action_bar_title", "id", "android");
//Set OnClick event on action bar title
getActivity().findViewById(abTitleId).setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
// Done
}
});
Referring above code, I have tried to detect action bar "Sub-Title" click event as follows, but there is something wrong and I'm getting "Sub-Title Id" null and it is throwing Null-Pointer Exception.
//Set OnClick event on action bar sub-title
final int abSubTitleId = getResources().getIdentifier("action_bar_sub_title", "id", "android");
getActivity().findViewById(abSubTitleId).setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
}
});
Hope you understand. Thanks..!

The best to handle this scenario is to use custom actionbar view, So create a simple actionbar layout with title textview and subtitle textview and implement respective listener. :)
final ViewGroup actionBarLayout = (ViewGroup) getLayoutInflater().inflate(R.layout.action_bar, null);
// Set up your ActionBar
final ActionBar actionBar = getActionBar();
actionBar.setDisplayShowHomeEnabled(false);
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setDisplayShowCustomEnabled(true);
actionBar.setCustomView(actionBarLayout);
// You customization
final int actionBarColor = getResources().getColor(R.color.action_bar);
actionBar.setBackgroundDrawable(new ColorDrawable(actionBarColor));
final TextView actionBarTitle = (TextView) findViewById(R.id.action_bar_title);
final TextView actionBarSubTitle = (TextView) findViewById(R.id.action_bar_sub_title);

The Action Bar subtitle is non clickable as far as i know. Instead you can add the icon there and make it clickable. You can get the click event of icon in onOptionsItemSelected().

Related

Is it possible to programmatically change the color of the split actionbar?

How do I change the color of the split actionbar in code, and not in xml? My user can pick the color of the actionbar, and I would like it so they could also change the color of the split action bar (the actionbar that appears at the bottom of the screen).
I'm implementing splitActionBar using android:uiOptions="splitActionBarWhenNarrow" in android manifest
so far I'm trying this, but it's not working
final int splitBarId = getResources().getIdentifier("split_action_bar", "id", "android");
final View splitActionBar = findViewById(splitBarId);
if (splitActionBar != null) {
splitActionBar.setBackgroundDrawable(new ColorDrawable(Color.parseColor(actionbar_colors)));
}
}
Use this to change the split actionbar color
getActionBar().setSplitBackgroundDrawable(new ColorDrawable(Color.parseColor("#33B5E5")));
Or use this if you're using support actionbar
getSupportActionBar().setSplitBackgroundDrawable(new ColorDrawable(Color.parseColor("#33B5E5")));
Source: http://scriptedpapers.com/2014/09/25/android-implement-spilit-action-bar-change-its-background-color/
The framework doesn't provide a way to change it programmatically; however, you can use Resources.getIdentifier to find the View and adjust the background Drawable from there.
The internal id is split_action_bar.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
final int splitBarId = getResources().getIdentifier("split_action_bar", "id", "android");
final View splitActionBar = findViewById(splitBarId);
if (splitActionBar != null) {
// Adjust the background drawable
}
}
Update
Evidently there's ActionBar.setSplitBackgroundDrawable. Definitely use that callback rather than Resources.getIdentifier.
Here's a screenshot of the results:
This should work
ActionBar mActionBar = getActionBar();
mActionBar.setBackgroundDrawable(new ColorDrawable(0xff00DDED));
mActionBar.setDisplayShowTitleEnabled(false);
mActionBar.setDisplayShowTitleEnabled(true);

Action Bar Title Clickable

I want Action bar title to be made clickable.
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeButtonEnabled(true);
here the home button and backbutton perform same intent but if we want to do different action over this that is suppose up button take to home activity and home button (title) take us to home2 activity how this can be done
You can use Resources.getIdentifier to call View.findViewById, then attach a View.OnClickListener to the TextView that contains the ActionBar title.
Here's an example:
final int abTitleId = getResources().getIdentifier("action_bar_title", "id", "android");
findViewById(abTitleId).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Do something
}
});

Hide only bottom action bar Android

Currently, I have a split action bar, and there is an edit text near the bottom of the screen.
When I click on the edit text, the keyboard shows up, but the bottom action bar ALSO shows up on top of the keyboard, making it cluttered. Here is what it looks like:
How can I hide only the bottom action bar when I click on edit text?
Here is my code:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
//bottom action bar
getMenuInflater().inflate(R.menu.defaultbottom_tabs, menu);
setActionBar();
return true;
}
// custom layout top action bar
private void setActionBar() {
ActionBar actionBar = getActionBar();
actionBar.setDisplayShowHomeEnabled(false);
// displaying custom ActionBar
View mActionBarView = getLayoutInflater().inflate(R.layout.home_top,null);
actionBar.setCustomView(mActionBarView);
actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
}
You can add the android:windowSoftInputMode="adjustPan" attribute to the Activity that shows your EditText. Alternatively, if you really want to do this in code, you can find the split action bar View by using Resources.getIdentifier.
final int id = getResources().getIdentifier("split_action_bar", "id", "android");
final View splitActionBar = findViewById(id);
if (splitActionBar != null) {
// Toggle the visibility
}

how to hide/unhide the actionbar list on android 3?

i am using android 3.0 actionbar with a dropdown and a searchWidget.
I would like to hide the dropdown when the user expands the search and unhide the dropdown when the user closes the search.
Here is a picture to explain bettere
and here is the code i use to make the dropdown
ActionBar bar = this.getActionBar();
bar.setTitle(this.getString(R.string.app_name));
bar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
...
ListDittaListener listener = new ListDittaListener(this);
bar.setListNavigationCallbacks( seleziona_ditta, listener);
Thanks to this https://stackoverflow.com/a/6454288/579646
searchView.setOnSearchClickListener( new OnClickListener() {
#Override
public void onClick(View v) {
Log.v(MyApp.TAG, "search view On Search Click");
ActionBar bar = FattureActivity.this.getActionBar();
bar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
}
});
searchView.setOnCloseListener( new OnCloseListener() {
#Override
public boolean onClose() {
Log.v(MyApp.TAG, "search view On Close");
ActionBar bar = FattureActivity.this.getActionBar();
bar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
return false;
}
});

How can I detect a click on the ActionBar title?

For specific customer requirement, I need to allow the user of my app ( won't be published in Market) to click on the ActionBar title to execute some actions.
I have been looking in the Android source, but I am not able to find an ID for the actionBar TextView title.
Is there a proper way to handle such clicks?
The title is non-clickable AFAIK. The icon/logo is clickable -- you'll get that via android.R.id.home in onOptionsItemSelected(). Conceivably, the title also routes this way, though they don't mention it and I wouldn't rely upon it.
It sounds like you want a Spinner for the user to choose the actions to execute. If so, use setListNavigationCallbacks(). If you want to remove the title as now being superfluous, use setDisplayOptions(0, DISPLAY_SHOW_TITLE).
If you want something other than a Spinner on the left side of the action bar, use setDisplayOptions(DISPLAY_SHOW_CUSTOM, DISPLAY_SHOW_CUSTOM) and setCustomView(). Note that this approach is not recommended ("Avoid using custom navigation modes in the action bar"), as it may not work well with phones, particularly in portrait mode.
Another possibility would be to remove the title and use a logo instead of the icon, and in the logo have your "title" as part of the image. The whole logo should be clickable, picked up via onOptionsItemSelected().
//onCreate
ActionBar actionBar = getActionBar();
actionBar.setDisplayShowHomeEnabled(false);
actionBar.setDisplayShowTitleEnabled(false);
// View actionBarView = getLayoutInflater().inflate(R.layout.action_bar_custom_view, null);
actionBar.setCustomView(actionBarView);
actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
//your logic for click listner
setListenerForActionBarCustomView(actionBarView);
private void setListenerForActionBarCustomView(View actionBarView) {
ActionBarCustomViewOnClickListener actionBarCustomViewOnClickListener = new ActionBarCustomViewOnClickListener();
actionBarView.findViewById(R.id.text_view_title).setOnClickListener(actionBarCustomViewOnClickListener);
}
private class ActionBarCustomViewOnClickListener implements OnClickListener {
public void onClick(View v) {
switch(v.getId()) {
case R.id.text_view_title:
//finish();
break;
}
}
You can set up a custom toolbar from Support Library by declaring <android.support.v7.widget.Toolbar> in your layout (see Chris Banes' answer for full toolbar layout example).
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<!-- We use a Toolbar so that our drawer can be displayed
in front of the action bar -->
<android.support.v7.widget.Toolbar
android:id="#+id/my_awesome_toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/main_toolbar"
android:minHeight="?attr/actionBarSize" />
<FrameLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
After you can add on click listener in your activity just like to most other Views.
Toolbar toolbar = (Toolbar) findViewById(R.id.my_awesome_toolbar);
setSupportActionBar(toolbar);
toolbar.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(MyActivity.this, "Test", Toast.LENGTH_LONG).show();
}
});
If you want to capture touch events on title:
toolbar.setOnTouchListener(new View.OnTouchListener() {
Rect hitrect = new Rect();
public boolean onTouch(View v, MotionEvent event) {
if (MotionEvent.ACTION_DOWN == event.getAction()) {
boolean hit = false;
for (int i = toolbar.getChildCount() - 1; i != -1; i--) {
View view = toolbar.getChildAt(i);
if (view instanceof TextView) {
view.getHitRect(hitrect);
if (hitrect.contains((int)event.getX(), (int)event.getY())) {
hit = true;
break;
}
}
}
if (hit) {
//Hit action
}
}
return false;
}
});

Categories

Resources