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/
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);
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");
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"
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.
I am able to add custom view in actionbar. My application is supporting from version 2.3 so I had used ActionbarActivity. Now my query is to remove app icon from the actionbar and only to have home up icon i.e back image.
I had tried lots of option that I found while searching. But its not working with custom view.
edit
ActionBar actionBar = getSupportActionBar();
LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT, Gravity.RIGHT
| Gravity.CENTER_VERTICAL);
View customNav = LayoutInflater.from(this).inflate(
R.layout.custom_action_view, null); // layout which contains
actionBar.setCustomView(customNav, lp);
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setDisplayShowHomeEnabled(true);
actionBar.setDisplayShowCustomEnabled(true);
actionBar.setBackgroundDrawable(new ColorDrawable(Color
.parseColor("#a40404")));
Help me out with this issue. Thanks in advance.
The up button is tied to home icon. That's why it's called "home as up" setDisplayHomeAsUpEnabled(true).
So you can't show up arrow with disabled "home" icon.
But you can make a View that looks like it in your custom View.
For example:
<LinearLayout
android:id="#android:id/home"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:orientation="horizontal"
android:background="?attr/actionBarItemBackground"
android:paddingRight="5dp">
<!-- this will show tha up arrow -->
<ImageView
android:id="#+id/up"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:src="?attr/homeAsUpIndicator"/>
<!-- place here any View that will be clickable along with "up" arrow (in this example, it's an icon) -->
<ImageView
android:id="#+id/home_icon"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginTop="#dimen/action_bar_icon_vertical_padding"
android:layout_marginBottom="#dimen/action_bar_icon_vertical_padding"
android:adjustViewBounds="true"
android:scaleType="fitCenter"
android:src="#drawable/app_icon"/>
</LinearLayout>
Where
android:src="?attr/homeAsUpIndicator"
Will set the up arrow based on your theme.
android:background="?attr/actionBarItemBackground"
Will set the blue selection background for pressed state
Now set an OnClickListener to a layout with arrow
View customNav = LayoutInflater.from(this).inflate(
R.layout.custom_action_view, null);
customNav.findViewById(android.R.id.home).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick() {
// up button clicked
}
});
actionBar.setCustomView(customNav, lp);
To remove Actonbar Icon:
Just add actionBar.setDisplayUseLogoEnabled(false); this line in oncreate() method:
To enable back button:
you need to extends SherlockActivity and insert this code in oncreate() method:
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
Handle back button action using this code:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
// do your action here when click on back button showing on action bar
onBackPressed();
}
}