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?
Related
I have been trying to add my own icon to the ActionBarDrawerToggle (android.support.v7.app.ActionBarDrawerToggle). The custom icon is visible but it is not responding to clicks. I can see my side menu when i slide my finger from left to right but click is not working.
I have tried this solution but it is working and i don't want to use toolbar. I want solution to add this icon without toolbar, like this.
This is my code. Sorry for too many lines.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
getSupportActionBar().setCustomView(R.layout.layout_for_title);
CustomTextViewToolbar head=(CustomTextViewToolbar)findViewById(getResources().getIdentifier("mytext", "id", getPackageName()));
head.setText("Home");
ImageView gender = (ImageView)findViewById(getResources().getIdentifier("gender" , "id" , getPackageName()));
gender.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
final Dialog dialog = new Dialog(MainActivity.this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.custom_dialog_gender);
dialog.show();
CircularImageView male = (CircularImageView)dialog.findViewById(R.id.male) ;
male.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
dialog.dismiss();
}
});
CircularImageView female = (CircularImageView)dialog.findViewById(R.id.female) ;
female.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
dialog.dismiss();
}
});
CircularImageView kids = (CircularImageView)dialog.findViewById(R.id.kids) ;
kids.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
dialog.dismiss();
}
});
}
});
ImageView imageView = (ImageView)findViewById(getResources().getIdentifier("show_cart" , "id" , getPackageName()));
imageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(getBaseContext() , CustomerCartActivity.class));
}
});
setContentView(R.layout.customer_home_screen);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mActivityTitle = getTitle().toString();
mExpandableListView = (ExpandableListView) findViewById(R.id.navList);
initItems();
LayoutInflater inflater = getLayoutInflater();
View listHeaderView = inflater.inflate(R.layout.nav_header, null, false);
mExpandableListView.addHeaderView(listHeaderView);
mExpandableListData = ExpandableListDataSource.getData(this);
mExpandableListTitle = new ArrayList(mExpandableListData.keySet());
addDrawerItems();
setupDrawer();
if (savedInstanceState == null) {
selectFirstItemAsDefault();
}
mDrawerToggle.setDrawerIndicatorEnabled(false);
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
mDrawerToggle.setHomeAsUpIndicator(R.drawable.menu_icon);
coverFlow = (FeatureCoverFlow) findViewById(R.id.coverflow);
mDrawerToggle.setToolbarNavigationClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
drawer.openDrawer(GravityCompat.START);
}
}
});
mDrawerLayout.setDrawerListener(mDrawerToggle);
horizontal_recycler_view= (RecyclerView) findViewById(R.id.horizontal_recycler_view);
data = fill_with_data();
horizontalAdapter=new HorizontalAdapter(data, getApplication());
LinearLayoutManager horizontalLayoutManager = new LinearLayoutManager(MainActivity.this, LinearLayoutManager.HORIZONTAL, false);
horizontal_recycler_view.setLayoutManager(horizontalLayoutManager);
horizontal_recycler_view.setAdapter(horizontalAdapter);
coverFlowOne = (FeatureCoverFlow) findViewById(R.id.coverflow_one);
coverFlowTwo = (FeatureCoverFlow) findViewById(R.id.coverflowtwo);
settingDummyData();
adapter = new CoverFlowAdapter(this, games);
coverFlow.setAdapter(adapter);
coverFlow.setOnScrollPositionListener(onScrollListener());
coverFlowOne.setAdapter(adapter);
coverFlowOne.setOnScrollPositionListener(onScrollListener());
coverFlowTwo.setAdapter(adapter);
coverFlowTwo.setOnScrollPositionListener(onScrollListener());
myHome = (LinearLayout)findViewById(R.id.c_home);
myOrder = (LinearLayout)findViewById(R.id.c_my_order);
myAccount = (LinearLayout)findViewById(R.id.c_profile);
myHome.setOnClickListener(this);
myOrder.setOnClickListener(this);
myAccount.setOnClickListener(this);
}
private void selectFirstItemAsDefault() {
if (mNavigationManager != null) {
String firstActionMovie = getResources().getStringArray(R.array.actionFilms)[0];
mNavigationManager.showFragmentAction(firstActionMovie);
}
}
private void initItems() {
items = getResources().getStringArray(R.array.film_genre);
}
private void addDrawerItems() {
mExpandableListAdapter = new CustomExpandableListAdapter(this, mExpandableListTitle, mExpandableListData);
mExpandableListView.setAdapter(mExpandableListAdapter);
mExpandableListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
#Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
String selectedItem = ((List) (mExpandableListData.get(mExpandableListTitle.get(groupPosition))))
.get(childPosition).toString();
mDrawerLayout.closeDrawer(GravityCompat.START);
return false;
}
});
}
private void setupDrawer() {
mDrawerToggle = new ActionBarDrawerToggle(this,
mDrawerLayout,
R.string.drawer_open,
R.string.drawer_close) {
/** Called when a drawer has settled in a completely open state. */
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
invalidateOptionsMenu();
}
/** Called when a drawer has settled in a completely closed state. */
public void onDrawerClosed(View view) {
super.onDrawerClosed(view);
getSupportActionBar().setTitle(mActivityTitle);
invalidateOptionsMenu();
}
};
mDrawerToggle.setHomeAsUpIndicator(R.drawable.menu_icon);
mDrawerToggle.setDrawerIndicatorEnabled(false);
mDrawerLayout.setDrawerListener(mDrawerToggle);
mDrawerLayout.requestLayout();
}
#Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
mDrawerToggle.syncState();
}
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
mDrawerToggle.onConfigurationChanged(newConfig);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Have you set you new icon to "clickable" in the xml?
Enable the Debug GPU Overdraw tool. It may give you an indication whether or not your icon is overlapped by another element that is consuming clicks (if so you will need to make that overlapping element clickable to false).
On your mobile device, go to Settings and tap Developer Options.
In the Hardware accelerated rendering section, select Debug GPU Overdraw.
In the Debug GPU overdraw popup, select Show overdraw areas.
https://developer.android.com/studio/profile/dev-options-overdraw.html
Alternatively add debugging onClick functions to potentially overlapping or conflicting elements.
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);
I'm a beginner but I would like to modify an older project.
On the Preferences Activity, there are already few buttons, but I would like to add a new button, which, when clicked, opens a new Activity and shows an image only.
For this I declared this new activity in the Manifest:
<activity
android:name=".VisualHelpActivity"
android:screenOrientation="portrait"
android:theme="#style/AppTheme" >
</activity>
I declared the button on the associated xml layout:
<Button
android:id="#+id/button_visualhelp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="visual_help"
android:text="Visual Help" />
I created the new VisualHelp layout xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:id="#+id/visualhelp_image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="fitXY"
android:src="#drawable/guide" />
Declared its new VisualHelpActivity java file:
public class VisualHelpActivity extends TranslatableActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_visualhelp);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.visual_help, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
#Override
protected void refreshLanguage() {
}
}
… and now I’m supposed to connect it all together in the "main" PreferencesActivity, which looks as follows:
public class PreferencesActivity extends TranslatableActivity {
public static String url = "https://www.paypal.com/cgi-bin....";
ImageView donate;
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
onBackPressed();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
#SuppressLint("NewApi")
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
setContentView(R.layout.activity_preferences);
donate = (ImageView) findViewById(R.id.button_donate);
donate.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
donate(v);
}
});
Spinner s = (Spinner) findViewById(R.id.combo_language);
Lang[] languages = LanguageUtil.Lang.values();
s.setAdapter(new ArrayAdapter<Lang>(this,
android.R.layout.simple_dropdown_item_1line, languages));
s.setSelection(getPosition(languages, LanguageUtil.getLanguage()), true);
}
private int getPosition(Object[] objs, Object item) {
for (int i = 0; i < objs.length; i++) {
if (objs[i] == item)
return i;
}
return -1;
}
public void update(View v) {
Maintenance.downloadDB(this, new onFinishListener() {
#Override
public void onFinish() {
LanguageUtil.loadLanguage(PreferencesActivity.this);
}
});
}
public void copy(View v) {
Maintenance.copyDB(this, new onFinishListener() {
#Override
public void onFinish() {
LanguageUtil.loadLanguage(PreferencesActivity.this);
}
});
}
public void save(View v) {
LanguageUtil.saveLanguage(this,
(Lang) ((Spinner) findViewById(R.id.combo_language))
.getSelectedItem());
LanguageUtil.loadLanguage(this);
setResult(RESULT_OK);
finish();
}
public void donate(View v) {
Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(i);
}
public void cancel(View v) {
finish();
}
#Override
protected void refreshLanguage() {
((Button) findViewById(R.id.button_update)).setText(LanguageUtil
.getDeviceLabel(LanguageUtil.dev_update));
((TextView) findViewById(R.id.language_label))
.setText(LanguageUtil.getDeviceLabel(LanguageUtil.dev_language));
}
}
According to androidbegin.com, I should add this code into the PreferencesActivity java file (code above):
Button button;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_visualhelp);
button = (Button) findViewById(R.id.button_visualhelp);
button.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
Intent myIntent = new Intent(PreferencesAtivity.this,
VisualHelpActivity.class);
startActivity(myIntent);
}
});
}
The only problem is that if I try to add this new public void, multiple marker errors pop up, and if I try to include all below somewhere else, my app fails to work. So, can anyone help me out where and how to include this code part to make it work?
add the code inside oncreate method like this in your PreferencesActivity class:-
Button button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
setContentView(R.layout.activity_preferences);
donate = (ImageView) findViewById(R.id.button_donate);
donate.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
donate(v);
}
});
button = (Button) findViewById(R.id.button_visualhelp);
button.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
Intent myIntent = new Intent(PreferencesAtivity.this,
VisualHelpActivity.class);
startActivity(myIntent);
}
});
Spinner s = (Spinner) findViewById(R.id.combo_language);
Lang[] languages = LanguageUtil.Lang.values();
s.setAdapter(new ArrayAdapter<Lang>(this,
android.R.layout.simple_dropdown_item_1line, languages));
s.setSelection(getPosition(languages, LanguageUtil.getLanguage()), true);
}
I have editText in Setup Activity. I used SharedPreference for saving this value and get later. I passed this edittext value to another activity button text. Initially i need to hide the button. If edittext values are coming from sharedPreference i need to display the button.Thanks alot
public class Setup extends Activity implements AdapterView.OnItemSelectedListener {
EditText editText;
Button button;
Spinner spinner;
TextView text;
ArrayList<String> ar = new ArrayList<String>();
// String a=editText.getText().toString();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_setup);
button = (Button) findViewById(R.id.button1);
editText = (EditText) findViewById(R.id.ed1);
spinner = (Spinner) findViewById(R.id.spinner1);
text = (TextView) findViewById(R.id.back);
getnameButton();
ArrayAdapter<String> arr = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, ar);
arr.setDropDownViewResource(android.R.layout.simple_list_item_single_choice);
spinner.setAdapter(arr);
spinner.setOnItemSelectedListener(this);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
SharedPreferences preferences = getSharedPreferences("sample", 0);
SharedPreferences.Editor editor = preferences.edit();
editor.putString("Name",editText.getText().toString());
editor.putInt("position",spinner.getSelectedItemPosition());
editor.commit();
Intent myIntent = new Intent(Setup.this, MainActivity.class);
startActivity(myIntent);
}
});
}
#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_setup, menu);
return true;
}
public void getnameButton() {
Intent intent = getIntent();
String text1 = intent.getStringExtra("text1");
String text2 = intent.getStringExtra("text2");
String text3 = intent.getStringExtra("text3");
String text4 = intent.getStringExtra("text4");
String text5 = intent.getStringExtra("text5");
String text6 = intent.getStringExtra("text6");
String text7 = intent.getStringExtra("text7");
String text8 = intent.getStringExtra("text8");
ar.add(text1);
ar.add(text2);
ar.add(text3);
ar.add(text4);
ar.add(text5);
ar.add(text6);
ar.add(text7);
ar.add(text8);
}
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
editText.setText(ar.get(position));
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
=>Activity Class
public class MainActivity extends Activity {
Button button1,button2,button3,button4,button5,button6,button7,button8;
TextView textView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button1=(Button) findViewById(R.id.btn1);
button2=(Button) findViewById(R.id.btn2);
button3=(Button) findViewById(R.id.btn3);
button4=(Button) findViewById(R.id.btn4);
button5=(Button) findViewById(R.id.btn5);
button6=(Button) findViewById(R.id.btn6);
button7=(Button) findViewById(R.id.btn7);
button8=(Button) findViewById(R.id.btn8);
textView=(TextView) findViewById(R.id.txtsua);
getName();
textView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(v.getContext(),Setup.class);
intent.putExtra("text1", button1.getText().toString());
intent.putExtra("text2",button2.getText().toString());
intent.putExtra("text3",button3.getText().toString());
intent.putExtra("text4", button4.getText().toString());
intent.putExtra("text5",button5.getText().toString());
intent.putExtra("text6",button6.getText().toString());
intent.putExtra("text7", button7.getText().toString());
intent.putExtra("text8", button8.getText().toString());
startActivity(intent);
}
});
// getnameButton();
}
public void getName() {
button1.setVisibility(View.GONE);
SharedPreferences preferences = getSharedPreferences("sample", 0);
String Namestr = (preferences.getString("Name", ""));
int position = (preferences.getInt("position", Integer.parseInt("")));
for (int i = 0; i < 8; i++) {
Button[] a = {button1, button2, button3, button4, button5, button6, button7, button8};
if (Namestr.length() > 0&&position==i) {
a[i].setVisibility(View.VISIBLE);
a[i].setText(preferences.getString("Name", ""));
}
break;
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}}
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.