1

I want a layout like this,

enter image description here

Currently, I have achieved this by adding buttons manually in the XML file of my custom layout for the AlertDialog. But the number of cities can keep changing and hence I would like to add them dynamically.

I tried adding them like this,

jobActivity.java

private FlexboxLayout fbBottomPopup;
private Dialog mBottomSheetDialog;
private String[] cities = {"Chennai", "Bangalore", "Tirichi", "Mumbai", "Coimbatore", "Vellore", "Karaikal", "Karaikudi", "Delhi", "Nellore", "Goa", "Kochi", "Manglore", "Jaipur", "Mysore"};

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_job);

    fbBottomPopup = findViewById(R.id.fb_bottom_popup);

    addButtonsToBottomPopup();
    createBottomPopupView();
}

private void createBottomPopupView(){
    mBottomSheetDialog = new Dialog(JobActivity.this, R.style.MaterialDialogSheet);
    mBottomSheetDialog.setContentView(R.layout.city_filter_layout); // your custom view.
    mBottomSheetDialog.setCancelable(true);
    mBottomSheetDialog.getWindow().setLayout(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
    mBottomSheetDialog.getWindow().setGravity(Gravity.BOTTOM);
}

private void addButtonsToBottomPopup(){
    for(int i=0; i<cities.length;i++) {
        Button btnCities = new Button(this);
        btnCities.setId(i);
        btnCities.setLayoutParams(new FlexboxLayout.LayoutParams(FlexboxLayout.LayoutParams.WRAP_CONTENT, FlexboxLayout.LayoutParams.WRAP_CONTENT));
        btnCities.setText(cities[i]);
        btnCities.setBackgroundResource(R.drawable.round_filter_city_ns);
        btnCities.setTextColor(Color.WHITE);
        btnCities.setTextSize(15);

        fbBottomPopup.addView(btnCities);
    }
}

public void onClickCityFilter(View view){
    mBottomSheetDialog.show();
}

My AlertDialog Layout, city_filter_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/colorPrimary"
    android:paddingTop="10dp"
    android:paddingRight="10dp"
    android:paddingLeft="10dp"
    android:orientation="vertical"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <TextView
        android:layout_marginTop="5dp"
        android:layout_marginBottom="10dp"
        android:layout_marginLeft="5dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:fontFamily="@font/abeezee_regular"
        android:textStyle="bold"
        android:textSize="20sp"
        android:textColor="@color/grey5"
        android:text="Choose Cities: "/>

    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:layout_marginBottom="10dp"
        android:background="@color/grey2"/>

    <com.google.android.flexbox.FlexboxLayout
        android:id="@+id/fb_bottom_popup"
        android:paddingBottom="50dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:flexWrap="wrap">
    <Button
        android:id="@+id/btn3"
        android:layout_margin="5dp"
        android:background="@drawable/round_filter_city_ns"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@color/colorWhite"
        android:fontFamily="@font/abeezee_regular"
        android:textSize="15sp"
        android:textAllCaps="false"
        android:textStyle="bold"
        android:minWidth="0dp"
        android:minHeight="0dp"
        android:paddingLeft="10dp"
        android:paddingRight="10dp"
        android:paddingTop="5dp"
        android:paddingBottom="5dp"
        android:drawableLeft="@drawable/bookmark1"
        android:drawablePadding="5dp"
        android:text="Bangalore"/>

</com.google.android.flexbox.FlexboxLayout>

<View
    android:layout_width="match_parent"
    android:layout_height="1dp"
    android:background="@color/grey1"/>

<LinearLayout
    android:layout_width="match_parent"
    android:weightSum="2"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <Button
        android:layout_weight="1"
        android:layout_width="0dp"
        android:layout_height="55dp"
        android:text="CLOSE"
        android:clickable="true"
        android:fontFamily="@font/abeezee_regular"
        android:textColor="@color/grey3"
        android:textStyle="bold"
        android:textSize="15sp"
        android:background="@android:color/transparent"
        android:layout_gravity="end"/>

    <View
        android:layout_width="1dp"
        android:layout_height="match_parent"
        android:layout_marginTop="8dp"
        android:layout_marginBottom="8dp"
        android:background="@color/grey1"/>

    <Button
        android:layout_weight="1"
        android:layout_width="0dp"
        android:layout_height="55dp"
        android:text="APPLY FILTER"
        android:clickable="true"
        android:fontFamily="@font/abeezee_regular"
        android:textColor="@color/deep_purple_darken_1"
        android:textStyle="bold"
        android:textSize="15sp"
        android:background="@android:color/transparent"
        android:layout_gravity="end"/>

</LinearLayout>

But this keeps giving me Null Pointer Exception, StackTrace:

02-16 11:53:19.724 11493-11493/com.vitvellore.vishistvarugeese.eezeejobs E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.vitvellore.vishistvarugeese.eezeejobs, PID: 11493
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.vitvellore.vishistvarugeese.eezeejobs/com.vitvellore.vishistvarugeese.eezeejobs.JobActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.google.android.flexbox.FlexboxLayout.addView(android.view.View)' on a null object reference
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2665)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
    at android.app.ActivityThread.-wrap12(ActivityThread.java)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:154)
    at android.app.ActivityThread.main(ActivityThread.java:6119)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
 Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.google.android.flexbox.FlexboxLayout.addView(android.view.View)' on a null object reference
    at com.vitvellore.vishistvarugeese.eezeejobs.JobActivity.addButtonsToBottomPopup(JobActivity.java:137)
    at com.vitvellore.vishistvarugeese.eezeejobs.JobActivity.onCreate(JobActivity.java:65)
    at android.app.Activity.performCreate(Activity.java:6679)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2618)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726) 
    at android.app.ActivityThread.-wrap12(ActivityThread.java) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477) 
    at android.os.Handler.dispatchMessage(Handler.java:102) 
    at android.os.Looper.loop(Looper.java:154) 
    at android.app.ActivityThread.main(ActivityThread.java:6119) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776) 

Please suggest me a better way to do the same task rather than adding them manually.

Vishist Varugeese
  • 1,500
  • 1
  • 17
  • 30

0 Answers0