Android application development, dynamic buttons and fields

Let me preface my question by saying that I have done a lot of research into creating dynamic buttons in an Android application, and most of them are simply wrong or have a different understanding of dynamics than I do. If I missed something, just send the link and I will check it.

What I'm looking for is a way to create a button in my application based on the information that I collect from online sources. For example, when someone creates a message on forums that interest me, the application will find this, analyze it for me, and return some information. Since I cannot put all this information on the screen for each published message, I want to create dynamically dynamically to view this information. For example, the username, date and a brief description (in the form of a preview, clicking on it will provide all the information in a separate action). For the sake of this post, let's pretend that I get this information from the text input location (not from the actual internt forum post).

First of all, how can I create a button dynamically? The other half of my question is less important to me. I would like to do this programmatically. The links to the tutorials are great.

Secondly, and less important ... Once I created this button dynamically, how can I get custom button representations based on the predicted format.

If something is unclear, just ask and I will try to clarify. Thanks for your help!

+1
source share
3 answers

In my opinion, the correct approach is to have an Adapter , which will display data on a specific view (a button in your case).

Which adapter you choose depends on how you decide to receive and store data from the Internet.

When new messages appear, you add them to the data source (database, list, etc.) and you call notifyDataSetChanged , which will update the list, dynamically creating as many views as necessary to display all the data.

+1
source

I think this answers your question. The idea is to programmatically create a button and then add it to the current layout. Somethiug like this:

Button newButton = new Button(this); newButton.setText("Click Me"); newButton.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { ... } }); container.addView(newButton); 

Where the container is the layout that holds the button (i.e. will become its parent). You can also add layout settings to the button.

0
source

Well, you can create a new Button and set onClickListener like this:

  Button button = new Button(context); button.setText("New Button"); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub } }); 

If you have anything else you need to set, such as an identifier, you can call the method as you wish. Then you need to add it to your layout as follows:

 LinearLayout layout = (LinearLayout)findViewById(R.id.linearLayout); LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT); button.setLayoutParams(params); layout.addView(button); 

Of course, you can do this, but this should get you started, and all you need is to find it by looking at the documents.

If you need help with something more specific, just comment and I will try to develop.

0
source

Source: https://habr.com/ru/post/1399173/


All Articles