My app supports 3 languages (english, french and arabic). I have already translated all ressources (values string and drawables files) and it work perfect according to the language set in the user device.
The app consists principally of two activities : mainActivity and Game Activity. mainActivity contain a button (Play) which leads to the GameActivity according to the device language. For example if language is english it will launch Game Activity, if f language is french it will launch GameFr Activity, and if language is arabic it will launch GameAr Activity.
Play.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (Locale.getDefault().getLanguage().equals("ar")){
Intent intgame=new Intent(MainActivity.this,GameAr.class);
startActivity(intgame);
}
else {
if (Locale.getDefault().getLanguage().equals("fr")){
Intent intgame=new Intent(MainActivity.this,GameFr.class);
startActivity(intgame);
}
else {
Intent intgame=new Intent(MainActivity.this,Game.class);
startActivity(intgame);
}
}
}
});
However, I would like to add 3 ImageView (flags) in the MainActivity through which users can change the language of the application, for this I added the following:
en = (ImageView) findViewById(R.id.en);
fr = (ImageView) findViewById(R.id.fr);
ar = (ImageView) findViewById(R.id.ar);
en.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
setLocale("en");
Intent uo = new Intent(MainActivity.this,Game.class);
startActivity(uo);
}
});
fr.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
setLocale("fr");
Intent uo = new Intent(MainActivity.this,GameFr.class);
startActivity(uo);
}
});
ar.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
setLocale("ar");
Intent uo = new Intent(MainActivity.this,GameAr.class);
startActivity(uo);
}
});
Nevertheless, when a user with a device which the language is set to english click the French flag, it will get successfully the french activity. However, if he comes back to the previous page and click on the button (Play), the page displayed is the one corresponding to the activity in English but with the resources (string values and Drawables) French.
This is because the following function:
if (Locale.getDefault().getLanguage().equals("ar"))
always test the language of the device, not the language that the user chose in the app.
is there a function that can give me the language chosen by the SetLocale function? or I should use a variable transfer between activities?
how can I fix this, do you have better suggestions?
use this
Locale.setDefault("Your Locale");
Related
I am developing an App using Eclipse. I have a page where it has different check boxes. I want the user if checking lest say options A and B and D then Activity 7 will open and if the user checks options A and C then Activity 5 will open.
Thank you
You can do so like this:
Get the IDs of the checkboxes. Then add an OnClickListener to the checkboxes like so:
OnClickListener checkBoxListener;
checkBoxListener = new OnClickListener()
{
#Override
public void onClick(View arg0) {
if (checkboxA.isChecked() && checkboxC.isChecked())
{
Intent i = new Intent(this,Activity5.class)
startActivity(i);
}
else if (checkboxA.isChecked() && checkboxB.isChecked() && checkboxD.isChecked())
{
Intent i = new Intent(this,Activity7.class)
startActivity(i);
}
}
};
checkboxA.setOnClickListener(checkBoxListener);
checkboxB.setOnClickListener(checkBoxListener);
checkboxC.setOnClickListener(checkBoxListener);
checkboxD.setOnClickListener(checkBoxListener);
Please give this a try.
I'm currently working on my 2nd program and stuck with phone calling, sending SMS, emailing, and showing location using Google Map. I'm using ImageButtons for all of these. User doesn't need to type any numbers or addresses (not using EditText to get user input). However, it doesn't work.
For example, when user click on Phone icon, it will make a call. However, I still need a user input (the actual message, not the destination number or email address) for sending SMS and email. I can do it if I use EditText. However, without using EditText, I cannot do it. How do I do this? I've added users permission in manifest file.
PhoneActivity .java
public class PhoneActivity extends Activity {
ImageButton btnphonecall;
String phoneNumber = "091111111";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_phone);
btnphonecall=(ImageButton)findViewById(R.id.imageButton1);
btnphonecall.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent i = new Intent(Intent.ACTION_CALL);
i.setData(Uri.parse("091111111"));
PhoneActivity.this.startActivity(i);
//I've tried startActivity(i) along but still doesn't work
}
});
}
Use this:
Uri number = Uri.parse("tel:"+091111111 );
Intent dial = new Intent(Intent.ACTION_CALL,
number);
dial.setPackage("com.android.phone");
PhoneActivity.this.startActivity(dial);
Try this:
Intent callIntent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:"+phoneNumber));
startActivity(callIntent);
As we can send data from one Activity to another within the app and also from one app to another app by using Intent
I'm able to send data from my one app to another by using following code
just consider there are two apps APP1 and APP2 and I'm sending data from APP1 to APP2 and vice verse.
In APP1 package name: (com.sush.myApp)
public class HomeActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
// to receive data from com.my.demo package and display it to TextView
TextView mText = (TextView)findViewById(R.id.textView1);
String name=getIntent().getStringExtra("User_Message");
mText.setText(name);
// to send data from com.sush.myApp package to com.my.demo package
Button btn1 =(Button)findViewById(R.id.button1);
btn1.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
try
{
Intent i = new Intent(Intent.ACTION_MAIN);
PackageManager manager = getPackageManager();
i = manager.getLaunchIntentForPackage("com.my.demo");
i.putExtra("User_ID", "sush19");
i.addCategory(Intent.CATEGORY_LAUNCHER);
startActivity(i);
}catch(Exception e)
{
e.printStackTrace();
Toast.makeText(v.getContext(), "App Not Found", Toast.LENGTH_LONG).show();
}
}
});
}
and from APP2 package name: (com.my.demo)
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// to receive data from com.sush.myApp package and display it to TextView
final TextView txt1 = (TextView)findViewById(R.id.Text1);
String name=getIntent().getStringExtra("User_ID");
txt1.setText(name);
// to send data from com.my.demo package to com.sush.myApp package
Button btnSending = (Button)findViewById(R.id.btnSend);
final EditText myMessage = (EditText)findViewById(R.id.txtMessage);
btnSending.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
if (myMessage.getText().toString().equals(""))
{
Toast.makeText(v.getContext(), "Enter your message", Toast.LENGTH_SHORT).show();
}
else
{
try
{
Intent i = new Intent(Intent.ACTION_MAIN);
PackageManager manager = getPackageManager();
i = manager.getLaunchIntentForPackage("com.sush.myApp");
i.putExtra("User_Message", myMessage.getText().toString());
i.addCategory(Intent.CATEGORY_LAUNCHER);
startActivity(i);
}catch(Exception e)
{
e.printStackTrace();
Toast.makeText(v.getContext(), "Error: "+e, Toast.LENGTH_LONG).show();
}
}
}
});
}
And its working perfect..
Now my problem is I want to do same operation i.e sending data from one app to another but my first app is in Java created using Eclipse and my second app is in ActionScript created using AIR for Android in Adobe Flash Professional CS6
Is there a way to use Intent and PackageManager in Actionscript so that I can send the data easily from AIR app to Android App, if yes then can anyone show me a example
Or else can anyone show me an example on how to send data from Android App to AIR for Android App and vice verse..
Thank you...
There's 2 methods I can think of for this.
The first is to use the Java code you have written (or similar), package it as a Native Extension, and build that into your app.
Another alternative is to use a URI scheme and read the data from the InvokeEvent. This SO question covers that method already.
I am building a login system in android and i need to accomplish the following:
save userdata on the local storage
when a user is created and i press on the button it needs to dynamically create a radiobutton with the user name and short name in it.
I have already managed to accomplish these things, but the only problem i have now is that i want to to have 2 classes, one where you register your account and the other where the radiobuttons with the user data is displayed. So my question: how can i program the calss in such a way that when i press on the register button that it creates the radiobuttons in a different class.
beneath is the code of the classes:
enter code here
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.Save:
Intent I = new Intent();
I.putExtra(
I.setClass(this, ShowUsers.class);
startActivity(I);
break;
}
}
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
// Start other Activity
switch (v.getId()) {
case R.id.create:
Intent intent = new Intent();
intent.setClass(Main.this, Register.class);
startActivity(intent);
break;
}
Bundle extras = getIntent().getExtras();
String userName;
if (extras != null) {
userName = extras.getString("editTextName,editTextPassword,editTextshortname");
// and get whatever type user account id is
}
}
}
when i click the save button it needs to create the radio button in the main class, now the question is how can i accomplish this?
If the second class is an Activity, then one solution is to send it an Intent with the data it needs to create the radio buttons. You do this by creating an Intent object, calling any of its setExtra() methods, then calling startActivity() with the Intent. Check out the Android API docs for more details.
I have a simple app that I'm putting together for my company. I have 4 buttons that I've created but can't seem to get them to link correctly. One button should open our mobile site, another button to call us, another button to map to us, and the final button linked to our "News" site. Any help would be greatly appreciated!
On your buttons, you should set OnClickListener, and to do some required actions you could see the example below:
To Open a Map with Certain Location
mapButton.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse("geo:0,0?q=" + your-location-geo-address));
startActivity(i);
}
});
To call certain number
callButton.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
Intent i = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + telephone-number));
startActivity(i);
}
});
To open a website
linkButton.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(website-address));
startActivity(i);
}
});
Change "location-address", "telephone-number", and "website-address" with your own String value.
I hope this helps.
anmustangs answer is very good, but one thing I would like to add for the button you are making for a link to your site, where anmustangs wrote (website-address) instead of just typing in a site, it needs to put formatted correctly. For example, you can use "http://www.google.com" and yes you do need to use the quotation marks I put in there. I know I am years late to this thread but who knows who my post may help.