Cannot get the text from the edit text widget (android) - java

I have no clue what I am doing wrong but when I print the text from the edit text (using getText().toString()), in the logcat it is always an empty string. I am wondering if it has to do with that it is being done inside the onClickfunction for the continue button. I'm putting the whole onCreate function because the code inside the onClick function seems to match exactly what countless tutorials have shown.
#Override
protected void onCreate(Bundle savedInstanceState)
{
Button add_test = new Button(this);
Button delete_test = new Button(this);
Button[] barray = new Button[100];
int trans_grey = Color.parseColor("#40000000");
final String[] test_types = new String[] {"Choose...","Terms", "Multiple choice", "Custom"};
final ArrayAdapter<String> type_adapter = new ArrayAdapter<String>(this,R.layout.spinner_item,test_types);
// Preliminary operations and display opening layouts
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
LinearLayout scroll_layout = (LinearLayout)findViewById(R.id.scroll_layout);
LayoutParams scroll_params = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT);
// Add the add a dynamic button
for (int index = 0; index <= 4; index++)
{
barray[index] = new Button(this);
barray[index].setBackgroundColor(trans_grey);
barray[index].setText("Array buttons");
barray[index].setTextColor(Color.parseColor("#CCffffff"));
scroll_layout.addView(barray[index], scroll_params);
}
add_test.setTextColor(Color.parseColor("#CCffffff"));
add_test.setBackgroundColor(trans_grey);
add_test.setText("Add a Test");
scroll_layout.addView(add_test, scroll_params);
add_test.setOnClickListener(new View.OnClickListener()
{#Override
public void onClick(View v)
{
AlertDialog.Builder add_test_builder = new AlertDialog.Builder(TestActivity.this);
final View add_test_view = getLayoutInflater().inflate(R.layout.add_test_menu, null);
Spinner type_spinner = (Spinner) add_test_view.findViewById(R.id.type);
add_test_builder.setView(add_test_view);
final Button continue_button = (Button) add_test_view.findViewById(R.id.continue_button);
AlertDialog dialog = add_test_builder.create();
dialog.show();
dialog.getWindow().setLayout(950,900);
type_spinner.setAdapter(type_adapter);
continue_button.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
View add_test_view = getLayoutInflater().
inflate(R.layout.add_test_menu, null);
// The view of the test attribute dialog
EditText test_title = (EditText) add_test_view.
findViewById(R.id.testTitle);
// Test title widget to hold title of test
String user_title = test_title.getText().toString();
Log.i(TAG, "onClick: " + user_title);
}
});
return;
}
});
// Add the delete a test button
delete_test.setTextColor(Color.parseColor("#CCffffff"));
delete_test.setBackgroundColor(trans_grey);
delete_test.setText("Delete a Test");
scroll_layout.addView(delete_test, scroll_params);
return;
}
}
Layout:
<EditText
android:id="#+id/testTitle"
android:maxLines="1"
android:lines="1"
android:inputType="textAutoCorrect"
android:layout_width="match_parent"
android:layout_height="35dp"
android:background="#00000000"
android:hint="Test Title"/>

Don't inflate the view yiu are assigning to dialog multiple times as you are creating multiple instances of it you are writing to one view but trying to fetch the data from another....Try doing it like this...
AlertDialog.Builder add_test_builder = new AlertDialog.Builder(TestActivity.this);
final View add_test_view = getLayoutInflater().inflate(R.layout.add_test_menu, null);
final EditText test_title = (EditText) add_test_view. findViewById(R.id.testTitle);
continue_button.setOnClickListener(new View.OnClickListener() {
#Override public void onClick(View v) {
String user_title = test_title.getText().toString();
Log.i(TAG, "onClick: " + user_title); } });

I think you should define EditText before and outside of your onlick method. Or get string after your onlick method.

IMO you may call editText string outside of Click method like above
final EditText test_title;
continue_button.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
View add_test_view = getLayoutInflater().
inflate(R.layout.add_test_menu, null);
// The view of the test attribute dialog
EditText test_title = (EditText) add_test_view.
findViewById(R.id.testTitle);
// Test title widget to hold title of test
}
});
String user_title = test_title.getText().toString();
Log.i(TAG, "onClick: " + user_title);

Related

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

going back to previous layout makes the button stop working

I am testing my knowledge of android studio and seem to be running into a problem I am trying to make an app that takes the value of a edittext and puts that value into a textview when you click a button. After that it brings you to a page with a back button on it and when I click that it brings me to the first page. But when I try to click the first button it doesn't work anymore.
Code:
public class TestApp extends AppCompatActivity {
private EditText ET;
private Button Add;
private TextView TV;
public String Array[] = new String[9999999];
private Button GoBack;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test_app);
ET = (EditText) findViewById(R.id.ET);
Add = (Button) findViewById(R.id.Add);
Add.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String getC = ET.getText().toString();
setContentView(R.layout.value);
TV = (TextView) findViewById(R.id.TV);
GoBack = (Button) findViewById(R.id.GoBack);
if (getC.length() > 0){
for (int i = 0; i < Array.length; i++){
if (Array[i] == null){
Array[i] = getC;
TV.setText(Reminders[i]);
}
}
}
GoBack.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
setContentView(R.layout.activity_calender_for_glasses);
}
});
}
});
}
}
thank you for your help.
If not necessary, don't modify the current content view. If you want multiple screens, you have to create multiple activities or fragments...
Moreover :
at the beginning, you inflate R.layout.activity_test_app
when you click on goBack button (1), you inflate R.layout.activity_calender_for_glasses and it's not the same as the first screen
(1) be careful to coding rules: https://source.android.com/setup/contribute/code-style

Why is this not creating a new table?

I have created a basic application to try and get this functionality working but I can't get this to work. When I click on the Add Table button it will create an instance of the layout file which includes a table layout and 3 table rows. I've got this far from just combining a bunch of answers to questions I've seen on here but I'm struggling to get this to work.
It creates two tables if I click on add table and so on if I keep clicking it. However the name is only changing for the first table like so:
Before:
After:
So table 1 should say "tableLayout0" and table 2 should say "tableLayout1". On each button click I want it to create a completely new table which just has the same layout properties.
int i;
Button addBtn;
LinearLayout linearLayout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
i = 0;
linearLayout = (LinearLayout) findViewById(R.id.linearLayout);
addBtn = (Button)findViewById(R.id.addBtn);
addBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
createTable();
i++;
}
});
}
public void createTable(){
String tableLayoutName = "tableLayout"+i;
TableLayout tableLayout = new TableLayout(this);
View tableCell = this.getLayoutInflater()
.inflate(R.layout.table_details, null, false);
tableLayout.addView(tableCell);
linearLayout.addView(tableLayout);
Button addSetBtn;
addSetBtn = (Button)findViewById(R.id.addSetBtn);
addSetBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
toastMessage("Click Registered");
}
});
TextView exerciseNameTextView = (TextView)findViewById(R.id.exerciseNameTextView);
exerciseNameTextView.setText(tableLayoutName);
}
//Adaptable Toast Message
private void toastMessage(String message){
Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
}

Adding Dynamic Text View

Trying to create notes application. I am trying to add dynamic text view after button click and it should be added to layout, one after another. In my code, only one text view is getting added successfully but from second time, app got crashed. Please help
XML Code:
<EditText
android:id="#+id/text_thingsToDo"
android:layout_width="#dimen/ThingsToDo_EditText_Width"
android:layout_height="#dimen/ThingsToDo_EditText_Height"
android:layout_margin="#dimen/Standardize_Margin"
android:background="#color/ThingsToDo_EditText_Color"/>
<Button
android:id="#+id/save_thingsToDo"
android:layout_toRightOf="#+id/text_thingsToDo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/ThingsToDo_Save_Button_Text"
android:layout_marginRight="#dimen/Standardize_Margin"
android:layout_marginTop="#dimen/Standardize_Margin"
android:layout_marginBottom="#dimen/Standardize_Margin"
android:background="#color/ThingsToDo_Save_Button"
android:textColor="#color/White"
android:layout_centerVertical="true"/>
Java Code:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_things_to_do);
final EditText editText = (EditText) findViewById(R.id.text_thingsToDo);
Button button = (Button) findViewById(R.id.save_thingsToDo);
final TextView notesView = new TextView(this);
final LinearLayout linearLayout = (LinearLayout) findViewById(R.id.rootView_ThingsToDo);
final ArrayList<String> arrayList = new ArrayList<String>();
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
notesView.setText(editText.getText().toString());
linearLayout.addView(notesView);
}
});
}
Perhaps create a new TextView every time you press the button? Try to declare the TextView as a class member and then in your OnClickListener do this:
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
notesView = new TextView(this);
notesView.setText(editText.getText().toString());
linearLayout.addView(notesView);
}
});
Hopefully this will fix your problem!

I want to access values from a popup back to the activity from which the popup has been called

I've an Activity where I've a button which will lead to popup popup window in the same Activity. In that popup, I've multiple fields. And What I all want is to get back those values from popup to the same activity.
I've been stuck with it. Need a help :)
Related Code as follows.
This is the code in onCreate method for calling the popup.
Button button = (Button) findViewById(R.id.button9);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
addProductDetails();
}
});
By calling addProductDetails() method popup gets displayed.
So in this method, the code as follows
private String addProductDetails() {
LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.storeproductdetails_layout, (ViewGroup) findViewById(R.id.popup_element1), false);
final PopupWindow pwindo = new PopupWindow(layout, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);
//get txt view from "layout" which will be added into popup window
//before it you tried to find view in activity container
/* Field Data */
product1Code = (EditText) layout.findViewById(R.id.productCodeee);
quantity1 = (EditText) layout.findViewById(R.id.quantityy);
details1 = (EditText) layout.findViewById(R.id.editText23);
orderValue1 = (EditText) layout.findViewById(R.id.editText36);
productC = String.valueOf(product1Code.getText());
qty = String.valueOf(quantity1.getText());
dts = String.valueOf(details1.getText());
orderVal = String.valueOf(orderValue1.getText());
StringBuffer sb = new StringBuffer("");
sb.append(productC+","+qty+","+dts+","+orderVal);
System.out.println("StringBuffer Value: "+sb.toString());
/* End of the field Data */
Button doneAddingProduct = (Button) layout.findViewById(R.id.doneAddingProduct);
//init your button
Button btnClosePopup = (Button) layout.findViewById(R.id.closePopup);
btnClosePopup.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
pwindo.dismiss();
}
});
doneAddingProduct.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
System.out.println("Inside onclick of done adding");
Product product = new Product();
product.setProductCode(productC);
product.setQuantity(qty);
product.setDetails(dts);
product.setOrderValue(orderVal);
app.setProduct(product);
Intent intent = new Intent(OrderReturnMgmtSecondActivity.this, OrderReturnMgmtSecondActivity.class);
startActivity(intent);
}
});
//show popup window after you have done initialization of views
pwindo.showAtLocation(layout, Gravity.CENTER, 0, 0);
return sb.toString();
}
Here I was trying to do is two methods in fetching those field data back to the activity.
1) Simply trying to return the field data by concatenating as a string to who ever calls this method.
2) Creating a pojo By the name as Product(which contains the field data) and setting it in application class i.e MyApplication extends Application ( you see that code as app.setProduct(product)) and redirecting it to the same activity and trying to fetch the data from that application.
But still I'm not able to get those field data.
Any help will be appreciated :)
Create an interface
interface Listener {
void onResult(Product product);
}
Pass a listener to the method and call it
addProductDetails(Listener listener){
//...
//when finished:
listener.onResult(product);
//...
}
The caller now has to implement the interface
addProductDetails(new Listener(){
void onResult(Product product){
//do something with product
}
});

Categories