The button are appearing over the main screen, but on clicking on them, nothing is happening.
This is my MainActivity.java file
public class MainActivity extends Activity implements View.OnClickListener
{
Button brsignin;
//ImageView image1;
Button Voicerecog;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
brsignin = (Button) findViewById(R.id.brsigin);
//image1 = (ImageView) findViewById(R.id.image1);
Voicerecog = (Button) findViewById(R.id.Voicerecog);
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.brsigin:
Intent i = new Intent(this,signin.class);
startActivity(i);
break;
case R.id.Voicerecog:
Intent j = new Intent(this, VoiceRecognitionActivity.class);
startActivity(j);
break;
}
You missed the line: yourButton.setOnClickListener(this);
You must add setOnClickListener to make system recognizes that your class is using On Click Listener class
brsignin.setOnClickListener(this);
Voicerecog.setOnClickListener(this);
Related
I made one of the simplest programs that creates a login page, however, I cannot add an OnClickListener to my button, and I do not know why. I'm really new to Android Studio and have no idea what to do. When I hover over the error it says "In View cannot be applied".
I've tried these bits of code found on the internet, but the error doesn't leave, and the machine says that the #Override does not override what's above.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_teacher_in_j);
regist1 = (Button)findViewById(R.id.btnregister1);
regist1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent teachtoregist = new Intent(TeacherInJ.this, TeacherRegisterInJ.class);
startActivity(teachtoregist);
}
});
etUsername = (EditText) findViewById(R.id.editText2);
etPassword = (EditText) findViewById(R.id.editText3);
bLogin = (Button) findViewById(R.id.btnlogin);
bLogin.setOnClickListener(this);
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btnlogin:
//Start activity one
break;
When I hover over the error it says "In View cannot be applied".
I've tried these bits of code found on the internet, but the error doesn't leave, and the machine says that the #override does not override what's above.
Both the above problems are because you didn't add the implements keyword to your Activity. You need to add it so the Activity can be regarded as OnClickListener interface by the button.
You need to do something like this (See the comments inside the code):
// see below the implements View.OnClickListener line that
// need to be added so the Activitiy can be regarded as the listener.
public class TeacherInJ extends AppCompatActivity implements View.OnClickListener {
...
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_teacher_in_j);
...
// now you can use this as the listener. It's because you have
// set the current Activity class as the View.OnClickListener
// this is refer to current Activity object.
bLogin.setOnClickListener(this);
}
// Now you can add the #Override to the onClick method from
// the View.OnClickListener.
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btnlogin:
//Start activity one
break;
}
}
}
I created a project that use RecyclerView and CardView (for PointOfInterest). These 5 activities are relate to each other :
PointOfInterest.java
PlacesAdapter.java
Places.java
layout_poi.xml
activity_point_of_interest.xml
Meanwhile in activity_main.xml I design the Main Menu together with some buttons. One of the button named Rapid Penang (id: rapid_btn). I call an activity of Rapid Penang (from MainActivity.java) like below:
public class MainActivity extends AppCompatActivity {
private Button button_for_rapid;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// to call Rapid Penang class
button_for_rapid = (Button) findViewById(R.id.rapid_btn);
button_for_rapid.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
openRapid();
}
});
}
public void openRapid()
{
Intent intent_rapid = new Intent(this, RapidPenang.class);
startActivity(intent_rapid);
}
}
RapidPenang consist of only one activity and it is success. But when I try to do exactly the same to PointOfInterest activites (as mention above), suddenly the app were crashed.
This is how I try to open PointOfInterest activites from a button in MainMenu called Point Of Interest:
private Button button_for_poi;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// to call Point Of Interest class
button_for_poi = (Button) findViewById(R.id.poi_btn);
button_for_poi.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
openPOI();
}
});
}
public void openPOI()
{
Intent intent_poi = new Intent(this, PointOfInterest.class);
Intent intent_poi2 = new Intent(this, PlacesAdapter.class);
Intent intent_poi3 = new Intent(this, Places.class);
startActivity(intent_poi);
}
Firstly check your activity is define in Android manifest file and then call
StartActivity(new Intent(getApplicationcontext(),RapidPenang.class));
That's it
I want to have my MainActivity which shows a list of different TextViews.
The second Activity contains an EditText field. When I hit 'Submit' in the second Activity, I want to add the String from EditText to the list of TextViews in MainActivity.
The problem I have is that there is a new MainActivity started each time, so that only the latest Text is shown. I want to go back to my MainActivity and kind of "collect" the texts in there. Here is some code:
The MainActivity:
public class FullTimeline extends AppCompatActivity {
private LinearLayout linLayout;
private FloatingActionButton addEntry;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_full_timeline);
linLayout = (LinearLayout) findViewById(R.id.entriesLinear);
addEntry = (FloatingActionButton) findViewById(R.id.floatingActionButton); //starts the second Activity with the EditText
//Here I want to get the text from my second Activity
Intent intent = getIntent();
String entry = intent.getStringExtra(EntryTextActivity.EXTRA_ENTRY);
linLayout.addView(createNewTextView(entry), 0);
}
public void openEntryActivity(View view){
Intent intent = new Intent(this, EntryTextActivity.class);
startActivity(intent);
}
private TextView createNewTextView(String text) {
final LinearLayout.LayoutParams lparams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
final TextView textView = new TextView(this);
textView.setLayoutParams(lparams);
textView.setText(text);
return textView;
}
}
This is the second Activity with the EditText:
public class EntryTextActivity extends AppCompatActivity {
public static final String EXTRA_ENTRY = "com.xyz.ENTRY";
private Button submitButton;
private EditText editTextEntry;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_entry_text);
submitButton = (Button) findViewById(R.id.submitButton);
submitButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(EntryTextActivity.this, FullTimeline.class);
editTextEntry = (EditText) findViewById(R.id.editTextEntry);
String entryText = editTextEntry.getText().toString();
intent.putExtra(EXTRA_ENTRY, entryText);
startActivity(intent);//this starts a new instance of the MainActivity ?
}
});
}
}
I hope that helps understanding my problem. I could not find any working solutions on here.
You need to use startActivityForResult()
Here's a Nishant's simple explanation on how to work with it:
- https://stackoverflow.com/a/10407371/5648172
Basically, you want to start your EntryTextActivity with startActivityForResult() instead of startActivity().
I haven't tested it but i think that in the case you don't want to use a database, you can set your MainActivity as "singleInstance" in your manifest :
<activity android:name="{your }" android:launchMode= "singleInstance" />
Then you can add the new text in an your MainActivity's onResume
#Override
public void onResume() {
super.onResume();
Intent intent = getIntent();
String entry = intent.getStringExtra(EntryTextActivity.EXTRA_ENTRY);
linLayout.addView(createNewTextView(entry), 0);
}
I have a working splash
public class MainSplash extends AppCompatActivity {
private final int SPLASH_DISPLAY_LENGTH = 1000;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
Intent mainIntent = new Intent(MainSplash.this, MainMenu.class);
MainSplash.this.startActivity(mainIntent);
MainSplash.this.finish();
}
}, SPLASH_DISPLAY_LENGTH);
}
}
and I want to add few buttons like share, more etc
and wen I do that by removing handler and SPLASH_DISPLAY_LENGTH, and adding buttons to xml and handling clicks on them just like any other other activity and set a Start button to start MainActivity, the MainActivity starts in few seconds ( after 1, 2 seconds of loading).
But I want to handle the loading time of MainActivity in SplashActivity,
How can I do that?
Here is the example SplashActivity after adding buttons
public class MainSplash extends AppCompatActivity implements View.OnClickListener {
//private final int SPLASH_DISPLAY_LENGTH = 1000;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
Button btn1 = (Button) findViewById(R.id.button);
Button btn2 = (Button) findViewById(R.id.button2);
Button btn3 = (Button) findViewById(R.id.button3);
Button btn4 = (Button) findViewById(R.id.button4);
btn1.setOnClickListener(this);
btn2.setOnClickListener(this);
btn3.setOnClickListener(this);
btn4.setOnClickListener(this);
}
#Override
public void onClick(View v) {
switch (v.getId()){
case R.id.button4:
Intent mainIntent = new Intent(this,MainMenu.class);
startActivity(mainIntent);
finish();
break;
}
}
}
you can try adding delay in loading time of MainActivity by putting a handler inside onCLick
well if anyone else having the same issue like when we load Mainactivity from SplashActivity we get a black screen for a second or two, which is the loading time for the MainActivity and it really doesn't look good.
To make it go away i just added a Fragment for splash Screen instead of SplashActivity and another fragment for Main interface.
This way Activity loads when app start so and transaction from splash fragment to main fragment takes no time :)
Hope it helps someone.
I have multiple setOnClickListener and what I want to do is, to make it more simple.
Here's my java
#Override
public void onActivityCreated(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);
// get the button view
ImageView foodImg = (ImageView) getView().findViewById(R.id.iconFoods);
ImageView barsImg = (ImageView) getView().findViewById(R.id.iconBars);
ImageView roomsImg = (ImageView) getView().findViewById(R.id.iconRooms);
ImageView wellnessImg = (ImageView) getView().findViewById(R.id.iconWellness);
ImageView beachesImg = (ImageView) getView().findViewById(R.id.iconBeaches);
ImageView kidsImg = (ImageView) getView().findViewById(R.id.iconKids);
ImageView attractionsImg = (ImageView) getView().findViewById(R.id.iconAttractions);
ImageView shopsImg = (ImageView) getView().findViewById(R.id.iconShops);
ImageView museumsUmg = (ImageView) getView().findViewById(R.id.iconMuseum);
// set a onclick listener for when the food button gets clicked
foodImg.setOnClickListener(new OnClickListener() {
// Start new list activity
public void onClick(View v) {
Intent mainIntent = new Intent(getActivity(),
Food.class);
startActivity(mainIntent);
}
});
// set a onclick listener for when the bars button gets clicked
barsImg.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent mainIntent = new Intent(getActivity(),
Bars.class);
startActivity(mainIntent);
}
});
As you can see I create one setOnClickListener for each ImageView.
My Question is, how to use if statement for multiple setOnClickListener?
So when I click ImageView Foods Icon I open the foods activity.
Thanks before :D
Create a common onClickListener
private OnClickListener onClickListener = new OnClickListener() {
#Override
public void onClick(final View v) {
switch(v.getId()){
case R.id.button1:
((TextView) getView().findViewById(R.id.textView1)).setText("Button 1 pushed");
break;
case R.id.button2:
((TextView) getView().findViewById(R.id.textView1)).setText("Button 2 pushed");
break;
case R.id.button3:
((TextView) getView().findViewById(R.id.textView1)).setText("Button 3 pushed");
break;
}
}
};
Then set each View to use that click listener, i.e:
Button btn1 = (Button) getView().findViewById(R.id.button1);
Button btn2 = (Button) getView().findViewById(R.id.button2);
Button btn3 = (Button) getView().findViewById(R.id.button3);
btn1.setOnClickListener(onClickListener);
btn2.setOnClickListener(onClickListener);
btn3.setOnClickListener(onClickListener);
Hope it helps.
Used Switch Case for that in onClick(...) like
public void onClick(View v) {
switch (v.getId()) {
case R.id.iconFoods:
//do your Intent
break;
case R.id.iconBars:
//do your Intent
break;
case R.id.iconRooms:
break;
case R.id.iconWellness:
//do your Intent
break;
case R.id.iconBeaches:
//do your Intent
break;
and so on....
}
}
But for this your Activity or Fragment must extends onClickListener Interface
public class MyFragment extends Fragment implements OnClickListener {
and you'll set this Listener to your ImageView like:
foodImg.setonClickListner(this);
roomsImg.setonClickListner(this);
...and so on
Assign the same listener to all of the views.
foodImg.setOnClickListener(this);
Then implement the listener interface in your Fragment.
public class MyFragment extends Fragment implements OnClickListener {
and add the onClick() method to your Fragment:
public void onClick(View v) {
switch (v.getId())
{
case R.id.iconBars:
// handle the click
break;
case R.id.iconMuseum:
// handle the click
break;
// add all the other cases here
}
}
Implement OnClickListener to your activity , just like this:-
public class MyActivity extends Activity implements OnClickListener
After Implementing it will ask you to add umimplemented method, then on that method you can use switch case just like following
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.foodImg :
break;
default:
break;
}
}
And dont forget to assign the same listener to all of the views.
For that you can make an method and call it on the OnCreate method
private void registrerListeners() {
foodImg.setOnClickListener(this);
//rly add rest oncliclisterner of your image
}