setVisibility() in onClick() makes app crash - java

I'm trying to show pic after pressing ImageButton, but when I test it on my phone it just crashes and drops me on previous Activity.
There are some more Activities, but each of them works normally.
ActionActivity.java
public class ActionActivity extends AppCompatActivity implements View.OnClickListener {
ImageButton PhotoHint;
ImageView PhotoPic;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.action);
ImageButton PhotoHint = (ImageButton) findViewById(R.id.PhotoHint);
ImageView PhotoPic = (ImageView) findViewById(R.id.Photo);
PhotoPic.setVisibility(View.INVISIBLE);
PhotoHint.setOnClickListener(this);
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.PhotoHint:
PhotoPic.setVisibility(View.VISIBLE);
}
}
}
action.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
...
<FrameLayout
android:id="#+id/frameLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/Photo"
android:layout_width="189dp"
android:layout_height="169dp"
android:layout_gravity="center"
android:background="#drawable/somepic"
/>
<ImageButton
android:id="#+id/PhotoHint"
android:layout_width="42dp"
android:layout_height="wrap_content"
android:layout_gravity="center|bottom"
app:srcCompat="#android:drawable/ic_menu_gallery" />
</FrameLayout>
</LinearLayout>

try now it will work fine...
the problem was with this line
ImageButton PhotoHint = (ImageButton) findViewById(R.id.PhotoHint);
ImageView PhotoPic = (ImageView) findViewById(R.id.Photo);
they were not globally initialized that's why you were facing crash.!
public class ActionActivity extends AppCompatActivity implements View.OnClickListener {
ImageButton photoHint;
ImageView photoPic;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.action);
photoHint = (ImageButton) findViewById(R.id.PhotoHint);
photoPic = (ImageView) findViewById(R.id.Photo);
photoPic.setVisibility(View.INVISIBLE);
photoHint.setOnClickListener(this);
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.PhotoHint:
photoPic.setVisibility(View.VISIBLE);
}
}
}

Related

How to call Custom Activity inside onCreate method

I am developing a simple app that has two activities, MainActivity and SecondActivity and a transparent CustomActivity that extends Dialog. The
MainActivity has two buttons (Yes_button and No_button).
When user clicks Yes_button, the SecondActivity is called via Intent and the
CustomActivity will be in front of it.
When user clicks No_button the SecondActivity will also be called but the CustomActivity will not be called alongside with it.
The calling of the CustomActivity is based on if-else statement expressions. The CustomActivity has a skip button, when clicked the CustomActivity will close only then can the SecondActivity be accessible to the user. The SecondActivity has just one button that calls the MainActivity and the cycle continues.
Problem
When the app launches and a user clicks on the No_button, the SecondActivity will be called without the CustomActivity (as expected!), but ONCE a user
clicks on the Yes_button, the SecondActivity will keep on displaying alongside the CustomActivity EVEN when the No_button is clicked.
Expectation
I want the SecondActivity to be called alongside the CustomActivity each time the Yes_button is clicked and also let only the SecondActivity be called when ever the No_button is click.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xml:toos="http://schemas.android.com/tools">
<TextView
android:id="#+id/text"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/text" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/button1" />
</RelativeLayout>
MainActivity.java
public class MainActivity extends BaseActivity implements View.OnClickListener {
private static int getNumber;
Button Yes_button;
Button No_button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Yes_button = findViewById(R.id.button1);
No_button = findViewById(R.id.button2);
Yes_button.setOnClickListener(this);
No_button.setOnClickListener(this);
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button1:
int get_input = 1; // will be use in if else statement.
getNumber = get_input;
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
startActivity(intent);
break;
case R.id.button2:
Intent intent2 = new Intent(MainActivity.this, SecondActivity.class);
startActivity(intent2);
break;
}
}
public static int get_Logic() {
return getNumber;
}
}
activity_second_activity.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xml:toos="http://schemas.android.com/tools">
<TextView
android:id="#+id/text"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/text"
android:onClick="Click"
android:text="Click" />
</RelativeLayout>
SecondActivity.java
public class SecondActivity extends AppCompatActivity {
private static int output;
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second_activity);
int received = MainActivity.get_Logic();
output = received;
Display();
}
public final void Display() {
if (output == 1) {
CustomActivity custom = new CustomActivity(SecondActivity.this);
custom.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
custom.show();
} else {
CustomActivity custom = new CustomActivity(SecondActivity.this);
custom.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
custom.cancel();
}
}
public void Click(View view) {
Intent intent = new Intent(SecondActivity.this, MainActivity.class);
startActivity(intent);
}
}
activity_custom_activity.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="180dp"
android:layout_height="250dp"
xml:toos="http://schemas.android.com/tools">
<TextView
android:id="#+id/text"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/text"
android:text="Skip" />
</RelativeLayout>
CustomActivity.java
class CustomActivity extends Dialog {
Button SkipButton;
private Activity main;
public CustomActivity(Activity constr) {
super(constr);
this.main = constr;
}
#Override
protected void onCreate(Bundle saveInstanceState) {
super.onCreate(saveInstanceState);
setCancelable(false);
setContentView(R.layout.activity_custom_activity);
SkipButton = findViewById(R.id.button);
SkipButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
dismiss();
}
});
}
}
checking.xml
<?XML version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape>
<solid android:color="#FFFF" />
<corners android:radius="20dp" />
<stroke android:width="4dp" android:color="#color/colorPrimary" />
<gradient />
</shape>
</item>
</selector>
In Android, to pass data from an activity to another, you don't need to declare a static variable (getNumber in MainActivity), you should use Intent and Bundle together. See the below solution.
MainActivity.java
public class MainActivity extends BaseActivity implements View.OnClickListener {
Button Yes_button;
Button No_button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Yes_button = findViewById(R.id.button1);
No_button = findViewById(R.id.button2);
Yes_button.setOnClickListener(this);
No_button.setOnClickListener(this);
}
#Override
public void onClick(View v) {
int input = v.getId() == R.id.button1 ? 1 : 0;
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
intent.putExtra("input", input);
startActivity(intent);
}
}
SecondActivity.java
public class SecondActivity extends AppCompatActivity {
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second_activity);
Display();
}
public final void Display() {
int input = getIntent().getIntExtra("input", 0);
CustomActivity custom = new CustomActivity(SecondActivity.this);
custom.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
if (input == 1) {
custom.show();
} else {
custom.cancel();
}
}
public void Click(View view) {
finish();
}
}

Android Progress Bar not working

I have made a progress bar in Android but it is not at all working. What am I doing wrong? I have just started to learn android. Below is my code.
MainActivity.java-
public class MainActivity extends AppCompatActivity {
ProgressBar progressBar;
int mporgress=0;
EditText time;
private Handler mHandler = new Handler();
private static final int PROGRESS = 0x1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void Startbuttononclick(View view){
Button startbutton=(Button) findViewById(R.id.button);
startbutton.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
mporgress = Integer.parseInt(time.getText().toString());
progressBar.setProgress(mporgress);
new Thread(new Runnable() {
public void run() {
while (mporgress < 100) {
// Update the progress bar
mHandler.post(new Runnable() {
public void run() {
progressBar.setProgress(mporgress);
mporgress = doWork();
try{
Thread.sleep(1000);
}catch (InterruptedException e){}
}
});
}
}
}).start();
}
});
}
public void doprogress(View view) {
}
public int doWork(){
mporgress++;
return mporgress;
}
}
Activity_main.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:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.android.progressbar.MainActivity">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter the time:"
android:inputType="number"
android:id="#+id/editText" />
<ProgressBar
style="?android:attr/progressBarStyleLarge"
android:layout_width="500dp"
android:layout_height="200dp"
android:id="#+id/progressBar4"
android:layout_gravity="center"
android:scrollbarSize="500dp" />
<Button
android:text="START"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/button"
android:layout_gravity="center"
android:onClick="Startbuttononclick" />
</LinearLayout>
Check out this image below-
- https://i.stack.imgur.com/2TBIb.png
Note- Enter the time in this image is an EditText with a hint and not a TextView.
It is appearing but not working.
I have done these changes so far.-
https://i.stack.imgur.com/UujpB.png
But still the progress bar is still continuously rotating.
Seems you forgot to initialize EditText -time and ProgressBar. Init it inside onCreate:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
progressBar =(ProgressBar) findViewById(R.id.progressBar4);
time = (EditText) findViewById(R.id.editText); //here
}
Also inside button click the bar:
progressBar = new ProgressBar(this);
mporgress = Integer.parseInt(time.getText().toString());
................
.............

menu using clickable images?

I'm trying to create a menu using clickable images, but they don't seem to be working? I was following a tutorial and it uses something like
Intent biodata = new Intent(mainActivity.this, profile.class);
but it's not working so I tried to look for something else and someone said to use v.getContext() instad of ....this
it works on my other page but it doesn't work on this page?
mainActivity.java
package skripsi.garden;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
public class mainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public ImageButton buttonBio;
public void init(){
buttonBio= (ImageButton)findViewById(R.id.buttonBio);
buttonBio.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent biodata = new Intent(v.getContext(), profile.class);
startActivity(biodata);
}
}
);
}
public ImageButton buttonList;
public void tombollist(){
buttonList=(ImageButton)findViewById(R.id.buttonList);
buttonList.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent listTaman = new Intent(v.getContext(), gardenlist.class);
startActivity(listTaman);
}
}
);
}
public ImageButton buttonWeather;
public void tombolcuaca(){
buttonWeather=(ImageButton)findViewById(R.id.buttonWeather);
buttonWeather.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent cuaca = new Intent(v.getContext(), weather.class);
startActivity(cuaca);
}
}
);
}
}
and this is the xml
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android: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"
android:background="#drawable/foliagemain"
tools:context="skripsi.garden.mainActivity">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView3"
android:src="#drawable/selamatdatang"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/buttonBio"
android:src="#drawable/buttonbiodata"
android:layout_above="#+id/buttonList"
android:layout_alignRight="#+id/buttonWeather"
android:layout_alignEnd="#+id/buttonWeather" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/buttonList"
android:src="#drawable/buttondaftartaman"
android:layout_above="#+id/buttonHelp"
android:layout_alignLeft="#+id/buttonHelp"
android:layout_alignStart="#+id/buttonHelp" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/buttonHelp"
android:src="#drawable/buttonhelp"
android:layout_above="#+id/buttonWeather"
android:layout_alignLeft="#+id/buttonBio"
android:layout_alignStart="#+id/buttonBio" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/buttonWeather"
android:src="#drawable/buttoncuaca"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
Put your buttons in your onCreate method. They do not need to be in their own method.
public class mainActivity extends AppCompatActivity {
public ImageButton buttonBio;
public ImageButton buttonList;
public ImageButton buttonWeather;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
buttonBio= (ImageButton) findViewById(R.id.buttonBio);
buttonBio.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent biodata = new Intent(mainActivity.this, profile.class);
startActivity(biodata);
}
});
buttonList=(ImageButton)findViewById(R.id.buttonList);
buttonList.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent listTaman = new Intent(mainActivity.this, gardenlist.class);
startActivity(listTaman);
}
});
buttonWeather=(ImageButton)findViewById(R.id.buttonWeather);
buttonWeather.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent cuaca = new Intent(mainActivity.this, weather.class);
startActivity(cuaca);
}
});
}
}
You could just call your methods init(), tombollist() and tombolcuaca() in onCreate method of your activity after setting the layout.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
tombollist();
tombolcuaca()
}
This would initialise the button to do the action written inside the onClick.
If the methods are not called, the button would not be initialised and hence the click will not work.
You could also use android:clickable="true" in your imageButtons, and then assign the click function init() to the button in your layout xml.
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/buttonBio"
android:src="#drawable/buttonbiodata"
android:layout_above="#+id/buttonList"
android:layout_alignRight="#+id/buttonWeather"
android:layout_alignEnd="#+id/buttonWeather"
android:clickable="true"
android:onClick="init"/>
Then you wouldn't need to initialise the buttons in your activity. On click of the button, the method would be called.

OnClick Method is not being called

here is my code:
public class MainActivity extends Activity implements View.OnClickListener {
Button b;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b = (Button) findViewById(R.id.button1);
}
#Override
public void onClick(View v) {
switch (v.getId()){
case R.id.button1:
Toast.makeText(getApplicationContext(), "onClick method was called", Toast.LENGTH_LONG).show();
break;
}
}
}
For some reason nothing happens and no toast shows up. Did I miss anything ?
Thanks
write:
b.setOnClickListener(this);
in onCreate:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b = (Button) findViewById(R.id.button1);
b.setOnClickListener(this);
}
you must set a listener to the button for onClick to work
As an alternative of Vipul answer you can also add the attribute onClick in the Button tag in the XML layout file.
Example:
<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" >
<Button id="#+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="myMethod" />
</RelativeLayout>
And, in your MainActivity.java, remove the View.OnClickListener implemented interface (thanks #donfuxx) add the following method:
public void myMethod(View v) {
//do something
}

setRetainInstance not retaining the instance

I'm trying to use setRetainInstance() but it seems not working for me! =(.
I simplified the problem to the easiest version:
I have a fragment with a TextView and a Button.
The initial text of the TextView is "I'm waiting" when the button is pressed the text changes to "Hello!"
That is my code:
The fragment:
public class SayHelloFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.say_hello_layout, container);
final TextView text = (TextView) view.findViewById(R.id.textView1);
view.findViewById(R.id.button1).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
text.setText("Hello!");
}
});
return view;
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
setRetainInstance(true);
}
}
The Activity:
public class MainActivity extends FragmentActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
The activity's layout:
<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/say_hello_fragment"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
class="com.example.retaintext.SayHelloFragment" />
The Fragment's layout:
<?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/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="I&apos;m Waiting"
android:textAppearance="?android:attr/textAppearanceLarge" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
But when I play it, press the button and change the device's orientation the text becomes reset.
What am I missing? How can I solve it? Thanks!
You are missing that onCreateView runs after the rotation again and inflates the default view with the default text again.
You need to restore the state of your views manually, e.g.:
public class SayHelloFragment extends Fragment {
private String mText;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.say_hello_layout, container);
final TextView text = (TextView) view.findViewById(R.id.textView1);
if (mText != null)
text.setText(mText);
view.findViewById(R.id.button1).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mText = "Hello!";
text.setText(mText);
}
});
return view;
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
setRetainInstance(true);
}
}

Categories

Resources