How to manage width of a custom view with layout using ConstraintLayout? - java

I have a fragment and I'm using an accordion view in it, which I downloaded form GitHub - https://github.com/riyagayasen/Android_accordion_view, and in its body (which is a RelativeLayout) I need a custom view named AccordionItem like the second one on the pic, which I simply made in fragment's xml:
https://pp.userapi.com/c846123/v846123865/1d925d/CaEhr8uSLFA.jpg
But my custom view (the first one) has a wrap_content width behavior for some reason.
The code follows:
Here is the body of an AccordionView in my fragment's xml
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<!-- This is my custom view -->
<!-- Notice that the width is set to match_constraint -->
<com.app.myapp.views.AccordionItem
android:id="#+id/item"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
app:isStarred="true"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:title="Another title" />
<!-- This is an example of how my view should look like -->
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:background="#drawable/accordion_item"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/item">
<CheckBox
android:id="#+id/favourite_checkBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:button="#drawable/star_checkbox"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="#+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#drawable/ic_t" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:text="Title"
android:textSize="18sp"
app:layout_constraintBottom_toBottomOf="#+id/imageView2"
app:layout_constraintStart_toEndOf="#+id/imageView2"
app:layout_constraintTop_toTopOf="#+id/imageView2" />
</android.support.constraint.ConstraintLayout>
</android.support.constraint.ConstraintLayout>
The xml of the custom view:
<?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="wrap_content"
android:background="#drawable/accordion_item">
<CheckBox
android:id="#+id/favourite_checkBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:button="#drawable/star_checkbox"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toEndOf="#+id/title"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="#+id/item_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#drawable/ic_t" />
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:text="Title"
android:textSize="18sp"
app:layout_constraintBottom_toBottomOf="#+id/item_icon"
app:layout_constraintStart_toEndOf="#+id/item_icon"
app:layout_constraintTop_toTopOf="#+id/item_icon" />
</android.support.constraint.ConstraintLayout>
Java class of the custom view:
public class AccordionItem extends ConstraintLayout {
private String titleString;
private TextView title;
private boolean isStarred;
public AccordionItem(Context context) {
super(context);
initView();
}
public AccordionItem(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray a = context.getTheme().obtainStyledAttributes(
attrs,
R.styleable.AccordionItem,
0, 0);
try {
titleString = a.getString(R.styleable.AccordionItem_title);
isStarred = a.getBoolean(R.styleable.AccordionItem_isStarred, false);
} finally {
a.recycle();
}
initView();
}
private void initView() {
View view = inflate(getContext(), R.layout.accordion_item, null);
addView(view);
}
public void setTitleString(String titleString) {
this.titleString = titleString;
invalidate();
requestLayout();
}
public String getTitleString() {
return titleString;
}
public void setStarred(boolean starred) {
this.isStarred = starred;
invalidate();
requestLayout();
}
public boolean isStarred() {
return isStarred;
}
}
And finally the attributes of my view:
<declare-styleable name="AccordionItem">
<attr name="title" format="string"/>
<attr name="isStarred" format="boolean"/>
<attr name="background_drawable" format="reference"/>
</declare-styleable>
I expect my view to take the whole width it is given, but it shrinks instead.
Am I using the wrong way of creating views, or the wrong layout that my view extends?
Thank you.

inflate method with no parent will lost layoutParams(height,width)
private void initView() {
inflate(getContext(), R.layout.accordion_item, this);
// or
// View view = LayoutInflater.from(getContext()).inflate(R.layout.accordion_item, this, false);
// addView(view);
}

You don't really need library for this simple layout, have a look at this example :
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Fragments.CheckoutScreen">
<!-- This is an example of how my view should look like -->
<CheckBox
android:id="#+id/favourite_checkBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
app:layout_constraintBottom_toBottomOf="#+id/textView4"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="#+id/textView4" />
<TextView
android:id="#+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:text="T"
android:textSize="18sp"
app:layout_constraintBottom_toBottomOf="#+id/textView4"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="#+id/textView4" />
<TextView
android:id="#+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:text="Title"
android:textSize="18sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toEndOf="#+id/textView5"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.383" />

Related

How can I change the color of my edit text which is entered by user?

I have created a signup page and I select the hint color blue now I want to change the color of text which is entered in the edit text please tell me how I can change the color of text
Here is the code of my XML file
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Signup"
android:background="#color/purple_500">
<LinearLayout
android:id="#+id/linearLayout2"
android:layout_width="match_parent"
android:layout_height="660dp"
android:layout_marginTop="80dp"
android:background="#drawable/linear_round"
android:orientation="vertical"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
</LinearLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:text="Please Signup..."
android:textColor="#color/white"
android:textStyle="bold"
android:textSize="29sp"
android:gravity="center"
android:layout_marginTop="16dp"
android:id="#+id/textView">
</TextView>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:textColor="#color/black"
android:hint="Enter your name"
android:layout_marginTop="250dp"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:gravity="center"
android:textColorHint="#color/purple_500"
android:id="#+id/name"/>
<EditText
android:id="#+id/pass"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:gravity="center"
android:hint="Enter your password"
android:inputType="textPassword"
android:textColor="#color/black"
android:textColorHint="#color/purple_500"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/name" />
<EditText
android:id="#+id/email"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:gravity="center"
android:hint="Enter your valid e-mail"
android:inputType="textEmailAddress"
android:textColor="#color/black"
android:textColorHint="#color/purple_500"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/pass" />
<EditText
android:id="#+id/number"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:gravity="center"
android:hint="Enter your cell number"
android:inputType="phone"
android:textColor="#color/black"
android:textColorHint="#color/purple_500"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/email" />
<com.airbnb.lottie.LottieAnimationView
android:id="#+id/submit"
android:layout_width="445dp"
android:layout_height="163dp"
android:layout_centerInParent="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/number"
android:layout_marginBottom="100dp"
app:lottie_autoPlay="true"
app:lottie_loop="true"
app:lottie_rawRes="#raw/submit" />
<com.airbnb.lottie.LottieAnimationView
android:id="#+id/trial"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_centerInParent="true"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/submit"
app:lottie_autoPlay="true"
app:lottie_loop="true"
app:lottie_rawRes="#raw/trial" />
<com.airbnb.lottie.LottieAnimationView
android:id="#+id/signup_animation"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_centerInParent="true"
app:layout_constraintBottom_toTopOf="#+id/name"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="70dp"
app:lottie_autoPlay="true"
app:lottie_loop="true"
app:lottie_rawRes="#raw/signup" />
</androidx.constraintlayout.widget.ConstraintLayout>
And how can I add the marquee effect on my text view? Can I make text view scrolling from right to left?
If TextView fill whole width you need to add these lines of code
<TextView
.
.
android:singleLine="true"
android:ellipsize="marquee"
android:marqueeRepeatLimit="marquee_forever"
/>
also do not forget to set selected for TexrtView as below
textview.setSelected(true);
and if TextView does not fill width the only thing is to call this method and pass TextView to it.
Kotlin
fun addMarquee(textView: TextView) {
textView.viewTreeObserver.addOnGlobalLayoutListener(object :
ViewTreeObserver.OnGlobalLayoutListener {
override fun onGlobalLayout() {
val pixels = textView.measuredWidth - 1
val params = textView.layoutParams
params.width = pixels
textView.layoutParams = params
textView.isSelected = true
textView.ellipsize = TextUtils.TruncateAt.MARQUEE
textView.isSingleLine = true
textView.marqueeRepeatLimit = -1
textView.viewTreeObserver.removeOnGlobalLayoutListener(this)
}
})
}
Java
public void addMarquee(TextView textView) {
textView.getViewTreeObserver().addOnGlobalLayoutListener(
new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
int pixels = textView.getMeasuredWidth() - 1;
ViewGroup.LayoutParams params = textView.getLayoutParams();
params.width = pixels;
textView.setLayoutParams(params);
textView.setSelected(true);
textView.setEllipsize(TextUtils.TruncateAt.MARQUEE);
textView.setSingleLine(true);
textView.setMarqueeRepeatLimit(-1);
textView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
}
});
}
NOTE: this method only work when textView width is wrap_content.
textColor attribute changes color of text which user entered in.
android:textColor="#color/black"
HorizontalScrollView has one chid view, and it can be scrolled horizontally.
There's many ways to move view.
Search with keyword like
move view animation android or move view programmatically android

Why is my layout not filling the screen in landscape mode?

I have the following layout for a video player:
video_view.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<com.google.android.exoplayer2.ui.AspectRatioFrameLayout
android:id="#+id/aspectRatioLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center">
<SurfaceView
android:id="#+id/surfaceView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<ImageView
android:id="#+id/posterImage"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:contentDescription="#string/poster_image_description" />
<com.google.android.exoplayer2.ui.SubtitleView
android:id="#+id/subtitleView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<com.google.android.exoplayer2.ui.PlayerControlView
android:id="#+id/nativeControls"
android:layout_width="match_parent"
android:layout_height="match_parent">
</com.google.android.exoplayer2.ui.PlayerControlView>
<com.sdk.adapters.controls.ControlBar
android:id="#+id/controls"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</com.sdk.adapters.controls.ControlBar>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="#+id/messageOverlay"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="#80000000">
<TextView
android:id="#+id/messageOverlayText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:text="#string/message_overlay"
android:textColor="#android:color/white"
android:textSize="30sp" />
</FrameLayout>
</FrameLayout>
</com.google.android.exoplayer2.ui.AspectRatioFrameLayout>
</FrameLayout>
The ControlBar is defined as:
control_bar_view.xml
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#80000000">
<!-- BEGIN: Strut -->
<View
android:id="#+id/exitPlaceholder"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintWidth_percent="0.1" />
<!-- END: Strut -->
<TextView
android:id="#+id/broadcastName"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:text="#string/broadcast_name"
android:textColor="#ffffff"
android:textSize="18sp"
app:layout_constraintEnd_toStartOf="#id/castButton"
app:layout_constraintStart_toEndOf="#id/exitPlaceholder"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/siteName"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:text="#string/site_name"
android:textColor="#ffffff"
android:textSize="18sp"
app:layout_constraintEnd_toStartOf="#id/castButton"
app:layout_constraintStart_toEndOf="#id/exitPlaceholder"
app:layout_constraintTop_toBottomOf="#id/broadcastName" />
<ImageButton
android:id="#+id/castButton"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_margin="10dp"
android:contentDescription="#string/send_to_google_chromecast"
android:visibility="invisible"
app:layout_constraintDimensionRatio="1:1"
app:layout_constraintEnd_toStartOf="#id/airplayButton"
app:layout_constraintHeight_percent="0.1"
app:layout_constraintTop_toTopOf="parent" />
<ImageButton
android:id="#+id/airplayButton"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_margin="10dp"
android:contentDescription="#string/send_to_apple_airplay"
android:visibility="invisible"
app:layout_constraintDimensionRatio="1:1"
app:layout_constraintEnd_toStartOf="#id/optionsButton"
app:layout_constraintHeight_percent="0.1"
app:layout_constraintTop_toTopOf="parent" />
<ImageButton
android:id="#+id/optionsButton"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_margin="10dp"
android:background="#android:color/transparent"
android:contentDescription="#string/fullscreen"
android:scaleType="fitStart"
android:src="#drawable/closed_caption_gray"
app:layout_constraintDimensionRatio="1:1"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHeight_percent="0.1"
app:layout_constraintTop_toTopOf="parent" />
<ImageButton
android:id="#+id/skipBackButton"
android:layout_width="0dp"
android:layout_height="0dp"
android:background="#android:color/transparent"
android:contentDescription="#string/skip_back_button"
android:scaleType="fitStart"
android:src="#drawable/back_30"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintDimensionRatio="1:1"
app:layout_constraintEnd_toStartOf="#id/playPauseToggle"
app:layout_constraintHeight_percent="0.20"
app:layout_constraintHorizontal_chainStyle="spread"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageButton
android:id="#+id/playPauseToggle"
android:layout_width="0dp"
android:layout_height="0dp"
android:background="#android:color/transparent"
android:contentDescription="#string/play_button"
android:scaleType="fitStart"
android:src="#drawable/play"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintDimensionRatio="1:1"
app:layout_constraintEnd_toStartOf="#id/skipForwardButton"
app:layout_constraintHeight_percent="0.25"
app:layout_constraintStart_toEndOf="#id/skipBackButton"
app:layout_constraintTop_toTopOf="parent" />
<ImageButton
android:id="#+id/skipForwardButton"
android:layout_width="0dp"
android:layout_height="0dp"
android:background="#android:color/transparent"
android:contentDescription="#string/skip_forward_button"
android:scaleType="fitStart"
android:src="#drawable/forward_30"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintDimensionRatio="1:1"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHeight_percent="0.20"
app:layout_constraintStart_toEndOf="#id/playPauseToggle"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/currentTime"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_margin="10dp"
android:text="#string/null_time"
android:textColor="#ffffff"
android:textSize="18sp"
app:layout_constraintBottom_toTopOf="#id/seekBar"
app:layout_constraintStart_toStartOf="parent" />
<TextView
android:id="#+id/remainingTime"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_margin="10dp"
android:text="#string/null_time"
android:textColor="#ffffff"
android:textSize="18sp"
app:layout_constraintBottom_toTopOf="#id/seekBar"
app:layout_constraintEnd_toEndOf="parent" />
<SeekBar
android:id="#+id/seekBar"
android:layout_width="0dp"
android:layout_height="0dp"
android:progress="50"
android:progressDrawable="#drawable/seek_bar"
android:thumb="#drawable/seek_thumb"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="#id/fullscreenButton"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="#id/fullscreenButton"
app:layout_constraintVertical_bias="0.0" />
<ImageButton
android:id="#+id/fullscreenButton"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_margin="10dp"
android:background="#android:color/transparent"
android:contentDescription="#string/fullscreen"
android:scaleType="fitStart"
android:src="#drawable/fullscreen"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintDimensionRatio="1:1"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHeight_percent="0.1" />
</androidx.constraintlayout.widget.ConstraintLayout>
In the layout editor in Android Studio, this looks good:
However when I run an actual app, the ControlBar (and only the ControlBar) isn't filling the screen! Everything else stretches to fill the screen perfectly:
What am I doing wrong?
If it helps, the ControlBar is a custom View which extends ConstraintLayout. Here's what the initialization code looks like:
ControlBar.java
public class ControlBar extends ConstraintLayout
{
private BFPlayer player;
private TextView broadcastName;
// ... references for the various subviews ...
private ImageButton fullscreenButton;
public ControlBar ( Context context )
{
super(context);
init(context);
}
public ControlBar ( Context context, AttributeSet attrs )
{
super(context, attrs);
init(context);
}
public ControlBar ( Context context, AttributeSet attrs, int defStyleAttr )
{
super(context, attrs, defStyleAttr);
init(context);
}
private void init ( Context context )
{
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.bf_control_bar_view, null);
addView(view);
broadcastName = findViewById(R.id.broadcastName);
// ... grab references to the various buttons ...
fullscreenButton = findViewById(R.id.fullscreenButton);
// ... set up listeners for the various buttons ...
}
// ... The rest of the class ...
}
What am I doing wrong? I'm sure it's something simple, but I've been staring for a minute and can't figure it out. (Note: I'm pretty new to Android development, and not very experienced. My job has me changing platforms and programming languages very often)
You're not telling it what the parent is when you inflate your View, so it doesn't apply the LayoutParams correctly:
View view = inflater.inflate(R.layout.bf_control_bar_view, null);
addView(view);
Instead, tell it to inflate directly into the parent:
View view = inflater.inflate(R.layout.bf_control_bar_view, this);

DialogFragment is expanding the XML to full screen?

i've implemented a DialogFragment , but it comes out looking like this:
instead of this (from XML preview):
fragment's onViewCreated
val button = view.findViewById(R.id.button_register) as Button
button.setOnClickListener{
Log.d(TAG, "Clicked")
val dialog = SuccessDialog()
dialog.show(fragmentManager, "success dialog")
}
SuccessDialog class
class SuccessDialog: DialogFragment() {
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val view = inflater.inflate(R.layout.dialog_success, container, false)
val button: Button = view.findViewById(R.id.button_ok)
button.setOnClickListener{
dialog.dismiss()
}
return view
}
}
dialog's XML:
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="250dp"
android:layout_height="200dp"
android:background="#color/colorPrimaryDark"
android:padding="5dp"
android:layout_gravity="center">
<ImageView
android:id="#+id/imageView"
android:layout_width="47dp"
android:layout_height="46dp"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:srcCompat="#drawable/ic_check_mark_24dp"
android:contentDescription="#string/checkmark" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:text="#string/success"
android:textColor="#android:color/white"
android:textSize="18sp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/imageView" />
<TextView
android:id="#+id/textView6"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:gravity="center"
android:text="#string/thank_you"
android:textColor="#android:color/white"
android:textStyle="italic"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView3" />
<Button
android:id="#+id/button_ok"
android:layout_width="86dp"
android:layout_height="40dp"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="8dp"
android:text="#string/ok"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView6" />
</android.support.constraint.ConstraintLayout>
tools:...
is just for xml preview . So please use :
app:srcCompat="..."
instead of
tools:srcCompat="..."
To change the dimensions use this in onCreateView of your SuccessDialog
if (getDialog() != null && getDialog().getWindow() != null) {
getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
getDialog().getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
}

Make my custom view update text fields in editor

So im currently working on a custom card view and my goal is pretty easy. I want to update the textfield to the string I set in the custom card view xml.
But somehow I can't get it working after many hours of trying. Sometimes the editor won't load my xml with the custom card view... sometimes it gives me a extremely long error message... and sometimes it just stays black...
Therefore I have the following xml file :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/CallEntry_Holder"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:tag="0">
<com.test.larsmatthaus.projectnurser.customs.Custom_CardView
android:id="#+id/Call_Entry"
android:layout_width="match_parent"
android:layout_height="50dp"
app:cardBackgroundColor="#color/colorCardBackgroundR"
app:cardCornerRadius="5dp"
app:personName="test"
app:room="100"
app:ward="test"
tools:layout_editor_absoluteX="1024dp">
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="50dp">
<TextView
android:id="#+id/WardName"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginBottom="10dp"
android:layout_marginEnd="200dp"
android:layout_marginLeft="200dp"
android:layout_marginRight="200dp"
android:layout_marginStart="200dp"
android:layout_marginTop="10dp"
android:autoSizeTextType="uniform"
android:fontFamily="#font/comfortaa_light"
android:gravity="center"
android:text="Louisa Burgess"
android:textColor="#color/colorOverlayTC"
android:textSize="18sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="#+id/icon_2"
android:layout_width="31dp"
android:layout_height="0dp"
android:layout_marginBottom="8dp"
android:layout_marginLeft="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:adjustViewBounds="true"
android:scaleType="fitCenter"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="#+id/icon_1"
app:layout_constraintHorizontal_bias="0.85"
app:layout_constraintStart_toEndOf="#+id/WardName"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#drawable/teddybear" />
<ImageView
android:id="#+id/icon_1"
android:layout_width="31dp"
android:layout_height="0dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:adjustViewBounds="true"
android:scaleType="fitCenter"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.95"
app:layout_constraintStart_toEndOf="#+id/WardName"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#drawable/bed" />
<TextView
android:id="#+id/RoomText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="8dp"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
android:autoSizeTextType="uniform"
android:fontFamily="#font/comfortaa_light"
android:gravity="center"
android:text="105"
android:textColor="#color/colorOverlayTC"
android:textSize="24sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="#+id/WardName"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/seperator_C"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:fontFamily="#font/comfortaa_light"
android:text="/"
android:textColor="#color/colorOverlayTC"
android:textSize="24sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="#+id/RoomText"
app:layout_constraintEnd_toStartOf="#+id/WardName"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toEndOf="#+id/RoomText"
app:layout_constraintTop_toTopOf="#+id/RoomText" />
<TextView
android:id="#+id/WardText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:fontFamily="#font/anaheim"
android:gravity="center"
android:text="Ward 2"
android:textColor="#color/colorOverlayTC"
android:textSize="18sp"
app:layout_constraintBottom_toBottomOf="#+id/seperator_C"
app:layout_constraintEnd_toStartOf="#+id/WardName"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toEndOf="#+id/seperator_C"
app:layout_constraintTop_toTopOf="#+id/seperator_C" />
</android.support.constraint.ConstraintLayout>
</com.test.larsmatthaus.projectnurser.customs.Custom_CardView>
It contains just one card view entry.
My custom card view class looks like this :
public class Custom_CardView{
public Context context;
public Map<Integer, Object> vars = new HashMap<Integer, Object>();
public Custom_CardView(#NonNull Context context, #Nullable AttributeSet attrs) {
super(context, attrs);
this.context = context;
setWillNotDraw(false);
View view = getRootView();
for(int index = 0; index<((ViewGroup)view).getChildCount(); ++index) {
View nextChild = ((ViewGroup)view).getChildAt(index);
vars.put(nextChild.getId(), nextChild);
}
Log.println(Log.DEBUG,"Custom_CardView","<Custom_CardView : Instantiated => ["+vars.size()+"] children>");
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.elements_call_entry, this, true);
TextView wardText = (TextView) findViewById(R.id.WardName);
wardText.setText("TEST");
}
protected void onDraw(Canvas canvas){
super.onDraw(canvas);
}
}
I want to update the TextView "wardName" to the attribute value i did set in the xml file...
But somehow the editor view crashes once I load it... Due to the Custom CardView Class... Theres no real error log...
The only one I received was this : Preview timed out while rendering the layout. This typically happens when there is an infinite loop or unbounded recursion in one of the custom views
What did I forgot ? What else could I try to modify the editors textview ?
Well it was pretty easy... just added a new layout activity which will inflate once the CardView is pressed. I just copy the CardView element and place it inside the inflated view => problem solved :)

ExpandableListView children width

I have an ExpandableListView in my app. I want to make the child layout a bit narrower than the group header and also keep it centered. Due to some visual effects i must set layout params from code however i can't seem to find the best solutions for this tried changing layout width from xml with no luck. Any advice will be helpful.
Thanks
adapter getChild() method:
#Override
public View getChildView(final int groupPosition, final int
childPosition,
boolean isLastChild, View convertView,
ViewGroup parent) {
final Item expandedListitem = (Item) getChild(groupPosition, childPosition);
Drawable drawable= ContextCompat.getDrawable(context,R.drawable.background_border);
GradientDrawable gradientDrawable= (GradientDrawable) drawable;
gradientDrawable.setStroke(5,expandedListitem.getColor());
if (Build.VERSION.SDK_INT >= 19) {
if (convertView == null) {
LayoutInflater layoutInflater = (LayoutInflater) this.context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(R.layout.list_item, null);
}
if (!isLastChild) {
View divider = convertView.findViewById(R.id.linearfaq);
divider.setVisibility(View.INVISIBLE);
ViewGroup.LayoutParams params = convertView.getLayoutParams();
int width = (MainActivity.display.getWidth());
params.height = 110;
params.width=width-40;
params.addRule(RelativeLayout.CENTER_IN_PARENT);
params.addRule(RelativeLayout.CENTER_HORIZONTAL);
convertView.setLayoutParams(params);
View padder = convertView.findViewById(R.id.padder);
padder.setVisibility(View.INVISIBLE);
} else {
View divider = convertView.findViewById(R.id.linearfaq);
RelativeLayout.LayoutParams params = new
[![enter image description here][1]][1]RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
int width = (MainActivity.display.getWidth());
params.height = 140;
params.width=width-40;
params.addRule(RelativeLayout.CENTER_IN_PARENT);
params.addRule(RelativeLayout.CENTER_HORIZONTAL);
convertView.setLayoutParams(params);
divider.setVisibility(View.VISIBLE);
View padder = convertView.findViewById(R.id.padder);
padder.setBackgroundColor(expandedListitem.getColor());
padder.setVisibility(View.VISIBLE);
}
}else{
if (convertView == null) {
LayoutInflater layoutInflater = (LayoutInflater) this.context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(R.layout.list_item_low, null);
}
}
Item layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:android="http://schemas.android.com/apk/res/android"
android:background="#color/back" >
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:layout_centerHorizontal="true"
android:layout_centerInParent="true"
>
<CheckBox
android:layout_alignParentLeft="true"
android:layout_marginTop="2dp"
android:layout_marginEnd="8dp"
android:clickable="true"
android:focusableInTouchMode="false"
android:layout_marginRight="8dp"
android:layout_alignParentStart="true"
android:layout_width="wrap_content"
android:focusable="false"
android:layout_height="wrap_content"
android:id="#+id/checkbox"
/>
<TextView
android:inputType="text"
android:paddingRight="8dp"
android:layout_width="wrap_content"
android:focusable="false"
android:layout_marginTop="6dp"
android:textSize="16sp"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/checkbox"
android:layout_toEndOf="#+id/checkbox"
android:id="#+id/list_text"/>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/del"
android:src="#android:drawable/ic_menu_delete"
android:layout_marginTop="3dp"
android:background="#null"
android:visibility="gone"
android:layout_marginRight="8dp"
android:layout_marginEnd="8dp"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"/>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/checkbox"
android:layout_alignBottom="#+id/checkbox"
android:layout_centerHorizontal="true"
android:layout_marginTop="2dp"
android:orientation="horizontal"
android:id="#+id/relativeLayout">
<Button
android:id="#+id/plus"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_centerVertical="true"
android:background="#drawable/round_button"
android:clickable="true"
android:focusable="false"
android:text="+" />
<TextView
android:id="#+id/quantity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_marginLeft="8dp"
android:layout_marginStart="8dp"
android:layout_toEndOf="#+id/plus"
android:layout_toRightOf="#+id/plus" />
<Button
android:id="#+id/minus"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_centerVertical="true"
android:layout_marginLeft="8dp"
android:layout_marginStart="8dp"
android:layout_toEndOf="#+id/quantity"
android:layout_toRightOf="#+id/quantity"
android:background="#drawable/round_button"
android:clickable="true"
android:focusable="false"
android:text="-" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/linearfaq"
android:layout_marginTop="35dp"
android:layout_width="match_parent"
android:layout_height="10dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:background="#color/back"
android:orientation="vertical" >
</RelativeLayout>
<RelativeLayout
android:id="#+id/padder"
android:layout_width="match_parent"
android:background="#color/blue"
android:visibility="gone"
android:layout_height="2dp"
android:layout_alignTop="#+id/linearfaq"
>
</RelativeLayout>
category layout:
<?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"
android:orientation="horizontal"
android:background="#drawable/parent_border">
<TextView
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginLeft="30dp"
android:layout_marginStart="30dp"
android:focusable="false"
android:textSize="20sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/cat_text"/>
I'd put all Views in one single ConstraintLayout. By attaching the background to the parent left and right, the View is default centered. By adding margins left and right, you achieve the View being smaller.
Like in this example:
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/imageView2"
android:layout_width="0dp"
android:layout_height="?attr/actionBarSize"
android:layout_marginBottom="8dp"
android:layout_marginEnd="16dp"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
android:background="#android:color/darker_gray"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<CheckBox
android:id="#+id/checkBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:buttonTint="#android:color/background_light"
android:text="CheckBox"
android:textColor="#android:color/background_light"
app:layout_constraintBottom_toBottomOf="#+id/imageView2"
app:layout_constraintStart_toStartOf="#+id/imageView2"
app:layout_constraintTop_toTopOf="#+id/imageView2" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginStart="24dp"
android:layout_marginTop="8dp"
android:text="Stuff"
android:textColor="#android:color/background_light"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toEndOf="#+id/checkBox"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
which looks like this:
the
android:layout_width="0dp"
actually means the view will be stretched to match the constraints. A bit like "match_parent", but within the bounds you set via its constraints.
Play around with the ConstraintLayout a bit. Once you get the hang of it, you will not want to miss it anymore :)

Categories

Resources