Changing ImageButton Image - java

I have set the Image for my ImageButton in XML but trying to change the image in the code below does nothing. Any ideas why?
public class Test extends Activity implements OnClickListener {
ImageButton myImageButton;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my_farm_main);
myImageButton = (ImageButton)findViewById(R.id.button);
myImageButton.setBackgroundResource(R.drawable.image1);
myImageButton.setOnClickListener(this);
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
}

You just make sure your image is available in all drawable folder you will create a common drawable folder in your res and then paste your image at that folder then you will try his concept and or otherwise you will try like this..You just Set the image source it will give the answer
public class MainActivity extends Activity {
private ImageButton imageView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView =(ImageButton) findViewById(R.id.imageButton1);
imageView.setImageResource(R.drawable.ic_launcher);
}
}

I guess you want change the image not background.
Substitute
myImageButton.setBackgroundResource(R.drawable.image1);
with
myImageButton.setImageResource(R.drawable.image1);
This code below works perfectly fine if you have a imagebutton in your layout file and a drawable name ic_launcher in drawable directory.
public class MainActivity extends Activity implements OnClickListener{
ImageButton myImageButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myImageButton = (ImageButton) findViewById(R.id.button);
myImageButton.setImageResource(R.drawable.ic_launcher);
myImageButton.setOnClickListener(this);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
}
}
Comment below if that's not what you want.

Related

Webview to load while slashscreen is on

Quite new to android studio but i'm facing a problem where my WebView is taking too long to load, so i've created a splashscreen for 4 seconds and plan to cover the loading using this splash screen. But I dont really know how to let it overlap so the WebView can load behind the splashscreen.
the java for my splash screen
public class MainActivity extends ActionBarActivity {
private static int SPLASH_TIME_OUT = 4000;
BottomBar mBottomBar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new Handler().postDelayed(new Runnable() {
#Override
public void run(){
Intent homeIntent = new Intent(MainActivity.this, HomeActivity.class);
startActivity(homeIntent);
finish();
}
},SPLASH_TIME_OUT);
getWindow().getDecorView().setBackgroundColor(Color.WHITE);
}
}
then i also have a bottombar that navigates to the first framgment with the webview and the java are as below
public class HomeActivity extends ActionBarActivity {
BottomBar mBottomBar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
getWindow().getDecorView().setBackgroundColor(Color.WHITE);
mBottomBar = BottomBar.attach(this,savedInstanceState);
mBottomBar.setItemsFromMenu(R.menu.menu_main,new OnMenuTabClickListener() {
#Override
public void onMenuTabSelected(#IdRes int i) {
if(i==R.id.Bottombaritemone) {
HomeFragment f = new HomeFragment();
getSupportFragmentManager().beginTransaction().replace(R.id.frame, f).commit();
}
else if(i==R.id.Bottombaritemtwo)
{
SearchFragment f =new SearchFragment();
getSupportFragmentManager().beginTransaction().replace(R.id.frame,f).commit();
}
else if(i==R.id.Bottombaritemthree)
{
AccountFragment f =new AccountFragment();
getSupportFragmentManager().beginTransaction().replace(R.id.frame,f).commit();
}
}
#Override
public void onMenuTabReSelected(#IdRes int i) {
}
});
mBottomBar.mapColorForTab(0,"#F2F2F2");
mBottomBar.mapColorForTab(1,"#F2F2F2");
mBottomBar.mapColorForTab(2,"#F2F2F2");
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
and finally the java for my webview in the first HomeFragment
public class HomeFragment extends Fragment {
WebView my2WebView;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.home,container,false);
getLoaderManager();
my2WebView = (WebView) view.findViewById(R.id.webView1);
WebSettings webSettings = my2WebView.getSettings();
my2WebView.getSettings().setRenderPriority(WebSettings.RenderPriority.HIGH);
webSettings.setUseWideViewPort(true);
webSettings.setSavePassword(true);
webSettings.setEnableSmoothTransition(true);
webSettings.setJavaScriptEnabled(true);
webSettings.getLoadsImagesAutomatically();
my2WebView.loadUrl("http://www.fidellaglobal.com/feed");
my2WebView.setWebViewClient(new WebViewClient());
return view;
}
so what can i do to making the webview in the first fragment to load when the splashscreen is on ? thank you :)
Initially set your WebView visibility invisible inside your layout file. Add you WebView in same screen which have delay Handler.
new Handler().postDelayed(new Runnable() {
#Override
public void run(){
// Make your webview visible
mWebView.setVisibility(View.VISIBLE);
}
},SPLASH_TIME_OUT);

Setting an long click event for calendar

I am trying to make a calendar that shows a popup window when i long press the calendarview. But nothing happens when i do long click the calendarview. I tried using other event listeners like on click,onhover,etc. But none of them respond. I even thought of trying to use the calendarview as a dialog but none of the listeners i found in android documentation for dialog look for a long clock.
This is what i have tried to do.
public class MainActivity extends Activity{
PopupWindow p;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
CalendarView c=(CalendarView)findViewById(R.id.calendarView1);
c.setOnLongClickListener(new OnLongClickListener() {
#Override
public boolean onLongClick(View arg0) {
openpopupwindow();
return true;
}
private void openpopupwindow() {
// TODO Auto-generated method stub
LayoutInflater inflator=(LayoutInflater)MainActivity.this.
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View lay=inflator.inflate(R.layout.add_appointment, (ViewGroup)findViewById(R.id.popupagenda));
p=new PopupWindow(lay,300,300,true);
p.showAtLocation(lay, Gravity.CENTER, 0, 0);
Button badd=(Button)findViewById(R.id.bAdd);
badd.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
p.dismiss();
}
});
}
});
}

different Property Animation of more than one ImageButtons

I'm doing a property animation of different imagebuttons. my code perfectly works for a single imagebutton, but whenever i add some more buttons or images, everything starts animating.
here is my code
public class MainActivity extends Activity {
// ImageButton jackfruit = (ImageButton) findViewById(R.id.btnjackfruit);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button but = (Button) findViewById(R.id.btn);
but.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
ball();
}
private void ball() {
ImageButton ball = (ImageButton) findViewById(R.id.btnball);
ball.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(final View v) {
//ImageButton b = (ImageButton) findViewById(R.id.btnball);
ImageView banan = (ImageView) findViewById(R.id.banana);
banan.setVisibility(View.VISIBLE);
} // onClick
});ObjectAnimator horizontalAnimator = ObjectAnimator.ofInt(new ImageButtonAnimatorHelper(ball), "marginLeft", 0, 600);
horizontalAnimator.setDuration(2000);
horizontalAnimator.setRepeatCount(ValueAnimator.INFINITE);
horizontalAnimator.setRepeatMode(ValueAnimator.REVERSE);
horizontalAnimator.setInterpolator(new LinearInterpolator());
horizontalAnimator.start();
} class ImageButtonAnimatorHelper {ImageButton ballButton;
private ImageButtonAnimatorHelper(ImageButton imagebutton) {ballButton = imagebutton; }
public void setMarginLeft(int margin) {
ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams) ballButton.getLayoutParams();
params.leftMargin = margin;
ballButton.setLayoutParams(params);
} // setMarginLeft
}} // ImageButtonAnimatorHelper
);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}}
Can anyone tell why each of my object is moving?

Doing an Android Silent Mode App, can't even start Main Activity?

I'm working with the Android Application Development for Dummies book by Donn Felker. Specifically, I'm working with the Silent Mode App.
It should be simple to follow the instructions step by step, but as it turns out I can't even start the main activity without crashing the app. I must've gone over my code a dozen times and I can't still figure out what's wrong and make it work without LogCat firing errors like crazy.
Debug device is a Samsung Galaxy SII w/ Android 4.0.3.
Here's the MainAcitivity class
public class MainActivity extends Activity {
Button m_toggleButton = (Button)findViewById(R.id.toggleButton);
AudioManager m_audio = (AudioManager)getSystemService(AUDIO_SERVICE);
boolean m_isPhoneSilent;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
checkMode();
toggleImage();
generateClick();
}
#Override
protected void onResume()
{
super.onResume();
checkMode();
toggleImage();
}
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
private void checkMode()
{
m_audio = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
switch(m_audio.getRingerMode())
{
case AudioManager.RINGER_MODE_NORMAL:
m_isPhoneSilent = false;
break;
case AudioManager.RINGER_MODE_SILENT:
m_isPhoneSilent = true;
break;
}
}
private void generateClick()
{
m_toggleButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v)
{
if (m_isPhoneSilent == true)
{
m_audio.setRingerMode
(AudioManager.RINGER_MODE_NORMAL);
m_isPhoneSilent = false;
}
else
{
m_audio.setRingerMode
(AudioManager.RINGER_MODE_SILENT);
m_isPhoneSilent = true;
}
toggleImage();
}
});
}
private void toggleImage()
{
ImageView imageView = (ImageView) findViewById(R.id.phone_icon);
Drawable newAsset;
if (m_isPhoneSilent == true)
{
newAsset = getResources().getDrawable(R.drawable.phone_silent);
}
else
{
newAsset = getResources().getDrawable(R.drawable.phone_on);
}
imageView.setImageDrawable(newAsset);
}
I've tried to debug, but I haven't found the problem. Please help.
public class MainActivity extends Activity {
Button m_toggleButton = (Button)findViewById(R.id.toggleButton); //<<<< here
//....your code
you are trying to find Button before setting layout for Activity so move it after setting setContentView as :
public class MainActivity extends Activity {
Button m_toggleButton ;
AudioManager m_audio;
boolean m_isPhoneSilent;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
m_toggleButton = (Button)findViewById(R.id.toggleButton);
m_audio = (AudioManager)getSystemService(AUDIO_SERVICE);
///... your code here
You can't call this
Button m_toggleButton = (Button)findViewById(R.id.toggleButton);
AudioManager m_audio = (AudioManager)getSystemService(AUDIO_SERVICE);
before your setContentView change it to this:
Button m_;
AudioManager m_audio;
boolean m_isPhoneSilent;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
m_toggleButton = (Button)findViewById(R.id.toggleButton);
m_audio = (AudioManager)getSystemService(AUDIO_SERVICE);
....

Some Serious looking bug in instnaceof or App package

I made 2 activities and an Intent extended class named SomeSpecialIntent, when you press on the first activity's textview you go to the second using new SomeSpecialIntent class instnace, but something strange goes with it on the way, because the phrase getIntent() instnaceof SomeSpecialIntent returns false on the second activity!
whats up with that?
the code for the check i made:
public class MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView textView = (TextView)findViewById(R.id.textView1);
textView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new SomeSpecialIntent(MainActivity.this,SomeActivity.class));
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
public class SomeActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d("Check",""+(getIntent() instanceof SomeSpecialIntent));//returns false!!!!
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
public class SomeSpecialIntent extends Intent {
public SomeSpecialIntent(Context context,
Class<?> class1) {
super(context,class1);
}
}

Categories