Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
My app crashes. I look throught most are using this approach public class MainActivity extends
where is the errors in this short code
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main5);
radioGroup=findViewById(R.id.radioGroup);
textView=findViewById(R.id.text_view);
Button ok=findViewById(R.id.ok);
ok.setOnClickListener(new view.onClicklisenn){
public void onClick(view view){
int radioId=radioGroup.getCheckedRadioButtonId();
radioButton=findViewById(radioId);
textView.setText("choise"+radioButton.getText());
}
}
}
Use this in your code if you have initialised all variables correctly
ok.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
int radioId=radioGroup.getCheckedRadioButtonId();
radioButton=findViewById(radioId);
textView.setText("choise"+radioButton.getText());
}
});
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I have implemented a system where there exist ImageViews, which are under LinearLayouts. I wish to set an onClickListener to every ImageView that calls the same method (programmatically, obviously, not through XML), but passing a different variable, let's call it action(int i), where i is the "global index" of the ImageViews. Since i is a variable used throughout my MainActivity, simply putting action(i); in the method won't work, because I need to pass the specific object's global index. Is there any way I can do that?
Thank you.
Try this out,
imageView1.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View view) {
imageClicked(1);
}
}
.
. //More imageViews here
.
imageView10.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View view) {
imageClicked(10);
}
}
public void imageClicked(int i)
{
switch(i)
{
case 1: //Code for when imageView1 is clicked
break;
.
.
.
case 10: //Code for when imageView10 is clicked
break;
}
}
Are you trying to a call method like action(index_of_the_imageView_that_is_clicked)? That is a lot of imageViews, but at any event if that is what you are trying to do, would it be possible to set up a hashmap that maps the ImageViews to their global index and then when an ImageView is clicked, use the get(imageView) method of the hashmap to get the global index.
class yourSpecialListener implements View.OnClickListener
{
private int globalInd;
public yourSpecialListener(int par_globalInd)
{
globalInd=par_globalInd;
}
public void onClick(View v) {
action(globalInd);
}
}
Make this as inner class to your class.
imageView1.setOnClickListener(new yourSpecialListener(getyourimageviewid));
imageView2.setOnClickListener(new yourSpecialListener(getyourimageviewid));
.
.
.
Try this :
View.OnClickListener customOnClickListener = new View.OnClickListener() {
#Override
public void onClick(View view) {
action(view.getId()));
}
};
yourImageView1.setOnClickListener(customOnClickListener);
yourImageView2.setOnClickListener(customOnClickListener);
....
public void action(int imageId) {
// Do your job in terms of the imageId
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
How can I change ImageButton through another activity?
example:
I have imageButton in MainActivity.class and when I click on button in another activity changes ImageButton in the MainActivity.class
Thanks
You can implement a Local Broadcast Receiver in the Activity from where you can access the ImageButton. This receiver will be capable of hearing for a message and do something that you specify in response.
So, when you want to change the ImageButton you can send a message from the other Activity that will be received by the first one and will change the ImageButton. This message will be in an Intent form and can contain many extras.
EDIT
In your Activity #1 (The one who controls the ImageButton)
public void onCreate(Bundle savedInstanceState){
...
LocalBroadcastManager.getInstance(this).registerReceiver(mLocalBroadcastReceiver, new IntentFilter("broadcastName"));
}
private BroadcastReceiver mLocalBroadcastReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
//MODIFY YOUR IMAGE BUTTON HERE
}
};
In your Activity #2 (The broadcast sender)
Call sendLocalBroadcast() in any part of this Activity when you want to send the message to update the ImageButton. You can add many parameters as you want, of course.
private void sendLocalBroadcast() {
Intent intent = new Intent("broadcastName");
LocalBroadcastManager.getInstance(getApplicationContext()).sendBroadcast(intent);
}
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I was wondering if there was a way to add a vertical and horizontal yellow lines in the middle of the width and length respectively on the camera, so that the user has a rough guideline when making the photo. I can post code if needed. Thanks in advance.
This code may help your:
final Dialog dialog = new Dialog(this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
dialog.setContentView(R.layout.guideline);
dialog.setCanceledOnTouchOutside(true);
//for dismissing anywhere you touch
View masterView = dialog.findViewById(R.id.guidelineView);
masterView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
dialog.dismiss();
}
});
dialog.show();
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I want to put more than one activity in my main.java file but I don't know how to as I'm a newbie. I have inserted activity one but now I want to put activity two but unable to. Can you help me with it, the code until now on my main.java is:
public class Main extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstaceState) {
super.onCreate(savedInstaceState);
setContentView(R.layout.main);
TextView b = (TextView) findViewById(R.id.text1);
b.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
startActivity(new Intent(Main.this, One.class));
}
});
}
I have already created another activity files named Two.class and two.xml. How will the code be for next activities?
If you want two activity in a single calss use Fragments.
Fragments are mini activity in Android. They combine two or more than two activity in a single activity class
see following link for more detail
http://developer.android.com/guide/components/fragments.html
Intent intent = new Intent(getApplicationContext(),SecondActivity.class);
startActivity(intent);
I think it will help you
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I'm trying to write listen for button
button = (Button) v.findViewById(R.id.choose_group_button);
how to write lister for button?
like this.
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//do your stuff
}
});
Two ways, either:
Assign an on click event to your button.
findViewById(R.id.button_id).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
For this, in your xml file for your button view you'd set:
android:onClick="onClick"
And in your class you'd have your on click method which would look like:
#Override
public void onClick(View view){
switch(view.getId()){
case R.id.button_id:
// do stuff
break;
}
}