Dynamically Creating/Removing Buttons in Android - java

Firstly, my apologies if this answer is already on here, as I've been searching for a few weeks and haven't found anything yet.
I am working on an Android app which needs to allow the user to create and remove buttons. I know how to normally create buttons statically through adding the button the XML file and creating it's functionality in the JAVA file.
Instead, I have a static button which I'll refer to as "Create Button". When the user presses on the Create Button, they should be given the option to add a new button to the current activity, allowing them to change the title of said button etc. When they close the app and open it back up; the button they added should still be there. Similarly, they should be given an option to remove buttons.
Can someone point me in the right direction? Most of the sources that I've come across only explain how to statically create buttons, like I first mentioned.
Thanks for the help!
EDIT: I was able to figure some stuff out based off of the feedback I've been given.
So far I have the following code in the onOptionsItemSelected( ) method:
if (id == R.id.add_button)
{
Button myButton = new Button(this);
myButton.setText("Push Me");
//myButton.setVisibility(View.VISIBLE);
return true;
}
I am still a little confused about how this can get added to the layout. Mainly, I am confused about the findViewById call:
RelativeLayout layout = (RelativeLayout)findViewById(R.id.?);
Which id should I be using? In the app's main XML file, there is no ID for the layout itself. It's just a "RelativeLayout".
EDIT2:
Actually, I solved the problem. Thanks for the advice! I just needed to give my layout an ID in the XML file. I knew that I could give buttons etc an ID, but never knew that I was able to do so for the actual layout itself!

Creating a button -
Button myButton = new Button(this);
Adding text to it -
myButton.setText("Push Me");
To make the button visible, you need to add it to a view like this. You can also add it to a statically created view -
LinearLayout ll = (LinearLayout)findViewById(R.id.buttonlayout);
LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
ll.addView(myButton, lp);
Removing button -
ll.removeView(myButton);
For additional customizations, check documentation.
If you are creating multiple buttons, then I recommend setting id. This example makes it clear.
For making buttons visible after closing the app, you need to store the data on memory. The simplest way to do this is to maintain a record of the buttons and their specifications and storing them before closing the app. After opening the app, you can read the stored data and create the buttons accordingly.
For more details, check Data Storing.

ViewGroup mViewGroup = (ViewGroup) findViewById(R.id.main_layout_id);
mViewGroup.addView(yourButton, 0, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));

Related

Set The Visibility of The Whole Box

So in my app I have a registration form used to register other users. In my app I have 3 ranks (Admin, Patient, HCP). In the 'New HCP' form I want to hide something that is in my .xml file for the TextView. I am well aware of setVisibilty(View.GONE); but this doesn't work for my options because I have the boxes in a Constraint Layout.
My Question is:
How do I hide the whole box including its contents?
I tried this (but it only hid the contents and left the drawable box behind:
TextView consent_TextView = (TextView) findViewById(R.id.Register_text_Activity_consent);
consent_TextView.setVisibility(View.GONE);
TextView remind_TextView = (TextView) findViewById(R.id.Register_text_Activity_remind);
remind_TextView.setVisibility(View.GONE);
Please remember that I want to change this in Java not XML because if I do it in XML I will hide the box for everyone. Thanks!
As said in the comments, you can get a reference to the box you want to set the visibility either through View#getParent() or you can set its id programatically via View#setId(), bind it and set its visibility.

Android set autoLink attribute programmatically

<TextView
android:text="123456789"
android:autoLink="phone">
</TextView>
I want to create this TextView from code, however I am encountering countless problems.
In the first place, I got halfway creating a TextView and adding this:
tw_phone.setAutoLinkMask(0x04);
This resulted in a clickable TextView, but when you clicked, a toast said "No application can perform this action", or something similar. I also tried with
Linkify.addLinks(tw_phone, Linkify.PHONE_NUMBERS); //and .ALL
but it gave me the same result.
When I decided to ask on StackOverflow, I tried to strip my code down incase there were something wrong with the way I have used Layouts (you never know), but now I'm not even able to make a TextView clickable. This is the code that, in my opinion, should work as it is just a stripped down version of what I am using deeper in my code.
TableLayout table = (TableLayout) findViewById(R.id.tableResult);
TableRow row = new TableRow(this);
TextView tw = new TextView(this);
tw.setText("123456789");
tw.setAutoLinkMask(Linkify.ALL);
row.addView(tw);
table.addView(row);
Can someone write a simple, small example of how you create a TextView, give it a number as text and then allows the user to click on it and choose whatever app they want to open the number with??
If you can point out whats wrong in my code aswell, that would be great, but I would much rather just get the answer straight away. The things I have tried are taken from other StackOverflow questions and answers.
TextView tv_contatti2 = new TextView(this); tv_contatti2.setText(contatti);
Linkify.addLinks(tv_contatti2, Linkify.PHONE_NUMBERS);
tv_contatti2.setLinksClickable(true);
where "contatti" has value +39012345678 with international prefix
In order to make text view clickable with any url try following code :
Linkify.addLinks(textView, Linkify.WEB_URLS)

Add textview in Cocos2d really need

I need to implement TextView widget in cocos2d-android-1 and don't know how please help
TextView textView = null;
textView.setText(R.string.billing_not_supported_message);
addChild(textView);
This code not working because addChild needs node. Please help really need.
I completely agree that the android version of cocos2d needs some serious 'standard UI' features adding to it.
At the moment, your only real option is to take a similar approach to my previous answer here
ANDROID:How to open web page in class extends CCLayer
whereby you have a layout which will put a textview on the screen (or you construct it yourself in your handler), and you use a handler from the activity which your scene is running in to show/hide it.
It's clunky and horrible but it works. In my field designer app i faced the same problem, but i also had to have a custom background to the text field, which resized with the text field, that had a rough edge, and the text view had to fall inside that rough edge so all the text was visible on the main bit of the background.
i achieve that using this same technique, but i created a layout xml file so that i had control over how the textview and it's background were displayed.
(note to show/hide the textview i had to give its root layout a constant ID and check for if that ID existed, and was visible, as when people touched outside it, i needed to make it vanish)

Android App Development, Dynamic Buttons & Fields

Let me preface my question by saying I have done a lot of research in creating dynamic buttons within an android app, and most are simply wrong or have a different view of dynamic than I do. If I missed something then just post the link and I'll check it out.
What I'm looking for is a way to create a button within my app based on information I gather from internet sources. For instance, when someone creates a post on a forums that I care to see, the app will find this, parse it for me, and return some info. Since I can't fit all this info on screen for each post that shows up, I want to create a button dynamically that previews this info. Such as name of user, date, and short description (as a preview that by clicking, will give all of the inforamtion in a separate activity). For the sake of this post, lets pretend I get this info from a text-entry location (not from an actual internt forum post).
First and foremost, how do I create the button dynamically? The other half of my question is less important to me. I would like to do this programmatically. Links to tutorials are great.
Secondly, and less important... Once I have created this button dynamically, how can I get custom views of the button based on a predictable format.
If anything is unclear, just ask and I'll try to clarify. Thanks for all your help!
In my opinion the right approach is to have an Adapter that will map the data to a certain view (a button in your case).
What adapter you choose will be depending on how you decide to fetch and store the data from internet.
When there are new posts you will be adding them to the data source (a database, a list, etc...) and you will call notifyDataSetChanged which will refresh the list, dynamically creating as many views as needed to display all the data.
I think this answers your question. The idea is to programatically 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 container is the layout that will hold the button (i.e. will become it's parent). You can also add layout settings to the button if desired.
Well, you can create a new Button, and set an onClickListener as so:
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 ID, you can call the method as you wish. You will need then add it to your layout as so:
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);
There is of course more you can do with it, but that should get you started, and anything else you need you should be able to find by perusing the docs.
If you need help with anything more specifically, then just comment and I will attempt to elaborate.

Dynamically Displaying Text with TextView (Android)

Basically what I want to do in my Android app is use TextView to display two different pieces of text at once. So in code, I want to be able to do something like this:
LinearLayout ll = new LinearLayout(this);
TextView text = new TextView(this);
text.setTextColor(0xFF000000);
text.setGravity(Gravity.CENTER_HORIZONTAL);
text.setTextSize(20f);
text.setText("Text1");
text.setTextSize(14f);
text.setColor(0xFF0000FF);
text.setText("\nText2");
ll.addView(text);
To clarify, I am trying to display a black "Text1" and a blue "Text2" at once using only a single TextView. Obviously this doesn't work out using the code above. I've considered using a second TextView but that seems like a waste of effort and memory to me. I'm sure the brilliant minds here can come up with the best solution to this.
Thank you very much in advance for your time and your assistance.
There are two options for you.
One is
Spannable
and other is
fromHtml (String source)
So that you can get your desired output.
I think with the current version of the code, you can see only the latest text (Text2).
If you want to have multiple look and feel for two texts, I would suggest use 2 separate TextViews. It would add more flexibility.
If you are not going to change this UI code later, then you can consider Html.toHtml() in setText() call.
It seems the problem is with:
LinearLayout.addView(text);
You are trying to add a view to a LinearLayout, but the layout doesn't exist (in the current activity). You need to add the TextView to a Layout defined in the .xml you are using. Suppose you have a LinearLayout with id "linearlayout01" in the xml file "activity1.xml", you would do something like:
setContentView(R.layout.activity1);
// Create and adjust TextView text
...
LinearLayout layout = (LinearLayout) findViewById(R.id.linearlayout01);
layout.addView(text);
Once a View is added to a ViewGroup of which LinearLayout is a descendant you do not need to readd it to update its display. If you preform any changes on a view that requires it to change its display it will handle all the proper notifications about required redraws or relayouts by calling View#invalidate and View#requestLayout where appropriate.
In addition, because all UI changes are handled on the same thread you do not need to worry about calling multiple methods that will update the UI. This is because of two reasons, first, the execution of the redraws will not occur until your code is finished, second, android has optimizations built in that combines multiple invalidate calls into one.
So, the only thing you need to worry about is getting a proper reference to your TextView instance and then you can call all the methods on it that you need to make it display what you wish.
Since you are creating your Views manually and not from xml you need to add your root ViewGroup to the Activity by calling Activity#setContentView.
Edit:
Then you're going to need to learn about SpannableString and SpannableStringBuilder. There is some very brief documentation here: Selecting, Highlighting, or Styling Portions of Text
when do you plan to update the textview ? If it is on click of a button then get a reference to the textview and in the onClickListener() update the text, color, etc whatever you want to do.
After seeing your other comments, I think SpannableString is what you are looking for

Categories