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()
Related
Through out the whole android application I want to capture button click, radio button clicks, link click etc... basically a user interaction in whole android application. Is there any common method to detect which element user click and its values.?
Try using onCickListener() on the buttons.
In Kotlin:
Add id to button in .xml files with android:id="#+id/nameOfButton". Every button needs an unique name.
In .kt file, use setOnClickListener with the id to set up the action when user click the button.
If there are several buttons, just follow step 1 and 2.
Example:
step 1
<Button
android:id="#+id/buttonSave"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Save" />
step 2
buttonSave.setOnclickListenter {
//TODO: your code goes here
}
Its very simple you just need to get the text and id of button from the onclick method. Here is java and xml code for it:
XML:
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/button"
android:onclick="getid"
android:text="Save" />
JAVA:
public void getid(View v){
int id = v.getId();
String text = v.getText();
}
As #Muthukumar Lenin asked here is listview in xml and java
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:padding="10dp"
tools:context=".MainActivity">
<TextView
android:id="#+id/textview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textStyle="italic"
android:textColor="#5f65ff"
android:padding="5dp"
android:text="Choose is the best football player?"/>
<ListView
android:id="#+id/listview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/textview" />
</RelativeLayout>
JAVA:
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView listView = (ListView) findViewById(R.id.listview);
final TextView textView = (TextView) findViewById(R.id.textview);
String[] players = new String[] {"CR7", "Messi", "Hazard", "Neymar"};
List<String> Players_list = new ArrayList<String>(Arrays.asList(players));
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, Players_list);
listView.setAdapter(arrayAdapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String selectedItem = (String) parent.getItemAtPosition(position);
textView.setText("The best football player is : " + selectedItem);
}
});
}
}
Some of these code are from tutorialspoint and some are edited by me.
Instead of Text, it's necessary to read the R.id.text text from TextView = My Text, I tried a lot but failed, how, please help.
This is the line:
public class MyWidgetProvider extends AppWidgetProvider {
// String array to show text at textview on refresh
private static final String[] refreshStrings = {"Text"};
<TextView
android:id="#+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="My Text"
android:textColor="#android:color/white"
android:textSize="18sp" />
I want it to be like this:
private static final String[] refreshStrings = R.id.text;
I found this How to find view id in appwidgetprovider?, so it is not possible to reference or modify in Appwidget directly. One solution could be to use AppCompatActivity. Here you can see how to update and modify with AppCompatActivity http://android4beginners.com/2013/06/lesson-1-3-how-to-modify-textview-in-java-code-findviewbyid-settext-and-gettext-methods/.
This is my code with AppCompatActivity how to get the text from the TextView by the R.id.<idname>
MainActivity:
package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView tv = (TextView) findViewById(R.id.text);
}
}
activity_main.xml (Don't forget to change the standard Layout, I chose LinearLayout):
<TextView
android:id="#+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="My Text"
android:textColor="#android:color/white"
android:textSize="18sp" />
So so what im trying to implement has two parts
One im giving my edittext the passwordToggle for which im using
android.support.design.widget TextInputLayout+TextInputEditText
So this is how my edittext looks like
Part two is i want to add validation and set appropriate error message.
I need the error message to be shown as follows
My layout code is as follows
<android.support.design.widget.TextInputLayout
style="#style/editTextBold"
android:id="#+id/input_pwd_parent"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:hintAnimationEnabled="false"
app:hintEnabled="false"
app:passwordToggleEnabled="true">
<android.support.design.widget.TextInputEditText
android:id="#+id/input_pwd"
style="#style/editTextBold"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/transparent"
android:ems="10"
android:hint="#string/hint_pwd"
android:inputType="textPassword"
android:padding="10dp" />
</android.support.design.widget.TextInputLayout>
So what i want to know is
1.How do i hide/unhide the password toggle icon in the edittext via code?
2.Also how do i make the setError message appear in place of the passwordToggle icon(once i hide it via code)
Ended up creating a custom view
The component can be found here
https://github.com/vnh1991/CustomValidatorEditText
Import the lib project as a module ,
in your layout create a container
<?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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.v2dev.customedittextdemo.MainActivity">
<LinearLayout
android:id="#+id/llContainer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:orientation="vertical"></LinearLayout>
<Button
android:id="#+id/btnValidate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_below="#+id/llContainer"
android:padding="10dp"
android:text="Validate" />
</RelativeLayout>
And in your activity load the component into the container
package com.v2dev.customedittextdemo;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Toast;
import com.v_sQr_dev.customvalidatoredittext.CustomEdittext;
public class MainActivity extends AppCompatActivity {
private LinearLayout llContainer;
private Button btnValidate;
private CustomEdittext inputPwd;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
llContainer = (LinearLayout) findViewById(R.id.llContainer);
btnValidate = (Button) findViewById(R.id.btnValidate);
inputPwd = new CustomEdittext(llContainer, this);
inputPwd.setHint("PASSWORD");
btnValidate.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(inputPwd.validate()){
Toast.makeText(MainActivity.this,"Input Valid",Toast.LENGTH_SHORT).show();
}
}
});
}
}
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);
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.