I have an imageview that should be changed on click
public class Settings extends Activity implements OnClickListener
{
private ImageView im1;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.settings);
im1 = (ImageView) findViewById( R.id.imageView1 );
im1.setOnClickListener(this);
}
#Override
public void onClick(View v)
{
// TODO Auto-generated method stub
if (v == im1 )
{
Log.d("test", "hey!");
v.setBackgroundResource(R.drawable.img1);
}
}
}
when clicked the method runs and prints out "hey!" but the image won't change?
EDIT: forgot to mention that imageview contains another image provided by xml layout file
By convention, you should be using setImageResource(R.drawable.img1); (or setImageDrawable(getResources().getDrawable(R.drawable.img1));) instead of setBackgroundResource(R.drawable.img1);.
ImageView i = (ImageView) findViewById( R.id.imageView1 );
i.setImageResource(R.id.logo);
or
i.setBackgroundResource(R.drawable.icon);
UPDATE
Now, you can use like below,
imageView.setImageDrawable(ContextCompat.getDrawable(this, R.drawable.img1));
Try This In API 25
imgSchedule.setImageDrawable(getResources().getDrawable(R.drawable.circle_image_selected));
Its no convention that setimageResource() should be used.
Both the APIs can be used.
Also, in your case, it just looks like the case of out of sync resources.
Related
I know that was already asked but it is outdated:
I have 2 buttons that represent 2 choices and if one is selected the background color gets changed to yellow. But if i want to change the choice i need to somehow reset the button:
I already try to set it back but some old design comes out. Can you provide me the id of the modern button style? And show me how to implement it?
int myChoice;
if (view == findViewById(R.id.choice1)){
myChoice = 1;
choice1.setBackgroundColor(getResources().getColor(R.color.highlightButton));
choice2.setBackgroundResource(android.R.drawable.btn_default);
}
else if (view == findViewById(R.id.choice2)){
myChoice = 2;
choice2.setBackgroundColor(getResources().getColor(R.color.highlightButton));
choice1.setBackgroundResource(android.R.drawable.btn_default);
}
}
Use Tags with getBackground(). This will assure you are always setting back to original.
Add following in beginning of function
if (v.getTag() == null)
v.setTag(v.getBackground());
Then instead of setBackgroundResource, use
v.setBackground(v.getTag());
Starting from here, you can store the default color of the button into a Drawable and grab the selection color (Yellow in your case) into anther Drawable, then toggle background colors of buttons with these Drawable variables
please check below demo
public class MainActivity extends AppCompatActivity {
private Drawable mDefaultButtonColor;
private Drawable mSelectedButtonColor;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button btn1 = findViewById(R.id.btn1);
final Button btn2 = findViewById(R.id.btn2);
mDefaultButtonColor = (btn1.getBackground());
mSelectedButtonColor = ContextCompat.getDrawable(this, R.color.buttonSelected);
btn1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
toggleButton(btn1, true);
toggleButton(btn2, false);
}
});
btn2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
toggleButton(btn1, false);
toggleButton(btn2, true);
}
});
}
private void toggleButton(Button button, boolean isSelected) {
button.setBackground(isSelected ? mSelectedButtonColor : mDefaultButtonColor);
}
}
I previously changed API from 21 to 26, then changed Android version 4 to version 7. When running the application on Android Lollipop I did not get an error but the application did not run properly. Strangely, it run well at another Android Version.
when clicking the button (button bPayment, for example), the views should change, but in lolipop it can't change. When it clicked, the action runs except the views won't change. So it looks like the previous screen / data piles and just stacked up on the front. What make it a bit confusing is, i still can access the views that placed under current views. Whereas in another android version, the current views just gone away after button is clicked.
So, in simplest words, the snippet code below run perfectly at any android version except Lolipop. How to get it works on android Lolipop ? Any help or hint will be much appreciated.
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
/**
* Inflate the layout for this fragment
*/
final View view = inflater.inflate(R.layout.favourite_layout, container, false);
context = getActivity();
activity = getActivity();
app = (MobileBankingApplication) context.getApplicationContext();
list = (ListView) view.findViewById(R.id.favourite_exchange_list);
list.addFooterView(new View(context), null, true);
list.setSmoothScrollbarEnabled(true);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD)
list.setOverScrollMode(View.OVER_SCROLL_ALWAYS);
list.setVisibility(View.VISIBLE);
list.startLayoutAnimation();
getFavouritePaymentHandler();
ImageView imageSeparator = (ImageView) getActivity().findViewById(R.id.imageViewSeparator);
Button buttonBack = (Button) getActivity().findViewById(R.id.button_back);
TextView title = (TextView) getActivity().findViewById(R.id.actionbar_title);
title.setText(getResources().getString(R.string.favourite).toUpperCase());
ImageView imageProfile = (ImageView) getActivity().findViewById(R.id.image_home);
imageProfile.setVisibility(View.INVISIBLE);
imageSeparator.setVisibility(View.INVISIBLE);
buttonBack.setVisibility(View.INVISIBLE);
tableRecurring = (TableLayout) view.findViewById(R.id.tableRecurring);
tableHeader = (TableLayout) view.findViewById(R.id.tableHeader);
layoutHeader = (LinearLayout) view.findViewById(R.id.layoutHeader);
layoutRecurring = (RelativeLayout) view.findViewById(R.id.layoutRecurring);
bPayment = (Button) view.findViewById(R.id.button1);
bPayment.setBackgroundResource(R.drawable.tab_favourite_selected);
bPayment.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
bRecurring.setBackgroundResource(R.drawable.tab_favourite);
bPayment.setBackgroundResource(R.drawable.tab_favourite_selected);
bTransfer.setBackgroundResource(R.drawable.tab_favourite);
bTopup.setBackgroundResource(R.drawable.tab_favourite);
layoutRecurring.setVisibility(View.GONE);
list.setVisibility(View.VISIBLE);
getFavouritePaymentHandler();
}
});
bTransfer = (Button) view.findViewById(R.id.button2);
bTransfer.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (Build.VERSION.SDK_INT == Build.VERSION_CODES.LOLLIPOP || Build.VERSION.SDK_INT == Build.VERSION_CODES.LOLLIPOP_MR1) {
Log.d("Transfer 1", "onClick: "+Build.VERSION.SDK_INT);
} else {
Log.d("Transfer 2", "onClick: "+Build.VERSION.SDK_INT);
}
bRecurring.setBackgroundResource(R.drawable.tab_favourite);
bPayment.setBackgroundResource(R.drawable.tab_favourite);
bTransfer.setBackgroundResource(R.drawable.tab_favourite_selected);
bTopup.setBackgroundResource(R.drawable.tab_favourite);
layoutRecurring.setVisibility(View.GONE);
list.setVisibility(View.VISIBLE);
getFavouriteTransferHandler();
}
});
bTopup = (Button) view.findViewById(R.id.button3);
bTopup.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
bRecurring.setBackgroundResource(R.drawable.tab_favourite);
bPayment.setBackgroundResource(R.drawable.tab_favourite);
bTransfer.setBackgroundResource(R.drawable.tab_favourite);
bTopup.setBackgroundResource(R.drawable.tab_favourite_selected);
layoutRecurring.setVisibility(View.GONE);
list.setVisibility(View.VISIBLE);
getFavouriteTopUpHandler();
}
});
bRecurring = (Button) view.findViewById(R.id.button4);
bRecurring.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
bRecurring.setBackgroundResource(R.drawable.tab_favourite_selected);
bPayment.setBackgroundResource(R.drawable.tab_favourite);
bTransfer.setBackgroundResource(R.drawable.tab_favourite);
bTopup.setBackgroundResource(R.drawable.tab_favourite);
layoutRecurring.setVisibility(View.VISIBLE);
list.setVisibility(View.GONE);
getFavouriteRecurringhandler();
}
});
return view;
}
I want to make a slideshow of numbers starting from 0 to 9 in pictures. When i click next button , show the picture of 1 and play sound as 'one' and so on.I want previous button to properly work.. like when I click previous button then go to previous pic and play sound which is related to that pic.
public class Numbers extends Activity {
int i = 1;
private ImageView iv;
Button next;
Button previous;
MediaPlayer ourSong;
private int currentImage = 0;
public int currentAudio = 0;
int[] images = { R.drawable.p1, R.drawable.p2, R.drawable.p3,
R.drawable.p4, R.drawable.p5, R.drawable.p6, R.drawable.p7,
R.drawable.p8, R.drawable.p9, R.drawable.p10};
int[] audios = { R.raw.a1, R.raw.a2, R.raw.a3, R.raw.a4, R.raw.a5,
R.raw.a6, R.raw.a7, R.raw.a8, R.raw.a9, R.raw.a10};
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.nextpre);
iv = (ImageView) findViewById(R.id.ivn);
next = (Button) findViewById(R.id.buttonn);
previous = (Button) findViewById(R.id.buttonp);
// Just set one Click listener for the image
next.setOnClickListener(iButtonChangeImageListener);
previous.setOnClickListener(gButtonChangeImageListener);
}
View.OnClickListener iButtonChangeImageListener = new View.OnClickListener() {
public void onClick(View v) {
try {
// Increase Counter to move to next Image
currentImage++;
currentImage = currentImage % images.length;
iv.setImageResource(images[currentImage]);
ourSong = MediaPlayer.create(Numbers.this,
audios[currentAudio+1]);
ourSong.start();
currentAudio++;
} catch (Exception e) {
}
}
};
View.OnClickListener gButtonChangeImageListener = new View.OnClickListener() {
public void onClick(View v) {
try {
// Decrease Counter to move to previous Image
currentImage--;
currentImage = (currentImage + images.length) % images.length;
iv.setImageResource(images[currentImage]);
MediaPlayer.create(Numbers.this, audios[currentAudio]);
ourSong.start();
currentAudio--;
} catch (Exception e) {
}
}
};
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
ourSong.release();
finish();
}
#Override
protected void onStart() {
super.onStart();
ourSong = MediaPlayer.create(Numbers.this,
audios[0]);
ourSong.start();
}
}
Hmm if you're trying to make a slide show, you might want to look into view pagers they look like this:
View pagers are highly customizable, You can add buttons and images and pretty much almost anything a fragment can hold on each screen. Not sure what your skill level is but ill tell you whats involved in getting this to work.
Create a layout with a view pager in it.
Create a class that extends the FragmentPagerAdapter
Override getItem() method in the adapter (this is where you define your different "screens"
Create a class that extends fragment for each screen you want to show your users.
Doing it this way in order to switch screens u just have to call setCurrentItem to change pages (when user clicks next or prev)
--edit--
Apparently theres also a something called an ImageSwitcher.
They look like this:
This is actually better for your case since you only want images. It looks a lot easier to implement than a view pager. This describes how to implement it: http://www.tutorialspoint.com/android/android_imageswitcher.htm
I would like to use RTEditor in my app. but I have some errors in code. My question is what can I do with that? Instruction about RTEditor is here: https://github.com/1gravity/Android-RTEditor
and that's part of my code:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setTheme(R.style.RTE_BaseThemeDark);
setContentView(R.layout.activity_editor);
newRowEdit = (EditText) findViewById(R.id.editNote);
newRowEdit1 = (EditText) findViewById(R.id.rtEditText);
// create RTManager
RTApi rtApi = new RTApi(this, new RTProxyImpl(this), new RTMediaFactoryImpl(this, true));
RTManager rtManager = new RTManager(rtApi, savedInstanceState);
// register toolbar
ViewGroup toolbarContainer = (ViewGroup) findViewById(R.id.rte_toolbar_container);
RTToolbar rtToolbar = (RTToolbar) findViewById(R.id.rte_toolbar);
if (rtToolbar != null) {
rtManager.registerToolbar(toolbarContainer, rtToolbar);
}
// register editor & set text
RTEditText rtEditText = (RTEditText) findViewById(R.id.rtEditText);
rtManager.registerEditor(rtEditText, true);
rtEditText.setRichTextEditing(true, message);
String text = rtEditText.getText(RTFormat.HTML);
}
Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
mRTManager.onSaveInstanceState(outState);
}
#Override
public void onDestroy() {
super.onDestroy();
mRTManager.onDestroy(isFinishing());
}
The error is: cannot resolve symbol: message and cannot resolve symbol:mRTManager.
Can anyone help me please?
I can't see the whole code but it seems that both variables are not declared inside the corresponding methods. Make sure they are declared as class member variables
Replace mRTManager with rtManager, and make sure rtManager is declared outside onCreate method.
In my Activity I have to add 10 times the same TextView.
Is it possible to load the definition of textview from layout.xml and repeat it programmatically?
for(int i=0;i<10;i++){
Textview text = new TextView(this);
mainlayout.add(text);
}
public class YourClassName extends Activity
{
#Override
public void onCreate(Bundle bundle)
{
super.onCreate(bundle);
// set activity layout
setContentView(R.layout.some_activity_layout);
LinearLayout mainActivityLayout = (LinearLayout)findViewById(R.id.main_layout);
LayoutInflater li = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// then see previous answer
// loop n times {
TextView yourTextView = _li.inflate(R.layout.text_view_layout, null);
mainActivityLayout.addView(yourTextView);
// } end loop
}
}
You may want to read this article on reusing UI components: http://developer.android.com/resources/articles/layout-tricks-reuse.html