How do I create a new TextView and display it? (with programming) - java

I'm still new to Android Studio, and for practice I want to make a simple To-Do list app. I'm troubles with creating a new TextView and displaying it.
I know that I probably need to manually insert the TextView into the layout, but I have no idea how to do that.
Here's my MainActivity code:
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
Button submitBtn;
EditText userInput;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
submitBtn = (Button)findViewById(R.id.submitBtn);
userInput = (EditText)findViewById(R.id.userInput);
submitBtn.setOnClickListener(this);
}
#Override
public void onClick(View v) {
if(v.getId() == R.id.submitBtn) {
String userInputString = userInput.getText().toString();
TextView listItem = new TextView(this);
listItem.setText(userInputString);
}
}
}
I am using ConstraintLayout for my layouting. If somebody could help guide me to the right direction, that would be greatly appreciated, thanks in advance!

In the simplest case, you get your ConstraintLayout using its ID and then call addView(View) with your TextView as the parameter on it.
Assuming your ConstraintLayout has the id myLayout:
ConstraintLayout layout = (ConstraintLayout)findViewById(R.id.myLayout);
layout.addView(listItem);

Related

Change image view with radio button and two activitis

I need to help whith my school project.
I have two activitis - main activity whith image view and setting activiti whit two radio button. I want the image on main activity to change when I clicked on radio button on setting activity.
This is my code of setting activity:
private RadioGroup radioGroup;
private ImageView imageView;
private Integer []photos = {R.drawable.red, R.drawable.blue};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.imageView = (ImageView ) findViewById(R.id.imageView);
this.radioGroup = (RadioGroup) findViewById(R.id.radioGroup);
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup radioGroup, int id) {
RadioButton radioButton = (RadioButton) radioGroup.findViewById(id);
int index = radioGroup.indexOfChild(radioButton);
imageView.setImageResource(photos[index]);
}
});
}
Thanks for the answers.
First thing you can't findViewById in class B within the id in class A
Second thing there are many ways to do that what you want
for example you can use interface
or static variable and this solution is so easy
just create an static variable in main activity and in OnResume function do your logic
somthing like this
static Integer photos = R.drawable.red;
Whey static??
because you can access it from any where
so just change it from your setting activity

Generate any number of Views

I want to generate multiple TextViews based on how many semicolons the users enters between inputs in an EditText.
And it works so far, the only problem is that all TextViews are on top of each other, so I need to have the second TextView be positioned below the first and so on.
I tried to use setY but it did not change the position of the Views or they disappeared completely.
Is there a method to achieve this or is this a case for the LayoutInflater?
public class MainActivity extends AppCompatActivity {
private EditText et1;
private ScrollView sv1;
private ConstraintLayout cl1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et1 = findViewById(R.id.editText);
sv1 = findViewById(R.id.scrollView);
cl1 = new ConstraintLayout(this);
sv1.addView(cl1);
}
public void splitToChips(View v) {
String content = et1.getText().toString();
String[] products = content.split(";");
TextView[] textViews = new TextView[products.length];
for(int i = 0; i<products.length; i++) {
textViews[i] = new TextView(this);
cl1.addView(textViews[i]);
textViews[i].setText(products[i]);
}
}
}
You should add views to LinearLayout. In my case, I created a layout with a Button, EditText, and LinearLayout along with a vertical orientation inside of a ScrollView. Then, in the Java code, I simply added the following line to the loop:
mLinearLayout.addView(textViews[i]);

Unable to display textview on button click in android

I want to create this textview, and then display it after this button is clicked, but no textview is displayed.
I dont't want to use findViewById(), if possible, because I want to create a new textview every time the button is pressed. I've tried making a linear layout first, but I've seen a lot of websites say that you don't need to, and I would prefer not to. Any advice would be helpful. Thank you.
EditText name=layout.findViewById(R.id.enterName);
final String Name=name.getText().toString();
Button create=layout.findViewById(R.id.create);
create.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
TextView ProgrammaticallyTextView = new TextView(MainActivity.this);
ProgrammaticallyTextView.setText(Name);
ProgrammaticallyTextView.setTextSize(22);
popup.dismiss();
}
});
There are no error messages and the logcat doesn't say that anything is wrong.
Try like this :
private LinearLayout lLayout;
private EditText lEditText;
private Button lButton;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
lLayout = (LinearLayout) findViewById(R.id.linearLayout);
lButton = (Button) findViewById(R.id.button);
lEditText = (EditText) findViewById(R.id.editText);
lButton.setOnClickListener(onClick());
TextView textView = new TextView(this);
textView.setText("Text New");
}
private OnClickListener onClick() {
return new OnClickListener() {
#Override
public void onClick(View v) {
lLayout.addView(createTextView(lEditText.getText().toString()));
}
};
}
private TextView createTextView(String text) {
final LayoutParams loutParams = new
LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
final TextView textView = new TextView(this);
textView.setLayoutParams(loutParams );
textView.setText("Text is " + text);
return textView;
}
Use the activity's findViewById() method to reference your layout views. For example, you can replace
EditText name=layout.findViewById(R.id.enterName);
Button create=layout.findViewById(R.id.create);
with
EditText name=getActivity().findViewById(R.id.enterName);
Button create=getActivity().layout.findViewById(R.id.create);
Note: if you are not using fragments then there is no need to use getActivity since findViewById() is a method of the superclass AppCompactActvity( or Activity).
I guess your code is not working because the Button View and Editext Views have not been reference when activity starts for the oncreate() method

Android: Create EditText on Runtime

I'm trying to create a view where the user can click a "plus" button, and have additional EditTexts be created. The goal is to have a base of 2 EditTexts, and each time the user clicks the button, add another 2 EditTexts.
How can I do this? I can add EditTexts from Java, but I can't figure out how to add and handle a list of them dynamically.
I was hoping to take however many pairs of EditTexts, and push it into a key/value HashMap or something.
Any ideas of how to do this? Thanks!
public class MyActivity extends Activity {
private LinearLayout main;
private int id = 0;
private List<EditText> editTexts = new ArrayList<EditText>();
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
main = new LinearLayout(this);
main.setOrientation(LinearLayout.VERTICAL);
Button addButton = new Button(this);
addButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
addEditText();
}
});
Button submit = new Button(this);
submit.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
for (EditText editText : editTexts) {
editText.getText().toString();
// whatever u want to do with the strings
}
}
});
main.addView(addButton);
main.addView(submit);
setContentView(main);
}
private void addEditText() {
LinearLayout editTextLayout = new LinearLayout(this);
editTextLayout.setOrientation(LinearLayout.VERTICAL);
main.addView(editTextLayout);
EditText editText1 = new EditText(this);
editText1.setId(id++);
editTextLayout.addView(editText1);
editTexts.add(editText1);
EditText editText2 = new EditText(this);
editText2.setId(id++);
editTextLayout.addView(editText2);
editTexts.add(editText2);
}
Do it in a ListView.
Then you can just add them to a ListAdapter.
And then use adapter.notifyDatasetChanged()
May be I am not clear but Instead of adding Individual edit text you can add as Group View like Linear layout here you can use any flag values to add dynamic name conversions also.
That view you can update into List View like inflating rows in the List View....

Creating a spinner dynamically

I was struggling to create a spinner dynamically. I had it on the page but every time I tried to select an option it would blow up. My original code is at the bottom. I fixed it by moving the addSpinner() function outside of the inner class and changing
Spinner newSpinner = new Spinner(getApplicationContext());
to
Spinner newSpinner = new Spinner(this);
It's fixed but I have no idea what it didn't work initially. Can anyone explain? (apologies if it's a noob question - I am new to both Java and android)
public class SpotManageActivity extends Activity
{
private SimpleCursorAdapter mSpots;
#Override
protected void onCreate(Bundle savedInstanceState)
{
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.reminder_manage_activity);
Button add_new_button = (Button) findViewById(R.id.add_new_spot_button);
add_new_button.setOnClickListener(new ButtonOnClickListener());
}
public class ButtonOnClickListener implements View.OnClickListener
{
#Override
public void onClick(View v)
{
addSpinner();
}
private void addSpinner()
{
LinearLayout layoutHolder =
(LinearLayout) findViewById(R.id.layout_holder);
LinearLayout spinnerHolder = new LinearLayout(getApplicationContext());
spinnerHolder.setOrientation(LinearLayout.HORIZONTAL);
spinnerHolder.setLayoutParams(
new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.FILL_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT, 1f));
Spinner newSpinner = new Spinner(getApplicationContext());
newSpinner.setLayoutParams(
new Spinner.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
newSpinner.setAdapter(mSpots);
layoutHolder.addView(spinnerHolder);
spinnerHolder.addView(newSpinner);
// A non-internal spot was selected
}
}
}
I am not sure at all, but if in the stack trace you're getting something about a wrong context, it's probably because a Spinner, when clicked, opens a Dialog, and a Dialog needs an Activity context.
For more info:
Android - what's the difference between the various methods to get a Context?
Dialog throwing "Unable to add window — token null is not for an application” with getApplication() as context

Categories