What's wrong with this code of changing background of an imagebutton? - android

I am trying to change the image of an imagebutton using the following codes. Eclipse crashes but there is no error discovered… I've also used setBackgroundResource instead of setImageResource, but it didn't work. Nothing in Logcat, nither in errors. Please help me to find the right way.
I have two images for this imagebutton. - edit_on & edit_off
xml
Java
imageblockButton = (ImageButton)findViewById(R.id.buttonblock);
imageblockButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
imageblockButton.setImageResource(R.drawable.edit_on);
Intent intent1 = new Intent (context, ActivityBlockList.class);
startActivity(intent1);
v.setClickable(true);
}
});
my xml looks like…
<ImageButton android:id="#+id/buttonblock"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="0sp"
android:scaleType="centerCrop"
android:background="#drawable/edit_off" />

In your xml
<ImageButton android:id="#+id/buttonblock"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="0sp"
android:scaleType="centerCrop"
android:src="#drawable/edit_off" />
use src instead of background
and
ImageButton btn = (ImageButton)findViewById(R.id.buttonblock);
btn.setImageResource(R.drawable.newimage);

Try this
imageblockButton.setBackgroundDrawable(getResources().getDrawable(R.drawable.edit_on));

Related

Android Change widget button

i've created my first widget but i've a problem when i change the button image.
below it's a part of code of java file for widget:
#Override
public void onReceive(Context context, Intent intent)
{
if (mp == null){
mp = MediaPlayer.create(context.getApplicationContext(), R.raw.segno);
}
final String action = intent.getAction();
if (ACTION_WIDGET_RECEIVER.equals(action)) {
if (mp.isPlaying()){
mp.stop();
mp.release();
mp = MediaPlayer.create(context.getApplicationContext(), R.raw.segno);
}
else{
RemoteViews control = new RemoteViews(context.getPackageName(), R.layout.widget);
control.setImageViewResource(R.id.button, R.drawable.pausa);
ComponentName cn = new ComponentName(context, Widget.class);
AppWidgetManager.getInstance(context).updateAppWidget(cn, control);
mp.start();
}
super.onReceive(context, intent);
}
super.onReceive(context, intent);
}
I read in the log this:
02-17 23:46:57.042: W/AppWidgetHostView(946): updateAppWidget couldn't find any view, using error view
I think this is a problem but i don't understand how resolve it.
when appear this error the widget say: Problem loading widget.
Thank you all
This is a XML Layout for widget
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:layout_margin="4dp">
<Button
android:id="#+id/button"
android:paddingRight="30dp"
android:layout_width="50dp"
android:layout_height="50dp"
android:background="#drawable/vai" />
<ImageView
android:id="#+id/imageView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_toRightOf="#+id/button"
android:maxHeight="100dp"
android:maxWidth="100dp"
android:src="#drawable/segno" />
</RelativeLayout>
you are trying to change the image of a Button which doesn't have the properties of Button according to this setImageViewResource is equivilant to ImageView properties so Instead of having a button I suggest you change it either to ImageButton or to ImageView that is clickable.
so maybe your XML will look like this:
<ImageButton
android:id="#+id/button"
android:paddingRight="30dp"
android:layout_width="50dp"
android:layout_height="50dp"
android:background="#drawable/vai" />
Update
the reason is that android:background is different than android:src it doesn't resize the photo it actually make the image as size of the photo (setImageViewResource is same as src) .. so you have either to resize the photo from the beginning or try to add this attribute to your ImageButton
android:scaleType="fitXY"
but I am not sure it will work, haven't tried it.
hope this works for you. try it out and give me a feedback.

Android: Unable to Prevent buttons from increasing size and overlapping with other buttons

I am new to Android Programming, what I am trying to do in this Android Application is to create a xml page filled with buttons.
When I click the button, the button would change to light green color and when I click it again, it would change to light grey
The error: I am getting is when I click the button, it increases in size and overlaps with the other buttons, please help me out here, it is not user friendly in this case
attached below is the code:
lockerbooking.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Button
android:id="#+id/sisButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="16dp"
android:layout_marginTop="28dp"
android:text="#string/sis"
/>
<Button
android:id="#+id/solButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/soeButton"
android:layout_alignBottom="#+id/soeButton"
android:layout_alignParentRight="true"
android:text="#string/sol" />
<Button
android:id="#+id/soeButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/sisButton"
android:layout_alignBottom="#+id/sisButton"
android:layout_centerHorizontal="true"
android:text="#string/soe" />
</RelativeLayout>
Code:
makeBooking.java
public class makeBooking extends Activity {
Button sisButton;
Button solButton;
Button soeButton;
Button sobButton;
super.onCreate(savedInstanceState);
// Get the message from the intent
setContentView(R.layout.lockerbookpage);
Intent intent = getIntent();
// Initialize TextViews
sisButton = (Button) findViewById(R.id.sisButton);
solButton = (Button) findViewById(R.id.solButton);
soeButton = (Button) findViewById(R.id.soeButton);
sobButton = (Button) findViewById(R.id.sobButton);
}
public OnClickListener solButtonListener = new OnClickListener(){
boolean flag = true;
public void onClick(View arg0) {
// TODO Auto-generated method stub
if(flag){
solButton.setBackgroundColor(Color.GREEN);
}
else{
solButton.setBackgroundColor(Color.LTGRAY);
}
flag=!flag;
}
};
...The code goes on
Please help me out here, I am eager to learn
to avoid overlapping of buttons, use fixed width and height for buttons:
change this:
android:layout_width="wrap_content"
android:layout_height="wrap_content"
to some this like this:
android:layout_width="100dp" //what ever size suits your layout
android:layout_height="50dp" //fixing this,will not overlap the buttons

android transparent ImageButton not clickable in MvvmCross

How does one create an ImageButton with transparent background that still clickable (still acts like a Button)?
This is the xml snippet:
<ImageButton
android:paddingRight="10dp"
android:paddingLeft="10dp"
android:paddingTop="7dp"
android:paddingBottom="7dp"
android:src="#drawable/serverschedule"
android:background="#null"
android:clickable="true"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="#string/predict"
local:MvxBind="Click PredictCmd" />
I have also tried android:background="#00000000" and android:background="#android:color/transparent"and in all cases, I do get the desired visual effect but button no longer can be clicked.
I am using MvvmCross framework to binding to the Click event of the button, hence there is no code behind.
I am testing against API Level 15, if this matters.
EDIT Added entire axml for button.
EDIT Adding MVVM framework as it may have something to do with problem.
TIA.
Thanks for all of the suggestions.
This is what finally worked for me:
android:background="?android:attr/selectableItemBackground"
Based on responses from this thread.
Please provide width and height of your button.
Also try this :
ImageButton theButton = (ImageButton )findViewById(R.id.theButton);
theButton.setVisibility(View.VISIBLE);
theButton.setBackgroundColor(Color.TRANSPARENT);
theButton.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
// DO STUFF
}
});
Hope this helps.
Do certain Necessary changes
<ImageButton
android:paddingRight="10dp"
android:paddingLeft="10dp"
android:paddingTop="7dp"
android:paddingBottom="7dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/serverschedule"
android:background="#null"
android:id="#+id/mybutton"
</ImageButton
Into Your code:
mybutton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
//Your Stuff here
}
});
try this...
<ImageButton
android:id="#+id/imageButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/timeText"
android:layout_centerHorizontal="true"
android:background="#android:color/transparent"
android:clickable="true"
android:contentDescription="image button"
android:src="#drawable/anchor" />
set clickable attribute to true, and handle onclick event in your activity like
findViewById(R.id.imageButton).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "text", Toast.LENGTH_SHORT).show();
}
});
No change in your xml file.. even no need to add this android:clickable="true"
edit your java file as below code...
ImageButton imageButton;
#Override
public void onCreate(Bundle savedInstanceState) {
imageButton = (ImageButton) findViewById(R.id.imageButton1);
imageButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Toast.makeText(MyAndroidAppActivity.this,
"ImageButton is clicked!", Toast.LENGTH_SHORT).show();
}
});
}
}
If this doesn't work you might have problem with your parent layout. Please post that if so....
what's the meaning of the "acts as a button" ? when you use
android:background="#null"
your ImageButton will have no background and you can use selector as the src ,you'll get the same acts as a button,also can click as usual
selector like this
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/button_bg_pressed" android:state_pressed="true"/>
<item android:drawable="#drawable/button_bg_pressed" android:state_focused="true"/>
<item android:drawable="#drawable/button_bg_normal"/>
</selector>
also see enter link description here
back.setBackgroundColor(Color.TRANSPARENT);
works for all android versions

dynamic textsize change in ice cream sandawich

I am trying to change text size when button is clicked.
xml :
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="hello_world"
android:textSize="30sp"
android:layout_margin=""/>
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView1"
android:layout_marginTop="72dp"
android:layout_toLeftOf="#+id/textView1"
android:text="Button1" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/button1"
android:layout_toRightOf="#+id/textView1"
android:text="Button2" />
This is my code :
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtmain=(TextView)findViewById(R.id.textView1);
txtmain.setTextSize(TypedValue.COMPLEX_UNIT_SP ,30);
//txtmain.setTextSize(TypedValue.COMPLEX_UNIT_SP ,30);
txtmain.setTextAppearance(getApplicationContext(), 12);
btn1=(Button)findViewById(R.id.button1);
btn2=(Button)findViewById(R.id.button2);
txtmain.setBackgroundColor(Color.YELLOW);
btn1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
txtmain=(TextView)findViewById(R.id.textView1);
txtmain.setTextSize(TypedValue.COMPLEX_UNIT_SP ,30);
System.out.println("txtmain get height:"+txtmain.getHeight());
//Toast.makeText(getApplicationContext(),"txtmain get
//height:"+txtmain.getHeight() , Toast.LENGTH_LONG).show();
}
});
btn2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
txtmain=(TextView)findViewById(R.id.textView1);
txtmain.setTextSize(TypedValue.COMPLEX_UNIT_SP ,80);
System.out.println("txtmain get height:"+txtmain.getHeight());
//Toast.makeText(getApplicationContext(),"txtmain get
//height:"+txtmain.getHeight() , Toast.LENGTH_LONG).show();
}
});
When I click button 1, it gives proper output but when I click button 2 after clicking button1 output changes.
Here is my output :
This would appear to be quite similar to known a known issue on ICS, see https://code.google.com/p/android/issues/detail?id=22493 and https://code.google.com/p/android/issues/detail?id=17343. The second of these suggests that a workaround is to add a "\n" to the text in the text view. Reading up those pages and those they link to may help resolve the issue for you.
The problem is
txtmain.setHeight(41);
in first button click, So it will change the height of the textview from WRAP CONTENT to a fixed size. Just remove it..
Why are you setting the text box height wen you click the button1?

Textview causing java.lang.ClassCastException: android.widget.TextView

I have three textviews and applied clickevent on them, but when i click on any of them the they cause Forceclose error in application. i have also tried changing ids textviews and then clean project and ran the project still that error is not removed.
My XML code is
for one textview
<LinearLayout
android:id="#+id/LL_fb"
android:layout_width="180px"
android:layout_height="27px"
android:layout_above="#+id/txt_msg_regs"
android:layout_alignLeft="#+id/LL_signup"
android:layout_marginBottom="25dp"
android:background="#drawable/facebook" >
<TextView
android:id="#+id/btn_txt_fb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Connect with facebook"
android:layout_gravity="center_vertical"
android:layout_marginLeft="23dp"
android:layout_marginTop="1dp"
android:clickable="true"
android:textColor="#FFFFFF" />
</LinearLayout>
for second textview
<LinearLayout
android:id="#+id/LL_signup"
android:layout_width="180px"
android:layout_height="29px"
android:layout_above="#+id/textView1"
android:layout_alignLeft="#+id/LL_login"
android:background="#drawable/lmyic_signup">
<TextView
android:id="#+id/btn_txt_sinup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="signup with email"
android:layout_gravity="center_vertical"
android:layout_marginLeft="25dp"
android:layout_marginTop="1dp"
android:clickable="true"
android:textColor="#FFFFFF" />
</LinearLayout>
for third one
<LinearLayout
android:id="#+id/LL_login"
android:layout_width="180px"
android:layout_height="29px"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="69dp"
android:background="#drawable/lmyic_login">
<TextView
android:id="#+id/btn_txt_login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Log in"
android:layout_gravity="center_vertical"
android:layout_marginLeft="70dp"
android:layout_marginTop="1dp"
android:textColor="#FFFFFF"
android:clickable="true"/>
</LinearLayout>
this is my android code. This class name in intent are also correct and i verified them.
TextView img_login;
img_login = (TextView)findViewById(R.id.btn_txt_login);
img_login.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(v.getId() == img_login.getId())
{
Intent i_lin = new Intent(LockmeifyoucanActivity.this,lmiyc_login.class);
startActivity(i_lin);
}
}
});
Please Tell me what is wrong with this. If logcat is needed then ask me for it....
Please post the complete XMl so that we can view the btn_txt_login in that.
It looks it is button because "findViewById(R.id.btn_txt_login);" would return null if this "btn_txt_login" id is not in xml.
you have some problem with textview in lmiyc_login activity.in lmiyc_login corrent line number 46 which maybe:
TextView txtview;
txtview = (TextView)yourlayout.findViewById(R.id.txtviewid);
findViewById(R.id.btn_txt_login);
Is this really a TextView or do you have a Button in your layout as well? I think you're trying to cast a Button to a TextView
Wrong resource ID...
img_login = (TextView)findViewById(R.id.btn_txt_login);
according to your XML should be
img_login = (TextView)findViewById(R.id.txt_login);
etc..
change ur code with this
TextView img_login;
img_login = (TextView)findViewById(R.id.txt_login);
img_login.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(v.getId().equals(img_login.getId()))
{
Intent i_lin = new Intent(LockmeifyoucanActivity.this,lmiyc_login.class);
startActivity(i_lin);
}
}
});
To resolve this error quickly, set one break point at line:
View view = findViewById(R.id.btn_txt_login);
Then, see on locals tab which component is, looking at gen/R.java.
these are lines from 42-48
setContentView(R.layout.lmyic_login_page);
txtBack = (TextView)findViewById(R.id.ImgViewTxtBack); txtBack.setTypeface(null,Typeface.BOLD);
iv_login = (ImageView)findViewById(R.id.Txtlogin);
iv_login.setOnClickListener(this);
iv_sign_up = (ImageView)findViewById(R.id.TxtSignup);
I think problem is with iv_login = (ImageView)findViewById(R.id.Txtlogin); – Dheeresh Singh 1 min ago edit
because this is 46 line and it should be textview and you are castin it in Imageview – Dheeresh Singh just now edit
if you have duplicate resource Id, that will throw ClasscastException. So make sure you don't have duplicate element id throughout the whole project.

Categories

Resources