How to Send Drawable as a parameter to a function - android

I am trying to set Imageview using a function
that if we send Image drawable to a function that function set that drawable to the respective Imageview.
This is function code in which it gets "id" and after that respective id function will call
public void showpeg(int id) {
switch (id) {
case 1:
showSeagreen(R.drawable.seagreen);
break;
}
}
This is my ShowSeagreen() function
public void showSeagreen(Drawable draw) {
if (iRow == 1) {
if (iPlace == 1) {
seagreen = 1;
one_first.setBackgroundResource(R.drawable.draw);
iPlace++;
} else if (iPlace == 2) {
seagreen = 2;
one_two.setBackgroundResource(R.drawable.draw);
iPlace++;
}
}
I know i am doing it wrong but i can't find a way to do this Please any help
Thanks in advance

instead of
showSeagreen(R.drawable.seagreen);
use:
showSeagreen(getResources().getDrawable(R.drawable.seagreen));

Any Resources is a int.
Do like this to "showSeagreen":
public void showSeagreen(int draw) {
if (iRow == 1) {
if (iPlace == 1) {
seagreen = 1;
one_first.setBackgroundResource(draw);
iPlace++;
} else if (iPlace == 2) {
seagreen = 2;
one_two.setBackgroundResource(draw);
iPlace++;
}
}

If what i understand from your question is true then i think your aim is to add drawable image as a parameter if thats what you want
then you should add the
#DrawableRes int draw
like
public void showSeagreen(#DrawableRes int draw) {
}
as your parameter then you can add drawable files in here

Related

Unity GameObject "flash" after SetActive on Android Project

I have a project that use Android to display Unity Player. So I export Untiy project as Android module which implemented by Android Application.
I create buttons in Android Activity which contains UnityPlayer, And when I click button, it send a message to Unity Player to invoke C# function, just like this:
findViewById(R.id.btnChange).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mUnityPlayer.UnitySendMessage("ScriptHolder", "ChangeSkin", "");
}
});
And the function named "ChangeSkin" is just to change some GameObjects' active. Just like this:
void ChangeSkin()
{
int prefab;
if (_currentPrefab == PREFAB_DEFAULT)
{
prefab = PREFAB_PRINCESS;
}
else
{
prefab = PREFAB_DEFAULT;
}
ShowSkin(prefab);
}
private void ShowSkin(int prefab)
{
_currentPrefab = prefab;
foreach (var item in _defaultDressList)
{
item.SetActive(prefab == PREFAB_DEFAULT);
}
foreach (var item in _princessDressList)
{
item.SetActive(prefab == PREFAB_PRINCESS);
}
}
And something weird happening: when I click button to change the person's cloth in Unity, the GameObjects which called SetActive(true) show at the position above the right position for a frame and become normal, it looks like they flash. Here is the gif of the project demo:
It looks like the position offset is equal to the height of status bar. If I create a button on Unity Scene and call "ChangeSkin" function, everything will be OK.
I tried all I can to fix this but not succeed. So I hope you will help me, thx.
You should check your Unity layout. If you have a layout group that has content size fitter component, and the child objects also have it (content size fitter), this could cause these glitches.
I fixed this problem by using a flag (or trigger). Just like this:
// set value true to trigger ChangeSkin() in Update(),
// instead of invoke ChangeSkin() directly
private bool _changingSkinTrigger = false;
void ChangeSkin()
{
if (_currentPrefab == PREFAB_DEFAULT)
{
_currentPrefab = PREFAB_PRINCESS;
}
else
{
_currentPrefab = PREFAB_DEFAULT;
}
_changingSkinTrigger = true;
}
void Update()
{
if (_changingSkinTrigger)
{
_changingSkinTrigger = false;
ShowSkin();
}
}
private void ShowSkin()
{
foreach (var item in _defaultDressList)
{
item.SetActive(_currentPrefab == PREFAB_DEFAULT);
}
foreach (var item in _princessDressList)
{
item.SetActive(_currentPrefab == PREFAB_PRINCESS);
}
}
Thank #AndriyMarshalek for enlightening me.

RecyclerView not clearing (notifyDataSetChanged not working)

I've had a look everywhere on here and nothing is related to my problem. Basically I've implemented a feature which changes the reading direction of my app (1 or -1) now I can get it to initially change direction and it works really well but when I get it to change back (using the same code but changing the direction) it just doesn't update. Just seems like notifyDataSetChanged(); doesn't want to work the second time...?
My code is as as follows:
private void flip() {
if (!isFlipped) {
mData.getItems().removeAll(mData.getItems());
mAdapter.notifyDataSetChanged();
isFlipped = true;
loadData(false, -1);
closeMenu();
} else {
mData.getItems().removeAll(mData.getItems());
mAdapter.notifyDataSetChanged();
isFlipped = false;
loadData(false, 1);
closeMenu();
}
}
What am I doing wrong?
Again, thanks in advance :)
Implement a public method in your RecyclerView code, e.g:
public void clearAll(){
mData.clear();
this.notifyDataSetChanged();
}
And then call that function from your activity (or Fragment):
private void flip() {
if (!isFlipped) {
mAdapter.clearAll();
isFlipped = true;
loadData(false, -1);
closeMenu();
} else {
mAdapter.clearAll();
isFlipped = false;
loadData(false, 1);
closeMenu();
}
}

Android - Circular array

I have a array of jokes
String jokes[]={"x","y","z","j"};
i am using two methods next and previous to go front and back.
public void nextJoke(View view) {
if (jokeNumber < jokes.length - 1) {
jokeNumber++;
tv1.setText(jokes[jokeNumber]);
}
}
public void prevJoke(View view) {
if (jokeNumber > 0) {
jokeNumber--;
tv1.setText(jokes[jokeNumber]);
}
I want to give this list a endless scroll functionality .
That means if the user next or previous - and list reaches the last three elements , it should again start from first element.
How can i achieve this functionality .
This is more of a programming question .
Any Help will be highly appreciated.
Using the remainder operator, you'll get the required behavior.
Replace:
jokeNumber++;
with:
jokeNumber = ++jokeNumber % jokes.length;
and do the same for jokeNumber--;
On next:
next(){
jokeNumber++;
if(jokeNumber>=jokes.length) { jokeNumber =0;}
}
on previous:
previous(){
jokeNumber--;
if(jokeNumber<=0) { jokeNumber =jokes.length;}
}
public void nextJoke(View view) {
if (jokeNumber < jokes.length ) {
jokeNumber = ++jokeNumber % jokes.length;
tv1.setText(jokes[jokeNumber]); } }
public void prevJoke(View view) {
if (jokeNumber > 0) {
jokeNumber = --jokeNumber % jokes.length;
tv1.setText(jokes[jokeNumber]);
}

If ImageView drawable equals a known drawable

I have an imageview that has its imageresource set programatically. This image could have had its resource set by any of 3 drawables, which is chosen using various variables earlier in the class.
The 3 drawables are:
R.drawable.well_done
R.drawable.try_again
R.drawable.sorry_failed
When I click on this image, it could have any of these three drawables set to it, so I'm trying to run a method something along the lines of this (I know this is totally wrong, but it might help visualise what I'm trying to do)
public void game(){
//STUFF
if(result>10){
imgView.setImageResource(R.drawable.well_done);}
//MORE STUFF
}
public void results_page(){
if(imgView=R.drawable.well_done){
//DO STUFF
}
if(imgView=R.drawable.try_again){
//DO STUFF
}
//etc...
}
Here is the proper code I have. Its a little more complex than my rubbish visualisation above, but I hope you understand what I'm trying to do.
ImageView iv_i;
//...
...onCreate(Bundle savedInstanceState){
//...
result1.setOnClickListener(onClL);
result2.setOnClickListener(onClL);
}
private void game() {
//...
result1.setImageResource(R.drawable.well_done);
result1.setTag(R.drawale.well_done)
result2.setImageResource(R.drawable.try_again);
result2.setTag(R.drawable.try_again);
//...
}
private OnClickListener onClL = new OnClickListener() {
#Override
public void onClick(View v) {
switch(v.getId()){
case R.id.result1:
ic_i = result1;
iv_i.setTag(result1.getTag());
break;
case R.id.result2:
iv_i = result2;
ic_i.setTag(result2.getTag());
break;
}
gotoResource();
}
};
private void gotoResource(){
Integer integer = (Integer) iv_i.getTag();
switch(integer){
case R.drawable.well_done:
//STUFF
break;
case R.drawable.try_again:
//STUFF
break;
case R.drawable.sorry:
//STUFF
break;
}
}
When I run this, I get a nullpointerexception at the line containing iv_i.setTag(result1.getTag());
What did i go wrong? Or how do I go about doing this properly, or another way of doing it that would be easier? Maybe converting the drawable to a string, then doing an if statement for the string, like: (also know this is completely wrong, but its just for a visualisation)
String resource = imgView.getDrawable().toString();
if(resource.equals("R.drawable.well_done")){
//DO SOMETHING
}
Thanks
Instead of relying upon the exact drawable the ImageView has, I suggest setting a tag on the ImageView to indicate which state it's in. I recently used this approach on a similar issue (determining whether a button is active or not), and it works well for me.
private static enum MessageViewType { SUCCESS, TRY_AGAIN, FAILURE }
public void game(){
//STUFF
if (result > 10){
imgView.setImageResource(R.drawable.well_done);}
imgView.setTag(R.id.image_view_status, MessageViewType.SUCCESS); // define image_view_status as a resource
}
//MORE STUFF
}
...
private void gotoResource(){
MessageViewType messageType = (MessageViewType) iv_i.getTag(R.id.image_view_status);
if (messageType == MessageViewType.SUCCESS) {
//STUFF
} else if (messageType == MessageViewType.TRY_AGAIN) {
// STUFF
} else if (messageType == MessageViewType.FAILURE) {
//STUFF
}
}
You can define the image_view_status id in any resource file. I personally use a file res > values > activity_resources.xml as follows:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<item name="image_view_status" type="id" />
</resources>

Roman Nurik's Wizard pager - how to access collected data?

I am trying to make a wizard using Roman Nurik's library (https://plus.google.com/113735310430199015092/posts/6cVymZvn3f4).
I am having trouble accessing the collected data from the Review Fragment.
I made mCurrentReviewItems public in ReviewFragment and then I tried it like this
mNextButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (mPager.getCurrentItem() == mCurrentPageSequence.size()) {
ReviewFragment reviewFragment = (ReviewFragment) mPagerAdapter.getItem(mPager.getCurrentItem());
for (ReviewItem item : reviewFragment.mCurrentReviewItems)
Log.d(MainActivity.TAG, "Item: " + item.getDisplayValue());
}
} else {
if (mEditingAfterReview) {
mPager.setCurrentItem(mPagerAdapter.getCount() - 1);
} else {
mPager.setCurrentItem(mPager.getCurrentItem() + 1);
}
}
}
});
However its always null.
Inside if (mPager.getCurrentItem() == mCurrentPageSequence.size()) { }
For single page variable:
String data = mWizardModel.findByKey("Sandwich:Bread").getData().getString(Page.SIMPLE_DATA_KEY);
For customized page:
String data =
mWizardModel.findByKey(THE_KEY).getData().getString(CustomerInfoPage.YOUR_DATA_KEY);
If you want to assign the data back to the wizard, put this at the end of onCreate in FragmentActivity:
Bundle data = new Bundle();
if (!TextUtils.isEmpty(DATA_STRING)) {
data.putString(Page.SIMPLE_DATA_KEY, DATA_STRING);
mWizardModel.findByKey("Sandwich:Bread"").resetData(data);
}
The key "Sandwich:Bread" is from the example, change whatever suit you. Never try the multi one, I think it is more or less the same.
Sorry for big delay, but I think that someone will found this info useful. I found a way to get all ReviewItems since you can have a lot of branches and you won't be able to use the first answer.
I'm pretty sure, that your mPagerAdapter::getItem code looked like in example (so it just returned new fragment, instead of returning current pager fragment). You have to use instantiateItem to get reference on your ReviewFragment.
Object o = mPager.getAdapter().instantiateItem(mPager, mPager.getCurrentItem());
if(o instanceof ReviewFragment) {
List<ReviewItem> items = ((ReviewFragment) o).getCurrentReviewItems();
if(items != null) {
Log.v(TAG, "Items are: " + items.toString());
}
}
This is my code #Anton_Shkurenko
mNextButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (mPager.getCurrentItem() == mCurrentPageSequence.size()) {
Object o = mPager.getAdapter().instantiateItem(mPager, mPager.getCurrentItem());
if(o instanceof ReviewFragment) {
List<ReviewItem> items = ((ReviewFragment) o).getCurrentReviewItems();
if(items != null) {
Log.v(TAG, "Items are: " + items.toString());
}
}
}
}
});
The best solution is to include this library in your project as module, and implement your own method for getting review items in ReviewFragment.
public List<ReviewItem> getReviewItems() {
return mCurrentReviewItems;
}
I am not sure why developer did not add that. It's the most important thing in project. Choose items and DO something with them.
Anyone still looking for a solution for this issue you can use following code
ArrayList<ReviewItem> reviewItems = new ArrayList<ReviewItem>();
for (Page page : mWizardModel.getCurrentPageSequence()) {
page.getReviewItems(reviewItems);
}

Categories

Resources