next image with text in same time - android

I'm a beginner Android developer. I have list text when clicked next change text in one textview. I created imagelist how can when click next change imageview same time textview
public class Home extends Fragment implements View.OnClickListener {
int stringIdList[] = {R.string.one,R.string.two,R.string.three,R.string.four,R.string.five};
int imageIdList[] = {R.drawable.one,R.drawable.two,R.drawable.three,R.drawable.four,R.drawable.five};
int stringListCounter = 0;
int imageListCounter = 0;
TextView text21;
ImageView image2;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.home, container, false);
text21=(TextView) rootView.findViewById(R.id.dyktext);
image2=(ImageView) rootView.findViewById(R.id.imagedyk);
ImageButton next = (ImageButton)rootView.findViewById(R.id.next);
ImageButton previous = (ImageButton) rootView.findViewById(R.id.previous);
#Override
public void onClick(View v) {
int id = v.getId();
if(id == R.id.next && stringListCounter < stringIdList.length - 1 ) {
stringListCounter++;
}else if (id == R.id.previous && stringListCounter > 0 ) {
stringListCounter--;
}
text21.setText(stringIdList[stringListCounter]);
}
}

Try this if I understood you correctly...
#Override
public void onClick(View v) {
int id = v.getId();
boolean updateText = false,
updateImage = false;
if(id == R.id.next) {
if (stringListCounter < stringIdList.length - 1 ) {
stringListCounter++;
updateText = true;
}
if (imageListCounter < imageIdList.length - 1 ) {
imageListCounter++;
updateImage = true;
}
}else if (id == R.id.previous) {
if (stringListCounter > 0 ) {
stringListCounter--;
updateText = true;
}
if (imageListCounter > 0 ) {
imageListCounter--;
updateImage = true;
}
}
if (updateText) {
text21.setText(stringIdList[stringListCounter]);
}
if (updateImage) {
image2.setImageResource(imageIdList[imageListCounter]);
}
}

I'm not sure, if I understand your question correctly.
You would like to change the image in the ImageView at the same moment as you change yout text in the TextView, right?
In this case, you simply can add the command for setting a image resource below your setText() command.
In your case:
...
text21.setText(stringIdList[stringListCounter]);
image2.setImageResource(imageIdList[stringListCounter]);
Another hint: you should give your variables meaningful names (for example "textViewTitle" or "descriptionTV" ... instead of "text21")
I hope that I could help you!

Related

set background color for imageview doesn't work

I am trying to change ImageView background when clicked(like Duolingo)
Here is my code from fragment :
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
ViewGroup rootView = (ViewGroup) inflater.inflate(
R.layout.frag_repeat, container, false);
final int[] a1 = {0};
final int[] a2 = {0};
final int[] a3 = {0};
final int[] a4 = {0};
TypedArray itemsIcon = getResources().obtainTypedArray(R.array.nav_drawer_icons);
ImageView wer1 = (ImageView) rootView.findViewById(R.id.imageView);
ImageView wer2 = (ImageView) rootView.findViewById(R.id.imageView2);
ImageView wer3 = (ImageView) rootView.findViewById(R.id.imageView23);
ImageView wer4 = (ImageView) rootView.findViewById(R.id.imageView43);
TextView textView1 = (TextView) rootView.findViewById(R.id.textView711);
textView1.setText(ss[i]);
wer1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
a1[0] = 1;
a2[0] = 0;
a3[0] = 0;
a4[0] = 0;
}
});
wer2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
a2[0] = 1;
a1[0] = 0;
a3[0] = 0;
a4[0] = 0;
}
});
wer3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
a3[0] = 1;
a2[0] = 0;
a1[0] = 0;
a4[0] = 0;
}
});
wer4.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
a4[0] = 1;
a2[0] = 0;
a3[0] = 0;
a1[0] = 0;
}
});
if(a1[0] > 0){
wer3.setBackgroundColor(Color.parseColor("#FFFFFF"));
wer2.setBackgroundColor(Color.parseColor("#FFFFFF"));
wer1.setBackgroundColor(Color.parseColor("#42A5F5"));
wer4.setBackgroundColor(Color.parseColor("#FFFFFF"));
} else if(a2[0] > 0){
wer1.setBackgroundColor(Color.parseColor("#FFFFFF"));
wer3.setBackgroundColor(Color.parseColor("#FFFFFF"));
wer2.setBackgroundColor(Color.parseColor("#42A5F5"));
wer4.setBackgroundColor(Color.parseColor("#FFFFFF"));
}else if(a3[0] > 0){
wer1.setBackgroundColor(Color.parseColor("#FFFFFF"));
wer2.setBackgroundColor(Color.parseColor("#FFFFFF"));
wer3.setBackgroundColor(Color.parseColor("#42A5F5"));
wer4.setBackgroundColor(Color.parseColor("#FFFFFF"));
} else if(a4[0] > 0){
wer1.setBackgroundColor(Color.parseColor("#FFFFFF"));
wer2.setBackgroundColor(Color.parseColor("#FFFFFF"));
wer4.setBackgroundColor(Color.parseColor("#42A5F5"));
wer3.setBackgroundColor(Color.parseColor("#FFFFFF"));
}
if(i1 == 1){
wer1.setImageResource(itemsIcon.getResourceId(i, -1));
} else if(i1 == 2){
wer2.setImageResource(itemsIcon.getResourceId(i, -1));
} else if(i1 == 3){
wer3.setImageResource(itemsIcon.getResourceId(i, -1));
} else if(i1 == 4){
wer4.setImageResource(itemsIcon.getResourceId(i, -1));
}
return rootView;
}
But ImageView background doesn't change! I have CardViews inside RelativeLayout and inside RelativeLayout I have ImageView.
Please share xml file. Maybe you don't set background for ImageView in xml
Each of your onClick listeners is just toggling values in an array. I suspect what you really want is to invoke the setBackground color code inside each handler.
From your code, I think what you are trying to do is this:
Whichever button was clicked - change it's background color from white to #42A5F5.
For the other three buttons not clicked, change the background color back to white.
I don't know what the name of your class is (I'll refer to it as "YourClass"). But make your 4 ImageViews member variables.
class YourClass extends Fragment // I'm guessing this is the declaration, it doesn't matter - use whatever you have now
{
ImageView _wer1;
ImageView _wer2;
ImageView _wer3;
ImageView _wer4;
Then inside onCreateView, assign each one of these member variables exactly where you assign the local variables:
_wer1 = (ImageView) rootView.findViewById(R.id.imageView);
_wer2 = (ImageView) rootView.findViewById(R.id.imageView2);
_wer3 = (ImageView) rootView.findViewById(R.id.imageView23);
_wer4 = (ImageView) rootView.findViewById(R.id.imageView43);
Now set each of your click listeners to invoke a "ToggleBackgroundColors". Notice that there's a different index value passed to this function in each callback handler.
_wer1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
YourClass.this.ToggleBackgroundColors(0);
}
});
_wer2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
YourClass.this.ToggleBackgroundColors(1);
}
});
_wer3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
YourClass.this.ToggleBackgroundColors(2);
}
});
_wer4.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
YourClass.this.ToggleBackgroundColors(3);
}
});
Then implement ToggleBackgroundColors:
void ToggleBackgroundColors(int which)
{
int highlight = Color.parseColor("#42A5F5");
ImageView [] views = {_wer1, _wer2, _wer3, _wer4};
for (int x = 0; x < views.length; x++)
{
int background = (which == x) ? highlight : Color.WHITE;
views[x].setBackgroundColor(background);
}
}
In your onClickListener, you can also set colorFilter on your imageView when you onClick it.
Consider using the onTouchListener instead though as you would like the imageView to change color when you touch the button and not when you click on it.
ImageView iv = (ImageView)findViewById(resIdOfImageToFilter);
iv.setColorFilter(Color.RED, PorterDuff.Mode.SRC_ATOP);

Changing EditText to default string

hello guys seriously am new to android and please i would like you to help me with this code...to edit it
public class LoginFragment extends Fragment implements OnClickListener {
private EditText login, password, domain;
private ImageView apply;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.setup_generic_login, container, false);
login = (EditText) view.findViewById(R.id.setup_username);
password = (EditText) view.findViewById(R.id.setup_password);
domain = (EditText) view.findViewById(R.id.setup_domain);
apply = (ImageView) view.findViewById(R.id.setup_apply);
apply.setOnClickListener(this);
return view;
}
#Override
public void onClick(View v) {
int id = v.getId();
if (id == R.id.setup_apply) {
if (login.getText() == null || login.length() == 0 || password.getText() == null || password.length() == 0 || domain.getText() == null || domain.length() == 0) {
Toast.makeText(getActivity(), getString(R.string.first_launch_no_login_password), Toast.LENGTH_LONG).show();
return;
}
SetupActivity.instance().genericLogIn(login.getText().toString(), password.getText().toString(), domain.getText().toString());
}
}}
i'd like to assign domain a default string, (like domain = "pearlcom.au.ug")
i've edited the xml file and removed the EditText field
please don't close my post.......just ignore!
thanks for those who are goin to help me!
you can just do:
android:text="#string/domain" and assign whatever domain string you choose in the strings.xml and you would have that as default.
-> If you want a placeholder displayed, please include
android:hint="#string/enter_domain"
Simply do this in onCreate():
domain.setText("pearlcom.au.ug");

How to iterate through a set of buttons in Android Studio?

I'm trying to make my first app which has a set of 15 buttons. when you press a button the color changes between two numbers.Then if you press a specificity different "commit" button the buttons won't change colors any more.
My question right now is how would I be able to iterate through the buttons on the screen? I believe I need a assign the buttons a "name", "type", or something like it, and then find all instances where that happens but I cannot find the relevant getter/setter methods.
Here is my code so far:
public void clickHandler(View view) {
Button btn = (Button) view;
int id = btn.getId();
boolean clicked = isChosen(btn);
if( clicked == true && id != 1) {
btn.setBackgroundColor(-3355444);
}
else{
btn.setBackgroundColor(-16777216);
}
}
public boolean isChosen(Button btn){
ColorDrawable buttonColor = (ColorDrawable) btn.getBackground();
int colorId = buttonColor.getColor();
if(colorId == -16777216){
return true;
}
else return false;
}
public boolean player = true;
public void changeTurn(View view) {
TextView t = (TextView) findViewById(R.id.textView3);
if(player == false) {
t.setText("Player 2's turn");
player = true;
}
else{
t.setText("Player 1's turn");
player = false;
}
Hope this piece of code helps
final int buttonCount = 15;
LinearLayout mLayout = (LinearLayout) findViewById(R.id.pager);
for (int i = 0; i < buttonCount; i++) {
TextView textView = new TextView(this);
textView.setTag(String.valueOf(i));
textView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
int buttonID = Integer.valueOf(view.getTag().toString());
updateColorForButtonAtPosition(buttonCount);
}
});
mLayout.addView(textView);
}
private void updateColorForButtonAtPosition (int buttonCount){
// add your functionality here
}

Scrolling page in ViewPager works very slow

Scrolling page in ViewPager on HTC Desire X is very slow, on Samsung Galaxy S4 is ok.
In my app i have view pager which display nine items on every page.
Each item has: name, small image(25kb-50kb, 500x350), and short descriptions.
Each image is downloaded from url via Universal Image Loader
When i set to adapter more than 20 items viewpager works slow, and i had warnings:
Skipped XX frames! The application may be doing too much work on its main thread.
Here is my instantiateItem() code from my PagerAadapter:
public Object instantiateItem(ViewGroup container, final int position) {
LayoutInflater inflater = (LayoutInflater) mActivity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View itemsView = inflater.inflate(R.layout.item_pager, container,
false);
LinearLayout[] line = new LinearLayout[3];
line[0] = (LinearLayout)itemsView.findViewById(R.id.item_pager_first_line);
line[1] = (LinearLayout)itemsView.findViewById(R.id.item_pager_second_line);
line[2] = (LinearLayout)itemsView.findViewById(R.id.item_pager_third_line);
int i = 0;
for(LinearLayout layout : line){
TextView[] itemsName = new TextView[3];
LinearLayout[] itemsLayout = new LinearLayout[3];
itemsLayout[0] = (LinearLayout)layout.findViewById(R.id.item1_layout);
itemsLayout[1] = (LinearLayout)layout.findViewById(R.id.item2_layout);
itemsLayout[2] = (LinearLayout)layout.findViewById(R.id.item3_layout);
final ImageView[] itemsImage = new ImageView[3];
TextView[] desc = new TextView[3];
final int objectId1 = (position*9)+(i*3);
final int objectId2 = (position*9)+(i*3)+1;
final int objectId3 = (position*9)+(i*3)+2;
if(mItemsList.size()>objectId1){
// Set appropriate name of item.
itemsName[0] = (TextView)layout.findViewById(R.id.item1_name);
itemsName[0].setText(mItemsList.get(objectId1).getName());
//Set appropriate image of item
itemsImage[0] = (ImageView)layout.findViewById(R.id.item1_image);
mImageLoader.displayImage(Image.getRegularUrl(mItemsList.get(objectId1)
.getImages()[0].getFilename()),itemsImage[0],mOptions);
desc[0] = (TextView)layout.findViewById(R.id.item1_desc_text_view);
desc[0].setText(mItemsList.get(objectId1).getDesc()));
//Set action after click on item.
itemsLayout[0].setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
onMyClikItem(mItemsList.get(objectId1).getItemId());
}
});
//Set action after long click on item.
itemsLayout[0].setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
onMyLongClikItem(objectId1);
return true;
}
});
// Check next item.
if(mItemsList.size()>objectId2){
itemsName[1] = (TextView)layout.findViewById(R.id.item2_name);
itemsName[1].setText(mItemsList.get(objectId2).getName());
itemsImage[1] = (ImageView)layout.findViewById(R.id.item2_image);
mImageLoader.displayImage(Image.getRegularUrl(mItemsList.get(objectId2)
.getImages()[0].getFilename()),itemsImage[1],mOptions);
desc[1] = (TextView)layout.findViewById(R.id.item2_desc_text_view);
desc[1].setText(mItemsList.get(objectId1).getDesc()));
itemsLayout[1].setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
onMyClikItem(mItemsList.get(objectId2).getItemId());
}
});
itemsLayout[1].setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
onMyLongClikItem(objectId2);
return true;
}
});
if(mItemsList.size()>objectId3){
itemsName[2] = (TextView)layout.findViewById(R.id.item3_name);
itemsName[2].setText(mItemsList.get(objectId3).getName());
itemsImage[2] = (ImageView)layout.findViewById(R.id.item3_image);
mImageLoader.displayImage(Image.getRegularUrl(mItemsList.get(objectId3)
.getImages()[0].getFilename()),itemsImage[2],mOptions);
desc[2] = (TextView)layout.findViewById(R.id.item3_desc_text_view);
desc[2].setText(mItemsList.get(objectId1).getDesc()));
itemsLayout[2].setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
onMyClikItem(mItemsList.get(objectId3).getItemId());
}
});
itemsLayout[2].setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
onMyLongClikItem(objectId3);
return true;
}
});
} else {
itemsLayout[2].setVisibility(View.INVISIBLE);
}
} else {
itemsLayout[1].setVisibility(View.INVISIBLE);
itemsLayout[2].setVisibility(View.INVISIBLE);
}
}else {
layout.setVisibility(View.INVISIBLE);
}
i++;
}
((ViewPager) container).addView(itemView);
return itemsView;
}
What should i change to speed up my ViewPager.
There are a lot of things you can do to improve performance in Android. Try to put all the loading etc in a AsycTask (http://developer.android.com/reference/android/os/AsyncTask.html).
Also there is documentation about how to optimise preformance: http://developer.android.com/training/articles/perf-tips.html. When I read your code I saw a lot of difficult to read code and a lot of repetetive code. Try to rethink the way you handeld the situation.

Emulator doesn't give Error but Device gives java.lang.NullPointerException on putExtra()

I have a big problem please someone help me please.
I have an ActionBar with Tabs and 3 Fragments.
In onCreateView method i have this:
MainActivity.java
...
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = null;
TextView textView;
final Intent intent = new Intent(getActivity(),ResultActivity.class);
if(getArguments().getInt(ARG_SECTION_NUMBER) == 1){
rootView = inflater.inflate(R.layout.fragment_main, container, false);
Button btn = (Button) rootView.findViewById(R.id.procura_ingredientes);
final EditText ing_1 = (EditText) rootView.findViewById(R.id.ingredient_1);
final EditText ing_2 = (EditText) rootView.findViewById(R.id.ingredient_2);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
intent.putExtra(MOD_SEARCH,1);
String ingredient_1 = ing_1.getText().toString();
String ingredient_2 = ing_2.getText().toString();
if(ingredient_1.length() <= 0) ingredient_1 = "";
if(ingredient_2.length() <= 0) ingredient_2 = "";
intent.putExtra("ingredient_1",ingredient_1);
intent.putExtra("ingredient_2",ingredient_2);
startActivity(intent);
}
});
}else if(getArguments().getInt(ARG_SECTION_NUMBER) == 2){
rootView = inflater.inflate(R.layout.fragment_two, container, false);
GridView gridview = (GridView) rootView.findViewById(R.id.gridview);
final ImageAdapter imageAdapter = new ImageAdapter(getActivity());
gridview.setAdapter(imageAdapter);
gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
//Toast.makeText(getActivity()," " + position, Toast.LENGTH_SHORT).show();
long cat = imageAdapter.getItemId(position);
if(cat <= 0) cat = 1;
intent.putExtra(MOD_SEARCH,2);
intent.putExtra("cat_id",cat);
startActivity(intent);
}
});
}else if(getArguments().getInt(ARG_SECTION_NUMBER) == 3) {
rootView = inflater.inflate(R.layout.fragment_three, container, false);
final EditText editText = (EditText) rootView.findViewById(R.id.recipe_search_term);
Button button = (Button) rootView.findViewById(R.id.submit_recipe_search);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
int cont = editText.getText().toString().length();
if (cont < 2) {
AlertDialog.Builder dialog = new AlertDialog.Builder(getActivity());
dialog.setTitle("Atenção");
dialog.setMessage("O nome da receita precisa ter mais de " + cont + " letras ");
dialog.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.dismiss();
}
});
dialog.show();
} else {
intent.putExtra(MOD_SEARCH,0);
String recipe = editText.getText().toString();
if(recipe.length() <= 0) recipe = "";
intent.putExtra("recipe_name",recipe);
startActivity(intent);
}
}
});
}
return rootView;
/* textView.setText(Integer.toString(getArguments().getInt(ARG_SECTION_NUMBER)));*/
}
}
If you look you see
if(getArguments().getInt(ARG_SECTION_NUMBER) == 1){
}else if(getArguments().getInt(ARG_SECTION_NUMBER) == 2){
else
this manage what tab user are in, all go to same ResultActivity but pass diferent ExtraStrings,
intent.putExtra(MOD_SEARCH,0);
intent.putExtra(MOD_SEARCH,1);
intent.putExtra(MOD_SEARCH,2);
the MOD_SEARCH control what Result was execute.
ResultActivity.java
public static final String MOD_SEARCH = null;
mode = getIntent().getIntExtra(MOD_SEARCH,0);
if(mode == 0){ ..
}else if(mode == 1) ..
}else if(mode == 2) ..
So, in emulator it works fine all, when i click in in each fragment it shows me the correct information.
After publishing app to Google Play few days ago and i get this errors:
java.lang.NullPointerException
at android.content.Intent.putExtra(Intent.java:5558)
at com.iondev.cozinhaparasolteiros.MainActivity$PlaceholderFragment$2.onItemClick(MainActivity.java:256)
and
java.lang.NullPointerException
at android.content.Intent.putExtra(Intent.java:5325)
at com.iondev.cozinhaparasolteiros.MainActivity$PlaceholderFragment$1.onClick(MainActivity.java:226)
I dont understand because in emulator it works without error and device have error, i don't know what is wrong, please someone have a suggestion, need repair because are in store.
Replace this
public static final String MOD_SEARCH = null;
for this
public static String MOD_SEARCH = "";
I think your problem is with the word "final" as initializing the variable with "null" being kind "final" will not be able to change its value later, so to assign to the Intent is Null.
I hope you serve.
Sorry for my low level of English.
You have not initialized Intent any where in your code because it giving NullPointerException here..
intent.putExtra(MOD_SEARCH,2);
intent.putExtra("cat_id",cat);
take it as local and initilize where ever you want..

Categories

Resources