onClick change imageButton in MainActivity - java

When i click onClick button change imageButton in MainActivity.class
This is Activity with onClick button:
public void onClick8 (View view) {
//here please code for change imagebutton from onClick
Intent myIntent = new Intent(view.getContext(), MainActivity.class);
startActivity(myIntent);
finish();
MediaPlayer mediaPlayer = MediaPlayer.create(zem13.this, R.raw.melody);
mediaPlayer.start();
}
This is code in MainActivity with imageButton:
ImageButton imageButton = (ImageButton)findViewById(R.id.imageButton2);
imageButton.setImageResource(R.drawable.psik);

if I understand well, you should pass some indicator to change image resource to the MainActivity and set it in onCreate(). Try to change your code like this:
public void onClick8 (View view) {
//here please code for change imagebutton from onClick
Intent myIntent = new Intent(view.getContext(), MainActivity.class);
myIntent.putExtra("shouldChangeButton", true);
startActivity(myIntent);
finish();
MediaPlayer mediaPlayer = MediaPlayer.create(zem13.this, R.raw.melody);
mediaPlayer.start();
}
and then in MainActivity:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
boolean defaultValue = false; //or true as you need
if(getIntent().getBooleanExtra("shouldChangeButton", defaultValue)) {
ImageButton ib = (ImageButton) findViewById(R.id.imagebutton);
ib.setImageResource(R.drawable.img);
}
}
or in case you want permanent change, you should keep it in SharedPreferences instead of passing it through intent:
public void onClick8 (View view) {
//here please code for change imagebutton from onClick
Intent myIntent = new Intent(view.getContext(), MainActivity.class);
SharedPreferences prefs = getSharedPreferences("YourAppNamePrefs", MODE_PRIVATE);
prefs.edit().putBoolean("shouldChangeButton", true).apply();
startActivity(myIntent);
finish();
MediaPlayer mediaPlayer = MediaPlayer.create(zem13.this, R.raw.melody);
mediaPlayer.start();
}
and
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
boolean defaultValue = false; //or true as you need
SharedPreferences prefs = getSharedPreferences("YourAppNamePrefs", MODE_PRIVATE);
boolean shouldChangeButton = prefs.getBoolean("shouldChangeButton", defaultValue);
if(shouldChangeButton) {
ImageButton ib = (ImageButton) findViewById(R.id.imagebutton);
ib.setImageResource(R.drawable.img);
}
}

You need to add a onClickListener to the view which would trigger onClick. And then you need override the onClick where you finish the operations to change imageButton.
Just Look at this:
Button btn=new Button(this);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//to change you imageButton here.
imageButton.setBackgroundResource(0);
}
});

Related

Cannot resolve symbol view

this public void onClick (View view) gives error
cannot resolve symbol view
whats the issue?
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btnCamera = (Button) findViewById(R.id.btnCamera);
ImageView imageView = (ImageView) findViewById(R.id.imageView);
btnCamera.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick (View view){
//view){
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, 0);
}
});
}
Your question is mostly just a typo, but to point out the problem, you are passing an instance of anonymous class to the Button#setOnClickListener method. The correct syntax for this is:
btnCamera.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick (View view) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, 0);
}
});
Note carefully that the anonymous class is demarcated using { ... } braces, in which you are overriding and providing an alternative implementation for the onClick() method.

images Intent does not open randomly. If I press the images in order intents are working

I have six images. If I press the first image intent is working. But if I press the third image firstly it is not working. it is working in order.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageView imageView = (ImageView) findViewById(R.id.indoor_activities);
imageView.bringToFront();
imageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent myIntent = new Intent (MainActivity.this,indoorActivities.class);
startActivity(myIntent);
ImageView imageView = (ImageView) findViewById(R.id.outdoor_activities);
imageView.bringToFront();
imageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent myIntent = new Intent(MainActivity.this, outdoorActivities.class);
startActivity(myIntent);
You are setting other view click listener inside R.id.indoor_activities
In above code, you are saying that assign click listener every time to outdoor_activities if user click on indoor_activities first.
To fix this
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageView imageView = (ImageView) findViewById(R.id.indoor_activities);
imageView.bringToFront();
imageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent myIntent = new Intent (MainActivity.this,indoorActivities.class);
startActivity(myIntent);
});// click listener for indoor activities
// click listener for outdoor activities when onCreated is called
ImageView imageView = (ImageView) findViewById(R.id.outdoor_activities);
imageView.bringToFront();
imageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent myIntent = new Intent(MainActivity.this, outdoorActivities.class);
startActivity(myIntent);
});
I think you should do like this.This will certainly works as your requirement.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageView imageView1 = (ImageView) findViewById(R.id.indoor_activities);
imageView1.bringToFront();
imageView1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent myIntent1 = new Intent (MainActivity.this,indoorActivities.class);
startActivity(myIntent1);
}
});
ImageView imageView2 = (ImageView) findViewById(R.id.outdoor_activities);
imageView2.bringToFront();
imageView2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent myIntent2 = new Intent(MainActivity.this, outdoorActivities.class);
startActivity(myIntent2); }
});
Hope this helps you.

how to display the clicked radio button value on my second activity in android studio?

This is my java code:
public class jenis extends AppCompatActivity {
public Button button3;
RadioGroup radioGroup2;
RadioButton radioButton2;
DatabaseReference databaseReference;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_jenis);
radioGroup2 = (RadioGroup) findViewById(R.id.radioGroup2);
databaseReference = FirebaseDatabase.getInstance().getReference();
final RadioButton stock = (RadioButton)findViewById(R.id.radioButton11);
final RadioButton property = (RadioButton)findViewById(R.id.radioButton12);
button3 = (Button) findViewById(R.id.button3);
button3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (stock.isChecked()){
Variable newVar = new Variable();
newVar.setAmountt("jeniss");
databaseReference.child(newVar.getAmountt()).setValue("Stock Waqf");
}
else if(property.isChecked()){
Variable newVar = new Variable();
newVar.setAmountt("jeniss");
databaseReference.child(newVar.getAmountt()).setValue("Property Waqf");
}
if(stock.isChecked()){
Intent intent = new Intent(jenis.this, stock.class);
startActivity(intent);
}
if(property.isChecked()){
Intent intent = new Intent(jenis.this, property.class);
startActivity(intent);
}
}
});
}
}
How can I display the clicked radio button value on my second activity?
I really appreciate your help, thank you.
pass the value as intent
Intent intent = new Intent(youractivity.this,secondactivity.class);
intent.putExtra("key",radiobutton.getText());
startActivity(intent);
to access it on second activity
in OnCreate() method
getIntent().getStringExtra("key");
or else
you can also store your radiobutton checked value in shared preference and
then access it on another activity... It will work like session
public class stock extends AppCompatActivity {
DatabaseReference databaseReference;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_stock);
final TextView textViewOut = (TextView) findViewById(R.id.textViewOut2);
textViewOut.setText(" ");
}
public void OnCreate() {
getIntent().getStringExtra("jeniss");
}
}
what did i missed ?

Remove activities manually from Android app stack

I been working on Android Native App , What i was trying to do is :
Activities - A -> B -> C Then A-> B -> C -> C .
From C Activity if it again point to C then i want to remove C , B from stack manually .
On my back it should move only to A .
I tried finish() but problem is :
Activities - A -> B -> C Then A-> B -> C -> C on finish A -> B -> C required state A-> C .
Is anyone know how to catch all activities in stack and remove specific activities from stack ??
In Activity C, override onBackPressed and add in something like:
#Override
public void onBackPressed() {
if (shouldGoBackToA) { // There are various ways this could be set
Intent intent = new Intent(this, AActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
} else {
finish();
}
}
FLAG_ACTIVITY_CLEAR_TOP will cause it to go down the stack to the existing copy of A Activity instead of starting a new one. From the docs:
public static final int FLAG_ACTIVITY_CLEAR_TOP
If set, and the activity being launched is already running in the current task, then instead of launching a new instance of that activity, all of the other activities on top of it will be closed and this Intent will be delivered to the (now on top) old activity as a new Intent.
While calling intent pass a flag called actvity clear top like this:
Intent newIntent=new Intent(this,MainActivity.class);
newIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(newIntent);
You can use this :
In A activity while passing to B activity, the intent should be added with a flag FLAG_ACTIVITY_NO_HISTORY like this,
Button b=(Button) findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent newIntent=new Intent(AActivity.this,Bactivty.class);
newIntent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
startActivity(newIntent);
}
});
While moving to CActivity:
Button b = (Button) findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent newIntent = new Intent(Bactivty.this, CActivity.class);
startActivity(newIntent);
}
});
On backpress will take you to AActivity now.
Step 1: Start activty for result A -> B -> C1 -> C2..
Call your Activity with startActivityForResult
Intent intent = new Intent(yourActivity.this, nextActivity.class);
startActivityForResult(intent, 1);
Step 2: In C2 specify that you want to go back to A..
Whenever you are done with your activity write the below code
Intent i = getIntent();
i.putString("Result","GottoA");
setResult(Activity.RESULT_OK, i);
finish();
Step 3: Whenever C2 finishes , previsus stack activit's onActivityResult is called.. so u can check in C1 and B onActivityResult whether you have set any result bck.. and finish accordingly
and impliment the following code in Activity B and c
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Intent i = getIntent();
if (resultCode == RESULT_OK && i.getString("Result","null").equals"GottoA") {
i.putString("Result","GottoA");
setResult(RESULT_OK, i);
finish();
}
}
In Activity C, when back button is pushed start activity A like this:
#Override
public void onBackPressed() {
Intent intent = new Intent(getApplicationContext(), A.class);
intent.putExtra("EXIT", true);
startActivity(intent);
}
Then in Activity A's onCreate() do this
if (getIntent().getBooleanExtra("EXIT", false)) {
finish();
}
this complete example may help you...
public class ActivityA extends Activity {
public static final int ID_TEXTVIEW = 0xDEAF1;
public static final int ID_BUTTON = 0xDEAF2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
View contentView = getContentView(this);
TextView textView = (TextView) contentView.findViewById(ID_TEXTVIEW);
textView.setText("ActivityA");
setContentView(contentView);
final Button button = (Button) contentView.findViewById(ID_BUTTON);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(ActivityA.this, ActivityB.class);
startActivity(intent);
}
});
}
public static View getContentView(Context context) {
LinearLayout layout = new LinearLayout(context);
layout.setOrientation(LinearLayout.VERTICAL);
layout.setGravity(Gravity.CENTER);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
layoutParams.gravity = Gravity.CENTER_HORIZONTAL;
TextView textView = new TextView(context);
textView.setLayoutParams(layoutParams);
textView.setId(ID_TEXTVIEW);
layout.addView(textView);
Button button = new Button(context);
button.setText("Next");
button.setLayoutParams(layoutParams);
button.setId(ID_BUTTON);
layout.addView(button);
return layout;
}
}
public class ActivityB extends Activity {
public static final String ACTION_FINISH = "com.myapp.test2.ACTION_FINISH";
public ActivityB() {
}
private FinishReceiver finishReceiver;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
View contentView = ActivityA.getContentView(this);
final TextView textView = (TextView) contentView
.findViewById(ActivityA.ID_TEXTVIEW);
textView.setText("ActivityB");
setContentView(contentView);
final Button button = (Button) contentView
.findViewById(ActivityA.ID_BUTTON);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(ActivityB.this, ActivityC.class);
startActivity(intent);
}
});
finishReceiver = new FinishReceiver();
IntentFilter filter = new IntentFilter(ACTION_FINISH);
registerReceiver(finishReceiver, filter);
}
#Override
protected void onDestroy() {
super.onDestroy();
unregisterReceiver(finishReceiver);
}
private class FinishReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(ACTION_FINISH)) {
finish();
}
}
}
}
public class ActivityC extends Activity {
public ActivityC() {
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
View contentView = ActivityA.getContentView(this);
final TextView textView = (TextView) contentView
.findViewById(ActivityA.ID_TEXTVIEW);
textView.setText("ActivityC");
setContentView(contentView);
final Button button = (Button) contentView.findViewById(ActivityA.ID_BUTTON);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(ActivityB.ACTION_FINISH);
sendBroadcast(intent);
intent = new Intent(ActivityC.this, ActivityC.class);
startActivity(intent);
finish();
}
});
}
}

Android. Call system dialog

Is there a way to open the system dialog settings->location & security->Install from SD card programmatically from my application?
You launch at least the security setting by the following code.
public class MainActivity extends Activity {
Button button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
final Intent intent = new Intent(Intent.ACTION_MAIN, null);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
final ComponentName cn = new ComponentName(
"com.android.settings",
"com.android.settings.SecuritySettings");
intent.setComponent(cn);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
});
}

Categories

Resources