Dynamically adding textbox in android - java

I am working on an app and need to add a textbox to the View when a button is selected. How can I do that or add any object dynamically. What class do I use or what method do I need to call? Thanks.

You just need to call the addView method to the target view. It's a method inherited from ViewGroup, see [here][2].
[2]:http://developer.android.com/reference/android/view/ViewGroup.html#addView(android.view.View, android.view.ViewGroup.LayoutParams)

One possibility is to define it in your layout XML, and set:
android:visibility="gone"
Then in your code you can do:
TextView myTextBox = (TextView) findViewById(R.id.myTextBoxId);
myTextBox.setVisibility(View.VISIBLE);
and to hide it again:
myTextBox.setVisibility(View.GONE);
Here is the documentation

Related

Eclipse JFace how to dynamically add SWT view

My view (Show Result) is defined in plugin.xml. Which is working as expected when I run the plugin Show Result widget is getting displayed. Now I want to load the view dynamically based on results. Such as for each result it should load the new Show Result (e.g Show Results1, Show Results2 etc) widget.
Any suggestion on how it can be achieved?
<view
allowMultiple="true"
class="com.eclipse.plugin.MyResultView"
icon="icons/res.png"
id="view1"
name="Show Results"
restorable="true">
</view>
If you want to show multiple instances of the same view use the
IViewPart showView(String viewId, String secondaryId, int mode);
method of IWorkbenchPage. The view needs to be defined with allowMultiple="true" in the plugin.xml.
The secondaryId is a arbitrary string that distinguishes the views, it can have any value (except it should not contain a :).
To set the name of the view call the ViewPart.setPartName(String name) method in the view part.

Android checkbox.isChecked is not working

I am trying to create a like and dislike button. I use Checkboxes to do so.
In the XML code I have two checkboxes one called like and the other dislike
I'm trying to toggle between the like and dislike buttons. Such that they both cannot be switched on at the same time.
public void onLike(View view) {
if (dislike.isChecked()) {
dislike.setChecked(false);
}
Toast.makeText(this,"liked",Toast.LENGTH_SHORT).show();
}
The issue that I am having is that set setChecked(true) is not doing anything.
For more context, the XML for the checkbox is defined inside a fragment that has a cardview. Each item in the card view has the checkboxes.
the way I initialized the checkbox in the main activity is as follows: -
View cardViewLayout = getLayoutInflater().inflate(R.layout.text_row_item,null);
like = (CheckBox) cardViewLayout.findViewById(R.id.like);
dislike = (CheckBox) cardViewLayout.findViewById(R.id.dislike);
any ideas what's going on?
ok, I've figured out the solution. Since I am using a recycler view with a custom adapter I need to bind the onClick listener via an interface.
Here is a link to another post that will show the necessary steps to implement click listeners in adapters: https://stackoverflow.com/a/49969478/11379938

How to choose a view group in my android app

I am developing an app and have successfully added a floating action button using the library shown here. The floating button displays well but when i navigate to another fragment through the navigation drawer the button still displays instead i want the button to only display in the activity i created it. I checked for those that had similar issues online and i saw comments...saying that i have to set the View by modifying this line of code found within the method show below.
ViewGroup root = (ViewGroup) activity.findViewById(android.R.id.content);
root.addView(button, params);
Please can tell me how to achieve this, thanks in advance.
Library Method
public FloatingActionButton create() {
final FloatingActionButton button = new FloatingActionButton(activity);
button.setFloatingActionButtonColor(this.color);
button.setFloatingActionButtonDrawable(this.drawable);
params.gravity = this.gravity;
ViewGroup root = (ViewGroup) activity.findViewById(android.R.id.content);
root.addView(button, params);
return button;
}
if you can access the FAB view using findViewById you can simply do:
button.setVisibility(VISIBILITY.GONE);
to hide it, that way you can set visibility back to visible when you go back to that activity.
or if you can access view called 'root' (or can access button via getActivity().findViewById for Fragments)
root.removeView(button);
Set the FloatingActionButton to be invisible by default in the XML file:
<ImageButton android:id="#+id/button" android:visibility="invisible"/>
and make it visible by using the following code in the required activity class:
button.setVisibility(View.VISIBLE);

Android setContentView with Textview

I'm completely new to Android application programming, and I was reading through Google's tutorial: http://developer.android.com/training/basics/firstapp/starting-activity.html.
On this page, under the "Display the Message" section, they create TextView object, and use setContentView with the textView object as the argument, to display some text. I was wondering, if I'm understanding correctly, instead of creating the TextView object within the code, can you define it in XML instead? If you define it in XML, would that require you to create a new XML file besides main_activity.xml? Thanks.
You can declare all your layouts and views inside a xml. For the given example, the code would look like the following
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Set your parent view
setContentView(R.layout.main_layout);
// Get the message from the intent
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
// Get the reference to the TextView and update it's content
TextView textView = (TextView)findViewById(R.id.my_text_view);
textView.setText(message);
}
And your main_layout.xml would look like
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="40sp"
android:id="#+id/my_text_view"/>
You can arbitrarily create view files (XML files) and specify the primary type of the view and the children it contains. You could create the TextView element within the main_activity.xml view and find it by the relative Id.
That being said in the article in question if you want to have a separate view for just the TextView element then you would likely need another XML file to define that view if you do not want to specify it programmatically.
In a standard application you will likely have a predefined view (XML file) that you will set as the content view and reference elements from within it (as well as possibly add new elements).
It is very flexible, in short to answer your question no, you do not need to generate a new XML view file, you could simply add a TextView to an existing view file or specify it at runtime.

Adding buttons on xml file other than main

I have two xml files in my application - main.xml and options.xml.
In both of them I use buttons. The problem is, that while interacting with the buttons in main.xml, I cannot do that with options.xml: if I write
Button b = (Button)findViewById(R.id.b1);
, b is going to be null. What is the cause of this problem and how do I fix it?
Thank you in advance.
You need to either inflate the options.xml or set it as content view:
setContentView(R.layout.options);
before you can use the views in that layout file.
It sounds like you want to be able to access both layouts so you should do something like this:
View view = LayoutInflater.from(context).inflate(R.layout.options, null);
Button b = (Button) view.findViewById(R.id.b1);

Categories

Resources