I see that Android released in Oreo a new attribute for textView:
android:autoSizeTextType
This adapts the layout of the textView based on the string of text that it is showing.
How would I be able to use this with a textSwitcher?
I don't know about the ones above but here's an solution with minimal coding. You can change the main layout height to see the autosizing in action, or you can tap the upper switcher. A cool feature is that the styling of the second text field carries over when tapped (i.e. text is white).
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
fun switchText(v: View) {
(v as? TextSwitcher)?.let{
it.tag = (((it.tag as? Int) ?: 0) + 1) % it.childCount
it.setText((it.children.toList()[it.tag as Int] as TextView).text)
}
}
}
activity_main.xml
<?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="350dp"
tools:context=".MainActivity">
<TextSwitcher
android:id="#+id/a"
android:layout_width="0dp"
android:layout_height="0dp"
android:background="#color/purple_200"
app:layout_constraintBottom_toTopOf="#id/b"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_chainStyle="spread"
android:onClick="switchText"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp">
<TextView
android:id="#+id/a1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="999 / 999"
android:gravity="center"
android:letterSpacing="-0.05"
app:autoSizeMaxTextSize="936sp"
app:autoSizeMinTextSize="28sp"
app:autoSizeTextType="uniform"
android:lines="1"
android:includeFontPadding="false" />
<TextView
android:id="#+id/a2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="1000000 / 10000000"
android:textColor="#color/white"
android:gravity="center"
android:letterSpacing="-0.05"
app:autoSizeMaxTextSize="136sp"
app:autoSizeMinTextSize="28sp"
app:autoSizeTextType="uniform"
android:lines="1"
android:includeFontPadding="false" />
</TextSwitcher>
<TextSwitcher
android:id="#+id/b"
android:layout_width="0dp"
android:layout_height="0dp"
android:background="#color/teal_200"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/a"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintVertical_chainStyle="spread"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp">
<TextView
android:id="#+id/b1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="999 / 999"
android:gravity="center"
android:letterSpacing="-0.05"
app:autoSizeMaxTextSize="136sp"
app:autoSizeMinTextSize="28sp"
app:autoSizeTextType="uniform"
android:lines="1"
android:includeFontPadding="false" />
</TextSwitcher>
</androidx.constraintlayout.widget.ConstraintLayout>
<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello" />
private ViewFactory viewFactory = new ViewFactory() {
public View makeView() {
LayoutInflater inflater = LayoutInflater.from(TextSwitcherTest.this);
TextView textView = (TextView) inflater.inflate(R.layout.textView, null);
return textView;
}
};
altitudeSwitcher = (TextSwitcher) findViewById(R.id.altitude);
altitudeSwitcher.setFactory(new ViewFactory() {
#Override
public View makeView() {
TextView t = new TextView(getApplicationContext());
t.setGravity(Gravity.TOP | Gravity.CENTER_HORIZONTAL);
t.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 50);
return t;
}
});
Related
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
}
The Dialog shows properly but the listview is always empty.
Here is what I get when I launch the Activity
enter image description here
public class MainFragment extends Fragment {
String[] cities = {"Tanger", "Meknes", "Casablanca",
"Fes", "Tetouan", "Taza", "Rabat"};
Dialog dialog;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
TextView currentCity = view.findViewById(R.id.city);
currentCity.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
dialog = new Dialog(getContext());
dialog.setContentView(R.layout.dialog_searchable_spinner);
dialog.getWindow().setLayout(800, 1000);
View spinnerView = getLayoutInflater().inflate(R.layout.dialog_searchable_spinner, container, false);
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
dialog.show();
EditText searchTextView = spinnerView.findViewById(R.id.editText);
ListView citiesListView = spinnerView.findViewById(R.id.citiesListView);
ArrayAdapter<String> adapter = new ArrayAdapter<>(getActivity(), android.R.layout.simple_list_item_1, cities);
citiesListView.setAdapter(adapter);
}
DialogLayout:
<?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"
android:background="#drawable/alert_dialog"
android:padding="16dp"
android:orientation="vertical"
android:gravity="center">
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="البحث عن المدينة"
android:textSize="25sp"
android:textColor="#color/primaryColor"
android:fontFamily="#font/alhurra"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="#+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:background="#android:drawable/editbox_background"
android:hint="إسم المدينة..."
android:fontFamily="#font/aj_bold"
android:textDirection="rtl"
android:layoutDirection="rtl"
android:padding="10dp"
android:singleLine="true"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView" />
<ListView
android:id="#+id/citiesListView"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginTop="5dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toBottomOf="#+id/editText"
tools:layout_editor_absoluteX="16dp" />
</androidx.constraintlayout.widget.ConstraintLayout>
FragmentLayout
<?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"
android:background="#drawable/activitybackground"
tools:context=".MainFragment">
<TextView
android:id="#+id/city"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:layout_weight="1"
android:fontFamily="#font/alhurra"
android:gravity="center"
android:text="مكنـاس"
android:textColor="#color/primaryColor"
android:textSize="20sp" />
</androidx.constraintlayout.widget.ConstraintLayout>
It is a problem with your layout. ConstraintLayout is somehow hiding the listview. You have to do some changes in your layout files. For example, this has worked for me using LinearLayout
Dialog dialog;
dialog = new Dialog(this);
dialog.setContentView(R.layout.dialog_searchable_spinner);
dialog.getWindow().setLayout(800, 1000);
//View spinnerView = getLayoutInflater().inflate(R.layout.dialog_searchable_spinner,binding.getRoot());
//dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
ListView citiesListView = dialog.findViewById(R.id.citiesListView);
ArrayAdapter<String> adapter = new ArrayAdapter<>(getApplicationContext(), android.R.layout.simple_list_item_1, cities);
citiesListView.setAdapter(adapter);
dialog.show();
==
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:padding="16dp"
android:orientation="vertical"
android:gravity="center">
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="البحث عن المدينة"
android:textSize="25sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="#+id/editText"
android:layout_below="#id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:background="#android:drawable/editbox_background"
android:hint="إسم المدينة..."
android:textDirection="rtl"
android:layoutDirection="rtl"
android:padding="10dp"
android:singleLine="true"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView" />
<ListView
android:id="#+id/citiesListView"
android:layout_below="#id/editText"
android:layout_width="match_parent"
android:layout_height="400dp"
android:layout_marginTop="5dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toBottomOf="#+id/editText"
tools:layout_editor_absoluteX="16dp" />
</RelativeLayout>
I have a view in an activity as below .I want to set Margin to the view.But i used Viewgroup.LayoutParams but it doesn't change the view of the row.Below i have given custom adapter which takes data from firstpagerowitems.xml.I have given the java code for adapter .I tried doing android:layout_marginbottom="10dp" for firstpagerowitems.xml and firstpage.xml but it doesnt work.
class MyAdapter extends ArrayAdapter<String> {
Context context;
ArrayList<String> rTitle;
ArrayList<String> rDescription;
ArrayList<String> rImgs;
MyAdapter (Context c, ArrayList<String> title,ArrayList<String> description,ArrayList<String> imgs) {
super(c, R.layout.firstpagerowitems, R.id.textView1, title);
this.context = c;
this.rTitle = title;
this.rDescription = description;
this.rImgs = imgs;
}
#NonNull
#Override
public View getView(int position, #Nullable View convertView, #NonNull ViewGroup parent) {
LayoutInflater layoutInflater = (LayoutInflater)getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View row = layoutInflater.inflate(R.layout.firstpagerowitems, parent, false);
TextView images = row.findViewById(R.id.textView3);
TextView myTitle = row.findViewById(R.id.textView1);
TextView myDescription = row.findViewById(R.id.textView2);
Log.d("entered last","yes");
Log.d("postion", String.valueOf(position));
Log.d("rimgs", String.valueOf(rImgs));
Log.d("desc", String.valueOf(rDescription));
Log.d("title", String.valueOf(rTitle));
// now set our resources on views
images.setText(rImgs.get(position));
myTitle.setText(rTitle.get(position));
myDescription.setText(rDescription.get(position));
Random random=new Random();
int trp=random.nextInt(16);
Log.d("enteredcolor",mycolors[trp]);
// images.setBackgroundResource(R.color.lightgreen);
String fd=mycolors[trp];
// images.setBackgroundColor(Color.parseColor(fd));
// LinearLayout mylinear=row.findViewById(R.id.mylinear);
// mylinear.setBackgroundColor(Color.parseColor(fd));
row.setBackgroundColor(Color.parseColor(fd));
// ViewGroup.MarginLayoutParams margins=new ViewGroup.MarginLayoutParams(row.getLayoutParams());
// margins.setMargins(0,100,0,100);
// ViewGroup.LayoutParams layouts=new ViewGroup.LayoutParams(margins);
// row.setLayoutParams(layouts);
// ViewGroup.LayoutParams params=new ViewGroup.LayoutParams(row);
return row;
}
}
code.java
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="one"
android:textColor="#color/colorWhite"
android:textStyle="bold"
android:layout_margin="5dp"
android:textSize="20sp"
android:layout_weight="0.2"
android:id="#+id/textView1"
/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="two"
android:textColor="#color/colorWhite"
android:textStyle="bold"
android:layout_margin="5dp"
android:textSize="20sp"
android:layout_weight="0.45"
android:id="#+id/textView2"
/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="three"
android:textColor="#color/colorWhite"
android:textStyle="bold"
android:layout_margin="5dp"
android:textSize="20sp"
android:layout_weight="0.35"
android:id="#+id/textView3"
/>
</LinearLayout>
firsypagerowitems.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout
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="#color/colorWhite"
tools:context=".Firstpage">
<LinearLayout
android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="vertical"
android:id="#+id/mylinear">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/listView"
android:layout_marginBottom="10dp"
android:layout_marginTop="10dp"
android:layout_marginEnd="10dp"
android:layout_marginStart="10dp"
>
</ListView>
</LinearLayout>
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="#+id/fabs"
android:layout_gravity="end|bottom"
android:layout_margin="16dp"
app:fabSize="normal"
app:backgroundTint="#color/design_default_color_error"
app:elevation="6dp"
android:src="#android:drawable/ic_input_add"
>
</com.google.android.material.floatingactionbutton.FloatingActionButton>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
firstpage.xml
Try whether this helps...
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="one"
android:layout_marginBottom="5dp"
android:textColor="#color/colorWhite"
android:textStyle="bold"
android:textSize="20sp"
android:layout_weight="0.2"
android:id="#+id/textView1"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="two"
android:textColor="#color/colorWhite"
android:textStyle="bold"
android:layout_marginBottom="5dp"
android:textSize="20sp"
android:layout_weight="0.45"
android:id="#+id/textView2"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="three"
android:textColor="#color/colorWhite"
android:textStyle="bold"
android:textSize="20sp"
android:layout_weight="0.35"
android:id="#+id/textView3"
/>
</LinearLayout>
Hope this helps. Feel free to ask for clarifications...
I am trying to add Text views programmatically to Relative layout parent one below other. However, the first view is not aligning correctly while the rest of the view got aligned correctly. See attached image.
My code:
private void setupQuestionChoices(int position) {
question.setText(questionList.get(position).question());
choicesTextViewsList.clear();
for (int i = 0; i < questionList.get(position).choices().size(); i++) {
TextView textView = new TextView(getActivity());
textView.setBackgroundResource(R.drawable.qa_button_background);
textView.setMinWidth(300);
textView.setGravity(Gravity.CENTER);
textView.setText(questionList.get(position).choices().get(i));
textView.setId(i);
RelativeLayout.LayoutParams layoutParams =
new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
if (i == 0) {
layoutParams.addRule(RelativeLayout.BELOW, question.getId());
} else {
layoutParams.addRule(RelativeLayout.BELOW, choicesTextViewsList.get(i - 1).getId());
}
layoutParams.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);
layoutParams.setMargins(20, 20, 20, 20);
relativeLayout.addView(textView, layoutParams);
choicesTextViewsList.add(textView);
choicesTextViewsList.get(i).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
answerSelected = true;
}
});
}
}
Relative layout xml where I add my views to:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<RelativeLayout
android:id="#+id/relative_parent" //here is where I add views to
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/question"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginTop="40dp"
android:layout_marginEnd="20dp"
android:layout_marginBottom="10dp"
android:orientation="horizontal"
android:textColor="#color/colorPrimary"
android:textSize="30sp"
android:textStyle="bold" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_gravity="center"
android:layout_marginBottom="30dp"
android:gravity="center">
<androidx.appcompat.widget.AppCompatButton
android:id="#+id/button_back"
style="?android:attr/borderlessButtonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="30dp"
android:layout_marginTop="10dp"
android:layout_marginEnd="30dp"
android:background="#drawable/rectangle_border"
android:text="Back"
android:textColor="#android:color/darker_gray" />
<androidx.appcompat.widget.AppCompatButton
android:id="#+id/button_next"
style="?android:attr/borderlessButtonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_marginStart="30dp"
android:layout_marginTop="10dp"
android:layout_marginEnd="30dp"
android:background="#drawable/rectangle_border"
android:text="Next"
android:textColor="#android:color/darker_gray" />
</RelativeLayout>
</RelativeLayout>
</ScrollView>
I read some posts related to this issue and followed the guidelines from them but that did not help.
How to lay out Views in RelativeLayout programmatically?
Create a new TextView programmatically then display it below another TextView
I was using the index of the for loop as id for the textViews which I changed to make Android generate Id for it as below and it is working fine now as expected.
I changed the line
textView.setId(i)
to
textView.setId(ViewGroup.generateViewId())
Thanks.
My recyclerView is not showing anything. I looked through the other similar questions posted on the site, but I can't find anything that seems relevant. When I open the activity, I just see the bar on top with the app name and nothing else below it.
My goal is to get a list of items, where each item has a "delete" icon that can be clicked to delete it. For this purpose, I have a custom layout for the recyclerView item.
I made a simplified version of the code to try and isolate the problem. Here it is:
Activity:
public class CategoryManagerActivity extends AppCompatActivity {
private RecyclerView categoryRecyclerView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_category_manager);
categoryRecyclerView = findViewById(R.id.category_edit_recycler);
ArrayList<String> categoryList = new ArrayList<>();
categoryList.add("CAT1");
categoryList.add("CAT2");
categoryList.add("CAT3");
CategoryEditRecyclerViewAdapter adapter = new CategoryEditRecyclerViewAdapter(categoryList);
categoryRecyclerView.setLayoutManager(new LinearLayoutManager(this));
categoryRecyclerView.setAdapter(adapter);
}
}
Adapter class:
public class CategoryEditRecyclerViewAdapter extends
RecyclerView.Adapter<CategoryEditRecyclerViewAdapter.CategoryEditViewHolder>{
private ArrayList<String> categories;
public CategoryEditRecyclerViewAdapter(ArrayList<String> items){
categories = items;
}
#NonNull
#Override
public CategoryEditViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
Context context = parent.getContext();
View view = LayoutInflater.from(context).inflate(R.layout.categories_edit_recycler_item,
parent, false);
return new CategoryEditRecyclerViewAdapter.CategoryEditViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull CategoryEditViewHolder holder, int position) {
final String category = categories.get(position);
holder.categoryName.setText(category);
holder.deleteButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
delete_category(category);
}
});
}
#Override
public int getItemCount() {
return categories.size();
}
private void delete_category(String category){
//I have code here to delete an item
}
public class CategoryEditViewHolder extends RecyclerView.ViewHolder{
TextView categoryName;
ImageView deleteButton;
public CategoryEditViewHolder(View itemView) {
super(itemView);
categoryName = itemView.findViewById(R.id.categoryEditText);
deleteButton = itemView.findViewById(R.id.deleteCategory);
}
}
}
recyclerView item layout:
(I used the #drawable/delete in other activities in the app, so I don't think it should be a problem)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<androidx.constraintlayout.widget.ConstraintLayout
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/categoryEditText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="24dp"
android:layout_marginTop="22dp"
android:layout_marginBottom="690dp"
android:text="TextView"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="1.0" />
<ImageView
android:id="#+id/deleteCategory"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginTop="22dp"
android:layout_marginEnd="16dp"
android:layout_marginBottom="679dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#drawable/delete" />
</androidx.constraintlayout.widget.ConstraintLayout>
</LinearLayout>
Activity layout:
<?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=".CategoryManagerActivity">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/category_edit_recycler"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
try this for item layout
<?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"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<TextView
android:id="#+id/categoryEditText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="24dp"
android:layout_marginTop="22dp"
android:text="TextView"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintEnd_toStartOf="#+id/deleteCategory"
app:layout_constraintVertical_bias="1.0" />
<ImageView
android:id="#+id/deleteCategory"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginTop="22dp"
android:layout_marginEnd="16dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#drawable/delete" />
</androidx.constraintlayout.widget.ConstraintLayout>
and this for activity layout
<?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=".CategoryManagerActivity">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/category_edit_recycler"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
Modify your RecyclerView Item Layout s below.
<RelativeLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:android="http://schemas.android.com/apk/res/android">
<TextView
android:id="#+id/categoryEditText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="24dp"
android:layout_marginTop="22dp"
android:text="TextView" />
<ImageView
android:id="#+id/deleteCategory"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginTop="22dp"
android:layout_marginEnd="16dp"
android:layout_alignParentEnd="true"
android:src="#drawable/ic_drawable_delete" />
</RelativeLayout>
Please modify your categories_edit_recycler_item Item Layouts below:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:padding="2dp">
<TextView
android:id="#+id/categoryEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginTop="5dp"
android:text="TextView"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="1.0" />
<ImageView
android:id="#+id/deleteCategory"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginEnd="10dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#drawable/delete" />
</androidx.constraintlayout.widget.ConstraintLayout>
</LinearLayout>