New Value EditText not being displayed , no errors whatsoever - java

EDIT : By the way , if i hard code edt4.setText("any text") IN THE SCOPE , it shows the value
EDIT2: tried rebuilding/cleaning project , could this be a bug in settext method, it looks like im doing it right . When i look at other code with settext
EDIT3: startactivityforresult might be my answer ? , i openend another question related to what i wanna try out , i still havent found another solution , stuck with this for a week now : (
this is the link to my question Can i use startActivityForResult , with one activity?
EDIT4 : now trying it with making views visible and invisible
I have read every single article about anything related on stackoverflow , dreamincode etc, and i cant find anybody that knows the answer for my problem.
I have a qr scanner and after a successful scan the result needs to be put in a EDIT TEXT field with name editText4 , my code throws no errors but it is NOT displaying any value .
I've posted on different forums but to no avail (https://www.dreamincode.net/forums/topic/412000-settext-is-not-showing-set-value-in-edittext-in-gui/page__st__15__gopid__2372214&#entry2372214)
, as you can see the commented code . That's pretty much also what I've tried , I think I somehow got to get my handle result method inside of the scope.
NOTE : Log.v outputs the result very well , but when i try anything else with the result it just don't works or being displayed
ps: I m beginner at java
Thanks for your help
here's my mainactivity
` public class MainActivity extends Activity implements
ZXingScannerView.ResultHandler {
Button sendButton;
EditText edt4;
EditText edt2;
private ZXingScannerView mScannerView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
edt4 = findViewById(R.id.editText4);
mScannerView = findViewById(xmlScannerView);
mScannerView.setFormats(ZXingScannerView.ALL_FORMATS);
mScannerView.setResultHandler(this);
mScannerView.startCamera();
EditText delete2;
Button button3;
button3 = findViewById(R.id.button3);
delete2 = findViewById(R.id.editText2);
final EditText finalEdittext = delete2;
button3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//Clear EditText
finalEdittext.getText().clear();
}
});
EditText delete4;
Button button4;
delete4 = findViewById(editText4);
button4 = findViewById(R.id.button4);
final EditText finalEdittext1 = delete4;
button4.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//Clear EditText
finalEdittext1.getText().clear();
}
});
}
#Override
public void onPause() {
super.onPause();
mScannerView.stopCamera(); // Stop camera on pause
}
public void onClick(View v){
RelativeLayout someid = findViewById(R.id.someId);
mScannerView.setVisibility(View.VISIBLE);
someid.setVisibility(View.INVISIBLE);
}
// EditText edt4;
#Override
public void handleResult(final Result result) {
//handle result
Log.v("handleResult", result.getText());
edt4 = findViewById(editText4);
edt4.setText(result.getText());
//edt4.setText(String.valueOf(result.getText()));
// edt4.setText(new
StringBuilder().append("Resultaat:").append(result.getText()).toString());
}
`
this is xml :
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:background="#mipmap/ic_launcher_foreground">
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true">
<me.dm7.barcodescanner.zxing.ZXingScannerView
android:id="#+id/xmlScannerView"
android:visibility="gone"
android:layout_height="match_parent"
android:layout_width="match_parent" />
<RelativeLayout
android:id="#+id/someId"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="#+id/editText4"
android:layout_width="match_parent"
android:layout_height="62dp"
android:layout_marginTop="67dp"
android:ems="10"
android:hint="#string/scan_locatie"
android:inputType="text"
android:text=""
tools:backgroundTint="#android:color/holo_red_light" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/editText4"
android:layout_centerHorizontal="true"
android:background="#android:color/holo_red_light"
android:onClick="onClick"
android:text="#string/scan_qr"
tools:text="scan qr code" />
<EditText
android:id="#+id/editText2"
android:layout_width="match_parent"
android:layout_height="61dp"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginTop="197dp"
android:ems="10"
android:hint="#string/scan_order"
android:inputType=""
android:visibility="visible"
tools:backgroundTint="#android:color/holo_red_light" />
<Button
android:id="#+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/editText2"
android:layout_centerHorizontal="true"
android:background="#android:color/holo_red_light"
android:onClick="onClick"
android:text="#string/scan_qr"
tools:text="scan qr code" />
<Button
android:id="#+id/sendButton"
android:layout_width="157dp"
android:layout_height="32dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="72dp"
android:background="#android:color/holo_red_light"
android:text="#string/button"
tools:text="Versturen.." />
<Button
android:id="#+id/button3"
android:layout_width="40dp"
android:layout_height="38dp"
android:layout_alignBaseline="#+id/editText2"
android:layout_alignParentEnd="true"
android:background="#android:drawable/ic_delete" />
<Button
android:id="#+id/button4"
android:layout_width="39dp"
android:layout_height="37dp"
android:layout_alignBaseline="#+id/editText4"
android:layout_alignParentEnd="true"
android:background="#android:drawable/ic_delete" />
</RelativeLayout>
EDIT:5 also changed XML , and main , still not working :(

try removing recreate();
you are recreating the activity after setting the value, so the previous value is lost

I think result.getText() is a String anyway, so you could change the following line:
edt4.setText(String.valueOf(result.getText()));
To:
edt4.setText(result.getText());
After that you call updateScannerData, which is also writing to the EditText once again, but from the UI-Thread. I would recommend to remove the UI-Thread code as well since I assume the code runs on that Thread anyway.

You can either use
edt4.clear();
or
edt4.setText("");

Related

Convert Edittext to readonly and Make it editable on Button Press

i got this nw. Before clicking on the button, my EditText is just viewable:
After button press, it should convert to an editable field:
I want the EditText field line to disappear and become read only and on button click make it editable. But this just fades the line and also the text inside it loses it visual appearance.
Here's my code:
et1.setEnabled(false);
et1.setFocusable(true);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
et1.setEnabled(true);
et1.requestFocus();
}
});
Your XML should look like this,
<?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:gravity="center"
android:orientation="vertical">
<EditText
android:id="#+id/et1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName"
android:text="Name" />
<TextView
android:id="#+id/tv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
<Button
android:id="#+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
Now, add the following code to your activity class.
//init the views.
et1.setVisiblity(GONE);
tv1.setVisiblity(VISIBLE);
//if the textView is shown, the btn click will hide it and show editText
//instead, and same happens for editText
btn1.setOnClickListener(new View.OnClickListener(){
public void onClick(){
if(et1.getVisiblity() != VISIBLE) {
et1.setVisbility(VISIBLE);
tv1.setVisibility(GONE);
} else {
et1.setVisbility(GONE);
tv1.setVisibility(VISIBLE);
}
} });
P.S: This isn't executable code, although, hopefully it will get you the idea.

Android Custom Dialog class button NPE

I am trying to create a custom dialog class with a custom layout, but, for reasons unknown to be, I am getting a null pointer exception when trying to access the buttons for the layout. Here is the XML for the dialog:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/enableTopRelativeLayout"
android:layout_width="460dp"
android:layout_height="wrap_content"
android:background="#drawable/dialog_background"
android:paddingBottom="15dp"
android:paddingLeft="15dp"
android:paddingRight="15dp"
android:paddingTop="15dp" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:src="#drawable/ic_launcher" />
<RelativeLayout
android:id="#+id/enableInnerRelativeLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/imageView1" >
<TextView
android:id="#+id/enableMessage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerInParent="false"
android:paddingBottom="10dp"
android:text="Start service?"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/enableStrut"
android:layout_width="25dp"
android:layout_height="wrap_content"
android:layout_below="#id/enableMessage"
android:layout_centerHorizontal="true"
android:text=" " />
<Button
android:id="#+id/noenable"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/enableMessage"
android:layout_toLeftOf="#id/enableStrut"
android:background="#drawable/button_background"
android:text="No"
android:textColorLink="#color/color2"
android:textStyle="bold" />
<Button
android:id="#+id/enable"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/enableMessage"
android:layout_toRightOf="#id/enableStrut"
android:background="#drawable/button_background"
android:text="Yes"
android:textColorLink="#color/color2"
android:textStyle="bold" />
</RelativeLayout>
Here is the code for the class:
public class DialogStartService extends Dialog implements android.view.View.OnClickListener{
public Button yes;
public Button no;
public TextView tv;
public DialogStartService(Context context) {
super(context);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.start_service_layout);
getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
tv = (TextView) findViewById(R.id.enableMessage);
yes = (Button) findViewById(R.id.enable);
no = (Button) findViewById(R.id.noenable);
yes.setOnClickListener(this);
no.setOnClickListener(this);
}
#Override
public void onClick(View v) {
dismiss();
}
}
I instantiate the dialog with this code:
DialogStartService enableDialog = new DialogStartService(this);
Button enable = (Button) enableDialog.findViewById(R.id.enable);
enable.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// Do stuff
}
});
Button noenable = (Button) enableDialog.findViewById(R.id.noenable);
noenable.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// Do stuff
}
});
enableDialog.show();
The stack trace indicates the NPE occurs on this line:
enable.setOnClickListener(new OnClickListener() {
My question for the experts is... why is this code causing a NPE, and what is the best way to fix it? Thank you for any input you have.
According to documentation of findViewById() method:
Finds a child view with the given identifier. Returns null if the specified child view does not exist or the dialog has not yet been fully created (for example, via show() or create()).
In your code you call findViewById() before show() - that's why Button enable is null.

Button setOnClickListener is not working only for one button

I have two different buttons in my activity, and an if statement to show them...
My if statement is working well and show the right button, but one of those buttons is not doing anything when I click on it! (Actually, it's not clickable at all!)
if (isNeeded)
{
// buttonImportant is not clickable
buttonImportant.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.d("LOG", "Clicked!"); // Even this log won't work!
Intent intentDetailsActivity = new Intent(MainActivity.this, DetailsActivity.class);
intentDetailsActivity.putExtra("extraPosition", String.valueOf(position));
startActivity(intentDetailsActivity);
}
});
linearLayoutImportant.setVisibility(View.VISIBLE); // This line is working
}
if (!isNeeded)
{
// buttonNormal is working well!
buttonNormal.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
linearLayoutNormal.setVisibility(View.GONE);
Intent intentDetailsActivity = new Intent(MainActivity.this, DetailsActivity.class);
intentDetailsActivity.putExtra("extraPosition", String.valueOf(position));
startActivity(intentDetailsActivity);
}
});
linearLayoutNormal.setVisibility(View.VISIBLE); // And this one is not working too!
}
The problem is, it won't return any error or log, to know what's the problem! It's log I disabled the button... (which I didn't)
I checked if it's for declarations, but everything is declared well...
I checked the XML, there's no problem there either:
<LinearLayout
android:id="#+id/linearLayoutImportant"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="50dp"
android:layout_centerInParent="true"
android:gravity="center_vertical|center_horizontal"
android:orientation="vertical"
android:visibility="gone" >
<TextView
android:id="#+id/textViewImportant"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical|center_horizontal"
android:text="#string/important"
android:textSize="20sp"
android:textColor="#FFFFFF"
android:textStyle="bold" />
<Button
android:id="#+id/buttonImportant"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="#string/details" />
</LinearLayout>
...
...
<LinearLayout
android:id="#+id/linearLayoutNormal"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_marginTop="25dp"
android:orientation="vertical"
android:visibility="gone" >
<Button
android:id="#+id/buttonClose"
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_gravity="end"
android:background="#android:drawable/ic_menu_close_clear_cancel" />
<TextView
android:id="#+id/textViewNormal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp"
android:layout_gravity="center_horizontal"
android:gravity="center_vertical|center_horizontal"
android:text="#string/normal"
android:textStyle="bold" />
<Button
android:id="#+id/buttonNormal"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_margin="2dp"
android:layout_gravity="center_horizontal"
android:text="#string/details" />
</LinearLayout>
EDIT:
Here's the declarations:
LinearLayout linearLayoutImportant, linearLayoutNormal;
Button buttonImportant, buttonClose, buttonNormal;
...
...
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
linearLayoutImportant = (LinearLayout) findViewById(R.id.linearLayoutImportant );
buttonImportant = (Button) findViewById(R.id.buttonImportant);
buttonClose = (Button) findViewById(R.id.buttonClose);
buttonClose .setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
linearLayoutNormal.setVisibility(View.GONE);
}
});
buttonNormal = (Button) findViewById(R.id.buttonNormal);
linearLayoutNormal = (LinearLayout) findViewById(R.id.linearLayoutNormal);
}
EDIT 2:
Alright guys, maybe I didn't made it clear...
That isNeeded is a variable and I control it from another place...
When I make it false, the second if will get executed very well, and when I make it true, the first if will execute too, but only the button is not clickable!
EDIT 3:
I also checked .setEnabled(true); or .setClickable(true);, still not working... :/
SOLVED:
I used the HierarchyViewer and found the problem. The problem was in XML... I had a ListView on top of second button, but it was empty, so I thought nothing's there...
Sorry everybody...
I used the HierarchyViewer and found the problem. The problem was in XML... I had a ListView on top of second button, but it was empty, so I thought nothing's there...
Sorry everybody...
Try without the if statement in order to know if is a validation problem and you're only setting one listener
The boolean variable isNeeded can take only one of the two values, either true or false. Therefore only one of the if conditions will be executed.
Since only one of the condition is true, you are only setting an onClickListener to one of the buttons and thus the other button won't be clickable and the log message within the onClick method won't be visible in the logs.
Hope this answered your question.
All the best :)
set isNeeded = true before
if (isNeeded)
{
// buttonImportant is not clickable
buttonImportant.setOnClickListener(new View.OnClickListener() {
then test it again.
Good lucky!
There was an error posting my ans so i have taken a screenshot of it. Please review it for your answer

Get values from Dynamic list

Good day everyone,
I'm learning Android development and I try to build some dynamic lists for my application. And I'm stuck... like for 4 hours already.
I have two layouts:
1. main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/action_buttons"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:gravity="center"
>
<Button
android:id="#+id/button_count"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/button_calculate" />
<Button
android:id="#+id/button_add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/button_count"
android:text="#string/button_add" />
</LinearLayout>
<ScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/action_buttons"
>
<LinearLayout
android:id="#+id/action_list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
</LinearLayout>
</ScrollView>
</RelativeLayout>
action_list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/action_list_item_root"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<EditText
android:id="#+id/action_list_item_edittext_drinkName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/action_list_item_button_remove"
android:layout_alignParentTop="true"
android:layout_toRightOf="#+id/action_list_item_title"
android:padding="10dp" />
<EditText
android:id="#+id/action_list_item_edittext_drinkPercent"
android:text="40"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toLeftOf="#+id/action_list_item_edittext_drinkAmount"
android:inputType="number"
android:padding="10dp"/>
<EditText
android:id="#+id/action_list_item_edittext_drinkAmount"
android:text="0.0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toLeftOf="#+id/action_list_item_button_remove"
android:layout_alignBottom="#+id/action_list_item_button_remove"
android:inputType="numberDecimal"
android:width="50dp"
android:padding="10dp" />
<Button
android:id="#+id/action_list_item_button_remove"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="#string/action_list_item_button_remove" />
And code that creates dynamic list:
public class MainActivity extends Activity {
LinearLayout actionList = null;
private Button btnAdd;
private Button btnCalculate;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
initActivityElements();
}
private void initActivityElements() {
initAddButton();
initCalculateButton();
}
private void initCalculateButton() {
btnCalculate = (Button) findViewById(R.id.button_count);
btnCalculate.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
TextView amount = (TextView) findViewById(R.id.action_list_item_edittext_drinkAmount);
//this retrives TextView value from first list
Toast.makeText(getApplicationContext(), amount.getText().toString(),
Toast.LENGTH_SHORT)
.show();
}
});
}
private void initAddButton() {
actionList = (LinearLayout) findViewById(R.id.action_list);
btnAdd = (Button) findViewById(R.id.button_add);
btnAdd.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
RelativeLayout listItem = (RelativeLayout) View.inflate(
MainActivity.this, R.layout.action_list_item, null);
TextView name = (TextView) listItem
.findViewById(R.id.action_list_item_edittext_drinkName);
name.setText("Here is the Title " + actionList.getChildCount());
listItem.findViewById(R.id.action_list_item_button_remove)
.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
actionList.removeView((View) v.getParent());
}
});
actionList.addView(listItem);
}
});
}
My problem is that I need to get all data from TextView boxes (Amount, Percent), but I can retrive that data only from first item of a list (look at onClickListener for btnCalculate) and can't figure out how to do it, but tried to add 'unique ids' for view (but that brakes layout, badly), tried setting Tags but again with no luck.
Maybe anyone can give a tip? I bet there is some easy way to do it, but I'm failing to find it and google is no help here.
Thanks
I'm not really sure what exactly you are trying to do but it sounds like you are trying to take in information and populate it into a view. I would recommend having a static text box that takes in your information and builds that into a listview. A good example of how to do that is located here http://developer.android.com/resources/tutorials/notepad/notepad-ex1.html.

Android Beginner(Add Button)

I'm a beginner in android and making my first app. Here, I had taken 3 textViews and a button named Add. When I click on this button, I need to show the content of 2 textViews on third textView. I used eventListener and buttonClick event but it doesn't work. Please guide me with the code of Add button.
First create the xml as below:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:layout_gravity="center_horizontal"
android:orientation="horizontal"
android:layout_weight="1"
android:id="#+id/singleEmployee"
android:background="#ffffff">
<TextView
android:text="TextView"
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:text="TextView"
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:text="TextView"
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Button
android:text="Button"
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
And then use this Java code to done your desire action:
public class MainActivity extends Activity {
/** Called when the activity is first created. */
TextView tv1,tv2,tv3;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.testing);
tv1 = findViewById(R.id.textView1);
tv2 = findViewById(R.id.textView2);
tv3 = findViewById(R.id.textView3);
tv1.setText("Hello");
tv2.setText("World");
Button add = findViewById(R.id.button1);
add.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
tv3.setText("");
String tvValue1 = tv1.getText().toString();
String tvValue2 = tv2.getText().toString();
tv3.setText("Value of First Text is: "+tvValue1+".And the value of second TextView is: "+tvValue2);
}
});
}

Categories

Resources