Android SearchView searchIcon(magnifying class when collapsed) right padding - android

I 'm trying to integrate a SearchView, on a custom Toolbar I 'm making.
The requirements are:
1) 2 ActionMenuViews
2) Centered Title
I have also subclassed SearchView to make it full width, so, apart from overriding onMeasure and applying a custom MaxWidth(Integer.MAX_VALUE), I, also. applied this terrible hack found online to remove the SearchView container's margins.
// Terrible hack (1) to set SearchView's main container margin to 0
LinearLayout searchEditFrame = (LinearLayout) findViewById(R.id.search_edit_frame); // Get the Linear Layout
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) searchEditFrame.getLayoutParams();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
params.setMarginStart(0);
} else {
params.leftMargin = 0;
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
params.setMarginEnd(0);
} else {
params.rightMargin = 0;
}
searchEditFrame.setLayoutParams(params);
// End terrible hack (1)
I also took the liberty (sic) and applied similar logic to the magnifying glass ImageView directly.
// Terrible hack (2) to remove hardcoded padding from search icon magnifying glass icon
ImageView magIcon = (ImageView) findViewById(R.id.search_mag_icon);
params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT
);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
params.setMarginStart(0);
} else {
params.leftMargin = 0;
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
params.setMarginEnd(0);
} else {
params.rightMargin = 0;
}
magIcon.setLayoutParams(params);
// End terrible hack (2)
(remember the ids are from abs_search_view.xml and they seem to match)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/search_bar"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<!-- This is actually used for the badge icon *or* the badge label (or neither) -->
<TextView
android:id="#+id/search_badge"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:layout_marginBottom="2dip"
android:drawablePadding="0dip"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="?android:attr/textColorPrimary"
android:visibility="gone" />
<ImageView
android:id="#+id/search_button"
style="?attr/actionButtonStyle"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="center_vertical"
android:focusable="true"
android:contentDescription="#string/abc_searchview_description_search" />
<LinearLayout
android:id="#+id/search_edit_frame"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="horizontal"
android:layoutDirection="locale">
<ImageView
android:id="#+id/search_mag_icon"
android:layout_width="#dimen/abc_dropdownitem_icon_width"
android:layout_height="wrap_content"
android:scaleType="centerInside"
android:layout_gravity="center_vertical"
android:visibility="gone"
style="#style/RtlOverlay.Widget.AppCompat.SearchView.MagIcon" />
<!-- Inner layout contains the app icon, button(s) and EditText -->
<LinearLayout
android:id="#+id/search_plate"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:layout_gravity="center_vertical"
android:orientation="horizontal">
<view class="android.support.v7.widget.SearchView$SearchAutoComplete"
android:id="#+id/search_src_text"
android:layout_height="36dip"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_gravity="center_vertical"
android:paddingLeft="#dimen/abc_dropdownitem_text_padding_left"
android:paddingRight="#dimen/abc_dropdownitem_text_padding_right"
android:singleLine="true"
android:ellipsize="end"
android:background="#null"
android:inputType="text|textAutoComplete|textNoSuggestions"
android:imeOptions="actionSearch"
android:dropDownHeight="wrap_content"
android:dropDownAnchor="#id/search_edit_frame"
android:dropDownVerticalOffset="0dip"
android:dropDownHorizontalOffset="0dip" />
<ImageView
android:id="#+id/search_close_btn"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:paddingLeft="8dip"
android:paddingRight="8dip"
android:layout_gravity="center_vertical"
android:background="?attr/selectableItemBackgroundBorderless"
android:focusable="true"
android:contentDescription="#string/abc_searchview_description_clear" />
</LinearLayout>
<LinearLayout
android:id="#+id/submit_area"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="match_parent">
<ImageView
android:id="#+id/search_go_btn"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="center_vertical"
android:paddingLeft="16dip"
android:paddingRight="16dip"
android:background="?attr/selectableItemBackgroundBorderless"
android:visibility="gone"
android:focusable="true"
android:contentDescription="#string/abc_searchview_description_submit" />
<ImageView
android:id="#+id/search_voice_btn"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="center_vertical"
android:paddingLeft="16dip"
android:paddingRight="16dip"
android:background="?attr/selectableItemBackgroundBorderless"
android:visibility="gone"
android:focusable="true"
android:contentDescription="#string/abc_searchview_description_voice" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
This is what I 'm getting. The left action menu view has an MenuItem defined via xml and has custom styling (applied through actionButtonStyle property in Toolbar's theme).
The right action menu view has a searchview MenuItem defined via xml
The same result is if I move SearchView to the left menu.
Can somebody explain to me how to strip the searchIcon image of its padding and/or right margin?
PS: This is the expanded SearchView. Notice the right part, where I click the button. It is obviously expanded to full width without margins

OK, so here's what I think is happening.
Notice this line
<ImageView
android:id="#+id/search_button"
style="?attr/actionButtonStyle"
I think the actionButtonStyle of the Toolbar gets applied twice. I haven't had the time to look it into depth...
If anyone's got any comment, be my guest :)

Related

Align Android seekbar with thumb

I'm trying to align the seekbar to the top of a view, but i can't center it with the thumb.
Is there some kind of "alignCenter" with the RelativeLayout children.
Here's a sample of my xml code :
<RelativeLayout
android:id="#+id/rl_fragment_audio_player"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="#color/black_transparent"
android:padding="5dp"
android:visibility="gone" >
<TextView
android:id="#+id/tv_fragment_audio_begin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:textColor="#color/white" />
<TextView
android:id="#+id/tv_fragment_audio_end"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:textColor="#color/white" />
<Button
android:id="#+id/btn_fragment_audio_play"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_centerInParent="true"
android:background="#drawable/btn_play"
android:visibility="gone" />
</RelativeLayout>
<SeekBar
android:id="#+id/sb_fragment_audio"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:visibility="gone"
android:layout_above="#id/rl_fragment_audio_player" />
For example, Google Play Music does it.
As you said the height of your seekbar might vary, unless you specify it different. Here is how you can make sure it fits all, always.
android:layout_above="#id/rl_fragment_audio_player"
is setting your seekbar bottom above rl_fragment_audio_player top. There is no direct method to specify centerAlignWith but I would remove the space it occupies within it's parent. To know the height of the seekbar you must wait until the global layout has changed. This code should be placed inside your onCreateView
sb = (SeekBar) rootView.findViewById(R.id.sb_fragment_audio);
sb.getViewTreeObserver().addOnGlobalLayoutListener(
new ViewTreeObserver.OnGlobalLayoutListener(){
#Override
public void onGlobalLayout() {
sb.getViewTreeObserver().removeOnGlobalLayoutListener(this);
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) sb.getLayoutParams();
params.setMargins(0, - sb.getHeight() / 2, 0, - sb.getHeight() / 2);
sb.setLayoutParams(params);
// I suggest you set the seekbar visible after this so that it won't jump
}
});
The last view in the xml will be in the front. Therefore make sure your seekbar is added after the the other views. Like this
<RelativeLayout
android:id="#+id/rl_fragment_audio_player"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true" >
// your bottom panel
</RelativeLayout>
<View
android:id="#+id/image_above"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#id/rl_fragment_audio_player" />
<SeekBar
android:id="#+id/sb_fragment_audio"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/image_above" />
Have you tried adding a negative margin to the seekbar?
<SeekBar
android:id="#+id/sb_fragment_audio"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:visibility="gone"
android:layout_marginBottom="-15dp"
android:layout_above="#id/rl_fragment_audio_player" />

Placing View Underneath Action Bar

I have a custom camera activity in my Android app. For android devices using KitKat, I needed to account for the visible navigation bar at the bottom of the screen. To do this, I use the following line to ensure my views are below the navigation bar:
getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN );
However, when I do this, one of my views becomes hidden behind the action bar. I'm trying to account for the height of the action bar by adding a top margin to this view, but it doesn't appear to be correct.
What am I doing wrong?
This is what I would like the view to look like (works on pre-KitKat devices):
And this is what it currently looks like on Kit Kat devices (you can see that the top of my view is cut off):
Here is my layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="left"
android:orientation="vertical" >
<HorizontalScrollView
android:id="#+id/gallery_scroll_view"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:scaleType="fitXY" >
<com.example.helperClass.PictureHorizontalLayout
android:id="#+id/mygallery"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:scaleType="fitXY" >
</com.example.helperClass.PictureHorizontalLayout>
</HorizontalScrollView>
<FrameLayout
android:id="#+id/camerapreview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#color/black" />
<LinearLayout
android:id="#+id/button_holder_customized_cam"
android:layout_width="match_parent"
android:layout_height="55dp"
android:layout_alignParentBottom="true"
android:background="#838B8B"
android:gravity="bottom"
android:orientation="horizontal"
android:weightSum="0.9"
>
<ImageButton
android:id="#+id/gallery_customized_camera"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginRight="1dp"
android:layout_weight="0.3"
android:adjustViewBounds="true"
android:background="#null"
android:contentDescription="#string/add"
android:maxWidth="75dp"
android:scaleType="center"
android:src="#drawable/ic_action_picture" >
<!-- android:background="#drawable/custom_button_blue" -->
</ImageButton>
<ImageButton
android:id="#+id/shutter_customized_camera"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginRight="1dp"
android:layout_weight="0.3"
android:adjustViewBounds="true"
android:background="#drawable/custom_button_blue"
android:contentDescription="#string/add"
android:maxWidth="75dp"
android:scaleType="center"
android:src="#drawable/ic_action_camera" >
</ImageButton>
<ImageButton
android:id="#+id/video_customized_camera"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="0.3"
android:adjustViewBounds="true"
android:background="#null"
android:contentDescription="#string/add"
android:maxWidth="75dp"
android:scaleType="center"
android:src="#drawable/ic_action_video" >
<!-- android:background="#drawable/custom_button_blue" -->
</ImageButton>
</LinearLayout>
<TextView
android:id="#+id/camera_timer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#id/button_holder_customized_cam"
android:layout_alignParentRight="true"
android:padding="15dp"
android:text="#string/no_time"
android:textColor="#color/red"
android:textSize="20sp"
android:visibility="invisible" >
</TextView>
</RelativeLayout>
And here is how I set margins for the HorizontalScrollView:
myHorizontalLayout = (PictureHorizontalLayout) findViewById(R.id.mygallery);
// apply margin to horizontal scroll view to take into account height of status bar
TypedValue tv = new TypedValue();
int actionBarHeight = 0;
if (getTheme().resolveAttribute(android.R.attr.actionBarSize, tv, true))
{
actionBarHeight = TypedValue.complexToDimensionPixelSize(tv.data,getResources().getDisplayMetrics());
Log.d(TAG, "actionbarHeight before: " + actionBarHeight);
FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) myHorizontalLayout.getLayoutParams();
Log.d(TAG, "actionbarHeight after: " + actionBarHeight);
params.setMargins(0, actionBarHeight, 0, 0);
Log.d(TAG, "ADJUST MARGINS OF HORIZONTAL LAYOUT");
myHorizontalLayout.setLayoutParams(params);
}
NOTE: I also tried adding android:layout_marginTop="?attr/actionBarSize" to the HorizontalScrollView in my layout file, but this didn't work either (the vertical offset was still incorrect)
The best way to do what you want, is to use ActionBar in Overlay Mode.
Then, you can use the following margin in your view to make sure it does not hide behind the action bar.
<!-- Support library compatibility -->
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="?attr/actionBarSize">
...
</RelativeLayout>
Change your RelativeLayout to use the android:fitsSystemWindows="true" attribute.
From the View.SYSTEM_UI_FLAG_LAYOUT_STABLE docs:
When using other layout flags, we would like a stable view of the
content insets given to fitSystemWindows(Rect).
Also, the attribute android:orientation isn't inherited by RealtiveLayout and android:scaleType isn't inherited by HorizontalScrollView.

Android - Can't hide progress bar

So I've checked the other questions to hide a progress bar but all seem to suggest doing what I'm already doing.
I'm trying to use
mProductListProgressBar.setVisibility(View.GONE);
and I'm finding it by
mProductListProgressBar = (ProgressBar) mRoot.findViewById(R.id.product_list_progressbar);
I know it's the correct progress bar as I can move it around the screen before with various LayoutParams commands. But it won't hide.
The current code I have (including the moving of the progress bar) is
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
mProductListProgressBar.setLayoutParams(params);
params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
mProductListProgressBar.setLayoutParams(params);
mProductListProgressBar.setVisibility(View.GONE);
mProductListErrorTextView.setVisibility(View.VISIBLE);
mProductListErrorTextView.setText(errorMessage);
The progress bar moves to left and bottom but is still visible. I have tried View.INVISIBLE as well as View.GONE but neither work.
It's driving me nuts!
Thanks
UPDATE 1
protected void showError(int errorMessage){
/*RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
mProductListProgressBar.setLayoutParams(params);
params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
mProductListProgressBar.setLayoutParams(params);
*/
mProductListProgressBar.setVisibility(View.GONE);
mProductListErrorTextView.setVisibility(View.VISIBLE);
mProductListErrorTextView.setText(errorMessage);
}
and calling it with
showError(R.string.wish_list_empty);
UPDATE 2
xml of fragment
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<GridView
android:id="#+id/product_list_gridview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/transparent"
android:horizontalSpacing="#dimen/standardMargin"
android:listSelector="#android:color/transparent"
android:numColumns="#integer/columns"
android:scrollbarStyle="outsideOverlay"
android:verticalSpacing="#dimen/standardMargin" />
<RelativeLayout
android:id="#+id/product_list_header"
android:layout_width="match_parent"
android:layout_height="#dimen/refineHeaderHeight"
android:background="#drawable/product_list_header"
android:visibility="gone" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:padding="10dp"
android:src="#drawable/dropdown_image" />
<TextView
android:id="#+id/product_list_header_title"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center|left"
android:paddingLeft="10dp"
android:textAllCaps="true"
android:textColor="#android:color/black"
android:textIsSelectable="false"
android:textSize="#dimen/standardTextSize" />
<TextView
android:id="#+id/product_list_refine_button"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:background="#drawable/button"
android:clickable="true"
android:gravity="center_vertical"
android:paddingLeft="20dp"
android:paddingRight="30dp"
android:text="#string/refine"
android:textAllCaps="true"
android:textColor="#838383"
android:textSize="#dimen/standardTextSize" />
</RelativeLayout>
<LinearLayout
android:id="#+id/product_list_footer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_gravity="center"
android:background="#drawable/product_list_footer"
android:gravity="center"
android:orientation="horizontal"
android:padding="15dp"
android:visibility="gone" >
<ProgressBar
android:layout_width="15dp"
android:layout_height="15dp"
android:layout_gravity="center"
android:layout_marginRight="10dp"
android:background="#android:color/transparent"
android:gravity="center" />
<TextView
android:id="#+id/product_list_footer_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:text="#string/loading_message"
android:textAllCaps="true"
android:textSize="#dimen/standardTextSize" />
</LinearLayout>
<ProgressBar
android:id="#+id/product_list_progressbar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:visibility="gone"/>
<TextView
android:id="#+id/product_list_error_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:textAllCaps="true"
android:textSize="#dimen/standardTextSize"
android:visibility="gone" />
<TextView
android:id="#+id/debug"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#aa000000"
android:gravity="left|center_vertical"
android:minLines="2"
android:padding="10dp"
android:textColor="#android:color/white"
android:visibility="gone" />
</RelativeLayout>
The problem with this behaviour usually happens when your view which you try to hide is needed / referenced by someone else.
In your case, as you mentioned in the comments, you use mProductListProgressBar for setEmptyView() of your GridView.
Another possibility of running into similar troubles when there other views in your relative container layout which set their position relatively to your mProductListProgressBar. This setting could be either in the code or in .xml.
Please make sure you don't have any of above and View.GONE should work fine.
As for setEmptyView() - it is only used to show something meaningful to the user when your adapter is empty. I recommend just setting up simple Layout for that with, say, TextView "no items", in the middle, and pass it to setEmptyView()
Hope that helps.
I know that it is an old question. But I just spent a lot of time debugging similar situation. I could not hide ProgressBar on top of unityPlayer view.
One more thing to ensure is that You are hiding it while in UI thread.
To ensure surround Your UI code with:
runOnUiThread(new Runnable() {
#Override
public void run() {
loadingIndicator.setVisibility(View.GONE);
}
});
A solution that worked for me was to hide the view in XML:
android:visibility="gone"
and then unhide/hide it at runtime when needed:
yourProgressBar.setVisibility(View.VISIBLE) // or View.VISIBLE depending on situation
I don't know how your code is written but had similar problem and resolved by removing the view object from declaration
like
mProductListProgressBar = (ProgressBar) mRoot.findViewById(R.id.product_list_progressbar);
should be
mProductListProgressBar = (ProgressBar) findViewById(R.id.product_list_progressbar);
Try it and check
Reason is the progress bar is on the top of every view in the hierarchy and it's not belong to any root view.

Title not showing on the mainActivity

I have put up a spinner on Android Action Bar, i have placed the spinner in test.xml layout and i am calling it like this in the main activity in the OnCreate() method
ActionBar actionBar = getActionBar();
actionBar.setBackgroundDrawable(new ColorDrawable(Color.GREEN));
actionBar.setDisplayShowCustomEnabled(true);
actionBar.setCustomView(R.layout.activity_test);
setContentView(R.layout.activity_main);
actionBar.setTitle("Home");
But my title is not showing. i want to show HOME as title on that action bar what should i do?
My activity_test
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Spinner
android:id="#+id/planets_spinner"
android:layout_width="100dp"
android:layout_height="50sp"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:background="#drawable/arrow2"
android:layout_weight="0.08"
android:drawSelectorOnTop="true" />
</RelativeLayout>
I have solved the problem by adding a textView to my activity_test, as this whole activity is showing on the ActionBar so i have added a text view and problem is solved :)
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<Spinner
android:id="#+id/planets_spinner"
android:layout_width="100dp"
android:layout_height="50sp"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:background="#drawable/arrow2"
android:drawSelectorOnTop="true" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/planets_spinner"
android:layout_alignParentLeft="true"
android:layout_marginBottom="6dp"
android:layout_marginLeft="6dp"
android:textStyle="bold"
android:textSize="20sp"
android:textColor="#ffffff"
android:text="Scene" />
</RelativeLayout>
When you set android:layout_alignParentRight="true" , RelativeLayout uses all the space including the title space and title is not displayed. You can check the behaviour by drawing a border around the RelativeLayout. But RelativeLayout doesnt consume more space if you dont use android:layout_alignParentRight="true". It doesnt consume more space if you use android:layout_alignParentLeft="true", too. It works weird for only android:layout_alignParentRight="true". In order to show a view at the right of the parent, the following worked for me:
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/profile_image"
android:layout_width="40dp"
android:src="#drawable/ic_launcher"
android:layout_height="40dp"
android:background="?android:selectableItemBackground"
android:padding="3dp"
android:scaleType="centerCrop" />
Do not use RelativeLayout. Just the view you need.
Then you can align right with the LayoutParams shown as following:
actionBar.setCustomView(R.layout.actionbar_layout);
actionBar.setDisplayShowCustomEnabled(true);
ImageView imageProfile = (ImageView)actionBar.getCustomView();
int length = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 40, getResources().getDisplayMetrics());
Toolbar.LayoutParams layoutParams = new Toolbar.LayoutParams(
length,
length, Gravity.RIGHT
| Gravity.CENTER_VERTICAL);
imageProfile.setLayoutParams(layoutParams);
Now you should be able to view both ActionBar title and the ImageView.

How to position layout off the screen

I need to reach the following result:
My RelativeLayout overlay initially should look like that . So only 20% of the layout should be seen.
when Button is pressed i apply Animation so that layout goes up and looks the following
Overlay layout is placed in the parent RelativeLayout and has number of ImageButtons in it.
<RelativeLayout
android:id="#+id/overlay"
android:layout_width="match_parent"
android:layout_below="#+id/actionbar"
android:layout_height="wrap_content"
android:background="#drawable/backgroundfeedme"
android:clickable="true"
>
-----number of img buttons here --
</RelativeLayout>
I was trying to set up its initial position as follows
rlOverlay = (RelativeLayout) v.findViewById(R.id.overlay);
ViewTreeObserver vto = rlOverlay.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
WindowManager wm = (WindowManager) MainActivity.appContext.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
int height = display.getHeight(); // deprecated, my app is for API 2.1+
rlOverlay.layout(0, (int)(height*0.8), rlOverlay.getMeasuredWidth(), rlOverlay.getMeasuredHeight());
}
});
Unfortunately the code above doesnt affect position of the layout.
I also cant use marginTop, because it squeezes the layout.
So .. how to position my layout so that only 20% of it is visible initially?
Thanks.
i had same requirment. what i did is put two relative layout. in first layout took button and below it second layout with rest of my views and Visiblity = gone.
from code: when i click on Button i made seond layout visiblity = Visible and animate it.
here's code:
<RelativeLayout
android:id="#+id/llShopping"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/bar_green" >
<Button
android:id="#+id/btnShoppingListAddItem"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/bar_green"
android:gravity="center"
android:paddingLeft="5dp"
android:text="Add Item"
android:textColor="#android:color/white"
android:textSize="20sp"
android:textStyle="bold" />
<RelativeLayout
android:id="#+id/relAddItemChild"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/btnShoppingListAddItem"
android:layout_marginBottom="10dp"
android:visibility="gone" >
<View
android:id="#+id/gl"
android:layout_width="200dp"
android:layout_height="1dp"
android:layout_centerHorizontal="true"
android:layout_marginTop="1dp"
android:background="#android:color/white" />
<EditText
android:id="#+id/edtItemName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/gl"
android:layout_centerHorizontal="true"
android:layout_marginTop="5dp"
android:hint="Item Name..." />
<Button
android:id="#+id/btnSpinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/edtItemName"
android:layout_centerHorizontal="true"
android:text="Please pick store" />
<Button
android:id="#+id/btnAddToList"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/btnSpinner"
android:layout_centerHorizontal="true"
android:layout_marginBottom="20dp"
android:layout_marginTop="15dp"
android:background="#drawable/btn_blue"
android:padding="5dp"
android:text="Add To List"
android:textColor="#android:color/white"
android:textSize="20sp" />
</RelativeLayout>
</RelativeLayout>

Categories

Resources