How to hide actionbar based on layout in Android - android

I want to disable actionBar when the layout is changed for smaller devices but I dont know how to do it. Probably I will do
ActionBar actionBar = getSupportActionBar();
actionBar.hide();
but how to check the layout?

Its simple you can use this example to get the size of the screen and then hide the ActionBar if needed.
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
double x = Math.pow(dm.widthPixels/dm.xdpi,2);
double y = Math.pow(dm.heightPixels/dm.ydpi,2);
double screenInches = Math.sqrt(x+y);
it will get you the size of the screen in inches. and if you want it in pixels then you can use
Display display = getWindowManager().getDefaultDisplay();
int width = display.getWidth();
int height = display.getHeight();
Hope it helps

There are a number of options in this situation.
Firstly, are you sure that your application will be useable without an action bar? e.g. Why do you need an action bar on large devices when you can do without it on smaller devices?
Are you using the Android Toolbar (Which I recommend)? If so you can choose not to include the toolbar in your layout for the smaller screen in your layout xml files. e.g. Include the toolbar in layout-large directory and exclude it from layout-small. Then when you bind the views in your activity/fragment handle the case where the toolbar view is not found.
Toolbar toolbar = (Toolbar)findViewById(R.id.myToolbar);
if(toolbar != null) {
//set up my toolbar here which might include
setSupportActionBar(toolbar);
}
Another option is to use multiple styles for different size screens. Where for the large screen your style is Theme.AppCompat.Light.DarkActionBar and for your small screen its Theme.AppCompat.Light.NoActionBar

Probably your best approach is to make a copy of your style.xml of the res/values to the res/values-mdpi (or your desired layout size) and change the AppTheme to your style + .NoActionBar.
Example:
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">

If you want to check which layout was loaded you can add tags to your layout for example:
ldpi layout:
<RelativeLayout
android:width="match_parent"
android:height="match_parent"
android:id="#+id/mainlayout"
android:tag="ldpi" >
mdpi layout:
<RelativeLayout
android:width="match_parent"
android:height="match_parent"
android:id="#+id/mainlayout"
android:tag="mdpi" >
and so on.
In your program code you can now check which tag the layout have:
RelativeLayout layout = (RelativeLayout) findViewById(R.id.mainlayout);
String tag = layout.getTag();
if(tag != null && tag.equals("ldpi")) {
// Ldpi Layout loaded, hide actionbar
}

Use DisplayMetrics
public void setAppInvisible() {
actionBar.setVisibility(View.INVISIBLE);
// Or actionBar.hide();
}
DisplayMetrics dm = new DisplayMetrics();
getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);
int screenWidth = dm.widthPixels;
if(screenWidth < 500){
setAppInvisible();
}

Related

AppCompat v7:21 Split Action Bar Broken?

I am currently developing an application in which I use a heavily modified Split Action Bar. Here is a link to the app's current state:
You'll notice a transparent action bar up top, with a custom view inflated into it, with a hacked together split action bar on bottom. The bottom view is actually a single action item with a custom view inflated into it and showAlways=true.
Currently I only support SDK v15+ and I don't really plan on changing that, but with the Lollipop AppCompat library that just released, I decided to implement it, so I could get some of that awesomeness in my app.
I've changed my theme to Theme.AppCompat.Light, and my MainActivity now extends ActionBarActivity instead of Activity.
All references to getActionBar have now been switched to getSupportActionBar, and with only those changes, this is what my activity now looks like:
You'll notice I got a UI dump from the Device Monitor, and it's shoving the bottom action bar into a weird space and calling that the action bar, and getting rid of my top custom view.
Here is my code for setting up my action bar:
public void initializeActionBar(){
View customNav = LayoutInflater.from(this).inflate(R.layout.action_bar_top, null);
actionBar = getSupportActionBar();
actionBar.setBackgroundDrawable(getResources().getDrawable(R.drawable.transparent_fifty_percent));
final PopupWindow window = addPopupWindow();
actionBarOptions = (ImageView)customNav.findViewById(R.id.options);
actionBarOptions.setVisibility(View.GONE);
actionBarOptions.setImageDrawable(app.svgToBitmapDrawable(getResources(), R.raw.vertical_ellipsis, app.scaleByDensity(48)));
actionBarOptions.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
window.showAsDropDown(actionBarOptions, 0, 0);
}
});
TextView title = (TextView) customNav.findViewById(R.id.screen_title);
Typeface font1 = Typeface.createFromAsset(getAssets(), "Merriweather-Italic.ttf");
title.setText("Parsley");
title.setTypeface(font1);
actionBar.setCustomView(customNav);
actionBar.setDisplayShowCustomEnabled(true);
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setDisplayShowHomeEnabled(false);
actionBar.setDisplayUseLogoEnabled(false);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
new MenuInflater(this).inflate(R.menu.test, menu);
LinearLayout fullMenu = (LinearLayout) menu.findItem(R.id.full_menu).getActionView();
ViewGroup.LayoutParams params;
icon1 = (ImageView) fullMenu.findViewById(R.id.action_item1);
params = icon1.getLayoutParams();
params.width = getResources().getDisplayMetrics().widthPixels / 4;
params.height = (int) (48 * getResources().getDisplayMetrics().density);
icon1.setImageDrawable(app.svgToBitmapDrawable(getResources(), R.raw.shopping_list_icon, app.scaleByDensity(32)));
icon2 = (ImageView) fullMenu.findViewById(R.id.action_item2);
icon3 = (ImageView) fullMenu.findViewById(R.id.action_item3);
icon4 = (ImageView) fullMenu.findViewById(R.id.action_item4);
icon2.setImageDrawable(app.svgToBitmapDrawable(getResources(), R.raw.recipe_box_icon, app.scaleByDensity(32)));
icon3.setImageDrawable(app.svgToBitmapDrawable(getResources(), R.raw.icon_search, app.scaleByDensity(32)));
icon4.setImageDrawable(app.svgToBitmapDrawable(getResources(), R.raw.icon_add, app.scaleByDensity(32)));
params = icon2.getLayoutParams();
params.width = getResources().getDisplayMetrics().widthPixels / 4;
params.height = (int) (48 * getResources().getDisplayMetrics().density);
params = icon3.getLayoutParams();
params.width = getResources().getDisplayMetrics().widthPixels / 4;
params.height = (int) (48 * getResources().getDisplayMetrics().density);
params = icon4.getLayoutParams();
params.width = getResources().getDisplayMetrics().widthPixels / 4;
params.height = (int) (48 * getResources().getDisplayMetrics().density);
if (!firstLoad) {
setBottomActionBarActive();
setActiveTab(0);
}
optionsLoaded = true;
return true;
}
initializeActionBar() is called from onCreate in my activity. Any ideas what I'm doing wrong?
Toolbar should be used. In your case it's one toolbar at the top, and one at the bottom. Check android team blog, they have nice integration guide.
If you just want your bottom action bar back, you can simply change back to appcompat v7:20 ,and it works for me. The problem is split action bar is no longer being supported in appcomat v7:21.
While user482277's solution may work for instances with a more traditional split action bar, utilizing action items, navigation drawer, etc, it didn't quite work for me. What I ended up doing was building a pair of custom (compound really) views to emulate both the top and bottom action bar. I found this situation to work much better, especially with backwards compatibility. I don't have to worry about earlier versions supporting action bar, because at the end of the day, it's just a pair of classes that extend LinearLayout. In addition, I don't have to worry about different screen sizes (particularly tablets) not supporting the split version.

increase ListFragment inbuilt empty text view size

I m using listfragment in my app. When the data fails to load I call setEmptyText with the failure message. This works fine on 4 inch phones but on 7 and 10 inch phones the size of the empty textview is very small and hard to read.
4 inch empty text view
7 inch empty text view
How can I increase the size of empty text view?
You can provide your own TextView to be used as the "empty text", it just needs to have android:id="#id/android:empty" and the ListFragment will use it instead of the standard one.
You could then customize the style of this TextView however you like (either statically in the xml layout):
<TextView android:id="#id/android:empty"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textSize="... />
or by calling findViewById() in onCreateView():
TextView emptyTextView = (TextView)view.findViewById(android.R.id.empty);
emptyTextView.setTextSize(...);
A (very good) alternative, as #Delblanco commented, is to call getListView().getEmptyView(). It will return either the TextView with id android.R.id.empty (if supplied in the layout), or the standard TextView that is used otherwise.
Here is a drop in extension for ListFragment which changes the size (and color) of the empty view programmatically:
/**
* Abstract list fragment that does customizations of the list style.
*/
public abstract class LoggingListFragment extends ListFragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = super.onCreateView(inflater, container, savedInstanceState);
TextView emptyView = null;
View possibleEmptyView = view.findViewById(0x00ff0001);
if (possibleEmptyView instanceof TextView) {
emptyView = (TextView) possibleEmptyView;
}
if (emptyView == null) {
possibleEmptyView = getListView().getEmptyView();
if (possibleEmptyView instanceof TextView) {
emptyView = (TextView) possibleEmptyView;
}
}
if (emptyView != null) {
emptyView.setTextColor(getActivity().getResources().getColor(R.color.list_subtitle_normal));
emptyView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 17);
}
return view;
}
}
Create different folders like res/values-sw720dp,res/values-sw600dp,
res/values-v11.
Create dimen.xml with different font size of different screen size.
Use values/dimen.xml:
<resources>
<dimen name="normal_font">16sp </dimen>
</resources>
4.Use in your widget in your xml.
android:textSize="#dimen/normal_font
I would do it a bit differently.
My idea: find out the dimensions of the screen and use a switch/case statement to adjust your textView text's size.
For example:
switch ( area ) {
case 7inch:
TextView emptyTextView = (TextView)view.findViewById(android.R.id.empty);
emptyTextView.setTextSize(...);
break;
case 4inch:
TextView emptyTextView = (TextView)view.findViewById(android.R.id.empty);
emptyTextView.setTextSize(...);
break;
default:
values_not_caught_above;
}
You can find the dimensions of the screen as follows:
If you want the display dimensions in pixels you can use getSize:
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;
int area = height*width;
If you're not in an Activity you can get the default Display via WINDOW_SERVICE:
WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
Before getSize was introduced (in API level 13), you could use the getWidth and getHeight methods that are now deprecated:
Display display = getWindowManager().getDefaultDisplay();
int width = display.getWidth(); // deprecated
int height = display.getHeight(); // deprecated
I like my way best because it's very easy to just add another case statement for an additional device(may be 10inch screen). Almost nothing in your code will need to change that way.
You may increase the size of the text by calling setEmptyText method with a Spannable, let me show you some code:
String yourEmptyTextMessage = getString(R.string.yourEmptyTextMessage);
Spannable spanText = new SpannableString(yourEmptyTextMessage);
spanText.setSpan(new RelativeSizeSpan(1.5f), 0, yourEmptyTextMessage.length(), 0);
setEmptyText(spanText)
You may want to change 1.5f multiplier value to desired ratio

What should be action bar's height compatible for all devices?

I am using different layout for action bar and add different items to it that matched to my requirements. and then calling it in java class like this
ActionBar actionBar = getActionBar();
actionBar.setCustomView(R.layout.actionbar);
actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
actionBar.setDisplayShowHomeEnabled(false);
It looks fine for 2 or 3 different devices but doesn't look compatible for nexus7 tablet and xhdpi. The height of action bar is small for that and it gives empty space.What should I do to manage this? I am using
android:layout_width="match_parent"
android:layout_height="48dp"
in actionbar layout.I have also used
android:layout_height="wrap_content"
but it doesn't work at all.I don't know how to fix it. Please help.
You can set the height of the action bar at source not in the resources.
so all you need is to calculate the height of the action bar :
TypedValue tv = new TypedValue();
if (getTheme().resolveAttribute(android.R.attr.actionBarSize, tv, true))
{
actionBarHeight = TypedValue.complexToDimensionPixelSize(tv.data,getResources().getDisplayMetrics());
}
ActionBar bar = getSupportActionBar();
Display d = getWindowManager().getDefaultDisplay();
DisplayMetrics dm = new DisplayMetrics();
d.getMetrics(dm);
LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams)myCustomView.getLayoutParams();
final int ACTION_BAR_HEIGHT = dm.heightPixels/10; // 1/10 from screen height
lp.height = ACTION_BAR_HEIGHT;
myCustomView.setLayoutParams(lp);
bar.setCustomView(myCustomView);

Get dimension of screen area used by activity

How do I determine the dimensions (width and height) of an activity during onCreate. The dimension must not be that of the entire screen but only of area that is used by the activity. A few methods I tried posted on SO all end up giving the device's maximum screen sizes.
Have you tried (not exactly onCreate):
activityRootView = findViewById(R.id.yourRootView);
activityRootView.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
//activityRootView.getHeight();
//...
}
}
});
And ofcourse, your activitys main layout has to match parent or fill content
The entire user content goes to android.R.id.content so what you can do is
final Rect rectgle = new Rect();
LinearLayout mContent = (LinearLayout)findViewById(android.R.id.content).getParent();
mContent.getWindowVisibleDisplayFrame(rectgle);
// and use
//rectgle.top;
//rectgle.left
//rectgle.right
this includes action bar if you want to exclude actionbar also directly use
//findViewById(android.R.id.content);
which returns a FrameLayout

How to custom logo on Android actionbarsherlock [duplicate]

I'm currently making one of my very first applications. I'm using ActionBarSherlock.
I would like to make my logo overlap the actionbar (scrollview).
Currently I have main_activity.xml. In MainActivity.java I use setContentView to view main_activity.xml. After that I use getSupportActionBar() for ActionBarSherlock. I've tried things out using RelativeLayout  (http://www.mkyong.com/android/android-relativelayout-example/). That didn't really work because there are multiple layouts.
So I've tried some things right and left, but it always ends up infront or behind the actionbar, or stops just before reaching the content. It's because of two different layouts, that's what I know. But how can I going to solve this? Is it possible? Thanks in advance!
What I want:
http://f.cl.ly/items/3N0w243N1t2Q3i1H1f1k/Untitled-1.png
You can either:
A. Split your image in two
Have the top part as the ActionBar logo, then show the bottom part over your content.
B. Use a single image
You'll need a layout file that contains just your logo (you'll probably want something like an ImageView inside a LinearLayout so you can easily set the correct margins).
Then after calling setContentView for your activity, add your logo view with:
ViewGroup decorViewGroup = (ViewGroup) getWindow().getDecorView();
decorViewGroup.addView(logoView);
Using a layout file
Example layout file (logo_view.xml):
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/logo_image"
android:scaleType="center"
android:layout_marginLeft="10dip"
/>
</LinearLayout>
Inflate the layout file:
LayoutInflater inflater = LayoutInflater.from(this);
View logoView = inflater.inflate(R.layout.logo_view, null, false);
Although the original answer works on some devices, on others the image sits under the status bar. I resolved this by getting the location of the top ActionBar and comparing it to the location of the top of the logo image and then just adding some top padding, as follows:
// Inflate logo layout
LayoutInflater inflater = LayoutInflater.from(this);
final View logoView = inflater.inflate(R.layout.menu_logo, null);
// Add logo to view
ViewGroup viewGroup = (ViewGroup) getWindow().getDecorView();
viewGroup.addView(logoView);
// Adjust the logo position
int resId = getResources().getIdentifier("action_bar_container", "id", "android");
final View actionBarView = viewGroup.findViewById(resId);
if (actionBarView != null) {
actionBarView.getViewTreeObserver().addOnGlobalLayoutListener(
new ViewTreeObserver.OnGlobalLayoutListener() {
public void onGlobalLayout() {
// Remove the listener
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
actionBarView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
} else {
actionBarView.getViewTreeObserver().removeGlobalOnLayoutListener(this);
}
// Measure views
int[] location = new int[2];
actionBarView.getLocationOnScreen(location);
int[] logoLocation = new int[2];
logoView.getLocationOnScreen(logoLocation);
// Add top padding if necessary
if (location[1] > logoLocation[1]) {
logoView.setPadding(0, location[1] - logoLocation[1], 0, 0);
}
}
}
);
}
This worked on a wide range of devices (phones, big/small tablets - inc Kindle Fire HDX) running Android versions 4.0 up to 4.4.4 as well as Android L preview.

Categories

Resources