My understanding was that all standard Views with an ID should save their state automatically, and upon trying this on an example I found it to be quite confusing.
I had only 1 activity and the main layout as listed below.
When I change the text of the TextView by clicking on the button, and then rotate the screen, the TextView does actually save it's state, but upon rotating again, it resets to it's default state.
Same happens if I edit it while in landscape and rotate: after first rotate it still saves it's state, but after another rotate (unless I change the text again) it resets to default value.
I'm really confused now. Could someone please explain this behavior to me. I wasn't able to find any other question (or answer) that explains this specific behavior.
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=".MainActivity">
<TextView
android:id="#+id/activity_main_textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:maxLines="2"
android:text="test"
android:textSize="50sp"
/>
<EditText
android:id="#+id/activity_main_editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:maxLength="30"
android:maxLines="1"
/>
<Button
android:id="#+id/activity_main_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:text="Set text"
/>
</RelativeLayout>
MainActivity.java:
package com.example.viewstatetest;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private TextView textView;
private EditText editText;
private View button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (TextView) findViewById(R.id.activity_main_textView);
editText = (EditText) findViewById(R.id.activity_main_editText);
button = findViewById(R.id.activity_main_button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
textView.setText(editText.getText());
}
});
}
}
If you look at the TextView sources (in particular TextView#onSaveInstanceState()), you will see that text is saved only in two cases:
When user explicitly asked to do that by setting freezeText flag to true via android:freezesText="true" or TextView#setFreezesText(true)
When text within TextView has selection.
In your case both of these cases are false, so your text gets reset to its default state during orientation change.
Simple change to your layout will solve the issue:
<TextView
android:id="#+id/activity_main_textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:freezesText="true"
android:maxLines="2"
android:text="test"
android:textSize="50sp"
/>
Related
This is my Main Activity code. I want to change CardView's width from my activity when the button pressed, but it is not working I tried other attributes as well, for example I can change CardView's color it works fine. But CardView's width and height properties not working.
import androidx.appcompat.app.AppCompatActivity;
import androidx.cardview.widget.CardView;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
private Button button;
private CardView cardView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button=findViewById(R.id.button);
cardView=findViewById(R.id.cardView);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
cardView.setMinimumWidth(20);//HERE IS THE PROBLEM
}
});
}
}
Below is my XML file which CardView written.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical"
tools:context=".MainActivity">
<androidx.cardview.widget.CardView
android:id="#+id/cardView"
android:layout_width="10dp"
android:layout_height="10dp"
android:layout_margin="180dp"
app:cardBackgroundColor="#color/purple_500"
app:cardCornerRadius="20dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/button" />
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="150dp"
android:padding="8dp"
android:text="Button"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</LinearLayout>
I tried vise versa in xml file CardView's width initialy 20dp when the button pressed 10dp , but did not work
(This is out of this question but when I use cardView.setRadius(); it works when innitially in xml CardView width greater than the value you want to setup
for example it works when CardView's radius initially 20dp you want to be 10dp, but it will not work vice versa)
I want to know is there anything which I am missing? Like to add some implementation...
Card width is setting by using LayoutParams. Add in your button onClick
LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) cardView.getLayoutParams();
lp.width = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 20, getResources().getDisplayMetrics());
cardView.setLayoutParams(lp);
I am currently developing an android music reading app in Android Studio. The language I am using is Java but I am a beginner (sorry for my English too).
The app has several exercises. Ideally, people can click on the next or back button to see one exercise or another. The problem is that I am also looking for the user, even if he exits the application, to continue in the last exercise (image) seen.
I have read about the cycle of activities and I understand it, but since I am a beginner in the language I cannot do what I need to do. I have learned that OnSaveInstanceState or OnRestoreInstanceState do not solve my problem and that the solution may be by using SharedPreferences but I don't know how to apply it to the code I currently have. With SharedPreferences I hope I can save and restore the last imageview seen and that I can continue from there by clicking next or back.
This is the current code and it allows me to: show the initial exercise and go from one exercise to another.
package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.View;
import android.widget.ImageView;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private int image_index = 0 ;
private static final int MAX_IMAGE_COUNT = 3;
private int[] mImageIds = {
R.raw.image1,
R.raw.image2,
R.raw.image3};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
showImage();
}
private void showImage() {
ImageView imgView = (ImageView) findViewById(R.id.myimage);
imgView.setImageResource(mImageIds[image_index]);
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case (R.id.previous_btn):
image_index--;
if (image_index == -1) {
image_index = MAX_IMAGE_COUNT - 1;
} else {
}
showImage();
break;
case (R.id.next_btn):
image_index++;
if (image_index == MAX_IMAGE_COUNT) {
image_index = 0;
} else {
}
showImage();
break;
}
}
}
This is the xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView 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"
android:background="#android:color/background_light"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:fontFamily="#font/gilroyextrabold"
android:text="Ejercicios"
android:textColor="#android:color/black"
android:textSize="24sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="#+id/myimage"
android:layout_width="wrap_content"
android:layout_height="370dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView" />
<LinearLayout
android:id="#+id/linearLayout5"
android:layout_width="match_parent"
android:layout_height="54dp"
android:layout_marginStart="10dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="40dp"
android:layout_marginEnd="10dp"
android:layout_marginRight="10dp"
android:orientation="horizontal"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/myimage">
<Button
android:id="#+id/previous_btn"
android:layout_width="190dip"
android:layout_height="wrap_content"
android:background="#drawable/btnatsslf"
android:onClick="onClick" />
<Button
android:id="#+id/next_btn"
android:layout_width="190dip"
android:layout_height="wrap_content"
android:background="#drawable/btnsgtslf"
android:onClick="onClick" />
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
</LinearLayout>
</ScrollView>
As I don't have that much knowledge, I seek your help and I also receive alternatives.
put these lines after setContentView, but before showImage in onCreate
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
image_index = sp.getInt("image_index", image_index);
and save this value inside showImage just after setImageResource line
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
sp.edit().putInt("image_index", image_index).apply();
you may keep reference to SharedPreferences sp as class member, so it won't be needed to obtain every time when showImage is called (also ImageView should be stored same way without need of findViewById everytime). also the key ("image_index") may be stored as some private static final String class member
also remember for future use: this is index in some array you have declared static. when you change it, e.g. stored value is 2 and you cut last item in images array then user get index out of bounds exception after app update installation, so inside onCreate check that this value is in range of array (just for safety)
I'm a real noob and my question may be stupid.
I'm building an app to convert from uppercase to lowercase and vice versa.
I have two editText and 2 buttons.
One editText is the user input and the other is the converted text; one button is to convert to lowercase and the other is to convert to uppercase.
this is my .xml so far:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
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"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="myapplication.example.falcoleo.lettercaseconverter.MainActivity"
tools:showIn="#layout/activity_main"
android:orientation="vertical">
<android.support.v7.widget.CardView
android:id="#+id/card_originalText"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginBottom="8dp"
android:layout_weight="1"
app:cardBackgroundColor="#color/grey800"
card_view:cardCornerRadius="4dp">
<EditText
android:id="#+id/originalText"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="8dp"
android:gravity="top"/>
</android.support.v7.widget.CardView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginBottom="8dp"
android:orientation="horizontal">
<Button
android:id="#+id/buttonSetToUppercase"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="convertToUpperCase" />
<Button
android:id="#+id/button2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="convertToLowerCase" />
</LinearLayout>
<android.support.v7.widget.CardView
android:id="#+id/card_convertedText"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
app:cardBackgroundColor="#color/grey800"
card_view:cardCornerRadius="4dp">
<EditText
android:text="TEST"
android:id="#+id/convertedText"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="8dp"
android:gravity="top"
android:editable="false"
android:inputType="none"
android:textIsSelectable="false"
android:background="#00000000"
/>
</android.support.v7.widget.CardView>
it's far from perfect but the real problem lies in the .java
package myapplication.example.falcoleo.lettercaseconverter;
import android.content.ClipData;
import android.content.Context;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.content.ClipboardManager;
import android.util.Log;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
TextView textView;
Button copyText;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
final TextView textView = (TextView) findViewById(R.id.convertedText); //the second editText is called textView
final TextView original =(TextView) findViewById(R.id.originalText); //the first editTExt is called original
final String upper = original.toString().toUpperCase(); //Convert to uppercase
final String lower = original.toString().toLowerCase(); //Convert to lowercase
Button convertToUpperCase=(Button)findViewById(R.id.buttonSetToUppercase); // il primo pulsante si chiama convertToUpperCase
convertToUpperCase.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
textView.setText(upper);//Set Text on button click via this function.
}
});
Button convertToLowerCase = (Button)findViewById(R.id.buttonSetToUppercase); // il secondo pulsante si chiama convertToLowerCase
convertToLowerCase.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
textView.setText(lower); //Set Text on button click via this function.
}
});
}
}
When I build the app it doesn't give any error.
If I run it on a device, when I tap the convertToLowerCase button it does nothing; and when i tap convertToUpperCase button it writes android.support.v7.widget.appcompatedittext{28f38fb7 vfed..cl......i. 0,0-0,0 #7f0c006b app:id/originaltext} on the convertedText EditText.
Nothing to see on the log.
What is it doing? i'm not even sure it is an error message.
You should do the following
final String upper = original.getText().toString().toUpperCase(); //Convert to uppercase
final String lower = original.getText().toString().toLowerCase(); //Convert to lowercase
The one thing that might be causing you problems is the element type;
You are referencing :
TextView original;
But the actual elements are:
EditText
Can you please fix those first then try?
Then make sure once you get the typed text, convert to String and perhaps check for empty strings?
I hope this helps!
I am realitavely new to Java coding and am trying to use a button press (in an Android App) to update a text view. The code I am currently using is causing my app to crash whenever I try to press the button.
Here is my XML
<LinearLayout>
<TextView
android:id="#+id/myTextView_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="calculateNewNumber"
android:text="Calculate New Number" />
</LinearLayout>
And Here is my Java
package com.example.android.myapp;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
int startNumber = 0;
public void calculateNewNumber(View view) {
startNumber = startNumber + 1;
display(startNumber);
}
private void display(int number) {
TextView myTextView = (TextView) findViewById(
R.id.myTextView_text_view);
myTextView.setText("" + number);
}
}
Thanks for any help you can give.
The code itself works fine so the problem must be with your XML, assuming you have provided it in full here.
Using this XML, your MainActivity code works fine for me.
<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:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin" tools:context=".MainActivity">
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="fill_parent">
<TextView
android:id="#+id/myTextView_text_view"
android:layout_width="60dp"
android:layout_height="wrap_content"
android:text="0" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="calculateNewNumber"
android:text="Calculate New Number" />
</LinearLayout>
</RelativeLayout>
I am just starting writing Android apps. I have done some beginner's tutorials, but I'm having trouble with the next step for which I could not find any helpful answer.
I want to write an app that computes the value of a complex function which depends on a three parameters. This works if I put everything on a single view:
For each parameter one TextField (with the parameter name) plus one EditText field to enter the value. At the bottom there is one Button, and when this is clicked, the result is displayed in another TextField.
But now I want improve the layout (plus I want to learn how to deal with more complex structures):
Two of the parameters are usually just set once and then kept at their values. So I decided to move these two parameters to a different view.
No I'm looking for recommendations (and hopefully links example code):
Which structure should I use? It seems that I do not need to use different activities, but one activity with different views should do the job.
ViewSwitcher sounds reasonable, since I only require two different views in this case. On the other hand ViewFlipper may be preferable, since I can reuse this later for other projects with multiple views. I also read that ViewPager allows to swipe between different views.
How can I read an EditField if this is on a different view?
I tried to use ViewSwitcher, and it worked when I added an additional button to switch to a second view. But when I moved the TextField and EditField for parameters one and two
to the second view, the app does not run on the emulator, just stating "error in app" (while Eclipse shows no errors). From this, I guess that I have to do some additional work to pass data in EditText between different views.
I have not found any examples for this - can anybody give me some advice/examples?
Any help is appreciated
I figured that my initial description was maybe not too helpful without any code.
Here is my layout
<?xml version="1.0" encoding="utf-8"?>
<ViewSwitcher xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/viewSwitcher1"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView android:id="#+id/pg1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1st view" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView android:id="#+id/v1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="value1" />
<EditText android:id="#+id/val1"
android:text="2.0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="numberDecimal" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView android:id="#+id/v2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="value2" />
<EditText android:id="#+id/val2"
android:text="3.0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="numberDecimal" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button android:layout_width="wrap_content"
android:id="#+id/calc"
android:layout_height="wrap_content"
android:text="2*Val1 + Val2 =" />
<TextView android:id="#+id/res"
android:text=""
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textSize="24sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button android:id="#+id/b1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="switch view 1" />
</LinearLayout>
</LinearLayout>
<!-- next view -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView android:id="#+id/pg2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="View 2" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button android:id="#+id/b2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="switch view 2" />
</LinearLayout>
</LinearLayout>
</ViewSwitcher>
and here is my main activity:
package com.example.testas2;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.ViewSwitcher;
public class MainActivity extends Activity {
ViewSwitcher switcher;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
switcher = (ViewSwitcher)findViewById(R.id.viewSwitcher1);
initControls();
}
private void initControls()
{
Button calculate=(Button)findViewById(R.id.calc);
calculate.setOnClickListener(new Button.OnClickListener()
{public void onClick (View v) { calculate(); }});
Button b1=(Button)findViewById(R.id.b1);
b1.setOnClickListener(new Button.OnClickListener()
{public void onClick (View v) { switcher.showNext(); }});
Button b2=(Button)findViewById(R.id.b2);
b2.setOnClickListener(new Button.OnClickListener()
{public void onClick (View v) { switcher.showPrevious(); }});
calculate();
}
private void calculate()
{
EditText val1=(EditText)findViewById(R.id.val1);
EditText val2=(EditText)findViewById(R.id.val2);
TextView res=(TextView)findViewById(R.id.res);
Double Val1=Double.parseDouble(val1.getText().toString());
Double Val2=Double.parseDouble(val2.getText().toString());
Double result = 2*Val1+Val2;
res.setText( String.format( "%.3f", result ) );
}
#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;
}
}
This version is working:
- when pushing the first button, it computes: result = 2 * value1 + value2)
- when pushing the button "switch view 1", it goes to the second view
- on the second view, when pushing the button "switch view 2", it goes back to view 1
But I can't get the following two pieces to work:
1) I would like to move the "switch view 1" button to the top of the first view, but this gives an error in the emulator ("Unfortunately testas2 has stopped").
2) I would like to move the input of the second value to the second view, but this gives the same error in the emulator.
What am I doing wrong?
Why does the order of the elements in the layout matter, and how can I modify my code to make it work?
Example code -
TextView tField= (add cast here)findViewById(R.id.editText)
tfield.getText();
The above answer can be improved if you post some code of yours because findViewById at times depends on the container view and the above code will not work then as it will look for the textfield in the default view.
I just did "Project > Clean", restarted the app in the emulator, and now it works!
In other words, the solution for the original question is trivial (in fact there was no real problem). One can simply move elements in the layout file (TextView, EditText, Button) within a view or between views. The posted activity works always and the data (from the different TextView and EditView fields) is accessible everywhere in the code. Therefore, no explicit "transfer" of data between views is required.
Only after moving elements in the layout file one should do "Project > Clean".
In your current Activity, create a new Intent:
Intent i = new Intent(getApplicationContext(), NewActivity.class);
i.putExtra("new_variable_name","value");
startActivity(i);
Then in the new Activity, retrieve those values:
Bundle extras = getIntent().getExtras();
if (extras != null) {
String value = extras.getString("new_variable_name");
}
For more info follow this link