I have multiple layout in a xml file and I want with checkbox from another xml file to show or hide the layout who is checked or unchecked.
This is the main layout and I want to show or hide Linearlayout1 or LinearLayout2 etc.
So I click on the button "Layout" and I can activate or desactivate layout by checking a checkbox
<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" >
<LinearLayout
android:id="#+id/linearLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button1" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Layout 1" />
</LinearLayout>
<LinearLayout
android:id="#+id/linearLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/linearLayout1"
android:layout_below="#+id/linearLayout1" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button2" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Layout 2" />
</LinearLayout>
<LinearLayout
android:id="#+id/linearLayout2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/linearLayout"
android:layout_below="#+id/linearLayout" >
<Button
android:id="#+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Layout 3" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
</LinearLayout>
<Button
android:id="#+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/linearLayout2"
android:layout_centerVertical="true"
android:layout_marginLeft="14dp"
android:text="Layout" />
</RelativeLayout>
With the java code:
package com.dlayout;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity implements OnClickListener{
Button button4;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button4 = (Button)findViewById(R.id.button4);
button4.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
startActivity(new Intent("com.dlayout.Checkbox"));
}
});
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
startActivity(new Intent("com.dlayout.Checkbox"));
}
}
and my other xml file with the checkbox
<?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" >
<CheckBox
android:id="#+id/cb1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CheckBox1" />
<CheckBox
android:id="#+id/cb2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CheckBox2" />
<CheckBox
android:id="#+id/cb3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CheckBox3" />
</LinearLayout>
And my java code of my checkbox class:
package com.dlayout;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.LinearLayout;
public class Checkbox extends MainActivity {
CheckBox cb1;
CheckBox cb2;
CheckBox cb3;
LinearLayout linearLayout1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.checkbox);
cb1 = (CheckBox) findViewById(R.id.cb1);
cb2 = (CheckBox) findViewById(R.id.cb2);
cb3 = (CheckBox) findViewById(R.id.cb3);
}
// *******************I know, this code is not working***************
public void onCheckboxClicked(View view){
boolean checked = ((CheckBox) view) .isChecked();
switch(view.getId()){
case R.id.cb1:
if (checked)
//linearLayout1.setVisibility(View.INVISIBLE);
else
//linearLayout1.setVisibility(View.VISIBLE);
break;
case R.id.cb2:
if (checked)
//LinearLayout2.setVisibility(View.INVISIBLE);
else
//linearLayout2.setVisibility(View.VISIBLE);
break;
case R.id.cb3:
if (checked)
//LinearLayout3.setVisibility(View.INVISIBLE);
else
//linearLayout2.setVisibility(View.VISIBLE);
break;
//**************************************************************
}
}
}
So when the checkbox1 is checked, the layout1 is visible when isn't, the layout1 is invisible.
Can you suggest me any ideas?
If this is all in one Activity and you want to show/hide a layout from a different xml file then you will first need to inflate that layout so you have a reference to it. Then you can use a checkChangedListener to know when the Checkbox state has changed and change the visibility of that layout.
If it is going from one Activity to another then you can simply check the state of the CheckBox in your onClick() and send some type of extra data in your intent to check in the next Activity to know to show/hide the layout.
Related
My 1st Activity java file source code is not working the way I want. I can not seem to get the animation to run at the start of my 1st activity. I can only get it to run after the 2nd Java Activity by physically going back to the 1st activity by hard pressing back on the Android keyboard.
package com.demotxt.droidsrce.welcomescreen;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.LinearLayout;
public class WelcomeActivity extends AppCompatActivity {
LinearLayout l1, l2;
Button btnsub;
Animation uptodown, downtoup;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_welcome);
btnsub = (Button) findViewById(R.id.buttonsub);
btnsub.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
NextActivity();
}
});
}
public void NextActivity() {
Intent intent = new Intent(this, NextActivity.class);
startActivity(intent);
l1 = (LinearLayout) findViewById(R.id.l1);
l2 = (LinearLayout) findViewById(R.id.l2);
uptodown = AnimationUtils.loadAnimation(this, R.anim.uptodown);
downtoup = AnimationUtils.loadAnimation(this, R.anim.downtoup);
l1.setAnimation(uptodown);
l2.setAnimation(downtoup);
}
}
This is my XML File for the 1st Activity and everything here is also running smoothly, but I have the same problem. There seems to be an issue running the java and XML script backward and forward to run the animation both ways.
<?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"
tools:context="com.demotxt.droidsrce.welcomescreen.WelcomeActivity"
android:orientation="vertical"
android:background="#drawable/background">
<LinearLayout
android:id="#+id/l1"
android:layout_width="match_parent"
android:layout_height="400dp"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="25dp"
android:text="Welcome to"
android:textColor="#color/lightorange"
android:textSize="30sp"
android:textStyle="bold" />
<TextView
android:layout_width="398dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="50dp"
android:text=" BLUEY AUSTRALIA"
android:textAlignment="textStart"
android:textColor="#android:color/holo_blue_light"
android:textColorHighlight="#android:color/holo_blue_bright"
android:textColorHint="#android:color/holo_blue_bright"
android:textColorLink="#android:color/holo_blue_bright"
android:textSize="30sp" />
<ImageView
android:id="#+id/Bluey_logo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:srcCompat="#drawable/bluey_logo" />
<TextView
android:id="#+id/textView3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TextView" />
</LinearLayout>
<LinearLayout
android:id="#+id/l2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="100dp"
android:background="#drawable/spaceullustration"
android:orientation="vertical">
<Button
android:id="#+id/buttonsub"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="#drawable/buttonstyle"
android:text="JOIN BLUEY"
android:textColor="#color/bluey"
android:textSize="30sp" />
</LinearLayout>
</LinearLayout>
try change the order
l1 = (LinearLayout) findViewById(R.id.l1);
l2 = (LinearLayout) findViewById(R.id.l2);
uptodown = AnimationUtils.loadAnimation(this, R.anim.uptodown);
downtoup = AnimationUtils.loadAnimation(this, R.anim.downtoup);
l1.setAnimation(uptodown);
l2.setAnimation(downtoup);
startActivity(intent); //<<<<<<<<<<<<<<,
I entered some text in the text boxes on first activity.
Then, On pressing the "SAVE" button, I am moved to the second activity.
Now, On pressing the "BACK" button, I am dropped back on the first activity where i am unable to see the values/text i entered before.
I have tried using shared preferences also.
Here is my code
MainActivity.java
package com.example.madhur.intentclear;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final EditText tv1 = findViewById(R.id.editText);
final EditText tv2 = findViewById(R.id.editText2);
final EditText tv3 = findViewById(R.id.editText3);
final Button bt1 = findViewById(R.id.button);
bt1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
SharedPreferences sharedPreferences1=getSharedPreferences("value1",Context.MODE_PRIVATE);
SharedPreferences.Editor editor=sharedPreferences1.edit();
editor.putString("tv1",tv1.getText().toString());
SharedPreferences sharedPreferences2=getSharedPreferences("value2",Context.MODE_PRIVATE);
SharedPreferences.Editor editor2=sharedPreferences2.edit();
editor2.putString("tv2",tv2.getText().toString());
SharedPreferences sharedPreferences3=getSharedPreferences("value3",Context.MODE_PRIVATE);
SharedPreferences.Editor editor3=sharedPreferences3.edit();
editor3.putString("tv3",tv3.getText().toString());
editor.apply();
editor2.apply();
editor3.apply();
tv1.setText(String.valueOf(sharedPreferences1.getString("tv1",null)));
tv2.setText(String.valueOf(sharedPreferences2.getString("tv2",null)));
tv3.setText(String.valueOf(sharedPreferences3.getString("tv3",null)));
Intent intent = new Intent(MainActivity.this, Main2Activity.class);
intent.putExtra("tv1",tv1.getText().toString());
intent.putExtra("tv2",tv2.getText().toString());
intent.putExtra("tv3",tv3.getText().toString());
startActivity(intent);
}
});
}
}
Main2Activity.java
package com.example.madhur.intentclear;
import android.content.Context;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import java.io.FileOutputStream;
import java.io.IOException;
public class Main2Activity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
final TextView tv1=findViewById(R.id.textView);
final TextView tv2=findViewById(R.id.textView2);
final TextView tv3=findViewById(R.id.textView3);
Button button=findViewById(R.id.button);
Button button1=findViewById(R.id.button1);
Button button2=findViewById(R.id.button2);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Bundle intent=getIntent().getExtras();
tv1.setText(intent.getString("tv1"));
tv2.setText(intent.getString("tv2"));
tv3.setText(intent.getString("tv3"));
}
});
button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String clear="";
tv1.setText(clear);
tv2.setText(clear);
tv3.setText(clear);
m1();
}
});
button2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent1=new Intent(Main2Activity.this,MainActivity.class);
startActivity(intent1);
}
});
}
public void m1()
{String clear="";
try {
FileOutputStream fileOutputStream;
FileOutputStream fileOutputStream2;
FileOutputStream fileOutputStream3;
fileOutputStream = openFileOutput("et1.txt", Context.MODE_PRIVATE);
fileOutputStream.write(clear.getBytes());
fileOutputStream2 = openFileOutput("et2.txt", Context.MODE_PRIVATE);
fileOutputStream2.write(clear.getBytes());
fileOutputStream3 = openFileOutput("et3.txt", Context.MODE_PRIVATE);
fileOutputStream3.write(clear.getBytes());
} catch (IOException e) {
e.printStackTrace();
}
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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:context="com.example.madhur.intentclear.MainActivity">
<android.support.constraint.Guideline
android:id="#+id/guideline"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.13" />
<android.support.constraint.Guideline
android:id="#+id/guideline2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.87" />
<android.support.constraint.Guideline
android:id="#+id/guideline3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.06" />
<android.support.constraint.Guideline
android:id="#+id/guideline4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.18" />
<android.support.constraint.Guideline
android:id="#+id/guideline5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.29" />
<android.support.constraint.Guideline
android:id="#+id/guideline6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.43" />
<android.support.constraint.Guideline
android:id="#+id/guideline7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.58" />
<android.support.constraint.Guideline
android:id="#+id/guideline8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.73" />
<android.support.constraint.Guideline
android:id="#+id/guideline9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.82" />
<android.support.constraint.Guideline
android:id="#+id/guideline10"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.95" />
<android.support.constraint.Guideline
android:id="#+id/guideline11"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.24" />
<android.support.constraint.Guideline
android:id="#+id/guideline12"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.77" />
<EditText
android:id="#+id/editText"
android:layout_width="0dp"
android:layout_height="0dp"
android:ems="10"
android:inputType="textPersonName"
android:hint="Enter name"
app:layout_constraintBottom_toTopOf="#+id/guideline4"
app:layout_constraintEnd_toStartOf="#+id/guideline2"
app:layout_constraintStart_toStartOf="#+id/guideline"
app:layout_constraintTop_toTopOf="#+id/guideline3" />
<EditText
android:id="#+id/editText2"
android:layout_width="0dp"
android:hint="Enter phone number"
android:layout_height="0dp"
android:ems="10"
android:inputType="textPersonName"
app:layout_constraintBottom_toTopOf="#+id/guideline6"
app:layout_constraintEnd_toStartOf="#+id/guideline2"
app:layout_constraintStart_toStartOf="#+id/guideline"
app:layout_constraintTop_toTopOf="#+id/guideline5" />
<EditText
android:id="#+id/editText3"
android:layout_width="0dp"
android:hint="Enter Email Adress"
android:layout_height="0dp"
android:ems="10"
android:inputType="textPersonName"
app:layout_constraintBottom_toTopOf="#+id/guideline8"
app:layout_constraintEnd_toStartOf="#+id/guideline2"
app:layout_constraintStart_toStartOf="#+id/guideline"
app:layout_constraintTop_toTopOf="#+id/guideline7" />
<Button
android:id="#+id/button"
android:layout_width="0dp"
android:layout_height="0dp"
android:text="SAVE"
app:layout_constraintBottom_toTopOf="#+id/guideline10"
app:layout_constraintEnd_toStartOf="#+id/guideline12"
app:layout_constraintStart_toStartOf="#+id/guideline11"
app:layout_constraintTop_toTopOf="#+id/guideline9" />
</android.support.constraint.ConstraintLayout>
activity_Main2.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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:context="com.example.madhur.intentclear.Main2Activity">
<TextView
android:id="#+id/textView"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toTopOf="#+id/guideline16"
app:layout_constraintEnd_toStartOf="#+id/guideline13"
app:layout_constraintStart_toStartOf="#+id/guideline14"
app:layout_constraintTop_toTopOf="#+id/guideline15" />
<TextView
android:id="#+id/textView2"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toTopOf="#+id/guideline19"
app:layout_constraintEnd_toStartOf="#+id/guideline13"
app:layout_constraintStart_toStartOf="#+id/guideline14"
app:layout_constraintTop_toTopOf="#+id/guideline20" />
<TextView
android:id="#+id/textView3"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toTopOf="#+id/guideline17"
app:layout_constraintEnd_toStartOf="#+id/guideline13"
app:layout_constraintStart_toStartOf="#+id/guideline14"
app:layout_constraintTop_toTopOf="#+id/guideline18" />
<android.support.constraint.Guideline
android:id="#+id/guideline13"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_begin="249dp" />
<android.support.constraint.Guideline
android:id="#+id/guideline14"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.33854166" />
<android.support.constraint.Guideline
android:id="#+id/guideline15"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.031311154" />
<android.support.constraint.Guideline
android:id="#+id/guideline16"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.13" />
<android.support.constraint.Guideline
android:id="#+id/guideline17"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.38" />
<android.support.constraint.Guideline
android:id="#+id/guideline18"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.29" />
<android.support.constraint.Guideline
android:id="#+id/guideline19"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.26" />
<android.support.constraint.Guideline
android:id="#+id/guideline20"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.16" />
<Button
android:id="#+id/button"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="52dp"
android:text="SHOW TEXT"
app:layout_constraintEnd_toStartOf="#+id/guideline13"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="#+id/guideline14"
app:layout_constraintTop_toTopOf="#+id/guideline17" />
<Button
android:id="#+id/button1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="44dp"
android:text="CLEAR TEXT"
app:layout_constraintEnd_toStartOf="#+id/guideline13"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="#+id/guideline14"
app:layout_constraintTop_toBottomOf="#+id/button" />
<Button
android:id="#+id/button2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="48dp"
android:text="BACK"
app:layout_constraintEnd_toStartOf="#+id/guideline13"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="#+id/guideline14"
app:layout_constraintTop_toBottomOf="#+id/button1" />
</android.support.constraint.ConstraintLayout>
Use savedInstanceState Bundle in onCreate
The savedInstanceState is a reference to a Bundle object that is passed into the onCreate method of every Android Activity. Activities have the ability, under special circumstances, to restore themselves to a previous state using the data stored in this bundle. If there is no available instance data, the savedInstanceState will be null.
For example, the savedInstanceState will always be null the first time
an Activity is started, but may be non-null if an Activity is
destroyed during rotation.
When do I save things to the Bundle?
All activities have an onSaveInstanceState method that can be overridden. When this method is called, any state-related data should be placed into the outState Bundle. This method is called when an Activity is being backgrounded (either after onPause() or onStop(), depending on different factors).
What should be saved?
The savedInstanceState Bundle should only save information directly related to the current Activity state. Examples of this include:
A typical example is User-submitted data – If a user writes their username into a text box, they would expect the username to still be present when the Activity is resumed. This data should be saved in onSaveInstanceState then restored when the Activity is re-created.
In your onCreate function, this Bundle is handed back to the program. The best way to check if the application is being reloaded, or started for the first time is:
if (savedInstanceState != null) {
// Then the application is being reloaded
}
To get the data back out, use the get* functions just like the put* functions. The data is stored as a name-value pair. This is like a hashmap. You provide a key and the value, then when you want the value back, you give the key and the function gets the value. Here's a short example.
#Override
public void onSaveInstanceState(Bundle outState) {
outState.putString("message", "This is my message to be reloaded");
super.onSaveInstanceState(outState);
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (savedInstanceState != null) {
String message = savedInstanceState.getString("message");
Toast.makeText(this, message, Toast.LENGTH_LONG).show();
}
}
// Promoting Local variable to Field
public class Main2Activity extends AppCompatActivity {
private TextView tv1;
private TextView tv2;
private TextView tv3;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
tv1 = (TextView) findViewById(R.id.textView);
tv2 = (TextView) findViewById(R.id.textView2);
tv3 = (TextView) findViewById(R.id.textView3);
...
}
}
Use shared Preference to store values temporally/ permanent
EDITED
Sorry i was not seen the code after seen code.
Your setting values inside onlCick again. You have set the values outside the onClick
Solution
Create one global SharedPreferences with MODE_PRIVATE, set the values onClick and read values on onCreate(outside onClick).
**I am making some app and cant see a button that I have created (this is when running the app on my phone) ,but if I run it on the emulator I can see everything - the button that I can't see on my phone is the clicked button (its id is "startBtn").
**
Here is my layout :
<?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/activity_main"
android:background="#798"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="unleashed.myprefs.MainActivity">
<TextView
android:id="#+id/textView"
android:layout_width="match_parent"
android:layout_height="90dp"
android:text="hello"
android:background="#ff14"
/>
<Button
android:id="#+id/startBtn"
android:text="start"
android:layout_below="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="addOne"
/>
<Button
android:id="#+id/resetBtn"
android:text="restart"
android:layout_below="#+id/textView"
android:layout_alignParentRight="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="restart"
/>
</RelativeLayout>
And this is my main activity
package unleashed.myprefs;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
Button restartNums;
SharedPreferences prefs;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
prefs = getSharedPreferences("main", MODE_PRIVATE);
}
private int getNum(){
return prefs.getInt("num",1);
}
private void setNum(int num){ // put the number into my shared preference
prefs.edit().putInt("num", num).apply();
}
public void addOne(View v) { //adding number to the starting button
int num = getNum();
((Button)v).setText("Clicked " + num);
setNum(num + 1);
}
public void restart(View v){
restartNums = (Button) findViewById(R.id.startBtn);
prefs.edit().remove("num").apply();
restartNums.callOnClick();
}
}
I will appreciate any help.
AlignParentEnd should be used in ResetButton.( Not sure )
(Alternative) If you trying to have two buttons in the same row, you can try this layout..
`
<TextView
android:id="#+id/textView"
android:layout_width="match_parent"
android:layout_height="90dp"
android:text="hello"
android:background="#ff14"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation = "horizontal"
android:weightsum="2"
android:layout_below="#+id/textView">
<Button
android:id="#+id/startBtn"
android:text="start"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:onClick="addOne"
/>
<Button
android:id="#+id/resetBtn"
android:text="restart"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:onClick="restart"/>
`
I am making a simple app in which context view changes and then a toast message is displayed as input given by user. I don't understand why the app keeps on crashing when changing the context view.
Also Android studio gives this warning:
Method "Toaster" is missing in "FirstActivity" or has incorrect signature.
Here is my code:
activity_me_clicked.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="#string/welcome"
android:textSize="36sp" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:paddingLeft="7dp"
android:paddingRight="7dp"
android:text="#string/intro"
android:textSize="24sp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:baselineAligned="false">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical">
<EditText
android:id="#+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="7dp"
android:layout_marginEnd="7dp"
android:layout_marginTop="10dp"
android:ems="10"
android:hint="Name"
android:inputType="textPersonName"
tools:ignore="HardcodedText" />
<Button
android:id="#+id/named"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:paddingBottom="10dp"
android:paddingTop="10dp"
android:onClick="MainProcess"
android:text="#string/done" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="horizontal"
tools:ignore="UselessLeaf"></LinearLayout>
</LinearLayout>
</LinearLayout>
FirstActivity.Java:
package com.example.nautatvanavlakha.abcd;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
public class FirstActivity extends AppCompatActivity {
public static String owner_string;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_me_clicked);
}
public void MainProcess(View view) {
final String TAG="DEBUG";
Log.d(TAG,"At least process started");
EditText owner = (EditText) findViewById(R.id.name);
owner_string = owner.getText().toString();
Log.d(TAG,"owner name stored");
TextView textView = (TextView) findViewById(R.id.welcome);
textView.setText("Hi " + owner_string + ".");
Log.d(TAG,"owner name is set");
setContentView(R.layout.activity_main_screen);
Log.d(TAG,"content shown");
}
}
activity_main_screen.xml:
<?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">
<TextView
android:id="#+id/welcome"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/textView4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/what_would_you_like_me_to_do_today"
android:textSize="18sp" />
<Button
android:id="#+id/Cam"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:padding="0dp"
android:paddingTop="15dp"
android:text="#string/camera" />
<Button
android:id="#+id/Mus"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:padding="0dp"
android:paddingTop="15dp"
android:text="#string/music" />
<Button
android:id="#+id/QR"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:padding="0dp"
android:paddingTop="15dp"
android:text="#string/scanQR" />
<EditText
android:id="#+id/toastText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="#string/order"
android:inputType="textPersonName" />
<Button
android:id="#+id/toaster"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:onClick="toaster"
android:padding="0dp"
android:paddingTop="15dp"
android:text="#string/toast"
android:layout_marginEnd="100dp"
android:layout_gravity="center"/>
</LinearLayout>
MainScreen.java:
package com.example.nautatvanavlakha.abcd;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
public class MainScreen extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_screen);
}
public void toaster(View view){
EditText toast = (EditText)findViewById(R.id.toastText);
String final_toast = toast.getText().toString();
Toast.makeText(getApplicationContext(), final_toast, Toast.LENGTH_SHORT).show();
}
}
EDIT: As suggested, I moved the toaser function to FirstActivity.Java and deleted the MainScreen.java file as it becomes pointless to keep it. But the major problem is when I press the button (id named) the app keeps stopping.
EDIT2: I found that setContentView(R.layout.activity_main_screen) in FirstActivity.Java needs to be above this code
TextView textView = (TextView) findViewById(R.id.welcome);
textView.setText("Hi " + owner_string + ".");
Log.d(TAG,"owner name is set");
so that Activity has access to all the layout components. Thanks solved :)
You replaced the content view in the first activity with a layout that include the onClick attribute, but you have no public void toaster(View view) method there.
So, either don't use setContentView a second time, or implement that method on both Activities.
The recommended way to replace the view is Fragments, by the way
This is a wrong way to change the contents of your Activity, The main problem here is that you have onClick attribute set to "toaster", And you don't have a function called "toaster" in your FirstActivity.
Beside that, The MainScreen Activity in your code will never be used.
So, Your problem is that you set the FirstActivity contents to "activity_main_screen.xml", And FirstActivity doesn't have "toaster" method in it, When you change the context, the Activity will try to find toaster method and the app will crash because the method doesn't exist in FirstActivity.
You can solve this problem by making a method called "toaster" inside FirstActivity, But
This is a very bad practice, You can use Fragments instead, Or you can use Intent to move to another activity.
Some useful links about Fragments:
https://developer.android.com/guide/components/fragments.html
https://www.tutorialspoint.com/android/android_fragments.htm
https://examples.javacodegeeks.com/android/core/app/fragment/android-fragments-example/
Why should I use fragment in Android?
I am trying to embed RadioGroup with LinearLayout, so to add elements dynamically to my Layout. My code goes here
`
<RadioButton
android:id="#+id/dailyCheck"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/dailyCheck"/>
<RadioButton
android:id="#+id/selectedCheck"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/selectedCheck" />
<RadioButton
android:id="#+id/specificCheck"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/specificCheck" />
`
Doing this shows an exception:
This RadioGroup layout or its LinearLayout parent is useless
Please suggest me what to do.
Why can't LinearLayout and RadioGroup be together in one file.
package app.test;
import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.widget.LinearLayout;
import android.widget.RadioGroup;
public class Test extends Activity implements
RadioGroup.OnCheckedChangeListener {
RadioGroup orientation;
RadioGroup gravity;
#Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
orientation = (RadioGroup) findViewById(R.id.orientation);
orientation.setOnCheckedChangeListener(this);
gravity = (RadioGroup) findViewById(R.id.gravity);
gravity.setOnCheckedChangeListener(this);
}
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch (checkedId) {
case R.id.horizontal:
orientation.setOrientation(LinearLayout.HORIZONTAL);
break;
case R.id.vertical:
orientation.setOrientation(LinearLayout.VERTICAL);
break;
case R.id.left:
gravity.setGravity(Gravity.LEFT);
break;
case R.id.center:
gravity.setGravity(Gravity.CENTER_HORIZONTAL);
break;
case R.id.right:
gravity.setGravity(Gravity.RIGHT);
break;
}
}
}
//main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<RadioGroup android:id="#+id/orientation"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5px">
<RadioButton
android:id="#+id/horizontal"
android:text="horizontal" />
<RadioButton
android:id="#+id/vertical"
android:text="vertical" />
</RadioGroup>
<RadioGroup android:id="#+id/gravity"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="5px">
<RadioButton
android:id="#+id/left"
android:text="left" />
<RadioButton
android:id="#+id/center"
android:text="center" />
<RadioButton
android:id="#+id/right"
android:text="right" />
</RadioGroup>
</LinearLayout>