DialogFragment is expanding the XML to full screen? - java

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);
}

Related

How to navigate from bottomsheet to activity

I have a bottomsheet implemented in my app. I used inflater to show the bottomsheet dialog, here is my code in NewSignUpActivity to show the bottomsheet dialog.
class NewSignUpActivity : AppCompatActivity() {
private lateinit var btnBottomSheet: RelativeLayout
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_rider_or_bike)
btnBottomSheet = findViewById(R.id.riderBtn)
val bottomSheetCallback = object : BottomSheetBehavior.BottomSheetCallback(){
override fun onStateChanged(bottomSheet: View, newState: Int) {}
override fun onSlide(bottomSheet: View, slideOffset: Float) {}
}
val bottomSheetView = layoutInflater.inflate(R.layout.layout_bottom_sheet, null)
val bottomSheetDialog = BottomSheetDialog(this)
bottomSheetDialog.setContentView(bottomSheetView)
val bottomSheetBehavior = BottomSheetBehavior.from(bottomSheetView.parent as View)
bottomSheetBehavior.setBottomSheetCallback(bottomSheetCallback)
btnBottomSheet.setOnClickListener {
bottomSheetBehavior.state = BottomSheetBehavior.STATE_EXPANDED
bottomSheetDialog.show()
}
}
}
Here's how it looks like in view.
As you can see, there's button on the bottomsheet I want to navigate to another layout activity once I clicked the YES button, how can I do that? I am very new to android development using Kotlin.
EDITED: Added my XML file for bottomsheet dialog please see below._ name of file is layout_bottom_sheet.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/layoutBtmSheet"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="#drawable/rectangle"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<androidx.appcompat.widget.AppCompatImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_marginTop="#dimen/_20sdp"
android:layout_marginRight="#dimen/_15sdp"
android:layout_marginBottom="#dimen/_30sdp"
android:src="#drawable/close_black" />
</RelativeLayout>
<LinearLayout
android:id="#+id/linearLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="52dp"
android:background="#drawable/bgwhite"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<LinearLayout
android:id="#+id/ll_one"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="#dimen/_30sdp"
android:layout_marginTop="#dimen/_20sdp"
android:layout_marginRight="#dimen/_10sdp"
android:orientation="vertical"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent">
<androidx.appcompat.widget.AppCompatImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/outline_sports_motorsports_black_48"
tools:layout_editor_absoluteX="3dp"
tools:layout_editor_absoluteY="3dp" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="#dimen/_10sdp"
android:orientation="vertical"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintLeft_toRightOf="#+id/ll_one"
app:layout_constraintTop_toTopOf="parent">
<androidx.appcompat.widget.AppCompatTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_there_partner_rider"
android:textSize="18sp"
android:textStyle="bold" />
<androidx.appcompat.widget.AppCompatTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="#dimen/_5sdp"
android:text="#string/lorem_ipsum" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:id="#+id/linearLayout3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/linearLayout">
</LinearLayout>
<LinearLayout
android:id="#+id/linearLayout4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/_100sdp"
android:layout_marginBottom="#dimen/_10sdp"
android:orientation="horizontal"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.49"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/linearLayout3"
app:layout_constraintVertical_bias="0.678">
<androidx.appcompat.widget.AppCompatButton
android:id="#+id/noBtn"
android:layout_width="169dp"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:background="#drawable/bgbtn1"
android:radius="#dimen/_50sdp"
android:text="No" />
<androidx.appcompat.widget.AppCompatButton
android:id="#+id/yellowBtn"
android:layout_width="169dp"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:textColor="#color/white"
android:background="#drawable/bgbtno"
android:text="#string/yes" />
</LinearLayout>
<androidx.appcompat.widget.AppCompatTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="17sp"
android:textColor="#color/reply_black_800"
android:text="#string/are_you_using_your_own_motorcycle"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/linearLayout"
app:layout_constraintVertical_bias="0.074" />
</androidx.constraintlayout.widget.ConstraintLayout>
Already the BottomSheetView layout is inflated with:
val bottomSheetView = layoutInflater.inflate(R.layout.layout_bottom_sheet, null)
And this layout hosts the YES button, so you can just use findViewById(). Assuming that the id of the YES button is btn_yes:
val btnYes = bottomSheetView.findViewById<Button>(R.id.btn_yes)
btnYes.setOnClickListener {
startActivity(myIntent) // Add your activity intent
}
UPDATE:
As you provided the id of the YES button which is yellowBtn, then you can use it:
val btnYes = bottomSheetView.findViewById<Button>(R.id.yellowBtn)
btnYes.setOnClickListener {
startActivity(myIntent) // Add your activity intent
}

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

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

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" />

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 :)

How to change background of a child in view

I'm trying to change the background of a particular constraint layout in a cardview but I can't figure out how to do it.
I have several cards with several layouts (all of them have id like unit1Layout,unit2Layout...)
I want to change the background of the clicked card.
I tried changing the cardview's background but I'm getting as shown in the photo.
<ScrollView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="256"
android:fillViewport="true">
<GridLayout
android:id="#+id/mainGrid"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="10"
android:alignmentMode="alignMargins"
android:columnCount="1"
android:columnOrderPreserved="false"
android:padding="14dp"
android:rowCount="10">
<!-- Row 1 -->
<android.support.v7.widget.CardView
android:id="#+id/unit1Card"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_columnWeight="1"
android:layout_marginBottom="16dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginTop="16dp"
android:layout_rowWeight="1"
android:background="#drawable/cardbackground"
app:cardCornerRadius="8dp"
app:cardElevation="5dp">
<android.support.constraint.ConstraintLayout
android:id="#+id/unit1Layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:background="#drawable/cardbackground">
<TextView
android:id="#+id/unit1Label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:fontFamily="#font/myfont"
android:text="Unit 1"
android:textColor="#color/fontColor"
android:textSize="28sp"
android:textStyle="bold"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/unit1Words"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="16dp"
android:fontFamily="#font/myfont"
android:text="423 Words"
android:textColor="#color/fontColor"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/unit1Label" />
<TextView
android:id="#+id/unit1Green"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:fontFamily="#font/myfont"
android:text="358"
android:textColor="#color/fontColor"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="#+id/unit1Yellow"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/unit1Label" />
<TextView
android:id="#+id/unit1Yellow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:fontFamily="#font/myfont"
android:text="32"
android:textColor="#color/fontColor"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/unit1Label" />
<TextView
android:id="#+id/unit1Red"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:fontFamily="#font/myfont"
android:text="8"
android:textColor="#color/fontColor"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#+id/unit1Yellow"
app:layout_constraintTop_toBottomOf="#+id/unit1Label" />
</android.support.constraint.ConstraintLayout>
</android.support.v7.widget.CardView>
this is how Im changing the cardview's background
private void setSingleEvent(GridLayout mainGrid){
for(int i = 0;i<mainGrid.getChildCount();i++){
final int finalI = i;
final CardView cardView = (CardView)mainGrid.getChildAt(i);
cardView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(unitNumbers[finalI+1] == 0) {
unitNumbers[finalI+1] = finalI+1;
cardView.setBackground(ContextCompat.getDrawable(cardView.getContext(), R.drawable.cardbackgroundselected));
}
else {
unitNumbers[finalI+1] = 0;
[![enter image description here][1]][1]
cardView.setBackground(ContextCompat.getDrawable(cardView.getContext(), R.drawable.cardbackground));
}
}
});
}
}
I just found the answer:
private void setSingleEvent(GridLayout mainGrid){
for(int i = 0;i<mainGrid.getChildCount();i++){
final int finalI = i;
final CardView cardView = (CardView)mainGrid.getChildAt(i);
cardView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String layoutID = "unit"+ (finalI + 1) + "Layout";
ConstraintLayout constraintLayout = findViewById(getResources()
.getIdentifier(layoutID,"id",getPackageName()));
Log.d("unit i layout", "unit"+ (finalI + 1) + "Layout");
if(unitNumbers[finalI+1] == 0) {
unitNumbers[finalI+1] = finalI+1;
constraintLayout.setBackground(ContextCompat
.getDrawable(constraintLayout.getContext(),R.drawable.cardbackgroundselected));
}
else {
unitNumbers[finalI+1] = 0;
constraintLayout.setBackground(ContextCompat
.getDrawable(constraintLayout.getContext(),R.drawable.cardbackground));
}
}
});
}
}

Categories

Resources