I've got a button that inserts user information into a database.
On top of this, I would like it to display the users entered details below in a list,
#Override
public void onClick(View v) {
insertuser();
LinearLayout createworkout = (LinearLayout)findViewById(R.id.createworkout);
TextView x = new TextView(getApplicationContext());
x.setText(editTextExercise.toString());
createworkout.addView(x);
}
This is what I have so far. When clicking the button the information is inserted, but I can not see a visible Textview. No errors are thrown either, I don't know what I have done wrong here.
All help appreciated
Thank you
Have your LinearLayout orientation property set?
try to add:
android:orientation="vertical"
or:
android:orientation="horizontal"
Try to change it as...
#Override
public void onClick(View v) {
insertuser();
LinearLayout createworkout = (LinearLayout)findViewById(R.id.createworkout);
TextView x = new TextView(YourActivity.this);
x.setText(editTextExercise.toString());
LayoutParams lp = new LayoutParams ( LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
createworkout.addView(x,lp);
}
Related
In my app there is a Spinner for colors and a Button for the user to add additional spinners in different colors. An existing spinner is coupled with a delete button for removal upon creation. It looks like this -
I'm trying to accomplish the following:
The user clicks "ADD PAINTYPE"
A Spinner is created, given it is not already present in the List
If the user deletes the item, it is available to insert again.
I´m pretty new to Android App development, and this logic is difficult for me to work through. Any suggestions on how to approach this would be appreciated.
Edit: Blow down is the function, which called after user click the button "ADD PAINTYPE". The arrayAdapter of the newly created spinner is as same as the fisrt one(which in the left side of the "ADD PAINTYPE" button). Right now all Spinners are sharing the sam adapter. What I want to achieve is, When I choose a Option in one spinner, then dynamiclly the option will not be listed in other spinners.(e.g. The "ANFALLSTARTIGER" would not be listed in the second and the third spinners). Is this possible?
#RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
protected void addItem(Context context,View view) {
if(clickedTimes < 16) {
clickedTimes++;
final LinearLayout linearLayout = new LinearLayout(context);
Spinner spinner = new Spinner(context);
spinner.setPopupBackgroundResource(R.color.white);
spinner.setBackgroundColor(getResources().getColor(R.color.white));
final Button button = new Button(context);
final Button button_delete = new Button(context);
button_delete.setText("DELETE");
button_delete.setTextColor(getResources().getColor(R.color.white));
button_delete.setBackground(context.getDrawable(R.drawable.mybutton));
linearLayout.setOrientation(LinearLayout.HORIZONTAL);
linearLayout.setWeightSum(6);
LinearLayout.LayoutParams params_0 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
linearLayout.setLayoutParams(params_0);
LinearLayout.LayoutParams params_1 = new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.MATCH_PARENT, 4.0f);
spinner.setLayoutParams(params_1);
params_1.setMargins(2, 2, 2, 2);
LinearLayout.LayoutParams params_2 = new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.MATCH_PARENT, 1.0f);
params_2.setMargins(2, 2, 2, 2);
button.setLayoutParams(params_2);
button_delete.setLayoutParams(params_2);
LinearLayout layout = (LinearLayout) view.findViewById(R.id.all_paintype);
layout.addView(linearLayout);
linearLayout.addView(spinner);
linearLayout.addView(button);
linearLayout.addView(button_delete);
button_delete.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
LinearLayout layoutAll = (LinearLayout) view.findViewById(R.id.all_paintype);
layoutAll.removeView(linearLayout);
spinners.remove(spinner);
clickedTimes--;
}
});
dropBoxButton(spinner, button, clickedTimes);
spinners.add(spinner);
}else {
Toast toast = Toast.makeText(getContext(),"Maximal Pain Type",Toast.LENGTH_SHORT);
toast.show();
}
}
protected void dropBoxButton(Spinner spinner, final Button button, int clickedTimes) {
arrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(arrayAdapter);
spinner.setSelection(clickedTimes);
spinner.setVisibility(View.VISIBLE);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#RequiresApi(api = Build.VERSION_CODES.KITKAT)
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String string_1 = ((TextView) view).getText().toString();
setButtonColor(button, string_1);
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
I want to make it so that every time someone types something in the editText and clicks the button, the text comes up. It does it in my code, but I also want to add a checkbox next to it. How can I add the checkbox every time the button is pressed?
This is how it looks in the emulator right now after I typed test in the editText:
https://gyazo.com/6b9a050976ecd2c4b509220263bbdce1
The second problem is: when I write a second todo, it overwrites the last one so right now I can only have 1 todo. Why?
Code:
final TextView textViewPrint;
Button btn_print;
final EditText editTextType;
textViewPrint = findViewById(R.id.print_text);
btn_print = findViewById(R.id.button_add);
editTextType = findViewById(R.id.test_textEdit);
btn_print.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
textViewPrint.setText(editTextType.getText().toString()) ;
editTextType.setText("");
}
});
You can add to your layout programmatically after it has been inflated. Add an id to your LinearLayout:
android:id ="#+id/layout"
Then in OnCreate (after your existing code), get a reference to it and add controls as you wish. And add these lines in OnCreate, For example:
LinearLayout l = (LinearLayout)findViewById(R.id.layout);
CheckBox cb = new CheckBox(this);
int lHeight = LinearLayout.LayoutParams.MATCH_PARENT;
int lWidth = LinearLayout.LayoutParams.WRAP_CONTENT;
l.addView(cb, new LinearLayout.LayoutParams(lHeight, lWidth));
setContentView(l);
First You need add to LinearLayout layout
LinearLayout l = (LinearLayout)findViewById(R.id.layout);
btn_print.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
CheckBox cb = new CheckBox(this);
LayoutParams lparams = new LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
cb.setLayoutParams(lparams);
l.addView(cb);
}
});
I was checking out a few online tutorials for getting input from a user using a button such as [here][1], however it is not what I was after and wanted to know if any of you have come across a similar problem.
I want to get the user to get prompted to enter an integer input ONLY when the button is clicked and I dont want the TextField etc to show on the screen until the user has clicked the button. The user input has to be saved in the int bias.
Any help would be appreciated.
Whatever is the textField that you are talking about, you can set input type like:
textFieldName.setRawInputType(Configuration.KEYBOARD_12KEY);
You can directly set it in the XML like:
android:inputType="number"
Also, even when you create the TextField, you can set the visibility to Invisible using :
textFieldName.setVisibility(View.INVISIBLE);
Then when user clicks the button you can set it to Visible using:
textFieldName.setVisibility(View.VISIBLE);
You can change the visibility on Button click as you have mentioned in the question.
You can add this textfield in the layout and set its visibility "gone"
boolean isClicked = false;
String userInput = null;
Button grayScaleFilterButton = (Button) findViewById(R.id.filter1_button);
grayScaleFilterButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(isClicked){
isClicked = false;
//Got the input and hide the textfield.
userInput = textField.getText().toString();
textField.setVisibility(View.GONE);
}else{
//make the textfield visible and user can enter the input.
isClicked = true;
textField.setInputType(InputType.TYPE_CLASS_NUMBER);
textField.setVisibility(View.VISIBLE);
//do something
}
}
});
Try the following code. The comments will guide you through it:
grayScaleFilterButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Show PopupWindow
showPopup();
// Everything after this will be handled in `doneInput(String)` method
}
});
Declare the PopupWindow as a global variable:
// Right before onCreate(Bundle)
PopupWindow popupWindow;
Create a method showPopup() in your activity:
public void showPopup() {
// Container layout to hold other components
LinearLayout llContainer = new LinearLayout(this);
// Set its orientation to vertical to stack item
llContainer.setOrientation(LinearLayout.VERTICAL);
// Container layout to hold EditText and Button
LinearLayout llContainerInline = new LinearLayout(this);
// Set its orientation to horizontal to place components next to each other
llContainerInline.setOrientation(LinearLayout.HORIZONTAL);
// EditText to get input
final EditText etInput = new EditText(this);
// TextView to show an error message when the user does not provide input
final TextView tvError = new TextView(this);
// For when the user is done
Button bDone = new Button(this);
// If tvError is showing, make it disappear
etInput.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
tvError.setVisibility(View.GONE);
}
});
// This is what will show in etInput when the Popup is first created
etInput.setHint("Please provide a number");
// Input type allowed: Numbers
etInput.setRawInputType(Configuration.KEYBOARD_12KEY);
// Center text inside EditText
etInput.setGravity(Gravity.CENTER);
// tvError should be invisible at first
tvError.setVisibility(View.GONE);
bDone.setText("Done");
bDone.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// If user didn't input anything, show tvError
if (etInput.getText().toString().equals("")) {
tvError.setText("Please enter a valid value");
tvError.setVisibility(View.VISIBLE);
etInput.setText("");
// else, call method `doneInput()` which we will define later
} else {
doneInput(etInput.getText().toString());
popupWindow.dismiss();
}
}
});
// Define LayoutParams for tvError
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
layoutParams.topMargin = 20;
// Define LayoutParams for InlineContainer
LinearLayout.LayoutParams layoutParamsForInlineContainer = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
layoutParamsForInlineContainer.topMargin = 30;
// Define LayoutParams for EditText
LinearLayout.LayoutParams layoutParamsForInlineET = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
// Set ET's weight to 1 // Take as much space horizontally as possible
layoutParamsForInlineET.weight = 1;
// Define LayoutParams for Button
LinearLayout.LayoutParams layoutParamsForInlineButton = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
// Set Button's weight to 0
layoutParamsForInlineButton.weight = 0;
// Add etInput to inline container
llContainerInline.addView(etInput, layoutParamsForInlineET);
// Add button with layoutParams // Order is important
llContainerInline.addView(bDone, layoutParamsForInlineButton);
// Add tvError with layoutParams
llContainer.addView(tvError, layoutParams);
// Finally add the inline container to llContainer
llContainer.addView(llContainerInline, layoutParamsForInlineContainer);
// Set gravity
llContainer.setGravity(Gravity.CENTER);
// Set any color to Container's background
llContainer.setBackgroundColor(0x95000000);
// Create PopupWindow
popupWindow = new PopupWindow(llContainer,
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
// Should be focusable
popupWindow.setFocusable(true);
// Show the popup window
popupWindow.showAtLocation(llContainer, Gravity.CENTER, 0, 0);
}
At last, the doneInput(String) method:
public void doneInput(String input) {
int bias = Integer.parseInt(input);
// Work with it // For example, show a Toast
Toast.makeText(this, "Number input by user was: " + bias, Toast.LENGTH_LONG).show();
// Do anything else with input!
}
Note: No xml files are required for this. Copy, follow the instructions, and paste to try it out. You can show this PopupWindow anywhere on screen, in any size, and in any color.
to display on number in keyboard add this line android:inputType="number" to your EditText
<EditText android:id="#+id/edittext"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="number"
/>
if you want to display edittext into dialog then use customdialog Dialogs Example and use mDialog.show() in button click listener.
here mDialog is object of Dialog
on button click it shows an alterDialogue for user to input. once some value is inputted it shows it in the corresponding textview. Here is the whole code:
package com.example.testandroidapp;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity {
TextView input1;
String value = "";
int bias;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button grayScaleFilterButton = (Button) findViewById(R.id.button1);
input1 = (TextView) findViewById(R.id.textView1);
grayScaleFilterButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
AlertDialog.Builder alert = new AlertDialog.Builder(v
.getContext());
alert.setTitle("Title");
alert.setMessage("Message");
// Set an EditText view to get user input
final EditText input = new EditText(MainActivity.this);
alert.setView(input);
alert.setPositiveButton("Ok",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int whichButton) {
value = input.getText().toString();
// Convert the inputted string into Integer
bias = Integer.parseInt(value);
}
});
alert.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int whichButton) {
// Canceled.
}
});
alert.show();
}
});
input1.setText(value);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
XML code
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:padding="5dp"
android:orientation="vertical" >
<EditText
android:id="#+id/edtInput"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="number"
android:visibility="gone" />
<Button
android:id="#+id/btnInput"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:text="Input"/>
</LinearLayout>
</LinearLayout>
Activity code
public class MyInputActivity extends Activity implements OnClickListener {
private EditText edtInput;
private Button btnInput;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.my);
edtInput = (EditText) findViewById(R.id.edtInput);
btnInput = (Button) findViewById(R.id.btnInput);
btnInput.setOnClickListener(this);
}
#Override
public void onClick(View arg0) {
if(edtInput.getVisibility() == View.VISIBLE){
edtInput.setVisibility(View.GONE);
// write your more code here on gone
}else{
edtInput.setVisibility(View.VISIBLE);
// write your more code here on visible
}
}
}
you can set input type number by this line in xml: using this code user can click 0,1,..9 digits only from sofrtware keyboard:
android:digits="1234567890" or android:inputType="number"
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....
I am having a silly issue with onClickListeners modifying GUI elements. Below I have a radioGroup holding two buttons, an EditText and a Spinner. When the choose_old radio button is selected, I simply wanted to toggle enable/disable between the Spinner and EditText. The variables select_run and new_run_name are both instance variables and are
obtained from my XML files.
select_run = (Spinner)runDialoglayout.findViewById(R.id.run_choice_spinner);
new_run_name = (EditText)runDialoglayout.findViewById(R.id.new_run_name);
RadioButton choose_old = (RadioButton)runDialoglayout.findViewById(R.id.select_old);
RadioButton choose_new = (RadioButton)runDialoglayout.findViewById(R.id.select_new);
choose_old.setOnClickListener(new OnClickListener(){
public void onClick(View arg0) {
Log.e("ERROR","GOT CLICK");
select_run.setEnabled(true);
new_run_name.setEnabled(false);
}
});
The 'GOT CLICK' message prints but I do not get the desired changes.
Note:
In trying solutions, I had wrapped the GUI changes in a handler.post with no effect.
If I set to enabled outside the onClickListener, it successfully changes it.
I've no doubt this is something dumb but can't seem to figure out why this is happening
Try putting this code in the listener.
boolean b = select_run.isEnabled();
select_run.setEnabled(!b);
new_run_name.setEnabled(b);
I tried the following code seems to work fine for me, please make sure views accessed in onClick are declared final
final Button button2 = (Button)findViewById(R.id.button2);
final EditText et = new EditText(this);
et.setText("Hi There");
RelativeLayout vg = (RelativeLayout)findViewById(R.id.relativeLayout1);
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.FILL_PARENT);
lp.addRule(RelativeLayout.BELOW, button2.getId());
vg.addView(et, lp);
final RadioButton rb = new RadioButton(this);
RelativeLayout.LayoutParams lp1 = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.FILL_PARENT);
lp1.addRule(RelativeLayout.RIGHT_OF ,button2.getId());
vg.addView(rb, lp1);
rb.setSelected(false);
rb.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
et.setEnabled(false);
}
});