Hi I currently made a game and just wish to add a simple menu for it, the are two buttons on the main menu was says English and the Other says French I have managed to get the English button working which takes me to my EnglishVersion.class but I cant seem to get my French button to work. can any one help please.
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.view.View;
import android.view.View.OnClickListener;
public class MainMenu extends Activity {
Button English;
Button French;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_menu);
addListenerOnButton();
}
public void addListenerOnButton(){
final Context context = this;
English = (Button) findViewById(R.id.engbtn);
French = (Button) findViewById(R.id.frenbtn);
English.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View arg0) {
Intent intent = new Intent(context, EnglishVersion.class);
startActivity(intent);
}
});
}
I have tried to do this and getting and im getting an error
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.view.View;
import android.view.View.OnClickListener;
public class MainMenu extends Activity {
Button English;
Button French;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_menu);
English.setOnClickListener(mButtonClickListener);
French.setOnClickListener(mButtonClickListener);
}
private View.OnClickListener mButtonClickListener = new View.OnClickListener() {
#Override
public void onClick(View view) {
if (view.getId()==R.id.engbtn) {
Intent intent = new Intent(context, EnglishVersion.class);
startActivity(intent);
} else if (view.getId()==R.id.frenbtn) {
Intent intent = new Intent(context, FrenchVersion.class);
startActivity(intent);
}
}
}
}
add this:
French.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(context, FrenchVersion.class);
startActivity(intent);
}
});
By the way, it's not a good way to do the internationalization.
Try to read little about it here
Supporting Different Languages
You can just create various files strings-XX.XML in your res/ directory, where XX is given language symbol like FR or EN. It will even work with your default one. So if phone user has French defined as a language, your application will take it as its.
Either use the onClick property in XML like this (for both buttons);
<Button
android:id="#+id/engbtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onClick"/>
<Button
android:id="#+id/frenbtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onClick"/>
then populate the onClick method in the activity;
public void onClick(View view) {
if (view.getId()==R.id.engbtn) {
//do something
} else if (view.getId()==R.id.frenbtn) {
//do something else
}
}
OR set the onClickListeners for both button like this;
English.setOnClickListener(mButtonClickListener);
French.setOnClickListener(mButtonClickListener);
and then define the shared click listener;
private View.OnClickListener mButtonClickListener = new View.OnClickListener() {
#Override
public void onClick(View view) {
if (view.getId()==R.id.engbtn) {
//do something
} else if (view.getId()==R.id.frenbtn) {
//do something else
}
}
}
Hope this helps!
Related
I didn't get any response from my Android App through setOnClickListener, and I didn't get any output on Clicking TextView. What should I do now?
My code is the following:
XML:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Login"
android:paddingLeft="10dp"
android:textStyle="bold"
android:textColor="#3ba8e7"
android:id="#+id/login_reg"
/>
Java:
package com.example.rehman.userloginregister;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class user_register extends AppCompatActivity {
TextView register_reg;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user_register);
register_reg = (TextView) findViewById(R.id.login_reg);
register_reg.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
setContentView(R.layout.activity_main);
}
});
}
}
If you want to start another activity after clicking on your textview (register_reg), you have to create an intent.
Assumming that your layout 'R.layout.activity_main' is associated to the activity class 'MainActivity.java':
public void onClick(View view) {
Intent myIntent = new Intent(this, MainActivity.class);
startActivity(myIntent);
}
Firstable create a new Activity to your activity_layout and than on your click listener write this code
Intent intentions = new Intent(UserRegister.this,YourSecondActivity.class);
startActivity(intent);
And please a class start with an uppercase letter
I have managed to successfully start two activities using the ImageButton along with .setOnClickListener tied to it, i have also included layouts with different ImageButtons. Each button launches an activity. I have created the activities. I have also managed to remover crash bugs, lint errors, have the latest Android SDK. However now the buttons stop working even though you hear the click. Neither the activity launches on the first imageButton nor the second one.
This happens the moment i put multiple ImageButtons in. It works with 1 button. I suspect the (this) command in the class is confused as to what to call. My intent call method to start new activities are streamlined and basic for quick, uncomplicated access.
Can someone please help me as to why the multiple setOnclickListener cannot be tied to the relevant imageButtons please?
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.content.Intent;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
public class MainActivity extends ActionBarActivity implements OnClickListener {
ImageButton imageButton1;
ImageButton imageButton2;
ImageButton imageButton3;
ImageButton imageButton4;
Intent intent;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.reusable_layout);
imageButton1=(ImageButton)findViewById(R.id.imageButton1);
imageButton2=(ImageButton)findViewById(R.id.imageButton2);
imageButton3=(ImageButton)findViewById(R.id.imageButton3);
imageButton4=(ImageButton)findViewById(R.id.imageButton4);
imageButton1.setOnClickListener(this);
imageButton2.setOnClickListener(this);
imageButton3.setOnClickListener(this);
imageButton4.setOnClickListener(this);
}
public void onClick1(View view) {
Intent intent =
new Intent(this, OtherActivity.class);
startActivity(intent);
}
public void onClick2(View view) {
Intent intent =
new Intent(this, OtherActivity2.class);
startActivity(intent);
}
If you notice I have only coded 2 of the four buttons (so once this code works in theory the others should).
This is what OtherActivity from imageButton1 calls.
package com.example.startanotheractivity;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
public class OtherActivity extends Activity
implements OnClickListener {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.other_layout);
Intent intent = getIntent();
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
}
and this is the activity imageButton2 calls
package com.example.startanotheractivity;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
public class OtherActivity2 extends Activity
implements OnClickListener {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.other_layout_2);
Intent intent = getIntent();
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
}
Here is my layout.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal"
android:background="#FFFFFF" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:orientation="vertical" >
<ImageButton
android:id="#+id/imageButton1"
android:layout_width="566dp"
android:layout_height="456dp"
android:background="#null"
android:src="#drawable/gatanga1"
android:onClick="onClick1" />
<ImageButton
android:id="#+id/imageButton2"
android:layout_width="540dp"
android:layout_height="189dp"
android:background="#null"
android:src="#drawable/gatanga2"
android:onClick="onClick2" />
<ImageButton
android:id="#+id/imageButton3"
android:layout_width="540dp"
android:layout_height="189dp"
android:background="#null"
android:src="#drawable/gatanga3"
android:onClick="onClick3" />
<ImageButton
android:id="#+id/imageButton4"
android:layout_width="540dp"
android:layout_height="189dp"
android:background="#null"
android:src="#drawable/gatanga4"
android:onClick="onClick4" />
</LinearLayout>
Thank you
Many many thanks.
use a switch statement in onClick method
#Override
public void onClick(View v) {
switch(v.getId()){
case R.id.imageButton1: /** Start a new Activity OtherActivity.java */
Intent intent = new Intent(MainActivity.this, MyCards.class);
this.startActivity(intent);
break;
case R.id.imageButton2: /** Start a new Activity OtherActivity2.java */
Intent intent2 = new Intent(MainActivity.this, OtherActivity2.class);
this.startActivity(intent2);
break;
}
The onClick method passes the clicked view as an variable, you can use a switch statement to determine which view was clicked and then you can perform your operations..
public void onClick(View v) {
switch(v.getId()){
case R.id.imageButton1:
Intent intent = new Intent(MainActivity.this, OtherActivity.class);
startActivity(intent);
break;
case R.id.imageButton2:
Intent intent = new Intent(MainActivity.this, OtherActivity2.class);
startActivity(intent2);
break;
}
}
Scholars, Educated Aristocrats and of course Plumbers.
Thank you for replying. It works now by the code i did, although i will try the above solutions. I have experimented with switches and i do like them. I want to make sure memory management and file sizes are kept to a minimum as the end app will have about 40 activities.
However the way i did it was i removed all the other ImageButtons declarations in MainActivity except:
public class MainActivity extends ActionBarActivity implements OnClickListener {
ImageButton imageButton1;
Intent intent;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.reusable_layout);
imageButton1=(ImageButton)findViewById(R.id.imageButton1);
}
The onClick methods then were left as onClick1,onClick2. Both activities worked. I added onClick3 and onClick4 like the below:
public void onClick1(View view) {
Intent intent =
new Intent(this, OtherActivity.class);
startActivity(intent);
}
public void onClick2(View view) {
Intent intent =
new Intent(this, OtherActivity2.class);
startActivity(intent);
}
public void onClick3(View view) {
Intent intent =
new Intent(this, OtherActivity3.class);
startActivity(intent);
}
public void onClick4(View view) {
Intent intent =
new Intent(this, OtherActivity4.class);
startActivity(intent);
}
I think what seemed to happen then is the code wasnt confused or stuck in a loop (it didnt crash on my original posting). But instead now it goes straight to layout, gets the "onClick1" for button 1 and "onClick2" and matches them with the public void onClick(X) in MainActivity, because i have just declared i am using the ImageButton and an Intent, right after onClickListener, it works.
Note if i move around this code it breaks or will give me the same error. Regardless, i am happy it works but am intrigued as to why i did not have to declare all my buttons. Maybe it is just the logic in Java Code working.
I will definately try the switches and amendments later experimenting.
With best wishes.
in the MainActivity you should correctly implement OnClickListener interface:
#Override
public void onClick(View v) {
switch(v.getId()) {
case R.id.imageButton1:
Intent intent =
new Intent(this, OtherActivity.class);
startActivity(intent);
break;
case R.id.imageButton2:
Intent intent =
new Intent(this, OtherActivity2.class);
startActivity(intent);
break;
}
}
Hi i'm working on a android launcher for education and I need it to be able to when the user clicks the school tools button it launches the school tools app that is installed on the device
Here's the code
package com.d4a.stzh;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.content.Intent;
import com.actionbarsherlock.app.SherlockFragment;
public class FragmentTab1 extends SherlockFragment {
private Button appbtn;
private Button webbtn;
private Button toolsbttn;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Get the view from fragmenttab1.xml
View view = inflater.inflate(R.layout.fragmenttab1, container, false);
//Get the button from layout
appbtn = (Button) view.findViewById(R.id.app);
webbtn = (Button) view.findViewById(R.id.web);
toolsbttn = (Button) view.findViewById(R.id.tools);
//show all apps installed on the device
appbtn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(FragmentTab1.this.getActivity(), MyLauncherActivity.class);
startActivity(intent);
}
});
//luanches google on the default web browser
webbtn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
String url = "http://www.google.com";
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);
}
});
//tools button i know ths code is wrong!I need help here!
toolsbttn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(FragmentTab1.this.getActivity(), MyLauncherActivity.class);
startActivity(intent);
}
});
return view;
}
#Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
setUserVisibleHint(true);
}
}
I am still new to android coding so please don't judge me
Any help would be amazing
Thanks way in advance
Regards
Rapsong11
This code snippet should do exactly what you are trying to achieve
Intent i;
PackageManager manager = getPackageManager();
try {
i = manager.getLaunchIntentForPackage("com.example.schoolToolApp");
if (i == null)
throw new PackageManager.NameNotFoundException();
i.addCategory(Intent.CATEGORY_LAUNCHER);
startActivity(i);
} catch (PackageManager.NameNotFoundException e) {
}
It will just launch another app by its package name
source - Open another application from your own (intent)
I'm trying to start a new activity from a non-activity class.
From the main menu:
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class Menu extends Activity {
Button start, options;
GameLoop Game = new GameLoop();
#Override
public void onCreate(Bundle mainStart) {
super.onCreate(mainStart);
setContentView(R.layout.menu);
start = (Button) findViewById(R.id.bStart);
options = (Button) findViewById(R.id.bOptions);
start.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent openStart = new Intent(Menu.this, Game.class);
startActivity(openStart);
}
});
options.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Context mContext = null; //Error called for mContext to be initialized so just tried setting to null. This is most likely the error cause it would make more sense for it to be equal to "getContext()" or something like that
Game.Start(mContext);//Here
}
});
}
}
I'm trying to open an activity from the Game.Start() method.
import android.content.Context;
import android.content.Intent;
public class GameLoop extends Menu{
boolean hello = false;
public void Start(Context sContext){
Intent openOptions = new Intent(sContext, Options.class);
startActivity(openOptions);
}
}
I'm not sure if using context would be the right way of going about this but I figured it was worth a try. Im entirely new to java and android so I'm pretty much lost on where to go next. Any help in what direction to take would be throughly appreciated.
Activity extends Context, so you can just use this when inside Activity.
Game.Start(Menu.this);
I use Menu.this because you are inside inner anonymous class (View.OnClickListener) where this refers to this inner class.
Do you added the new activities to the androidmanifest.xml?
I try to learn JAVA and I try to write an app for Android. My Code is simple and often I've seen code like this. But when I push the second time a button, the message does not return. The first time it works. What is my error?
package com.test.helloworld;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class HelloWorldApp extends Activity {
private Button closeButton;
private Button buttonAnswer1;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
buttonAnswer1 = (Button)findViewById(R.id.button1);
closeButton = (Button)findViewById(R.id.buttonEnde);
buttonAnswer1.setFocusable(false);
closeButton.setFocusable(false);
buttonAnswer1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
setContentView(R.layout.main);
showToastMessage("1");
}
});
closeButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
setContentView(R.layout.main);
showToastMessage("2");
}
});
}
private void showToastMessage(String msg){
Toast toast = Toast.makeText(this, msg, Toast.LENGTH_SHORT);
toast.show();
}
}
Don't call the setContentView method inside the click listener:
buttonAnswer1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
showToastMessage("1");
}
});
In your onClick functions, you are replacing the entire content view, which will replace the existing button objects with new instances. These new instances no longer have any OnClickListeners.
There is no reason to replace the content view in this case, so the solution is to eliminate those calls from the onClick functions. But if for some reason you needed to replace the content view, then you would need to go through the entire process of finding the new buttons and calling setOnClickListener for each.