Android image button problem - android

imgvw_back.setOnClickListener(this);
imgvw.setOnClickListener(this);
static id=10
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId())
{
case R.id.back:
Log.v("back",""+id--);
break;
case R.id.forward:
Log.v("next",""+id++);
break;
}
}
i am using this kind of concept but mostly fire R.id.back part ,what can i do plz give solution for this problem...

The id may not be what you think it is, you could do something like this.
imgvw_back.setOnClickListener(this);
imgvw.setOnClickListener(this);
static id=10
#Override
public void onClick(View v)
{
if(v == imgvw_back)
{
Log.v("back",""+id--);
}
else if(v == imgvw)
{
Log.v("next",""+id++);
}
}

Use inline onClickListeners for each button.
imgvw_back.setOnClickListener(new onClickListener(){
#Override
public void onClick(View v){
Log.v("back", "")
}
});

Is imgvw your forward button? Just wondering, cause your back button is imgvw_back, would assume forward would be named accordingly imgvw_forward.
You're probably missing missing to set the clickListener to the forward button as well.

Related

How to know which child view is clicked in a relativelayout

I have a relative layout to which child views are added and removed dynamically(any number can be added or removed)
My question is how to know which view was clicked so that i can add different onclicklisteners depending on the type of child views
Adding and retrieving the tag while click event can help. Here is the code.
For adding tags:
customView1.setTag(someTag);
customView1.setOnClickListener(myClickListner);
For retrieiving:
OnClickListener myClickListener = new onClickListener(){
#Override
public void onClick(View v) {
if(v.getTag() == someTag){
//do stuff
}else if(v.getTag() == otherTag){
//do something else
}
}
in your adapter class you need to write like this am sharing the sample code snippet
public static class ChatListItemsViewHolder extends
RecyclerView.ViewHolder {
public ChatListItemsViewHolder(View v) {
super(v);
// TODO Auto-generated constructor stub
v.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// write your code here
}
});
}
let me know if you need more clarity.

How do I remove numbers on phonepad if I press backspace button

I am trying to make a a phonepad in android and I need to know how I make my backspace button work when I press it, it remove my last number and so on.
b13.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
int len=text.length();
text.setText(text.getText().toString());
String res=text.substring(0,len-1);
}
});
b13 is button id for remove button
You're on the right track. Try this:
#Override
public void onClick(View view) {
String newText = myTextView.getText().toString()
.substring(0, myTextView.getText.toString.length()-1);
myTextView.setText(newText);
}
Take a look at KeyEvent, there is a constant called KEYCODE_BACK that should achieve this behavior

Android better way to do button clicks?

So right now I'm tidying up some code, and I have a lot of else/ifs for buttons and was wondering what is a good way to do it and make it neater?
So I have like 12 buttons, and each button plays a sound and changes colour when clicked. I have a method for this but I was wondering is there a good way to just detect the button instead of if/else?
public void onClick(View v) {
int id = v.getId();
changeToWhite();
if (id == R.id.a_button) {
currentButton(a, 81);
} else if (id == R.id.aSharp_button) {
currentButton(aSharp, 82);
} else if (id == R.id.b_button) {
currentButton(b, 83);
} else if (id == R.id.c_button) {
currentButton(c, 72);
}
Etc...
So is there a better way of having this? I know having a lot of else/ifs is bad so I wanted to try improve it.
Thanks!!
You can use "switch-case" instead.
>
public void onClick(View v) {
switch(v.getId())
{
case R.id.a_button:
changeToWhite();
break;
case R.id.aSharp_button:
currentButton(aSharp,82);
break;
.....
default:
break;
}
}
How about using a case statement instead?
public void onClick(View v) {
// Perform action on click
switch(v.getId()) {
case R.id.a_button:
currentButton(a, 81);
break;
case R.id.aSharp_button:
currentButton(aSharp, 82);
break;
/*
and the rest of the cases here.
*/
}
}
I assume you're using XML and setting the onClick property.
An easier/tidier way is to use anonymous inner classes.
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_foo);
findViewById(R.id.view_buttonone).setOnClickListener(new OnClickListener(){
public void onClick(View view){
// button one clicked
}
});
findViewById(R.id.view_buttontwo).setOnClickListener(new OnClickListener(){
public void onClick(View view){
// button two clicked
}
});
}
First of all there is virtually no penalty for using if/else nesting. There's no need to try this level of micromanaging your app. You will gain no benefits from it. Try to think more of optimizing this point in terms of readability.
Now, to answer your question, you could use a switch/case construct instead.
public void onClick(View v) {
switch (item.getItemId()) {
case R.id.aBar_item1:
//Item onClick logic
return true;
case R.id.aBar_item2:
//Item onClick logic
return true;
case R.id.aBar_item3:
//Item onClick logic
return true;
...
}
}

Android - Show new view

I am starting now to study Android. I came from iOS development. How can I do for show a new view by clicking a button in Android?
Thanks
You should create an on click listener for your button
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
// as asked by Pepi, what view are you planning to display??
}
});
Write the code inside depending on what has to be displayed
if you are going for a simple view not a activity. as you said in comment then you should use LayoutInflater on click or any event you want
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
View myNewViewToadd = inflater.inflate(R.layout.abc_view, null);
myParentView.removeAllViews();
myParentView.addView(myNewViewToadd, 0);
public void onClick(View v) {
// TODO Auto-generated method stub
if(v == btn_login)
{
Intent login=new Intent(this,Login.class);
Activity.this.startActivity(login);
}
else if(v == btn_register)
{
Intent register=new Intent(this,Register.class);
Activity.this.startActivity(register);
}
}
button.setOnClickListener(new View.onClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent("[package name].NEWVIEW"));
}
});
Then you need to create a new activity section in your manifest file with the name .[what's the name for your new class] and for the action you need to import your "[package name].NEWVIEW" and the .MAIN below this need to be rewrite to .DEFAULT
Hope it helped.

Android onClick method

I have two onclick method in android project
clr=(Button)findViewById(R.id.Button01);
clr.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
tv1.setText("CLR");
et1.setText("");
refrigerant = "";
pres = "";
temperature = "";
superheat_oda = 0;
sub_cool = 0;
}
});
And i have onther onClick method in which i have to call that method directly
prs=(Button)findViewById(R.id.Button02);
prs.setOnClickListener(new OnClickListener() {
----- I have to call that method---
}
});
Is there Any Solution for this?
You want to call the first onClick from the second? Just extract the contents of your first onClick in a separate method and call that method from each onClick.
Edit: As per st0le's comment, you can do what you want by calling clr.performClick(). (Didn't know that.) Still, extracting it into a separate method seems cleaner.
you can do something like this in the XML file
<Button
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:onClick="some_function" />
and put this function in the Java file
public void some_function(View view) {
// stuff...
}
and put the some_function in both "onClick"s
You should turn to use the simplest way that I always do as below:
#Override
public void onCreate(Bundle savedInstanceState) {
button1.setOnClickListener(onClickListener);
button2.setOnClickListener(onClickListener);
}
private OnClickListener onClickListener = new OnClickListener() {
#Override
public void onClick(final View v) {
switch(v.getId()){
case R.id.button1:
//DO something
break;
case R.id.button2:
//DO something
break;
}
}
};
I would recommend to use the same OnClickListener for both buttons if both buttons really have to do the same thing:
OnClickListener l=new OnClickListener() {
public void onClick(View v) {
tv1.setText("CLR");
et1.setText("");
refrigerant = "";
pres = "";
temperature = "";
superheat_oda = 0;
sub_cool = 0;
}
};
clr=(Button)findViewById(R.id.Button01);
clr.setOnClickListener(l);
prs=(Button)findViewById(R.id.Button02);
prs.setOnClickListener(l);
or if its not exactly the same you could access the listener method by l.onClick(null); manually..

Categories

Resources