My Custom ToolBar is not showing up the title
image:
My Main:
Toolbar toolbar_exit = (Toolbar) findViewById(R.id.toolbar_simple_exit); // Attaching the layout to the toolbar object
setSupportActionBar(toolbar_exit); // Setting toolbar as the ActionBar with setSupportActionBar() call
getSupportActionBar().setDisplayShowTitleEnabled(true);
getSupportActionBar().setTitle("Help");
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
my Manifest:
<activity
android:name=".Activities.HelpActivity"
android:configChanges="orientation|keyboardHidden|screenSize"
android:label="Work Stupid Title"
android:theme="#style/NoActionBar" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".Activities.AtividadePrincipal" />
</activity>
why its not showing? ive tried this.setTitle and other, nothing worked
Please be specific when you ask questions by posting your customized layout files and related java code.
Set your style theme's parent of type .NoActionBar? and set the following attributes.
<item name="windowNoTitle">true</item>
<item name="windowActionBar">false</item>
<item name="windowActionModeOverlay">true</item>
You should set a custom layout for the action bar, then set text for the textview on the custom layout of the action actionbar. This is my code:
private void setCustomActionbarView() {
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
toolbar.setContentInsetsAbsolute(0, 0);
setSupportActionBar(toolbar);
mActionBar = getSupportActionBar();
mActionBar.setDisplayShowHomeEnabled(false);
mActionBar.setDisplayShowTitleEnabled(false);
mActionBar.setDisplayShowCustomEnabled(true);
// kiem tra lai ham nay
mActionBar.setDisplayHomeAsUpEnabled(true);
LayoutInflater mInflater = LayoutInflater.from(this);
View mActionBarView = mInflater
.inflate(R.layout.custom_actionbar, null);
mActionBar.setCustomView(mActionBarView);
}
and my custom layout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res/com.orangestudio.mpromotion"
android:id="#+id/linBang"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<TextView
android:id="#+id/textAcb"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="#string/home_khuyen_mai"
android:textColor="#color/acb_text_color"
app:font="#string/font_bold" />
Set the actionbar title:
View actionbarView = getSupportActionBar().getCustomView();
TextView text =(TextView) actionbarView.findViewById(R.id.textAcb);
text.setText("YOUR TITLE");
Related
I want to align the action bar title to centre without the help of custom view . I would appreciate any help.
Without using the custom view, modifying only default action bar title
You can align the title to the center when you use ActionBar, but you can use Toolbar to do this.
Toolbar is more useful and easier than ActionBar, you can use this layout to define the center title TextView for you activity:
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="40dip">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="#string/app_name" />
</android.support.v7.widget.Toolbar>
And use this code for a back button:
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.setContentView(R.layout.activity_toolbar);
Toolbar toolbar = (Toolbar) findViewById(R.id.single_toolbar);
setSupportActionBar(toolbar);
ActionBar actionBar = getSupportActionBar();
if(actionBar != null)
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
onBackPressed();
return true;
}
return super.onOptionsItemSelected(item);
}
You also need to override onCreateOptionsMenu method for the menu, and you can refer to this project : chrisbanes/cheesesquare.
Ok, You can try this:
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="#style/AppTheme.PopupOverlay" >
<TextView
android:textColor="#fff"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_gravity="center"
android:text="#string/app_name" />
</android.support.v7.widget.Toolbar>
And remember to add this line in your activity's java code:
getSupportActionBar.setTitle("");
It seems there is no way to do this without custom view. You can get the title view:
View decor = getWindow().getDecorView();
TextView title = (TextView) decor.findViewById(getResources().getIdentifier("action_bar_title", "id", "android"));
But changing of gravity or layout_gravity doesn't have an effect. The problem in the ActionBarView, which layout its children by itself so changing of layout params of its children also doesn't have an effect. To see this excecute following code:
ViewGroup actionBar = (ViewGroup) decor.findViewById(getResources().getIdentifier("action_bar", "id", "android"));
View v = actionBar.getChildAt(0);
ActionBar.LayoutParams p = new ActionBar.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
p.gravity= Gravity.CENTER;
v.setLayoutParams(p);
v.setBackgroundColor(Color.BLACK);
I want to display to use a custom view on my action bar that completely covers the action bar (width match parent)
But my custom view does not cover the entire width of the action bar.
public class MyActivity extends AppCompatActivity {
private ActionBar actionBar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.data_entry_screen);
setActionBar();
}
private void setActionBar() {
actionBar = getSupportActionBar();
actionBar.setDisplayShowHomeEnabled(false);
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
View abView = actionBar.getCustomView();
LayoutInflater inflator = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LayoutParams layout = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
View v = inflator.inflate(R.layout.action_bar, null);
actionBar.setCustomView(v, layout);
actionBar.setDisplayShowCustomEnabled(true);
}
action_bar.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="fill_horizontal">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/mvp_logo"
android:layout_alignParentRight="true" />
</RelativeLayout>
I have referred the following links:
How to display custom view in ActionBar?
Manually inflating custom view yields different layouts for ActionBar custom view
Android center custom view in actionbar
Android: How to center a component in a custom view in action bar?
Thanks
The Toolbar might be better for you, but as a note, this is breaking the design guidelines and will make your app not 'fit' in the Android ecosystem, that area is usually where the context menu is found and most users will have learned that behaviour.
You can fix the behaviour by removing content insets as follows
View myLogo = mInflater.inflate(R.layout.actionbar_custom, null);
getSupportActionBar().setCustomView(myLogo);
((Toolbar) myLogo.getParent()).setContentInsetsAbsolute(0,0);
If you've already changed to Toolbar you can fix it in the XML for the custom view
app:contentInsetLeft="0dp"
app:contentInsetStart="0dp"
When I try to add a view immediately under the ActionBar, it only seems to work if I add an ImageView, as shown below.
The same doesn't work with LinearLayout, or RelativeLayout, and instead removes all of the ActionBar's initial content and replaces it with the Linear/ Relative Layout content.
Why can't I successfully add LinearLayout, or RelativeLayout, or what might I be getting wrong. I just started working with Android.
I'm aiming for something like this:
-- THE CODE
Extend our ActionBar by adding an ImageView
ActionBar actionBar = getActionBar();
actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
ImageView imageView = new ImageView(actionBar.getThemedContext());
imageView.setScaleType(ImageView.ScaleType.CENTER);
imageView.setImageResource(R.drawable.center16);
ActionBar.LayoutParams layoutParams = new ActionBar.LayoutParams(
ActionBar.LayoutParams.WRAP_CONTENT,
ActionBar.LayoutParams.WRAP_CONTENT, Gravity.BOTTOM
| Gravity.CENTER_VERTICAL);
layoutParams.rightMargin = 40;
imageView.setLayoutParams(layoutParams);
actionBar.setCustomView(imageView);
Using a Linear or Relative layout
LayoutInflater inflator = (LayoutInflater) this .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View linearorrelative = inflator.inflate(R.layout.actionbarextend, null);
ActionBar.LayoutParams layoutParams = new ActionBar.LayoutParams(
ActionBar.LayoutParams.WRAP_CONTENT,
ActionBar.LayoutParams.WRAP_CONTENT, Gravity.BOTTOM
| Gravity.CENTER_VERTICAL);
layoutParams.rightMargin = 40;
linearorrelative.setLayoutParams(layoutParams);
actionBar.setCustomView(linearorrelative);
If you're wanting a search bar below the ActionBar, you should consider using the new Toolbar. Which is:
a generalization of action bars for use within application layouts
Here's an example implementation using the new AppCompat library, based on your image:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:orientation="vertical">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?attr/actionBarSize" />
<android.support.v7.widget.SearchView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="8dp"
android:paddingRight="8dp" />
</LinearLayout>
<!-- Your content here -->
</LinearLayout>
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
final Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
}
}
<style name="Your.Base.Theme" parent="Theme.AppCompat.NoActionBar">
...
</style>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"...>
<application
...
android:theme="#style/Your.Base.Theme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Results
Otherwise, as suggested by #ChrisStillwell, consider using an ActionView and placing the SearchView in the ActionBar as a MenuItem.
Is it possible to add EditText to action bar layout? Can't find any info about that.
You can create a custom view for your action bar.
Here's how to inflate and add the custom layout to your ActionBar.
// Inflate your custom layout
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 can create listener over the EitText
final EditText actionBarText = (EditText) findViewById(R.id.action_bar_text);
actionBarText.addTextChangedListener(this);
Here's a custom layout :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:enabled="false"
android:orientation="horizontal"
android:paddingEnd="8dip" >
<EditText android:id="#+id/action_bar_text"
<!-- don't forget to define the EditText style here -->
/>
</LinearLayout>
So, hope it helps.
Ya it is possible... You can set a custom View for the ActionBar
ActionBar actionBar = getActionBar();
actionBar.setCustomView(R.layout.custom_actionbar_view);
For more info check this link
http://www.sanfoundry.com/java-android-program-add-custom-view-actionbar/
I am trying to implement an Action Bar on an Android Activity which implements Jeremy Feinstein's sliding menu and actionbarSherlock. The issue I am facing is to centre align the title text in the Action Bar. My action bar has a menu icon named "icon_sliding_menu" on the left of the text to activate the left menu sliding action.
I have used this example to write attached below. However, when I add the line "getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);" to try to customize my view, my title just goes away, and I don't see any text at all.
ActionBar when the line mentioned above is not in the code:
ActionBar when the line mentioned above is written in the code:
How can I align the text to centre? I am attaching my code below.
actionbar.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center" >
<TextView
android:id="#+id/action_bar_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textSize="18sp" />
</LinearLayout>
HomeActivity.java
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
final View actionBarLayout = LayoutInflater.from(this).inflate(
R.layout.actionbar, null);
getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
getSupportActionBar().setDisplayShowCustomEnabled(true);
getSupportActionBar().setCustomView(actionBarLayout);
getSupportActionBar().setTitle("Home");
getSupportActionBar().setHomeButtonEnabled(true);
initSlidingMenu();
}
private void initSlidingMenu() {
slidingMenu = new SlidingMenu(this);
slidingMenu.setMode(SlidingMenu.LEFT_RIGHT);
slidingMenu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
slidingMenu.setAboveOffset(50);
slidingMenu.setBehindOffset(150);
slidingMenu.attachToActivity(this, SlidingMenu.SLIDING_CONTENT);
slidingMenu.setOnOpenListener(new OnOpenListener() {
#Override
public void onOpen() {
getSupportActionBar().setTitle("Menu");
}
});
slidingMenu.setOnCloseListener(new OnCloseListener() {
#Override
public void onClose() {
getSupportActionBar().setTitle("Home");
}
});
slidingMenu.setSecondaryOnOpenListner(new OnOpenListener() {
#Override
public void onOpen() {
getSupportActionBar().setTitle("Current Trip");
}
});
// Add left menu
mTransaction = this.getSupportFragmentManager().beginTransaction();
slidingMenu.setMenu(R.layout.left_menu);
mTransaction.commit();
// Add right menu
mTransaction = this.getSupportFragmentManager().beginTransaction();
slidingMenu.setSecondaryMenu(R.layout.right_menu);
mTransaction.commit();
}
#Override
protected void onResume() {
super.onResume();
getSupportActionBar().setIcon(R.drawable.icon_sliding_menu);
}
You can do it using titleTextStyle attribute of ActionBar.
In your styles.xml, in the App theme, set actionBarTabSyle to a custom style as,
<item name="titleTextStyle">#style/TitleTextStyle</item>
And heres the CustomActionBarTabs,
<!-- action bar tab styles -->
<style name="TitleTextStyle" parent="#style/TextAppearance.AppCompat.Widget.ActionBar.Title">
<item name="android:textColor">#color/actionbar_text</item>
<item name="android:gravity">center</item> //center the title
</style>
Using this you can set various styles to the Title Text like color etc. Remember this will only center the text for the space provided to the Title within the ActionBar. In case this doen't help much you can always create your own custom view and add it to ActionBar as,
getSupportActionBar().setCustomView(R.layout.action_bar);
In case you want to create your own layout, follow these 2 simple steps:
Java Code
getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
getSupportActionBar().setCustomView(R.layout.actionbar);
Where R.layout.actionbar is the following XML.
XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="YOUR ACTIVITY TITLE"
android:textColor="#ffffff"
android:textSize="24sp" />
</LinearLayout>
Source.