Make my custom view update text fields in editor - java

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

Related

Nested Spinner Android Design Issues ConstraintLayout

I'm working with spinner for the first time, and I don't understand why the second spinner doesn't look exactly like the first one even though they were created practically the same (the only difference is the data). These are nested.
The design implemented it with "constraint layout"
<Spinner
android:id="#+id/spCategorie"
android:layout_width="0dp"
android:layout_height="28dp"
android:layout_marginStart="10dp"
android:layout_marginTop="32dp"
android:layout_marginEnd="32dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toEndOf="#+id/categorieText"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/categorieText"
android:layout_width="73dp"
android:layout_height="28dp"
android:layout_marginStart="16dp"
android:layout_marginTop="32dp"
android:text="Categorie:"
android:textAlignment="viewStart"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Spinner
android:id="#+id/spProduct"
android:layout_width="0dp"
android:layout_height="28dp"
android:layout_marginStart="10dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="32dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#+id/productText"
app:layout_constraintTop_toBottomOf="#+id/spCategorie" />
This is how it looks in the emulator
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, R.layout.support_simple_spinner_dropdown_item, productsCat);
arrayAdapter.setDropDownViewResource(R.layout.support_simple_spinner_dropdown_item);
categorie.setAdapter(arrayAdapter);
categorie.setOnItemSelectedListener(new SpinnersEvents());
product.setOnItemSelectedListener(new SpinnersEvents());
private class SpinnersEvents implements AdapterView.OnItemSelectedListener {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
if (parent.getId() == R.id.spCategorie){
String[] productsName = getProductName(productsCat[position]);
ArrayAdapter<String> arrayAdapterChild = new ArrayAdapter<String>(getBaseContext(),R.layout.support_simple_spinner_dropdown_item, productsName);
arrayAdapterChild.setDropDownViewResource(R.layout.support_simple_spinner_dropdown_item);
product.setAdapter(arrayAdapterChild);
}else{
price.setText(String.valueOf(tempList.get(position).getPrice()));
imgPrd.setImageResource(tempList.get(position).getImage());
}
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
}
Please try this in your code may help you, you can also set wrapcontent in spinner or increase the size to view text proper
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
tools:ignore="MissingConstraints">
<TextView
android:id="#+id/categorieText"
android:layout_width="73dp"
android:layout_height="28dp"
android:layout_marginStart="10dp"
android:layout_marginEnd="10dp"
android:layout_marginBottom="10dp"
android:gravity="center"
android:text="Categorie:"
android:textAlignment="viewStart"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:ignore="RtlCompat" />
<Spinner
android:id="#+id/spCategorie"
android:layout_width="0dp"
android:layout_height="28dp"
android:layout_marginStart="10dp"
android:layout_marginEnd="10dp"
android:layout_marginBottom="10dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_chainStyle="spread"
app:layout_constraintHorizontal_weight="1"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toEndOf="#+id/categorieText"/>
<TextView
android:id="#+id/productText"
android:layout_width="73dp"
android:layout_height="28dp"
android:layout_marginTop="32dp"
android:layout_marginStart="10dp"
android:layout_marginEnd="10dp"
android:layout_marginBottom="10dp"
android:text="Product:"
android:textAlignment="viewStart"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/categorieText"/>
<Spinner
android:id="#+id/spproduct"
android:layout_width="0dp"
android:layout_height="28dp"
android:layout_marginTop="32dp"
android:layout_marginStart="10dp"
android:layout_marginEnd="10dp"
android:layout_marginBottom="10dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_chainStyle="spread"
app:layout_constraintHorizontal_weight="1"
app:layout_constraintTop_toBottomOf="#+id/spCategorie"
app:layout_constraintStart_toEndOf="#+id/categorieText"/>
</androidx.constraintlayout.widget.ConstraintLayout>
The solution was in the code, not in the layout.
The second spinner must be:
ArrayAdapter<String> arrayAdapterChild = new ArrayAdapter<String>(MainActivity.this,R.layout.support_simple_spinner_dropdown_item, productsName);
instead:
ArrayAdapter<String> arrayAdapterChild = new ArrayAdapter<String>(getBaseContext(),R.layout.support_simple_spinner_dropdown_item, productsName);
In this case, calling the getBaseContext() method generated that particular problem.
Sorry for the confusion.

NullPointerException Java android studio setMovementMethod(LinkMovementMethod.getInstance())

I'm still new with coding but I have a project with android studio right now. What I wanted to do was to add hyperlinks in a page. I've followed the steps and tutorials to do this very carefully but I can't seem to find the problem. In my Logcat it says
PID: 17644 java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setMovementMethod(android.text.method.MovementMethod)' on a null object reference
but I don't understand how it was caused. I have read some similar problems stating the findByViewId might be wrong but I don't think that is the case for my problem but I would love to be wrong. Here is my code. Any help is much appreciated.
public class LearnFragment extends Fragment {
private Button search;
private TextView faq_link_1, faq_link_2, faq_link_3, faq_link_4, faq_link_5;
EditText editSearch;
public LearnFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View viewRoot = inflater.inflate(R.layout.fragment_account, container, false);
faq_link_1 = (TextView) viewRoot.findViewById(R.id.faq1);
faq_link_2 = (TextView) viewRoot.findViewById(R.id.faq2);
faq_link_3 = (TextView) viewRoot.findViewById(R.id.faq3);
faq_link_4 = (TextView) viewRoot.findViewById(R.id.faq4);
faq_link_5 = (TextView) viewRoot.findViewById(R.id.faq5);
faq_link_1.setMovementMethod(LinkMovementMethod.getInstance());
faq_link_2.setMovementMethod(LinkMovementMethod.getInstance());
faq_link_3.setMovementMethod(LinkMovementMethod.getInstance());
faq_link_4.setMovementMethod(LinkMovementMethod.getInstance());
and this is the 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:id="#+id/frame_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".boundary.LearnFragment">
<AutoCompleteTextView
android:id="#+id/search_bar"
android:layout_width="0dp"
android:layout_height="44dp"
android:layout_marginStart="16dp"
android:layout_marginTop="28dp"
android:hint="Search..."
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintEnd_toStartOf="#+id/b_search"
app:layout_constraintHorizontal_bias="0.158"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="#+id/b_search"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginTop="28dp"
android:layout_marginEnd="16dp"
android:layout_marginBottom="14dp"
android:layout_weight="1"
android:text="Search"
app:layout_constraintBottom_toTopOf="#+id/textView2"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#+id/search_bar"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/faq1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="37dp"
android:text="#string/link_period_pain"
android:textSize="23sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView2"
android:linksClickable="true"/>
<TextView
android:id="#+id/faq2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="27dp"
android:text="#string/menstrual_cramp_link"
android:textSize="23sp"
app:layout_constraintBottom_toTopOf="#+id/faq3"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/faq1"
android:linksClickable="true" />
<TextView
android:id="#+id/faq3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="32dp"
android:text="#string/late_period_link"
android:textSize="23sp"
app:layout_constraintBottom_toTopOf="#+id/faq4"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/faq2"
android:linksClickable="true" />
<TextView
android:id="#+id/faq4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="30dp"
android:text="#string/cervical_screening_link"
android:textSize="23sp"
app:layout_constraintBottom_toTopOf="#+id/faq5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/faq3"
android:linksClickable="true" />
<TextView
android:id="#+id/faq5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="35dp"
android:text="#string/pap_smear_link"
android:textSize="23sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/faq4"
android:linksClickable="true" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="13dp"
android:text="Not looking what you're looking for? Use the search bar to find out more!"
android:textSize="23sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/search_bar" />
</androidx.constraintlayout.widget.ConstraintLayout>

How to create a component and place on xml page using a button?

Basically, this is the code I have right now...
ViewCard activity
import androidx.appcompat.app.AppCompatActivity;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Switch;
import android.widget.TextView;
import com.team46.stubank.R;
public class DisplayCards extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_cards);
}
public void viewCard(View view){
Intent intent = new Intent(this, ViewCard.class);
startActivity(intent);
}
#SuppressLint("SetTextI18n")
public void toggleDetails(View view){
Switch simpleSwitch = findViewById(R.id.switch1);
TextView tv = findViewById(R.id.textView4);
TextView tv1 = findViewById(R.id.textView);
TextView tv2 = findViewById(R.id.textView5);
TextView tv3 = findViewById(R.id.textView6);
if (simpleSwitch.isChecked()){
tv.setText("card.getCardNumber");
tv1.setText("user.getName");
tv2.setText("card.getSortCode");
tv3.setText("card.getAccountNumber");
//needs to call getter methods from card and user classes
}
else{
tv.setText("0000-0000-0000-0000");
tv1.setText("Joe Bloggs");
tv2.setText("11-11-11");
tv3.setText("3411-4885-6324-7195");
}
}
}
xml page:
?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:id="#+id/cardView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".card_activities.DisplayCards">
<androidx.cardview.widget.CardView
android:layout_width="362dp"
android:layout_height="172dp"
android:layout_marginStart="16dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="100dp"
android:layout_marginEnd="16dp"
android:layout_marginRight="16dp"
android:onClick="viewCard"
app:cardBackgroundColor="#6C4095"
app:cardCornerRadius="20dp"
app:cardElevation="15dp"
app:cardMaxElevation="20dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:onClick="viewCard">
<TextView
android:id="#+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="8dp"
android:fontFamily="#font/roboto_medium"
android:text="#string/card_number_default"
android:textColor="#FFFFFF"
android:textSize="22sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView" />
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="8dp"
android:fontFamily="#font/roboto_medium"
android:text="#string/name_default"
android:textColor="#FFFFFF"
android:textSize="16sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/imageView2" />
<ImageView
android:id="#+id/imageView2"
android:layout_width="59dp"
android:layout_height="47dp"
android:layout_marginStart="16dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="8dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#drawable/mastercard_small" />
<Switch
android:id="#+id/switch1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:fontFamily="#font/roboto_medium"
android:onClick="toggleDetails"
android:text="#string/toggle_card_details"
android:textColor="#FFFFFF"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="14dp"
android:fontFamily="#font/roboto_medium"
android:text="#string/sortcode_default"
android:textColor="#FFFFFF"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView4" />
<TextView
android:id="#+id/textView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="14dp"
android:layout_marginEnd="16dp"
android:layout_marginRight="16dp"
android:fontFamily="#font/roboto_medium"
android:text="#string/account_number_default"
android:textColor="#FFFFFF"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView4" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="79dp"
app:cardBackgroundColor="#6d0095"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<TextView
android:id="#+id/cards_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="16dp"
android:fontFamily="#font/roboto_medium"
android:text="#string/cards_menu"
android:textColor="#FFFFFF"
android:textSize="36sp" />
</androidx.cardview.widget.CardView>
</androidx.constraintlayout.widget.ConstraintLayout>
The code here is basically a screen with a card, made up of various components, in order to resemble a credit/debit card containing the user's information. Similar to this. What I want to do, is to allow the user to add multiple "cards" by clicking onto a button. I'm not sure how to go about this exactly so any help would be much appreciated.
You have 3 (I think) basic ways to do it - they all involve pulling your single card layout into its own XML file:
Use the layout as a list item in a ListView, or (preferably) a ViewHolder in a RecyclerView. Each item gets a card layout, and you set the details on the things - the actual data for each card is stored in a list and you just display the appropriate stuff
Create a custom View (compound controls really, just bundling some Views together) which inherits from something like FrameLayout, and inflate your XML layout into that. That way each card is its own object and you can use them as widgets in any other layout
Make each card a Fragment which is a little simpler than creating a View (since it handles all the layout inflation stuff for you) but is probably a bit harder to work with in terms of adding them to a layout.
Really it depends what you want - a simple list with a RecyclerView might be best, if you want flexibility you could make the custom View and stick one of those in a basic layout and use that as your ViewHolder layout

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

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

Categories

Resources