I want to pass a from my method to the button clicked method.
Method that generates a:
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
String str = arg0.getItemAtPosition(arg2).toString();
double a = 0;
switch(arg2){
case 0:
a = 4;
break;
case 1:
a = 3.75;
break;
case 2:
a = 3.50;
break;
case 3:
a = 3.25;
break;
case 4:
a = 3;
break;
case 5:
a = 2.75;
break;
case 6:
a = 2.5;
break;
case 7:
a = 2.25;
break;
}
Button clicked method
public void button(View v){
}
Make a a field of your activity instead of an instance variable like so:
public class MyActivity extends Activity {
double a = 0;
...
}
Related
I have developed an App in Android which translates text (from EditText), Application uses Firebase API. it translates successfully when I select Source and Target Language in hard code. (FirebaseTranslatorOption's setSource and setTarget language settings even integer code i.e. EN (11), FR(17) etc. but it is not taking any dynamic input from Spinner object's setOnItemSelectedListener.
'''
//Spinners itemselection events
input_lang_spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(parent.getContext(),
"OnItemSelectedListener : " + parent.getItemAtPosition(position),
Toast.LENGTH_SHORT).show();
selected_source_lang = parent.getItemAtPosition(position).toString();
switch (position){
case 0:
Toast.makeText(getApplicationContext(),"Plz select a language",Toast.LENGTH_LONG).show();
case 1:
SourceLangCode=11;
break;
case 2:
SourceLangCode=17;
break;
case 3:
SourceLangCode=56;
break;
case 4:
SourceLangCode=48;
break;
case 5:
SourceLangCode=9;
break;
}
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
Toast.makeText(getApplicationContext(),"Plz select a language",Toast.LENGTH_LONG).show();
}
});
//end of input language selection
output_lang_spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(parent.getContext(),
"OnItemSelectedListener : " + parent.getItemAtPosition(position),
Toast.LENGTH_SHORT).show();
selected_target_lang = parent.getItemAtPosition(position).toString();
switch (position){
case 0:
Toast.makeText(getApplicationContext(),"Plz select a language",Toast.LENGTH_LONG).show();
case 1:
targetLangCode=11;
break;
case 2:
targetLangCode=17;
break;
case 3:
targetLangCode=56;
break;
case 4:
targetLangCode=48;
break;
case 5:
targetLangCode=9;
break;
}
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
Toast.makeText(getApplicationContext(),"Plz select a language",Toast.LENGTH_LONG).show();
}
});//end of output language selection
//firebaseTranslatorOptions object
FirebaseTranslatorOptions options =
new FirebaseTranslatorOptions.Builder()
.setSourceLanguage(SourceLangCode) //this is not taken as I select item
.setTargetLanguage(targetLangCode) //item selection is working
.build();
final FirebaseTranslator languageTranslator =
FirebaseNaturalLanguage.getInstance().getTranslator(options);
'''
I have three functions in my Main class, onCreate,onClick and click, I have two options, declare TextViews and some other views at the beginning of the project as global variables, which means they will stay in the entire app lifetime, or get them separately in each function(will cause the computer to work a bit more but the variables will not stay in memory the whole time). To describe the question further:
OPTION 1:
public static final int L = 6;
TextView[] textViews = new TextView[L];
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Random rand = new Random();
int[] digits = {rand.nextInt(10), rand.nextInt(10), rand.nextInt(10), rand.nextInt(10), rand.nextInt(10), rand.nextInt(10)};
for (int i = 0; i < L; i++) {
textViews[i] = (TextView) findViewById(getResources().getIdentifier("num" + String.valueOf(i), "id", getPackageName()));
textViews[i].setOnClickListener(this);
textViews[i].setText(String.valueOf(digits[i]));
}
}
#Override
public void onClick(View view) {
int id = view.getId();
switch (id) {
case R.id.num0:
click(0);
break;
case R.id.num1:
click(1);
break;
case R.id.num2:
click(2);
break;
case R.id.num3:
click(3);
break;
case R.id.num4:
click(4);
break;
case R.id.num5:
click(5);
break;
}
}
public void click(int clicked) {
textViews[clicked].setText("Clicked");
}
OPTION 2: (Note to shorten your time - the change is in the last function).
public static final int L = 6;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Random rand = new Random();
int[] digits = {rand.nextInt(10), rand.nextInt(10), rand.nextInt(10), rand.nextInt(10), rand.nextInt(10), rand.nextInt(10)};
for (int i = 0; i < L; i++) {
textViews[i] = (TextView) findViewById(getResources().getIdentifier("num" + String.valueOf(i), "id", getPackageName()));
textViews[i].setOnClickListener(this);
textViews[i].setText(String.valueOf(digits[i]));
}
}
#Override
public void onClick(View view) {
int id = view.getId();
switch (id) {
case R.id.num0:
click(0);
break;
case R.id.num1:
click(1);
break;
case R.id.num2:
click(2);
break;
case R.id.num3:
click(3);
break;
case R.id.num4:
click(4);
break;
case R.id.num5:
click(5);
break;
}
}
public void click(int clicked) {
((TextView) findViewById(getResources().getIdentifier("num" + String.valueOf(clicked),"id",getPackageName())).setText("Clicked");
}
Take into consideration I have more than a TextView array, and in some function I have to get 5 views again, will it be better to declare them as global variables? I read somewhere most of the time using global variables is not good and it is against the whole idea of functions, but it seems more simple here.. Sorry about my English and thank you all.
First off, you do not need to worry about performance issues if the textViews object is declared globally. It is minuscule.
Now in your code,if the only reason to declare textViews array object is to reference it in click() method, then there is no need of declaring it globally.
Instead, you can pass the view object provided by the overridden onClick() method to your click() method.
#Override
public void onClick(View view) {
int id = view.getId();
switch (id) {
case R.id.num0:
click(view);
break;
case R.id.num1:
click(view);
break;
case R.id.num2:
click(view);
break;
case R.id.num3:
click(view);
break;
case R.id.num4:
click(view);
break;
case R.id.num5:
click(view);
break;
}
}
public void click(View viewClicked) {
((TextView))viewClicked.setText("Clicked")
}
In this application i am displaying 6 images randomly from an array containing drawables(named vmarray[12] ) and after 1 minute(using runnable postdelayed) switching is performed to another layout containing a single button with id BUTTON1 and setting the button background to one of the image from the earlier displayed images. I am using LAYOUTINFLATER for switching between layouts
On that button (Button1) onclick i want to do some work, but the PROBLEM IS when i implement onclicklistener and onclick method, the app crashes.
IF i remove the implements onclicklistener everything runs fine. The BUTTON1 is located perfectely through findviewbyid and its background correctly sets to an image. BUT Implementing onclicklister is crashing the app.
here is the code..
int[] vmarray= {R.drawable.vm1bulb, R.drawable.vm2chair, R.drawable.vm3comb,
R.drawable.vm4cycle,R.drawable.vm5dairy,R.drawable.vm6fan,R.drawable.vm7mobile,
R.drawable.vm8pen,R.drawable.vm9shoes,R.drawable.vm10toothbrush,
R.drawable.vm11bangle, R.drawable.vm12watch};
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
setContentView(R.layout.cacrvisualmem);
firstview =(LinearLayout)this.findViewById(R.id.firsthumlayout);
secondview = (LinearLayout) inflater.inflate(R.layout.cacrvisualmempart1, null);
for(int i=0;i<6;i++)
{
final int a = i;
button_var[a] = (Button)findViewById(idArray[a]);
}
// IN FIRST LAYOUT, THERE ARE 6 BUTTONS AND THEIR BACKGROUND IS RANDOMLY SET
// DRAWABLES FROM ARRAY "vmarray". the following code is for that
Random randomGenerator = new Random();
Random rand6 = new Random();
while (numbers.size() < 6) {
random1 = randomGenerator.nextInt(12);
random2 = rand6.nextInt(6);
if (!numbers.contains(random1) && (!numbers2.contains(random2)))
{
numbers.add(random1);
numbers2.add(random2);
b[random2].setBackgroundResource(vmarray[random1]);
count++;
}//if ends
}//while ends
Handler changeview = new Handler();
Runnable r1 = new Runnable(){
#Override
public void run() {
// TODO Auto-generated method stub
// NOW, here, after 1 minute, second view will be inflated and button1
// background is set to one the image from the array
setContentView(secondview);
b11=(Button)findViewById(R.id.button1);
queryrandvar=queryrand.nextInt(12);
switch(queryrandvar)
{
case 0:
b11.setBackgroundResource(vmarray[0]);
queryval=0;
break;
case 1:
b11.setBackgroundResource(vmarray[1]);
queryval=1;
break;
case 2:
b11.setBackgroundResource(vmarray[2]);
queryval=2;
break;
case 3:
b11.setBackgroundResource(vmarray[3]);
queryval=3;
break;
case 4:
b11.setBackgroundResource(vmarray[4]);
queryval=4;
break;
case 5:
b11.setBackgroundResource(vmarray[5]);
queryval= 5;
break;
case 6:
b11.setBackgroundResource(vmarray[6]);
queryval= 6;
break;
case 7:
b11.setBackgroundResource(vmarray[7]);
queryval= 7;
break;
case 8:
b11.setBackgroundResource(vmarray[8]);
queryval= 8;
break;
case 9:
b11.setBackgroundResource(vmarray[9]);
queryval= 9;
break;
case 10:
b11.setBackgroundResource(vmarray[10]);
queryval= 10;
break;
case 11:
b11.setBackgroundResource(vmarray[11]);
queryval= 11;
break;
}//switch ends
}//run ends
};//runnable ends
b11.setOnClickListener(null);
changeview.postDelayed(r1,10000);
} //on create method ends here
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId())
{
case R.id.button1:
{
b11.setBackgroundResource(vmarray[1]);
break;
}
}//switch ends
}//onclick ends
i got it.inside runnable after switch case i have put the following and button onclick is working.RUNING FINE WITH NO APP CRASH
b11.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
b11.setBackgroundResource(vmarray[0]); //SOME RANDOM IMAGE FOR CHECKING
}
});
I am working on a project based on Mapping the Remote control keys with android device.Here I can able to get the keycodes of Remote keys and return values for it based on the key codes.But the problem is, I don't know how to append the values inside the edit text.If I pressed the remote keys, it always returns numbers only.Guide me in a right way to make this work. Thanks in advance.
Sample Code:
I am having the below code in a Global Activity.And I extended all other Activities from this one.
public String switchedMethod(int key)
{
String letter = null;
switch(key)
{
case 9:
letter = "a";
break;
case 10:
letter = "b";
break;
case 11:
letter = "c";
break;
case 12:
letter = "d";
break;
case 13:
letter = "e";
break;
case 14:
letter = "f";
break;
case 15:
letter = "g";
break;
case 16:
letter = "h";
break;
}
Log.v("LETTER", ""+letter);
return letter;
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
Log.v("KEYCODE", ""+keyCode);
switchedMethod(keyCode);
return super.onKeyDown(keyCode, event);
}
//Code Related to Edit Text
public class SettingsSubElementCreator extends GlobalActivity
{
protected void handleSDEditText(int optionItemsCurrentItemIndex)
{
LinearLayout subLayout = new LinearLayout(this);
subLayout.setOrientation(LinearLayout.VERTICAL);
subLayout.removeAllViews();
createButtons();
// I NEED TO APPEND THE TEXT FOR THE BELOW EDITTEXT VIEW
final EditText SDEditText = new EditText(this);
SDEditText.setTypeface(boldTypeface);
subLayout.addView(SDEditText);
subLayout.addView(okcancelButtonLayout);
final ModifySettingsDialog dialog = new ModifySettingsDialog(this,
R.string.video_aspect_adjustment_sd,
optionItemsCurrentItemIndex, subLayout, false);
dialog.show();
okButton.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View view)
{
Toast.makeText(view.getContext(),"inside video aspect adjustment sd"+ SDEditText.getText(), Toast.LENGTH_LONG).show();
dialog.dismiss();
}
});
cancelButton.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
dialog.dismiss();
}
});
}
I am developing an android application in which i have 5 items in a listview,I have to show different view on each click at the item,,,How to do this,also i have to put an arrow on the left and right side of the each item,,,
Here is my listview java code
http://pastebin.com/LsunQU9z
and here is my listview xml
http://pastebin.com/1S4uD5mH
Thanks in advance
Tushar
lv1.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3)
{
switch(position)
case 0:
//write for first item
break;
case 1:
//second
break;
case 2:
//third
break;
case 3:
//for
break;
}
});
Well it all depends on what you are going to do or show in those 5 items.
One way would be to only use one class, let's call it ShowActivity and then pass extras to that activity to see what it should show. And then just fetch that in onCreate in the ShowActivity.
One way would just to show it in the activity that you are in.
Two different examples are below:
lv1.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3)
{
Intent intent = new Intent(this,ShowActivity.class);
intent.putExtra("ITEM_INDEX",position);
startActivity(intent);
}
});
With the one above use something similar in the onCreate # ShowActivity
int id = getIntent().getIntExtra("ITEM_INDEX", 0);
switch(id) {
case 0:
//Show item 0
break;
case 1:
//Show item 1
break;
case 2:
//Show item 2
break;
case 3:
//Show item 3
break;
case 4:
//show item 4
break;
}
This is the second example, use this in your list activity.
lv1.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3)
{
switch(position) {
case 0:
//Show item 0
break;
case 1:
//Show item 1
break;
case 2:
//Show item 2
break;
case 3:
//Show item 3
break;
case 4:
//show item 4
break;
}
}
});