Show String from EditText - android

I want to show TextView editable like the app "Google Keep" but
EditText text = (EditText)findViewById(R.id.edittext);
String value = text.getText().toString();
didn't work

Hello I've made a example that I think you can use:
Layout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:textColor="#FF0000"
android:text="#string/hello_world" />
<EditText
android:id="#+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="#+id/getInfoButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="GET INFO"
android:layout_gravity="center_horizontal"
/>
</LinearLayout>
MainActivity
package com.example.testedittext;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity {
private TextView info;
private EditText input;
private Button getInfo;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
info = (TextView) findViewById(R.id.textView);
input = (EditText)findViewById(R.id.editText);
getInfo = (Button)findViewById(R.id.getInfoButton);
getInfo.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
String inputText = input.getText().toString();
info.setText(inputText);
}
});
}
}
Here is the output:
Hope this helps,
Cheers

That is the correct syntax for getting the text in string format of an edittext.
The value of String "value" is actually "" right now because you called
text.getText().toString();
IMMEDIATELY after EditText text was instantiated. As you can imagine, the moment it was created, there was no text inside it, so that's why "value" has an empty string.
If you want to retrieve the value of the edittext at a specific call, I'd recommend adding a button in your xml layout, and in your code, add this:
Button button = (Button) findViewById(R.id.somebutton);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
text.getText().toString();
}
});
This will get the current String value of the edittext when you click on the button.

You have to use the value String somewhere else it will tell you that its not used.
Then you have to use that piece of code at a point of time where you have had a change to set the value of that ediettext.

Related

Access string_array from activity and send selected item to another activity in android

hello friends today my question is about string_array.On my activity i created two buttons.Now on (strings.xml) i created string array name with two items and i have inserted some text as shown below.On each button click i would like to access each item individually.for example on button1 click show me first item and on button 2 click give me the 2nd item on.Can you please give me the code for button click as i have little idea on how to access item.I have created a textview activity to display my item with every click.my code is working fine till now but i need help with extra code that i have to add.Please help me .
// Button activity page
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" />
</ScrollView>
<Button
android:layout_height="wrap_content"
android:layout_marginTop="90dp"
android:layout_marginEnd="60dp"
android:layout_marginRight="50dp"
android:id="#+id/btn1"
android:layout_width="wrap_content"
android:text="#string/OKILA"
android:textSize="18sp"
android:textStyle="bold" />
<Button
android:id="#+id/button1"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:layout_marginTop="160dp"
android:layout_marginRight="50dp"
android:text="1"
android:textSize="18sp"
android:textStyle="bold" />
</RelativeLayout>
//strings.xml
<string-array name="chapters">
<item>this is tes1</item>
<item>this is test2</item>
</string-array>
//main
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import org.w3c.dom.Text;
public class Lipok extends AppCompatActivity implements View.OnClickListener {
Toolbar mActionBarToolbar;
Button btnOne;
Button btnTwo;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_lipok);
mActionBarToolbar=(Toolbar)findViewById(R.id.CHAPTERS);
setSupportActionBar(mActionBarToolbar);
getSupportActionBar().setTitle("CHAPTERS");
String[] chapters=getResources().getStringArray(R.array.chapters);
btnOne = findViewById(R.id.btn1);
btnTwo = findViewById(R.id.button1);
btnOne.setOnClickListener(this);
btnTwo.setOnClickListener(this);
}
#Override
public void onClick(View v) {
String text="";
switch(v.getId()){
case R.id.btn1 : {
text = getResources().getStringArray(R.array.chapters)[0];
break;
}
case R.id.button1 : {
text = getResources().getStringArray(R.array.chapters)[1];
break;
}
}
Intent intent = new Intent(Lipok.this,lipokchapters.class);
intent.putExtra("DATA", text);
startActivity(intent);
}
}
//textview activity page
package com.Aolai.temeshilai;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
public class lipokchapters extends AppCompatActivity {
TextView textView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_lipokchapters);
textView=findViewById(R.id.textv);
String text= getIntent().getStringExtra("Data");
textView.setText(text);
}
}
Try this :
xml file
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" />
</ScrollView>
<Button
android:layout_height="wrap_content"
android:layout_marginTop="90dp"
android:layout_marginEnd="60dp"
android:layout_marginRight="50dp"
android:id="#+id/OKILA"
android:layout_width="wrap_content"
android:text="#string/OKILA"
android:textSize="18sp"
android:textStyle="bold" />
<Button
android:id="#+id/button1"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:layout_marginTop="160dp"
android:layout_marginRight="50dp"
android:text="1"
android:textSize="18sp"
android:textStyle="bold" />
</RelativeLayout>
strings.xml
<string-array name="chapters">
<item>this is tes1</item>
<item>this is test2</item>
</string-array>
Main Class
public class Lipok extends AppCompatActivity implements View.OnClickListener {
Toolbar mActionBarToolbar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_lipok);
mActionBarToolbar=(Toolbar)findViewById(R.id.CHAPTERS);
setSupportActionBar(mActionBarToolbar);
getSupportActionBar().setTitle("CHAPTERS");
}
#Override
public void onClick(View v) {
String text="";
switch(v.getId()){
case R.id.OKILA : {
text = getResources().getStringArray(R.array.testArray)[0];
break;
}
case R.id.button1 : {
text = getResources().getStringArray(R.array.testArray)[1];
break;
}
}
Toast.makeText(this, text, Toast.LENGTH_LONG).show();
Intent intent = new Intent(this, YOUR_ACTIVITY_NAME.class);
intent.putExtra("DATA", text);
startActivity(intent);
// Or else you can do whatever you want to do here with that text
}
}
Then in your other activity
String data = getIntent().getStringExtra("DATA");
YOUR_TEXTVIEW_NAME.setText(data);
This is basics of Android so before answering I suggest you to go
through basic tutorials before posting on Stackoberflow.
Now,
Here is how you can access the items from your string_array.
String[] chapters= getResources().getStringArray(R.array.chapters);
Now,here is pseudo code that might help you.
btn1.setOnClickListener(
your_text_view.setText(chapters[0])
)
//Here 0 means your first item, likewise you can access items by their index in
array

Number picker data is not showing correctly android

Hello guys I am facing a problem with number picker. I have some string that needs to be displayed as
but on select second option 'menu' with select button and reopen the dialog containing number picker the data is being shown something like this
Here it can be seen that menu 1 is being shown in both of the Number picker option menu the upper one and the middle one. But on scrolling the correct data is being displayed. The code is as follows
MainActivity
package com.example.anakumar6.numberpickerexample;
import android.app.Dialog;
import android.graphics.Color;
import android.os.Bundle;
import android.app.Activity;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.TextView;
import android.widget.NumberPicker;
public class MainActivity extends AppCompatActivity {
int index =0;
TextView tv;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Get the widgets reference from XML layout
tv = (TextView) findViewById(R.id.tv);
tv.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
showDialog(MainActivity.this);
}
});
}
public void showDialog(Activity activity){
final Dialog dialog = new Dialog(activity);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setCancelable(false);
dialog.setContentView(R.layout.dialog);
final NumberPicker np = (NumberPicker) dialog.findViewById(R.id.np);
//Set TextView text color
tv.setTextColor(Color.parseColor("#FF2C834F"));
//Initializing a new string array with elements
final String[] values= {"Menu 1", "Menu", "Menu 2"};
// modifyDataForNumberPicker(values);
//Populate NumberPicker values from String array values
//Set the minimum value of NumberPicker
np.setMinValue(0); //from array first value
//Specify the maximum value/number of NumberPicker
np.setMaxValue(values.length-1); //to array last value
//Specify the NumberPicker data source as array elements
np.setDisplayedValues(values);
np.setValue(index);
//Gets whether the selector wheel wraps when reaching the min/max value.
np.setWrapSelectorWheel(true);
//Set a value change listener for NumberPicker
np.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
#Override
public void onValueChange(NumberPicker picker, int oldVal, int newVal){
//Display the newly selected value from picker
tv.setText("Selected value : " + values[newVal]);
index = newVal;
}
});
Button dialogButton = (Button) dialog.findViewById(R.id.btnClick);
dialogButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/rl"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff"
android:padding="16dp"
tools:context=".MainActivity">
<TextView
android:id="#+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Select a number..."
android:textSize="25dp" />
</RelativeLayout>
dialog.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ffffffff"
>
<NumberPicker
android:id="#+id/np"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_below="#id/tv"
android:layout_weight="1"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/btnClick"
android:text="select"/>
</LinearLayout>
I think it is android number picker native issue. Please help me to fix this issue.
I have tried many a workarounds to fix it like extending class, in which there is a function getSelectedPos which returns the position on the base of start with function rather than equals or equalignorecase. But due to being private method of NumberPicker class it can't be done. I also tried other work around that did not work.
Finally I got a simple work around for this issue. I created my own function to modify the data to be set on NumberPicker before set on NumberPicker.
modifyDataForNumberPicker(values);
np.setDisplayedValues(values);
private void modifyDataForNumberPicker(String[] dataList){
int i=0;
for(String data : dataList){
int pos = i++;
dataList[pos] = data+" ";
}
}
This function prevents any string to be substring of any string from the given array to be set on NumberPicker.
I have faced the same issue, and I have resolved by determine fixed width for NumberPicker.
<NumberPicker
android:id="#+id/np"
android:layout_width="50dp" // <=== replace "wrap_content" with fixed width
android:layout_height="0dp"
android:layout_below="#id/tv"
android:layout_weight="1"/>

New to coding, app keeps crashing on start up

I'm trying to have a text field and a text view, and set the text view text to whatever is on the the field.
This is my code:
MainActivity.java
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView helloMessage = (TextView) findViewById(R.id.hello); //define textView to show the message
EditText nameField = (EditText) findViewById(R.id.Name); // Text field to get the name from
String getName = nameField.getText().toString(); // set a variable with the text field's value
helloMessage.setText(getName); // set text on the hello textview to be the name.
}
}
activity_main.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#B388FF"
android:orientation="vertical"
tools:context=".MainActivity">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:ems="10"
android:id="#+id/Name" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/hello"/>
</LinearLayout>
Thanks in advance for any sort of help!
Brother you need to setContentView(R.layout.activity_main); before finding any other view
You forgot to add setContentView(R.layout.activity_main) to onCreate()

How to make a texField open with click of a button?

I am new to android programming and there are a few things I don't quite know how to do yet. I am doing a course on udemy and was trying to put together everything I have learned up to a certain point.
What I am trying to do is have the user click on a button (i have 12) and have it bring up a textField where they can enter two numbers. I just want to be able to get the user's two numbers and i'm pretty sure I can figure out the rest ( I hope). I just don't understand how to go about doing this.
Any help would be much appreciated. Basically all I want to do is to be able to have the user click on one of the 12 buttons and be asked to enter two values, then take those values and perform a calculation on it.
Your xml could be 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:orientation="vertical" >
<Button android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/b_ok"
android:text="Click Me"/>
<EditText android:layout_width="fill_parent"
android:layout_height="match_parent"
android:visibility="invisible"
android:id="#+id/et_showme"/>
</LinearLayout>
and your activity could be like this:
package com.william.kinaan.welcomeback;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class Test1 extends Activity implements OnClickListener {
private Button b_ok;
private EditText et_showme;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test1);
initialize();
}
private void initialize() {
this.b_ok = (Button) findViewById(R.id.b_ok);
this.b_ok.setOnClickListener(this);
this.et_showme = (EditText) findViewById(R.id.et_showme);
}
#Override
public void onClick(View v) {
this.et_showme.setVisibility(View.VISIBLE);
}
}
Create an Activity that has 2 EditTexts
When user click a button the Activity is started and user can enter the Numbers
Try this.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingLeft="16dp"
android:paddingRight="16dp" >
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/tv_id"
android:visibility="invisible"
android:text="sample text"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="click"
android:id="#+id/click"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
And in your activity, you can do like this.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
Button button = (Button) findViewById(R.id.click);
final EditText text = (EditText) findViewById(R.id.tv_id);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
text.setVisibility(View.VISIBLE);
}
});
}

Android application stopped working-Eclipse

I'm trying to develop an Android application that will solve some math functions according to the code. But when I save it, then run on emulator and when the emulator is launched it says me The application has stopped working.
Java Code;
package com.example.fibonacci;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends ActionBarActivity {
Button btn1;
TextView tv1;
EditText edt1;
String msj= "d";
double n = Double.valueOf(edt1 . getText().toString());
double a = (1 / (Math.sqrt(5)));
double b = ((1+(Math.sqrt(5)))/2);
double c = ((1-(Math.sqrt(5)))/2);
double i = a * ((Math.pow(b,n)) - (Math.pow(c,n)));
String k = String.valueOf(i);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn1 = (Button) findViewById(R.id.btn1);
tv1 = (TextView) findViewById(R.id.tv1);
edt1= (EditText) findViewById(R.id.edt1);
btn1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Click kodu buraya yazılacak
msj = edt1.getText().toString();
tv1.setText( k );
}
});
}
}
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.fibonacci.MainActivity" >
<EditText
android:id="#+id/edt1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:ems="10"
android:hint="number to cal."
android:inputType="number" >
</EditText>
<Button
android:id="#+id/btn1"
style="?android:attr/buttonStyleSmall"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/edt1"
android:layout_below="#+id/edt1"
android:layout_marginTop="18dp"
android:text="button" />
<TextView
android:id="#+id/tv1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignLeft="#+id/btn1"
android:layout_below="#+id/btn1"
android:layout_marginTop="72dp"
android:text="result" />
double n = Double.valueOf(edt1 . getText().toString());
is outside of a method which means it runs before onCreate() which means your EditText is null. That can't be run until you initialize your EditText (i.e. must be after edt1= (EditText) findViewById(R.id.edt1);)
You are going to run into problems then with those calculations trying to run before onCreate(). Put them in a method and call them when you need to.
In the future, please provide the logcat with your post when your app crashes.
You initialize the n with the edit text's value, but your edit text object is null yet.
double n = Double.valueOf(edt1 . getText().toString());
Initialize after
edt1= (EditText) findViewById(R.id.edt1);

Categories

Resources