简体   繁体   中英

how do I add a custom layout to a Linear Layout on the click of a button

So I am trying to create a task app, in which, on the click of a button, I want my subtask custom view, which contains a text view, and 2 image views, inside a horizontal linear layout, to be displayed inside a vertical linear layout. The data required for the subtask is taken from a different activity, and then transferred using intents. Also, I need the id for every subtask that is added to be different. I have no idea how to do this. Please help. I couldn't code anything for this. I tried using layout inflater, searched up the net on what to do, but I still have no clue. Please help.

You can user dialog fragment for this and is look effective also Let me give you link... dialog fragment

I have done something similar in my project, adding horizontal linear layouts in a vertical linear layout programmatically.

Please check the below code:

View view = View.inflate(getActivity(), R.layout.item_horizontal_layout, null);
    
Button btnSave = view.findViewById(R.id.btnSaveModule);

layoutCount += 1; // A global value to store added layout counts

btnSave.setTag(layoutCount); // Set the tag on button so that we can use it later

btnSave.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        // Access the layout position on button click
        Log.d("Layout Position", ":" + (int)btnSave.getTag())
    }
});

verticalLL.addView(view); // Add horizontal layout to vertical Linear Layout

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM