Reputation: 831
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.
Upvotes: 1
Views: 6213
Reputation: 24848
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
}
}
}
Upvotes: 1
Reputation: 51581
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.
Upvotes: 1
Reputation: 1750
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;
}
}
Upvotes: 1
Reputation: 1856
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
}
}
});
Upvotes: 2
Reputation: 2182
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"
Upvotes: 0
Reputation: 26017
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.
Upvotes: 2
Reputation: 6738
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
Upvotes: 1