I'm working with a class that extends Dialog. It should display a progressbar element. Sometimes there will be a message, sometimes there isn't.
The progressbar shows when there is no message but does not show when there is a message and I do not understand why.
Here is the layout code:
<TextView
android:id="#+id/dialog_message"
style="#style/arial"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="20dp"
android:layout_marginLeft="100dp"
android:layout_marginRight="100dp"
android:layout_marginBottom="20dp"
android:text=""
android:textAlignment="center"
android:textColor="#android:color/white"
android:textSize="18sp"
/>
<ProgressBar
android:id="#+id/progress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginBottom="30dp"
android:layout_marginLeft="100dp"
android:layout_marginRight="100dp"
android:layout_marginTop="10dp"
android:visibility="gone" />
Here is the java:
public void progressDialog () {
if(this.message.getText().length() > 0){
message.setVisibility(View.VISIBLE);
}else{
message.setVisibility(View.GONE);
}
progress.setVisibility(View.VISIBLE);
leftButton.setVisibility(View.GONE);
rightButton.setVisibility(View.GONE);
}
When message is set GONE the progressbar shows, when message is set VISIBLE the progressbar does not show. I assume it is getting hidden by the message somehow.
I've tried moving it around in the layout as this question might suggest was needed ProgressBar not displaying, but to no avail.
How do I get the progressbar to always show when calling progressDialog()?
Edit***** Added full layout xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/dialog_background"
android:orientation="vertical">
<TextView
android:id="#+id/dialog_title"
style="#style/arialBold"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginBottom="5dp"
android:layout_marginTop="35dp"
android:ellipsize="end"
android:maxLines="1"
android:text="#string/waiting"
android:textColor="#android:color/white"
android:textSize="21sp" />
<TextView
android:id="#+id/dialog_message"
style="#style/arial"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="30dp"
android:text=" "
android:textAlignment="center"
android:textColor="#android:color/white"
android:textSize="20sp" />
<ProgressBar
android:id="#+id/progress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginBottom="30dp"
android:layout_marginLeft="100dp"
android:layout_marginRight="100dp"
android:layout_marginTop="30dp"
android:visibility="gone" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginBottom="30dp"
android:orientation="horizontal">
<LinearLayout
android:id="#+id/dialog_button_one"
style="#android:style/Widget.Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="#color/purple"
android:orientation="horizontal">
<TextView
android:id="#+id/dialog_button_one_text"
style="#style/arialBold"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:layout_marginLeft="#dimen/alert_dialog_button_margin_left_right"
android:layout_marginRight="#dimen/alert_dialog_button_margin_left_right"
android:layout_marginTop="10dp"
android:ellipsize="end"
android:maxLines="1"
android:textColor="#android:color/white"
android:textSize="20sp" />
</LinearLayout>
<LinearLayout
android:id="#+id/dialog_button_two"
style="#android:style/Widget.Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginStart="20dp"
android:background="#color/purple"
android:orientation="horizontal">
<TextView
android:id="#+id/dialog_button_two_text"
style="#style/arialBold"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:layout_marginLeft="#dimen/alert_dialog_button_margin_left_right"
android:layout_marginRight="#dimen/alert_dialog_button_margin_left_right"
android:layout_marginTop="10dp"
android:ellipsize="end"
android:maxLines="1"
android:textColor="#android:color/white"
android:textSize="20sp" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
I think you are using linear layout. So use Relative Layout instead of linear layout.
Add a parent constraint layout to your linear layout and move progress bar out of linear layout.
<constraint>
<linear/>
<progress/>
</constraint>
Some thing like this:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/dialog_background"
android:orientation="vertical"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
>
<!-- Your complete layout -->
</LinearLayout>
<ProgressBar
android:id="#+id/progress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:visibility="gone" />
</android.support.constraint.ConstraintLayout>
Ok, it looks like you use some wrong property for what you want, look:
android:layout_gravity="center" will put your TextView in center of the layout, but you also set it in your ProgressBar this mean that only one of then will be shown, it is only useful when you want to show one view at time, if you want to show both, try to remove one of this property.
Another thing, you set android:layout_margin="30dp" on TextView, so your progressBar will be far removed from TextView, which may also cause the invisibility of the ProgressBar, try 16dp.
<TextView
android:id="#+id/dialog_message"
style="#style/arial"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center" // remove here or in ProgressBar
android:layout_margin="16dp" // I just replace 30dp to 16dp
android:text=" "
android:textAlignment="center"
android:textColor="#android:color/white"
android:textSize="20sp" />
EDIT - Another Answer
I edit your full XML code, replacing some margin with padding, and show the progressBar always. You can now hide/show the textView or progressbar, it should work.
I also replace some property, since I have not it here, make sure to add again. Hope it help you.
I recommend to use RelativeLayout to improve your UI. Below I show the current XML behavior.
When ProgressBar is showed
The full source code on link below, I am unable to paste the code here.
https://gist.github.com/pedromassango/a6dc213823b297931f1ef2e6b6954466
Related
Here is my layout, Why is there a white line between the status bar and the title? How do I remove this white line。
I made an album, but found white lines in the nested layout. I didn't search for relevant content on Google, so I posted a post for you to look at. Here are my code and screenshots.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="#color/image_display_color">
<FrameLayout
android:id="#+id/surface_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center">
<com.github.chrisbanes.photoview.PhotoView
android:id="#+id/big_image"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>
<!-- toolbar -->
<LinearLayout
android:id="#+id/layout_top"
android:layout_width="match_parent"
android:layout_height="48dp"
android:layout_marginTop="30dp"
android:visibility="visible"
android:background="#80000000">
<ImageView
android:id="#+id/back"
android:layout_width="48dp"
android:layout_height="48dp"
android:contentDescription="TODO"
android:paddingLeft="10dp"
android:src="#drawable/video_back" />
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="48dp"
android:paddingLeft="10dp"
android:textColor="#android:color/white"
android:textSize="18sp" />
</LinearLayout>
<!-- 播放进度-->
<LinearLayout
android:background="#80000000"
android:id="#+id/layout_bottom"
android:layout_width="match_parent"
android:layout_height="50dp"
android:alpha="0.9"
android:layout_alignParentBottom="true"
android:layout_marginBottom="20dp"
android:gravity="center_vertical"
android:orientation="horizontal"
android:weightSum="5"
android:visibility="visible">
<Button
android:id="#+id/btn_share"
android:background="#android:color/transparent"
android:layout_width="wrap_content"
android:layout_marginStart="10dp"
android:text="分享"
android:textColor="#color/white"
android:drawableTop="#drawable/ic_baseline_share_24"
android:layout_weight="1"
android:layout_gravity="center_vertical"
android:layout_height="wrap_content" />
<Button
android:id="#+id/btn_favaite"
android:background="#android:color/transparent"
android:layout_weight="1"
android:text="收藏"
android:textColor="#color/white"
android:drawableTop="#drawable/ic_baseline_favorite_24"
android:layout_width="wrap_content"
android:layout_marginStart="10dp"
android:layout_gravity="center_vertical"
android:layout_height="wrap_content" />
<Button
android:id="#+id/btn_copy"
android:background="#android:color/transparent"
android:layout_weight="1"
android:textColor="#color/white"
android:text="复制"
android:drawableTop="#drawable/ic_copy_white"
android:layout_width="wrap_content"
android:layout_marginStart="10dp"
android:layout_gravity="center_vertical"
android:layout_height="wrap_content" />
<Button
android:id="#+id/btn_delete"
android:textColor="#color/white"
android:background="#android:color/transparent"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_marginStart="10dp"
android:text="删除"
android:drawableTop="#drawable/ic_baseline_delete_24"
android:layout_gravity="center_vertical"
android:layout_height="wrap_content" />
<Button
android:id="#+id/btn_more"
android:background="#android:color/transparent"
android:layout_weight="1"
android:textColor="#color/white"
android:layout_width="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginRight="10dp"
android:layout_marginStart="10dp"
android:drawableTop="#drawable/ic_baseline_more_vert_24"
android:text="更多"
android:layout_height="wrap_content" />
</LinearLayout>
</RelativeLayout>
Here is my screenshot。
image1
image2
The "line" is because your dark overlay doesn't exactly line up with the top of your content/bottom of the status bar. There's a gap where the content shows through:
You can see the photos through that gap. I'm assuming this layout is overlaid on the entire screen, and you're using layout_marginTop="30dp" to try and move it below the status bar? That's going to have problems - the size of the bar depends on the device. On stock Android (following Material Design guidelines) the status bar is 24dp. On your device, it's obviously close to 30dp - but not exactly! That's why it doesn't line up.
I think making this work perfectly is going to be difficult. If I were you, I wouldn't try to overlay the entire screen. If you use a NoActionBar theme, and add a Toolbar into your layout instead, then you can add your overlay in the same layout. You can cover the Toolbar if you like, and the top of the Activity/Fragment will be below the status bar.
But it looks like you're making some kind of overlay over other apps - in that case, have a look at this thread for some ideas about getting the current device's status bar height (if any): Height of status bar in Android
I am new to android and am trying to create a list view row item as follows:
On the far left of the listview item I need two stacked text views
In the center of the listview item I need an additional two stacked text view
On the far right I need a checkbox
I want to make the width of the center textviews a constant size, irrespective to the length of text.
Every time I adjust the middle linear layout, it pushes the checkbox off the screen unless I manually give it a width (which will not work for multiple screen sizes). Can someone please help?
I've tried using weights and all but I can't figure it out.
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:gravity="left"
android:orientation="horizontal">
<LinearLayout
android:id="#+id/left"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical">
<TextView
android:id="#+id/starttime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="0dp"
android:layout_marginTop="5dp"
android:padding="2dp"
android:singleLine="true"
android:text="10:35am"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#929692"
android:textSize="12sp" />
<TextView
android:id="#+id/endtime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="0dp"
android:singleLine="true"
android:text="11:55am"
android:textColor="#929692"
android:textSize="12sp" />
</LinearLayout>
<LinearLayout
android:id="#+id/middle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="0dp"
android:layout_toRightOf="#id/left"
android:orientation="vertical">
<TextView
android:id="#+id/sessionname"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="5dp"
android:ellipsize="end"
android:padding="2dp"
android:singleLine="true"
android:text="ok this is where my text goes it is very long
and it will push stuff off"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#000000"
android:textSize="14sp" />
<TextView
android:id="#+id/location"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:ellipsize="end"
android:singleLine="true"
android:text="#android:string/ok"
android:textColor="#929692" />
</LinearLayout>
<LinearLayout
android:id="#+id/right"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="0dp"
android:layout_toRightOf="#id/middle"
android:orientation="vertical">
<CheckBox
android:id="#+id/list_checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="right"
android:checked="false" />
</LinearLayout>
</RelativeLayout>
Used constraintlayout and it worked!
I have a "problem" with my application and I don't know what has happened.
I have this interface:
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="1dp"
android:orientation="vertical" >
<Button
android:layout_width="fill_parent"
android:layout_height="50dp"
android:background="#color/white"
android:gravity="center"
android:textColor="#color/black" />
<TextView
android:id="#+id/detailServiceTxtPhone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="-35dp"
android:ellipsize="end"
android:gravity="center"
android:maxLength="50"
android:singleLine="true"
android:textColor="#color/black" />
</LinearLayout>
I get the field value from a web service and set text in java code:
m_txtPhone.setText(m_currentService.PassengerPhone);
In all the devices where I check it this is the result:
But I have a problem in with Samsung J5 (5.1.1). When I try to set text in that TextView (and similars) I have this result:
It seems that I have some problem setting the text or I don't know. Any idea?
NOTE:
I implement the interface in this way because in other cases I do something like this:
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="1dp"
android:orientation="vertical" >
<Button
android:id="#+id/detailServiceBtnClient"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:background="#color/white"
android:drawableRight="#drawable/icon_detail"
android:gravity="left|center_vertical"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:text="#string/detailServiceBtnClient"
android:textColor="#color/black" />
<TextView
android:id="#+id/detailServiceTxtClient"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_marginRight="25dp"
android:layout_marginTop="-35dp"
android:ellipsize="end"
android:gravity="center"
android:maxLength="50"
android:singleLine="true"
android:textColor="#color/black" />
</LinearLayout>
You are using LinearLayout. In this layout items are added after each element. Your orientation is vertical, hence, it is below the button. Use RelativeLayout and then use alignTop and alignBottom properties of TextView.
android:alignTop="#+id/button"
android:alignBottom="#+id/button"
android:gravity="center"
<LinearLayout
android:id="#+id/layout_information"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="0.17"
android:gravity="center"
android:orientation="horizontal" >
<TextView
android:id="#+id/text_view_destination"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="\# Home in"
android:textColor="#color/solid_white"
android:textSize="19sp"
android:textStyle="bold"
android:layout_gravity="bottom"
/>
<TextView
android:id="#+id/text_view_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:textColor="#color/solid_white"
android:textSize="25sp"
android:textStyle="normal"
android:layout_gravity="bottom"
/>
</LinearLayout>
I want to center the two textViews vertically,
but I want them to be aligned to one line at their bottom.
Now they are alined to the same bottom,
but they are at the bottom of their parent layout.
How can I do this?
I have thought to wrap their current layout parent ("parent1") with another layout
("parent2")
and make "parent1" be in the center of "parent2".
Is there another way without adding elements?
If your problem is that both views are aligned at the bottom of their parent you can use this.
Both views are still aligned to the same bottom and centered on the screen.
Your layout looks like a part of a bigger XML (weight=0.17), that is why I have used fill_parent on layout_width.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/layout_information"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="0.17"
android:gravity="center"
android:orientation="horizontal" >
<TextView
android:id="#+id/text_view_destination"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:text="\# Home in"
android:textColor="#color/solid_white"
android:textSize="19sp"
android:textStyle="bold" />
<TextView
android:id="#+id/text_view_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:text="\# Home in"
android:textColor="#color/solid_white"
android:textSize="25sp"
android:textStyle="normal"
android:layout_alignParentBottom="true"
android:layout_toRightOf="#id/text_view_destination"
android:layout_alignBaseline="#id/text_view_destination" />
</RelativeLayout>
try this way
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/layout_information"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:gravity="center_vertical|bottom"
android:orientation="horizontal" >
<TextView
android:id="#+id/text_view_destination"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="HOme in"
android:textColor="#color/solid_white"
android:textSize="19sp"
android:textStyle="bold" />
<TextView
android:id="#+id/text_view_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:textColor="#color/solid_white"
android:textSize="25sp"
android:text="Home out"
android:textStyle="normal" />
</LinearLayout>
I have a custom DialogPreference with my own layout defined. The layout file is as follows:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingTop="5dp"
android:paddingLeft="10dp"
android:paddingRight="10dp" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/enter_password_label"
android:textAppearance="?android:attr/textAppearanceLarge" />
<EditText
android:id="#+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword">
<requestFocus />
</EditText>
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/confirm_password_label"
android:textAppearance="?android:attr/textAppearanceMedium"
/>
<EditText
android:id="#+id/editText2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword"
/>
On the displayed dialog both TextViews are missing:
What is the reason of TextViews not being displayed?
One of the reason for this behavior is due to manufacturer's customization of themes. Other reasons could be that the selected text appearance has same color as of background. To avoid this, you may simply set a color to your TextView, for e.g.:
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/enter_password_label"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#000000" />
*Also make sure android:text does have a text to show on screen