How the custom the ActionBar by using xml in Android? - android

I am try to custom the ActionBar by using xml in Android.
The actionbat.xml is like the following:
<?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:background="#drawable/bar_background" >
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="#drawable/bar_icon"/>
</RelativeLayout >
And I using the following code to set ActionBar in Fragment
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.function_list, container, false) ;
ActionBar bar = getActivity().getActionBar();
bar.setCustomView(R.layout.actionbar);
But the ActionBar didn't change anything , did I missing something ?
---------------------------EDIT------------------------------
I have try getSupportActionBar, but it show The method getSupportActionBar() is undefined for the type Activity.
Thanks in advance.

You can try like this
View view=getLayoutInflater().inflate(R.layout.vw_custom_action_bar, null);
ActionBar actionBar=getActivity().getSuppotedActionBar() // for ActionBarActivity
//ActionBar actionBar=getActivity().getActionBar() // for Fragmentactivity
actionBar.setDisplayShowCustomEnabled(true);
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setDisplayHomeAsUpEnabled(false);
actionBar.setDisplayShowHomeEnabled(false);
actionBar.setHomeButtonEnabled(false);
actionBar.setCustomView(view);

Try this..
View view = inflater.inflate(R.layout.function_list, container, false) ;
ActionBar bar = getActivity().getSuppotedActionBar();
bar.setCustomView(R.layout.actionbar);

I think you are not able to access the activity in onCreateView(). Try this in onActivityCreated().

For this you should have an activity that extends ActionbarActivity and that activity have fragments.inside your activity do below code -
actionbar = getSupportActionBar();
mFragmentManager = getSupportFragmentManager();
actionbar.setDisplayShowCustomEnabled(true);
actionbar.setDisplayUseLogoEnabled(false);
actionbar.setDisplayShowHomeEnabled(false);
actionbar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
actionbar.setCustomView(getLayoutInflater().inflate(R.layout.actionbar, null),
new ActionBar.LayoutParams(
ActionBar.LayoutParams.WRAP_CONTENT,
ActionBar.LayoutParams.MATCH_PARENT,
Gravity.CENTER
)
);
mImageView = (ImageView) actionbar.getCustomView().findViewById(R.id.image_view);
hope this answer helpfull for you.

But wait.
what you are asking to say is very bad.
The fragment should not know it's activity like this.
you should do something like that:
Fragment:
void onAttach(Activity activity){
parent = (MyFragmentListener) activity;
}
void onActivityCreated(Bundle b){
parent.askToDoSomethingSpecial();
}
Activity implements MyFragmentListener:
void askToDoSomethingSpecial(){
getActionBar.setCustomView(R.layout.actionbar);
// or getSupportedActionBar.setCustomView(R.layout.actionbar);
}

In order to set custom Actionbar in Android, All you need to do is:-
getSupportActionBar().setDisplayShowCustomEnabled(true);
LayoutInflater inf = LayoutInflater.from(this);
View v = inf.inflate(R.layout.custom_tv, null); //**custom_tv is layout for actionbar**
((TextView)v.findViewById(R.id.title)).setText(title);
getSupportActionBar().setCustomView(v);
/*
* Use this if you are working with fragment
* getActivity().getSupportActionBar().setDisplayShowCustomEnabled(true);
*/
Thats it. Hope this helps.

Related

Drawer menu icon disappears after applying a custom view to ActionBar

I have a drawer menu icon before adding a custom action bar view, in this custom view, it's a just a imageView:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/bell_icon"
android:layout_gravity="right"
android:layout_marginRight="#dimen/double_spacer"
android:id="#+id/ic_notif"/>
</LinearLayout>
and in the fragment that will have this custom view applied, I have the following lines:
#Override
public void onStart() {
super.onStart();
ActionBar actionBar = getActivity().getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
LayoutInflater mInflater = LayoutInflater.from(getActivity());
View customView = mInflater.inflate(R.layout.action_bar_custom_layout, null);
setHasOptionsMenu(true);
actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM|ActionBar.DISPLAY_SHOW_HOME|ActionBar.DISPLAY_SHOW_TITLE);
actionBar.setDisplayShowHomeEnabled(true);
actionBar.setDisplayShowTitleEnabled(true);
actionBar.setTitle(R.string.plan_member_services);
actionBar.setCustomView(customView);
actionBar.setDisplayShowCustomEnabled(true);
ImageView img_notification = customView.findViewById(R.id.ic_notification);
img_notif.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
System.out.println("*** icon clicked ***");
}
});
if(isShowIcon){
img_notifi.setVisibility(View.VISIBLE);
}else{
img_notifi.setVisibility(View.GONE);
}
...
With above code, I am able to see the icon of the action bar defined in "AndroidMenifest.xml":
<application
android:name=".MyApp"
android:allowBackup="true"
android:allowClearUserData="false"
android:fullBackupContent="#xml/backup"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/MyActionBarTheme">
and also I'm able to see the title, but the menu icon for the drawer menu is gone! What have I missed?
I found when I put those codes in the activity instead of the fragment, the drawer menu icon appears when the fragment loaded.

Creating and adding a toolbar to an AppCompatActivity programmatically

I am trying to set the toolbar for a class that extends AppCompatActivity programmatically, but the activity does not have a toolbar when it is run. Every tutorial I have been able to find on using toolbars have the toolbar being created and added in XML, but I am looking for a better way to add the toolbar to all my activities, so I am trying to do it programmatically. However, the toolbar is either not visible or not added when I run, and I cannot find the problem. This is my code:
public class MainActivity extends AppCompatActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = new Toolbar(this);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT, R.attr.actionBarSize);
toolbar.setLayoutParams(layoutParams);
toolbar.setPopupTheme(R.style.AppTheme);
toolbar.setBackgroundColor(getResources().getColor(R.color.colorPrimary));
toolbar.setTitle("This is the title");
toolbar.setVisibility(View.VISIBLE);
setSupportActionBar(toolbar);
}
}
And this is my XML file activity_main.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
</LinearLayout>
I cannot figure out what is wrong with my toolbar. Thank you in advance for any help.
Add Toolbar to your Activity layout using YOUR_LAYOUT.addView(toolbar, 0):
public class MainActivity extends AppCompatActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = new Toolbar(this);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT, 168);
toolbar.setLayoutParams(layoutParams);
toolbar.setPopupTheme(R.style.AppTheme);
toolbar.setBackgroundColor(getResources().getColor(R.color.colorPrimary));
toolbar.setTitle("This is the title");
toolbar.setVisibility(View.VISIBLE);
// Assuming in activity_main, you are using LinearLayout as root
LinearLayout ll = (LinearLayout) findViewById(R.id.your_linear_layout);
ll.addView(toolbar, 0);
setSupportActionBar(toolbar);
}
}
Use MainActivity theme AppTheme.NoActionBar to avoid RuntimeException:
This Activity already has an action bar supplied by the window decor.
Do not request Window.FEATURE_SUPPORT_ACTION_BAR and set
windowActionBar to false in your theme to use a Toolbar instead.
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
OUTPUT:
Hope this will help~

How to align action bar title to centre without using custom view

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);

Fill horizontal Custom View in action bar

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"

Put EditText to action bar instead of title

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/

Categories

Resources