Background: I'm making an application that can creates timed based alarms, location based or both and in the activity that creates the alarm I have checkbox for each- timepicker and map and you uncheck it if you don't want one of these features in your alarm. Here's a picture of this activity-http://prntscr.com/dr1s74 .
If you uncheck the map checkbox it removes the fragment and sets layout params of the radioGroup to below the title (because the map is gone now), center it and to add margin.
While addRule(RelativeLayout.BELOW, id) works, both setMargins(left, top, right, bottom) and addRule(RelativeLayout.CENTER_HORIZONTAL) doesn't work and because of that the radioGroup is off center and without the correct space below the title.
XML Activity:
Description of the possible problem: The only reason I can think it happens is that I have CoordinatorLayout as the parent layout (because its interaction between FAB and SnackBar) and ScrollView inside it containing RelativeLayout (Open to suggestions if you have a better way). And that for some reason makes problems when trying to add rules to layout params.
XML Code (Removed the radioButtons and CheckBox for easier viewing):
<android.support.design.widget.FloatingActionButton
android:id="#+id/saveAlarm"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|right"
android:layout_marginBottom="#dimen/fab_margin"
android:layout_marginRight="#dimen/fab_margin"
android:onClick="fabClicked"
app:srcCompat="#android:drawable/ic_menu_save" />
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/relative_layout_alarm_creator"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:focusable="true"
android:focusableInTouchMode="true"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.rome.locationalarm.AlarmCreator">
<EditText
android:id="#+id/alarmTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="sans-serif"
android:hint="Alarm title" />
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="150dp"
android:layout_below="#+id/alarmTitle"
android:layout_marginTop="10dp"
tools:context="com.example.rome.locationalarm.MapsFragment" />
<com.example.rome.locationalarm.CustomTimePicker
android:id="#+id/timePicker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_below="#+id/days"
android:layout_marginTop="10dp" />
<TextView
android:id="#+id/chooseLocation"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/map"
android:layout_centerHorizontal="true"
android:text="Choose Location" />
<RadioGroup
android:id="#+id/days"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/map"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:orientation="horizontal">
</RadioGroup>
</RelativeLayout>
</ScrollView>
JAVA Code (Only the important part):
if(locationCheckBox.isChecked()){
//If user wants location
RelativeLayout.LayoutParams layoutParams= new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
layoutParams.addRule(RelativeLayout.BELOW, mapFragment.getId());
//All of these don't work
layoutParams.setMargins(0, 10, 0, 0); // (left, top, right, bottom)
//All of these are tries to center the radioGroup
layoutParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
layoutParams.addRule(RelativeLayout.ALIGN_PARENT_START);
layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT);
layout.removeView(radioGroup);
layout.addView(radioGroup, layoutParams);
//layout.updateViewLayout(radioGroup, layoutParams); doesn't work as well.
Thank you for taking your time trying to help, have a nice year!
you need to do this to get the LayoutParams
RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) getLayoutParams();
Related
I am developing an Android quiz game.
When a user replies to the first question I open this dialog to show its progress:
This is the layout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<View
android:layout_width="300dp"
android:layout_height="wrap_content"
android:layout_alignBottom="#id/txtLevel1"
android:layout_alignTop="#+id/txtLevel1"
android:layout_centerInParent="true"
android:background="#color/orange" />
<TextView
android:id="#+id/txtLevel3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="5.000" />
<TextView
android:id="#+id/txtLevel2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/txtLevel3"
android:text="2.500"/>
<TextView
android:id="#+id/txtLevel1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/txtLevel2"
android:text="1.000"/>
</RelativeLayout>
Is there any way to move the View element (the orange box) from txtLevel1 (1.000 points) to txtLevel2 (2.500 points) maybe with an animation/transition and not statically?
You could try this to move the orange box from txtLevel1 to txtLevel2:
View orangeBox = getActivity().findViewById(R.id.orange_box);
TextView tv = getActivity().findViewById(R.id.txtLevel2);
RelativeLayout.LayoutParams p = new RelativeLayout.LayoutParams(300, RelativeLayout.LayoutParams.WRAP_CONTENT);
p.addRule(RelativeLayout.ALIGN_BOTTOM, tv.getId());
p.addRule(RelativeLayout.ALIGN_TOP, tv.getId());
orangeBox.setLayoutParams(p);
For the animation this might help you.
I have a vertical LinearLayout with 2 TextSwitcher inside. Sometimes only the first one(#+id/ts1) will show, sometimes both of them will show on screen. The font size for ts1 is 20, for ts2 is 16.
<LinearLayout
android:id="#+id/linearLayout1"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:gravity="center_vertical"
android:focusable="false"
android:layout_marginLeft="#dimen/dimen_left1"
android:visibility="gone">
<TextSwitcher
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:layout_gravity="center_vertical"
android:id="#+id/ts1"/>
<TextSwitcher
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:layout_gravity="center_vertical"
android:id="#+id/ts2"/>
</LinearLayout>
When I tested it, when both of them showed on screen, it worked fine, but when there's only ts1 shown, the text is not centered vertically, it's more like on the top vertically instead of centered. I programmatically set the visibility of these 2 TextSwitchers.
Does anyone know why this happens?
Thanks!!!
Setting the layout_gravity on TextSwitchers does not change the text android:gravity of the underlying TextViews. You can override the default TextViews that TextSwitchers use by putting them in your XML. From there, you can set the android:layout_gravity or android:gravity on them to center the text vertically. This should work:
<TextSwitcher
android:id="#+id/ts1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center" />
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center" />
</TextSwitcher>
<TextSwitcher
android:id="#+id/ts2"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center" />
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center" />
</TextSwitcher>
You need to add gravity option to TextView, not the TextSwitcher.
For example, you can add layout_width, layout_height, and gravity option dynamically.
mSwitcher1 = (TextSwitcher) findViewById(R.id.textSwitcher1);
mSwitcher1.setFactory(new ViewSwitcher.ViewFactory() {
public View makeView() {
TextView myText = new TextView(MainActivity.this);
myText.setGravity(Gravity.TOP | Gravity.CENTER_HORIZONTAL);
myText.setTextSize(36);
myText.setTextColor(Color.BLUE);
// Add this
myText.setGravity(Gravity.CENTER);
// Add this
myText.setLayoutParams(new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT));
return myText;
}
});
Have you tried using the ConstraintLayout? Using it may help you out. If you give more information on what the behavior of everything needs to be, I can give you a code example.
I'm trying to change the height of RelativeLayout as user scrolls, so it smoothly moves up. It's kinda working but its all flickering and glitching.
I tried to use setTranslationY, but that doesn't move the scrollview with the RelativeLayout.
int top = mScrollView.getScrollY();
int maxRelative = formulas.dpToPx(250);
int actualRelative = (int)((1.0-((float)top)/300.0)*((float)maxRelative));
RelativeLayout.LayoutParams layout_description = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,
actualRelative);
mCollapsingLayout.setLayoutParams(layout_description);
mCollapsingLayout.requestLayout();
This is how it looks
http://imgur.com/7PL6Yt5
If you have any better idea how to shrink the RelativeLayout as you scroll, its appreciated.
Edit: here's my xml
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="#dimen/app_bar_height"
android:id="#+id/collapsing.view">
<ImageView
android:id="#+id/collapsing.view.image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="#drawable/theflash"
android:tint="#7F000000"
android:scaleType="centerCrop"/>
<ImageButton
android:id="#+id/favorite.button"
android:layout_width="70dp"
android:layout_height="70dp"
android:background="?android:attr/selectableItemBackground"
android:scaleType="fitCenter"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
android:layout_marginBottom="30dp"
android:src="#drawable/favorite_button"
/>
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:paddingBottom="30px">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="The Flash"
android:textColor="#fff"
android:textSize="25sp"
android:layout_centerInParent="true"
/>
</FrameLayout>
</RelativeLayout>
<ScrollView
android:id="#+id/detail.scrollview"
android:layout_below="#+id/collapsing.view"
android:layout_width="match_parent"
android:fillViewport="true"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
</LinearLayout>
</ScrollView>
There are several approaches to this, including readily available classes such as this one which basically does what you want. You may need to remove the parts for the shrinking text and so forth, but it's right where you need it
You need to place your RelativeLayout inside the ScrollView and try setTranslationY again - it should work
I have a Linear layout then programatically I'm adding some spinners and buttons and so on, but I have xml button Wrap content (width) and then on java I add spinner (or anything else) and it goes below this view even if both views are wrap content:
progBar = new ProgressBar(this);
pBarToca = new ProgressBar(this);
pBarToca.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
linToca = (LinearLayout) findViewById(R.id.tetoca);
linToca.addView(pBarToca);
and it's placed under the button of xml:
<LinearLayout android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" android:layout_marginTop="6dp"
android:id="#+id/tetoca">
<TextView style="#style/StylePartida"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/te_toca_jugar" />
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#A7E9A9" android:onClick="callJugar"
android:text="#string/jugar" />
</LinearLayout>
edit!!!!!!
I want textview on first line then on next line button + progressbar (for example)
You have android:orientation=vertical so the Views will be laid out starting at the top and going down.
If you want them to all be next to each other, remove that from your xml since the default orientation for a LinearLayout is horizontal. If you do this, you will obviously need to change the android:width to wrap_content for your TextView or else it will take up the entire screen.
After your comment, a RelativeLayout would work best here.
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<TextView
style="#style/StylePartida"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/te_toca_jugar"
android:id="#+id/tvID" /> // give it an id
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="6dp"
android:id="#+id/tetoca"
android:layout_below="#/id=tvID"> // place it below the TV
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#A7E9A9" android:onClick="callJugar"
android:text="#string/jugar" />
</LinearLayout>
</RelativeLayout>
Note the changes in the comments. Now when you add your progressbar to the LL, it should be next to the Button. You may need some changes but this should give you approximately what you want.
<LinearLayout android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="6dp"
android:id="#+id/tetoca">
<TextView style="#style/StylePartida"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/te_toca_jugar"
android:text="#string/te_toca_jugar" />
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#A7E9A9" android:onClick="callJugar"
android:text="#string/jugar" />
</LinearLayout>
In your textView you are matching the parent
android:layout_width="match_parent"
This will cause the textview to take up the entire width of the parent view.
android:layout_width="wrap_content"
and
android:orientation="horizontal"
android:orientation="vertical" will cause the elements to be stacked.
If you are using "horizontal" it's important not to have a child element with width matching parent.
EDIT:
After OPs change to question:
I have used a textview, two buttons and listview to give you an idea of how you can format it. There are many ways to achieve the same thing, this is one suggestion.
The internal linearlayout has a horizontal orientation (by default).
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="6dp"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="te_toca_jugar"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#A7E9A9"
android:text="jugar"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#A7E9A9"
android:text="jugar2"/>
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/lv">
</ListView>
</LinearLayout>
</LinearLayout>
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.