Trying to create notes application. I am trying to add dynamic text view after button click and it should be added to layout, one after another. In my code, only one text view is getting added successfully but from second time, app got crashed. Please help
XML Code:
<EditText
android:id="#+id/text_thingsToDo"
android:layout_width="#dimen/ThingsToDo_EditText_Width"
android:layout_height="#dimen/ThingsToDo_EditText_Height"
android:layout_margin="#dimen/Standardize_Margin"
android:background="#color/ThingsToDo_EditText_Color"/>
<Button
android:id="#+id/save_thingsToDo"
android:layout_toRightOf="#+id/text_thingsToDo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/ThingsToDo_Save_Button_Text"
android:layout_marginRight="#dimen/Standardize_Margin"
android:layout_marginTop="#dimen/Standardize_Margin"
android:layout_marginBottom="#dimen/Standardize_Margin"
android:background="#color/ThingsToDo_Save_Button"
android:textColor="#color/White"
android:layout_centerVertical="true"/>
Java Code:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_things_to_do);
final EditText editText = (EditText) findViewById(R.id.text_thingsToDo);
Button button = (Button) findViewById(R.id.save_thingsToDo);
final TextView notesView = new TextView(this);
final LinearLayout linearLayout = (LinearLayout) findViewById(R.id.rootView_ThingsToDo);
final ArrayList<String> arrayList = new ArrayList<String>();
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
notesView.setText(editText.getText().toString());
linearLayout.addView(notesView);
}
});
}
Perhaps create a new TextView every time you press the button? Try to declare the TextView as a class member and then in your OnClickListener do this:
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
notesView = new TextView(this);
notesView.setText(editText.getText().toString());
linearLayout.addView(notesView);
}
});
Hopefully this will fix your problem!
My problem is the following.
my app has a 1 welcome screen where the user ckick the "continue" button and it goes to next screen. The next one contains a menu with several buttons.
my problem is that I can not open another activity on the second screen (on the first screen it opens normal)
more or less this scheme below
(| activity1> button continue | >> | activity2> button continue2 |> does not respond)
to compliment and test apk on a galaxy grand duos 4.2.2
code below
code 1 screen (welcome).
public class Main2Activity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
Button button7 = (Button) findViewById(R.id.button7);
button7.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
setContentView(R.layout.activity_main4);
}
});}}
code 2 tela
public class Main4Activity extends AppCompatActivity {
private Button prova;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main4);
prova = (Button) findViewById(R.id.button5);
prova.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent( Main4Activity.this, Main3Activity.class);
startActivity(intent);
}
});
}}
2 tela code xml button
<Button
android:id="#+id/button5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="#+id/button4"
android:layout_marginTop="11dp"
android:text="tela 2"/>
First, I want to make sure that you understand what you are writing.
button7.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
setContentView(R.layout.activity_main4);
}
});
In your onclick, you only set your view of Main2Activity to screen of activity_main4.xml. But you are still standing in Main2Activity (It means you are in Main2Activity with view activity_main4.xml).
In this case, Main4Activity hasn't initialized and the button prova hasn't been initialized too. So when you press prova button, it won't do anything.
Second, to solve your problem, make Main4Activity be initialized, you must start it. So, instead of using:
setContentView(R.layout.activity_main4);
in Main2Activity, which only change the view, not the Activity. You should use
Intent intent = new Intent(Main2Activity.this, Main4Activity.class);
startActivity(intent);
Hope you can understand this!
Your problem is that you can't use setContentView(R.layout.activity_main4); to open another activity .You can use startActivity method to open another activity .
You can try this .
1.remove the code in your Main2Activity
setContentView(R.layout.activity_main4);
2.add change to this
button7.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent i=new Intent(Main2Activity.this,Main4Activity.this);
startActivity(i);
}
});}}
I'm new to android develop. i just stated this week.
i dont know if im asking stupid question but i want to learn.
i have created an app just click a button change the background color.
my layout is this
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
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:id="#+id/background"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.seluhadu.colorpicker.MainActivity">
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
tools:layout_editor_absoluteX="136dp"
tools:layout_editor_absoluteY="1dp"
android:elevation="8dp"
android:layout_alignBaseline="#+id/button3"
android:layout_alignBottom="#+id/button3"
android:layout_alignParentEnd="true"
android:layout_marginEnd="52dp"/>
</RelativeLayout>
and i have a class "java" like this
public class MainActivity extends AppCompatActivity {
Button button;
RelativeLayout relativeLayout;
Integer[] colors = {
R.color.color1,
R.color.color2,
R.color.color3,
R.color.color4,
R.color.color5,
R.color.color6,
R.color.color7,
R.color.color8,
R.color.color9,
R.color.color10,
R.color.color11,
R.color.color12,
R.color.color13,
R.color.color14,
R.color.color15,
R.color.color16,
R.color.color17,
R.color.color18,
R.color.color19,
R.color.color20
};
Random random;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
random = new Random();
relativeLayout = (RelativeLayout) findViewById(R.id.background);
button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
relativeLayout.setBackgroundColor(ContextCompat.getColor(getApplicationContext(),
colors[random.nextInt(colors.length)]));
}
});
}
it just change the background color random.
when it change the color if i rotat the screen it change to default back.
my question is how to make a onSaveInstanceState and onRestoreInstanceState method to save the change color even if i reopen the app to stay the color that i change.
OR how can i do like that in other way?
thank you for your help!
When your activity is recreated after it was previously destroyed, you can recover your saved state from the Bundle that the system
passes your activity. Both the onCreate() and onRestoreInstanceState()
callback methods receive the same Bundle that contains the instance
state information.
Because the onCreate() method is called whether the system is creating
a new instance of your activity or recreating a previous one, you must
check whether the state Bundle is null before you attempt to read it.
If it is null, then the system is creating a new instance of the
activity, instead of restoring a previous one that was destroyed.
public class MainActivity extends AppCompatActivity {
Button button;
RelativeLayout relativeLayout;
Integer[] colors = {
R.color.color1,
R.color.color2,
R.color.color3,
R.color.color4,
R.color.color5,
R.color.color6,
R.color.color7,
R.color.color8,
R.color.color9,
R.color.color10,
R.color.color11,
R.color.color12,
R.color.color13,
R.color.color14,
R.color.color15,
R.color.color16,
R.color.color17,
R.color.color18,
R.color.color19,
R.color.color20
};
Random random;
Boolean backgroundChanged = false;
int colorIndex;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Check whether we're recreating a previously destroyed instance
if (savedInstanceState != null) {
// Restore background color from saved state
backgroundChanged = savedInstanceState.getBoolean("backgroundChanged", false);
colorIndex = savedInstanceState.getInt(colorIndex, 0);
}
random = new Random();
relativeLayout = (RelativeLayout) findViewById(R.id.background);
if(backgroundChanged) {
relativeLayout.setBackgroundColor(ContextCompat.getColor(getApplicationContext(), colors[colorIndex]));
}
button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
backgroundChanged = true;
colorIndex = random.nextInt(colors.length);
relativeLayout.setBackgroundColor(ContextCompat.getColor(getApplicationContext(), colors[colorIndex]));
}
});
}
#Override
public void onSaveInstanceState(Bundle savedInstanceState) {
savedInstanceState.putBoolean("backgroundChanged", backgroundChanged);
if(backgroundChanged) {
savedInstanceState.putInt("colorIndex", colorIndex);
}
// Always call the superclass so it can save the view hierarchy state
super.onSaveInstanceState(savedInstanceState);
}
Update
Here's my code below, plugged into your posted example:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
random = new Random();
relativeLayout = (RelativeLayout) findViewById(R.id.background);
button = (Button) findViewById(R.id.button);
SharedPreferences settings = getSharedPreferences("myPrefs", 0);
int colorIndex = settings.getInt("colorIndex", 0);
updateBackground(colorIndex);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int colorIndex = random.nextInt(colors.length);
SharedPreferences settings = getSharedPreferences("myPrefs", 0);
settings.edit().putInt("colorIndex", colorIndex).apply();
updateBackground(colorIndex);
}
});
}
private void updateBackground(int colorIndex) {
relativeLayout.setBackgroundColor(ContextCompat.getColor(getApplicationContext(), colors[colorIndex]));
}
If you have to store the color even through app restarts, you should use SharedPreferences instead of using onSaveInstanceState().
In your onClick(), when you generate the random index for your color array, write that value to your SharedPreferences object. In onCreate() when you set everything up, read the value out of the SharedPreferences.
To write:
int colorIndex = random.nextInt(colors.length);
SharedPreferences settings = getSharedPreferences("myPrefs", 0);
settings.edit().putInt("colorIndex", colorIndex).apply();
To read:
SharedPreferences settings = getSharedPreferences("myPrefs", 0);
int colorIndex = settings.getInt("colorIndex", 0);
I want to create a simple application that has a button with an image (ben1), and when you click the button it plays an audio clip (audiofile) and the image changes to a second image (ben2).
Once the button is no longer clicked the image changes back to the original.
I currently have the below code which plays an audio file when the button is clicked
And below that is code I had from an other app that change the button image when it was clicked.
Can someone assist me with exactly how I would merge theses.
Play audio File
public class MainActivity extends Activity{
private SoundPool soundPool;
private int soundID;
boolean loaded = false;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final MediaPlayer benSoundMP = MediaPlayer.create(this,R.raw.audiofile);
Button playbenSound = (Button) this.findViewById(R.id.button1);
playbenSound.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
benSoundMP.start();
}
});
}
}
Change Button Image
public class MainActivity extends Activity {
ImageButton button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button= (ImageButton)findViewById(R.id.button);
button.setOnClickListener(imgButtonHandler);
}
View.OnClickListener imgButtonHandler = new View.OnClickListener() {
public void onClick(View v) {
button.setBackgroundResource(R.drawable.icon_red);
}
};
}
The right way to do this, is to set as resource of the button a selector xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="false" android:drawable="#drawable/button_standby"/>
<item android:state_pressed="true" android:drawable="#drawable/button_pressed"/>
</selector>
And you don't need to change or add anything to the button listener, on your button declaration on layout, you add this file as the background.
Remove the imgButtonHandler and add this:
button.setOnClickListener(new OnclickListener(){
public void onClick(View v) {
button.setBackgroundResource(R.drawable.icon_red);
}
});
I'm supporting an android 2.2+ app. I'm using the code image.setOnClickListener(new View.On Click Listener()
and a dialog to show a larger image in another layout.
Current project has an error. When I debug the project, I see a NullPointerException on image.setOnClickListener(new View.OnClickListener() saying the image is null.
Main Activity
#SuppressLint("ClickableViewAccessibility")
public class MainActivity extends ListActivity {
Context c;
Intent intent;
ImageView image, image1;
#SuppressLint("SdCardPath")
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
image = (ImageView) image.findViewById(R.id.image_my);
// *** mean here
image_my.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TO DO Auto-generated method stub
show(v);
}
});
}
public void show(View v) {
final Dialog dialog = new Dialog(MainActivity.this);
dialog.setContentView(R.layout.show);
image1 = (ImageView) image1.findViewById(R.id.image_my1);
dialog.show();
}
}
Layout XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<ImageView
android:id="#+id/image_my1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="left|center_vertical"
android:src="#drawable/image_def" />
</LinearLayout>
I'm not sure where you got this code from, but you are repeatedly using variables before you have assigned them:
image = (ImageView) image.findViewById(R.id.image_my);
Here you are calling findViewById on your image instance, but image will be null here. You probably want:
image = (ImageView) this.findViewById(R.id.image_my);
Similarly, in your show() method:
image1 = (ImageView) image1.findViewById(R.id.image_my1);
should be
image1 = (ImageView) this.findViewById(R.id.image_my1);
You might want to try using more descriptive names for your variables too - it makes it a bit easier to spot problems like this.