i use this for make floating activity
public class Conversation extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setUpWindow();
setContentView(R.layout.main);
}
public void setUpWindow() {
// Nothing here because we don't need to set up anything extra for the full app.
}
}
this is simple activity now i will make it floating by use this
make another class
public class PopupConversation extends Conversation {
#Override
public void setUpWindow() {
// Creates the layout for the window and the look of it
requestWindowFeature(Window.FEATURE_ACTION_BAR);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND,
WindowManager.LayoutParams.FLAG_DIM_BEHIND);
WindowManager.LayoutParams params = getWindow().getAttributes();
params.alpha = 1.0f; // lower than one makes it more transparent
params.dimAmount = 0f; // set it higher if you want to dim behind the window
getWindow().setAttributes(params);
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;
if (height > width) {
getWindow().setLayout((int) (width * .9), (int) (height * .7));
} else {
getWindow().setLayout((int) (width * .7), (int) (height * .8));
}
}
}
now when i start the class PopupConversation it must show my Conversation activity as floating activity and that's what happen
but the problem its show or start MainActivity in my application behind it
see photo
it must be like this
as we see its floating outside application
now my code not floating outside application its start application and float it over the application
as we see the application start and the floating activity start over it
this is my activity in manifest
<activity
android:name=".activity.PopupConversation"
android:theme="#style/Theme.FloatingWindow.Popup"
android:configChanges="orientation|screenSize"
android:windowSoftInputMode="adjustResize|stateAlwaysHidden"
android:clearTaskOnLaunch="true"
android:exported="true"
tools:ignore="ExportedActivity"
>
</activity>
and this is my style code
<style name="Theme.FloatingWindow.Popup" parent="Theme.AppCompat.NoActionBar" >
<item name="android:windowIsFloating">false</item>
<item name="android:windowSoftInputMode">stateUnchanged</item>
<item name="android:windowContentOverlay">#null</item>
<item name="android:windowActionModeOverlay">true</item>
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowFrame">#null</item>
<item name="android:windowAnimationStyle">#style/PopupAnimation</item>
<item name="android:windowCloseOnTouchOutside">true</item>
<item name="android:backgroundDimEnabled">false</item>
</style>
i want my activity be like first photo i want show it in every where in phone not in application
at the end i start the class from service by call this
Intent window = new Intent(this, PopupConversation.class);
window.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(window);
i solve it by use this when i start it
Intent i = new Intent();
i.setClass(this, PopupConversation.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
startActivity(i);
so i just add
Intent.FLAG_ACTIVITY_MULTIPLE_TASK
How to limit the content of my activity to not climb below the ActionBar when I'm using Translucent StatusBar? My content is below the ActionBar -.-"
Style (using in AppTheme):
<item name="android:windowTranslucentStatus" tools:ignore="NewApi">true</item>
<item name="android:fitsSystemWindows" tools:ignore="NewApi">true</item>
Solved using this code after setContentView:
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
FrameLayout.LayoutParams params
= new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT);
params.setMargins(0, getStatusBarHeight() + getActionBarHeight(), 0, 0);
drawer.setLayoutParams(params);
Methods:
public int getStatusBarHeight() {
int result = 0;
int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
if (resourceId > 0) {
result = getResources().getDimensionPixelSize(resourceId);
}
return result;
}
public int getActionBarHeight(){
TypedValue tv = new TypedValue();
int actionBarHeight = 0;
if (getTheme().resolveAttribute(android.R.attr.actionBarSize, tv, true)){
actionBarHeight = TypedValue.complexToDimensionPixelSize(tv.data,getResources().getDisplayMetrics());
}
return actionBarHeight;
}
Check for the android:windowActionBarOverlay property in your style and set it to false. if this property is true the activity will be drawn so that it occupies the entire screen space and the action bar is drawn on top of the activity content.
On your layout, set the property android:fitsSystemWindows="true"
For example:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
This will make your content fits between the action bar and the navigation bar.
And you will avoid having to programatically set the margin.
0I'm using startActionMode(ActionMode) on my app.
By default it's add a "Done" button on the bar, I want to remove it.
Also, if there's a way to change it's text, I want to know too, cause a different description than "Done" can make the action be correspondent to the behaviour of what it does.
I agree with #CommonsWare that it is invalid design to hide the Done button.
However, there are customers that want to have this button removed and I can understand that the checkmark may cause confusion to users because it actually does nothing in some cases.
So, here is how to remove the button with styles:
<style name="AppTheme" parent="android:Theme.Holo">
<item name="android:actionModeCloseButtonStyle">#style/NoCloseButton</item>
</style>
<style name="NoCloseButton" parent="#android:style/Widget.ActionButton.CloseMode">
<item name="android:visibility">gone</item>
</style>
If you are using ActionBarSherlock you can hide the "Done" button with this style:
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<style name="NoCloseButton" parent="#style/Widget.Sherlock.ActionButton.CloseMode">
<item name="android:visibility">gone</item>
</style>
<style name="AppBaseTheme" parent="Theme.Sherlock.Light.DarkActionBar">
<item name="actionModeCloseButtonStyle">#style/NoCloseButton</item>
</style>
<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
</style>
</resources>
Please make sure to put this style in each style directory (values, value-v11, values-v14). After that you can create a custom menu with this code:
LayoutInflater inflater = LayoutInflater.from(getSherlockActivity());
View actionView = inflater.inflate(R.layout.actionmode, null);
ActionMode am = getSherlockActivity().startActionMode(mActionModeCallback);
am.setCustomView(actionView);
Even I was stuck with same and I found this way to solve it.
int doneButtonId = Resources.getSystem().getIdentifier("action_mode_close_button", "id", "android");
LinearLayout layout = (LinearLayout) findViewById(doneButtonId);
TextView doneview = (TextView) layout.getChildAt(1);
doneview.setText("");
Hope this helps!!
Thanks for Matthias Robbers, it works. But i perfer to use "invisible":
<style name="NoCloseButtonActionModeStyle">
<item name="android:visibility">invisible</item>
</style>
So the custom view position in ActionMode will not indent to parent left.
any one find out screen width solution? – Mayur R. Amipara Jun 5 '15 at 10:27
override your custom view's onMeasure, set it's width to screen width. and in onLayout, relayout your child views.
it works for me.
import android.content.Context;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.View;
import android.widget.RelativeLayout;
import com.android.gallery3d.R;
public class SelectActionModeCustomView extends RelativeLayout {
private View mLeftButton;
private View mRightButton;
private int mScreenWidth;
private static final String TAG = "SelectActionView";
public SelectActionModeCustomView(Context context, AttributeSet attrs) {
super(context, attrs);
DisplayMetrics metrics = context.getResources().getDisplayMetrics();
mScreenWidth = metrics.widthPixels;
}
#Override
protected void onFinishInflate() {
super.onFinishInflate();
mLeftButton = findViewById(R.id.cancel);
mRightButton = findViewById(R.id.select_action);
}
#Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
this.setMeasuredDimension(mScreenWidth, this.getMeasuredHeight());
}
#Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
super.onLayout(changed, l, t, r, b);
int childLeft = 0;
int childTop = 0;
childLeft = 0;
childTop = ((b - t) - mLeftButton.getMeasuredHeight()) / 2;
mLeftButton.layout(childLeft, childTop,
childLeft + mLeftButton.getMeasuredWidth(),
childTop + mLeftButton.getMeasuredHeight());
childLeft = (r - l) - mRightButton.getMeasuredWidth();
childTop = ((b - t) - mRightButton.getMeasuredHeight()) / 2;
mRightButton.layout(childLeft, childTop,
childLeft + mRightButton.getMeasuredWidth(),
childTop + mRightButton.getMeasuredHeight());
}
}
I am trying to get the height of the ActionBar (using Sherlock) every time an activity is created (specially to handle configuration changes on rotation where the ActionBar height might change).
For this I use the method ActionBar.getHeight() which works only when the ActionBar is shown.
When the first activity is created for the first time, I can call getHeight() in the onCreateOptionsMenu callback. But this method is not called after.
So my question is when can I call getHeight() and be assured that it doesn't return 0?
Or if it is not possible, how can I set the height of the ActionBar ?
While #birdy's answer is an option if you want to explicitly control the ActionBar size, there is a way to pull it up without locking the size that I found in support documentation. It's a little awkward but it's worked for me. You'll need a context, this example would be valid in an Activity.
// Calculate ActionBar height
TypedValue tv = new TypedValue();
if (getTheme().resolveAttribute(android.R.attr.actionBarSize, tv, true))
{
int actionBarHeight = TypedValue.complexToDimensionPixelSize(tv.data,getResources().getDisplayMetrics());
}
Kotlin:
val tv = TypedValue()
if (requireActivity().theme.resolveAttribute(android.R.attr.actionBarSize, tv, true)) {
val actionBarHeight = TypedValue.complexToDimensionPixelSize(tv.data, resources.displayMetrics)
}
In XML, you should use this attribute:
android:paddingTop="?android:attr/actionBarSize"
Ok I was googling around and get to this post several times so I think it'll be good to be described not only for Sherlock but for appcompat also:
Action Bar height using appCompat
Pretty similiar to #Anthony description you could find it with following code(I've just changed resource id):
TypedValue tv = new TypedValue();
if (getActivity().getTheme().resolveAttribute(R.attr.actionBarSize, tv, true))
{
int actionBarHeight = TypedValue.complexToDimensionPixelSize(tv.data,getResources().getDisplayMetrics());
}
Pre Sandwitch Problem
I needed action bar height because on pre ICE_CREAM_SANDWITCH devices my view was overlaping action bar. I tried setting android:layout_marginTop="?android:attr/actionBarSize", android:layout_marginTop="?attr/actionBarSize", setting overlap off to view/actionbar and even fixed actionbar height with custom theme but nothing worked. That's how it looked:
Unfortunately I found out that with method described above I get only half of the height(I assume that options bar is not taken in place) so to completely fix the isue I need to double action bar height and everything seems ok:
If someone knows better solution(than doubling actionBarHeight) I'll be glad to let me know as I come from iOS development and I found most of Android view's stuff pretty confusing :)
Regards,
hris.to
#Anthony answer works for devices that support ActionBar and for those devices which support only Sherlock Action Bar following method must be used
TypedValue tv = new TypedValue();
if (getTheme().resolveAttribute(android.R.attr.actionBarSize, tv, true))
actionBarHeight = TypedValue.complexToDimensionPixelSize(tv.data,getResources().getDisplayMetrics());
if(actionBarHeight ==0 && getTheme().resolveAttribute(com.actionbarsherlock.R.attr.actionBarSize, tv, true)){
actionBarHeight = TypedValue.complexToDimensionPixelSize(tv.data,getResources().getDisplayMetrics());
}
//OR as stated by #Marina.Eariel
TypedValue tv = new TypedValue();
if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.HONEYCOMB){
if (getTheme().resolveAttribute(android.R.attr.actionBarSize, tv, true))
actionBarHeight = TypedValue.complexToDimensionPixelSize(tv.data,getResources().getDisplayMetrics());
}else if(getTheme().resolveAttribute(com.actionbarsherlock.R.attr.actionBarSize, tv, true){
actionBarHeight = TypedValue.complexToDimensionPixelSize(tv.data,getResources().getDisplayMetrics());
}
i think the safest way would be :
private int getActionBarHeight() {
int actionBarHeight = getSupportActionBar().getHeight();
if (actionBarHeight != 0)
return actionBarHeight;
final TypedValue tv = new TypedValue();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
if (getTheme().resolveAttribute(android.R.attr.actionBarSize, tv, true))
actionBarHeight = TypedValue.complexToDimensionPixelSize(tv.data, getResources().getDisplayMetrics());
} else if (getTheme().resolveAttribute(com.actionbarsherlock.R.attr.actionBarSize, tv, true))
actionBarHeight = TypedValue.complexToDimensionPixelSize(tv.data, getResources().getDisplayMetrics());
return actionBarHeight;
}
To set the height of ActionBar you can create new Theme like this one:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="Theme.FixedSize" parent="Theme.Sherlock.Light.DarkActionBar">
<item name="actionBarSize">48dip</item>
<item name="android:actionBarSize">48dip</item>
</style>
</resources>
and set this Theme to your Activity:
android:theme="#style/Theme.FixedSize"
If you are using a Toolbar as the ActionBar,
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
then just use the layout_height of the toolbar.
int actionBarHeight = toolbar.getLayoutParams().height;
Java:
int actionBarHeight;
int[] abSzAttr;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
abSzAttr = new int[] { android.R.attr.actionBarSize };
} else {
abSzAttr = new int[] { R.attr.actionBarSize };
}
TypedArray a = obtainStyledAttributes(abSzAttr);
actionBarHeight = a.getDimensionPixelSize(0, -1);
xml:
?attr/actionBarSize
If you're using Xamarin/C#, this is the code you're looking for:
var styledAttributes = Context.Theme.ObtainStyledAttributes(new[] { Android.Resource.Attribute.ActionBarSize });
var actionBarSize = (int)styledAttributes.GetDimension(0, 0);
styledAttributes.Recycle();
you can use this function to get actionbar's height.
public int getActionBarHeight() {
final TypedArray ta = getContext().getTheme().obtainStyledAttributes(
new int[] {android.R.attr.actionBarSize});
int actionBarHeight = (int) ta.getDimension(0, 0);
return actionBarHeight;
}
A ready method to fulfill the most popular answer:
public static int getActionBarHeight(
Activity activity) {
int actionBarHeight = 0;
TypedValue typedValue = new TypedValue();
try {
if (activity
.getTheme()
.resolveAttribute(
android.R.attr.actionBarSize,
typedValue,
true)) {
actionBarHeight =
TypedValue.complexToDimensionPixelSize(
typedValue.data,
activity
.getResources()
.getDisplayMetrics());
}
} catch (Exception ignore) {
}
return actionBarHeight;
}
Here's an updated version with Kotlin I used assuming phone is higher than Honeycomb:
val actionBarHeight = with(TypedValue().also {context.theme.resolveAttribute(android.R.attr.actionBarSize, it, true)}) {
TypedValue.complexToDimensionPixelSize(this.data, resources.displayMetrics)
}
This helper method should come in handy for someone. Example: int actionBarSize = getThemeAttributeDimensionSize(this, android.R.attr.actionBarSize);
public static int getThemeAttributeDimensionSize(Context context, int attr)
{
TypedArray a = null;
try{
a = context.getTheme().obtainStyledAttributes(new int[] { attr });
return a.getDimensionPixelSize(0, 0);
}finally{
if(a != null){
a.recycle();
}
}
}
you just have to add this.
public int getActionBarHeight() {
int height;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
height = getActivity().getActionBar().getHeight();
} else {
height = ((ActionBarActivity) getActivity()).getSupportActionBar().getHeight();
}
return height;
}
I found a more generic way to discover it:
int[] location = new int[2];
mainView.getLocationOnScreen(location);
int toolbarHeight = location[1];
where 'mainView' is the root view of your layout.
The idea is basically get the Y position of the mainView as it is set right below the ActionBar (or Toolbar).
The action bar height differs according to the applied theme.
If you're using AppCompatActivity the height will be different in most of cases from the normal Activity.
If you're using AppCompatActivity you should use R.attr.actionBarSize instead of android.R.attr.actionBarSize
public static int getActionBarHeight(Activity activity) {
TypedValue typedValue = new TypedValue();
int attributeResourceId = android.R.attr.actionBarSize;
if (activity instanceof AppCompatActivity) {
attributeResourceId = R.attr.actionBarSize;
}
if (activity.getTheme().resolveAttribute(attributeResourceId, typedValue, true)) {
return TypedValue.complexToDimensionPixelSize(typedValue.data, activity.getResources().getDisplayMetrics());
}
return (int) Math.floor(activity.getResources()
.getDimension(R.dimen.my_default_value));
}
In xml, you can use ?attr/actionBarSize, but if you need access to that value in Java you need to use below code:
public int getActionBarHeight() {
int actionBarHeight = 0;
TypedValue tv = new TypedValue();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
if (getTheme().resolveAttribute(android.R.attr.actionBarSize, tv,
true))
actionBarHeight = TypedValue.complexToDimensionPixelSize(
tv.data, getResources().getDisplayMetrics());
} else {
actionBarHeight = TypedValue.complexToDimensionPixelSize(tv.data,
getResources().getDisplayMetrics());
}
return actionBarHeight;
}
The action bar now enhanced to app bar.So you have to add conditional check for getting height of action bar.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
height = getActivity().getActionBar().getHeight();
} else {
height = ((ActionBarActivity) getActivity()).getSupportActionBar().getHeight();
}
A simple way to find actionbar height is from Activity onPrepareOptionMenu method.
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
....
int actionBarHeight = getActionBar().getHeight());
.....
}
In javafx (using Gluon), the height is set at runtime or something like it. While being able to get the width just like normal...
double width = appBar.getWidth();
You have to create a listener:
GluonApplication application = this;
application.getAppBar().heightProperty().addListener(new ChangeListener(){
#Override
public void changed(ObservableValue observable, Object oldValue, Object newValue) {
// Take most recent value given here
application.getAppBar.heightProperty.get()
}
});
...And take the most recent value it changed to. To get the height.
After trying everything that's out there without success, I found out, by accident, a functional and very easy way to get the action bar's default height.
Only tested in API 25 and 24
C#
Resources.GetDimensionPixelSize(Resource.Dimension.abc_action_bar_default_height_material);
Java
getResources().getDimensionPixelSize(R.dimen.abc_action_bar_default_height_material);
My dialog is using a linearlayout for its root, and its using the following code as its custom theme:
<style name="HuskyBusDialog">
<item name="android:windowNoTitle">true</item>
<item name="android:windowFrame">#null</item>
<item name="android:windowBackground">#drawable/panel_background</item>
<item name="android:windowIsFloating">true</item>
<item name="android:windowContentOverlay">#null</item>
<item name="android:windowAnimationStyle">#android:style/Animation.Dialog</item>
<item name="android:windowSoftInputMode">stateUnspecified|adjustPan</item>
<item name="android:colorBackgroundCacheHint">#null</item>
</style>
Is it possible to set a max width? Its fine on phones but im trying to optimize it for tablets and they are too big.
The width will be dealt with in the XML for the linear layout, not in the style that you apply to it. Use the android:layout_width tag in XML to specify how wide it could possibly be.
An old question, however no replies are suitable, but you can find some hint here: How to customize the width and height when show an Activity as a Dialog
This helps customize the width and height, but not set the max width and height! To achieve that on an activity using a dialog theme, I had to do a couple of things:
1) set a layout listener to know when the dialog layout is set.
2) Adjust the dialog layout if its size was beyond the desired limit, effectively setting max size
Here is the working snippet I'm currently using, called after setContentView():
getWindow().getDecorView().getViewTreeObserver().addOnGlobalLayoutListener(
new OnGlobalLayoutListener()
{
#TargetApi(Build.VERSION_CODES.JELLY_BEAN)
#SuppressWarnings("deprecation")
#Override
public void onGlobalLayout()
{
adjustDialog();
}
});
Now the dialog size adjustment, as a percentage of actual screen size:
private void adjustDialog()
{
Window w = getWindow();
w.setGravity(Gravity.CENTER);
int current_width = w.getDecorView().getWidth();
int current_height = w.getDecorView().getHeight();
WindowManager.LayoutParams lp = w.getAttributes();
DisplayMetrics dm = getResources().getDisplayMetrics();
int max_width = (int) (dm.widthPixels * 0.90);
int max_height = (int) (dm.heightPixels * 0.90);
if (current_width > max_width)
{
lp.width = max_width;
}
if (current_height > max_height)
{
lp.height = max_height;
}
w.setAttributes(lp);
}
yeaa if u wanna do with Widht GO for ur xml
and try to do
android:layout_width="Defined in pixels//20px"
or u can try this also
android:width="Defined in pixels// 30px"
i hope it will be hepful to u