I'm writing a simple application where a Japanese character is displayed in the middle of the screen as a TextView with 4 buttons(home, shuffle, sound, and next) on the bottom. When the next button is pressed, the next character will be displayed in the TextView.
The application works perfectly on the eclipse simulator and my Samsung Galaxy S2 (Android 4.0.4). However, on my LG G2 (Android 4.4.2), every now and then (maybe once every 30 chars), I get a blank TextView when I click the next button.
I did the following experiments:
1) I added some debugging code to dump the value of the character on a Toast before and after the call to setText(), and the correct value is displayed on the Toast.
2) I pressed the 'sound' button when the TextView is blank and verified that the sound button is associated with the correct character.
3) I added one second delay right after the call to setText() to see if I was overwriting the TextView after setting the right character. In which case, I expected the correct character to be set in the TextView first, and then it becomes blank after the one second delay. However, I saw the blank TextView as soon as I clicked the next button.
So, I think, maybe prematurely, setText() is clearing the TextView somehow.
Does anybody know what the problem might be?
Part of my code looks like this:
public class KatakanaFragment extends Fragment {
private ImageButton mNextButton;
private TextView mCharTextView;
private void updateChar() {
int strId;
Katakana[] activeKatakana;
activeKatakana = KatakanaTable.getActiveChars();
if ((activeKatakana == null) || (activeKatakana.length == 0)) {
Toast.makeText(getActivity(), "No Char Selected", Toast.LENGTH_SHORT).show();
return;
}
if (activeKatakana.length <= KatakanaTable.sCurrentIndex) {
Toast.makeText(getActivity(), "Current Index Too Big", Toast.LENGTH_SHORT).show();
return;
}
strId = activeKatakana[KatakanaTable.sCurrentIndex].getCharId();
mCharTextView.setTextSize(getResources().getDimension(R.dimen.textsize));
mCharTextView.setText(strId);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup parent,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.activity_katakana, parent, false);
mCharTextView = (TextView)v.findViewById(R.id.char_text_view);
mNextButton = (ImageButton)v.findViewById(R.id.next_button);
mNextButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (KatakanaTable.getActiveChars().length == 0) {
Intent i = new Intent(getActivity(), MainActivity.class);
startActivity(i);
return;
}
KatakanaTable.sCurrentIndex = (KatakanaTable.sCurrentIndex + 1) % KatakanaTable.getActiveChars().length;
updateChar();
}
});
updateChar();
return v;
}
}
Related
I am creating a project in Android Studio, in which there is a small home screen with a button. When a user clicks this button on the home screen, the application must go to another screen, which contains several checkboxes.
However, I am having two problems:
The code accesses the main page. But when you click on a button to go to another page, the application leaves the screen instead of going to another page. I already put the name of the method that is on another page, which I want you to execute, in the onclick attribute but it still doesn't work.
I would like that after a user clicks on a button that is on the home screen, opens a screen that contains several checkboxes (this screen is already made). Each check box contains a value. When a user clicks on a checkbox, I created a variable that adds up.
When the user clicks on the checkboxes the user wants, and after the user clicks the send button, the sum of the checkbox values will appear in a textView.
This is the checkbox code:
int result = 0;
private CheckBox blue;
private CheckBox red;
private CheckBox yellow;
private CheckBox green;
private CheckBox black;
private CheckBox white;
private CheckBox orange;
private CheckBox purple;
private CheckBox gray;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.id.interfacegraphicalname);
blue = findViewById(R.id.blue);
red = findViewById(R.id.red);
yellow = findViewById(R.id.yellow);
green = findViewById(R.id.green);
black = findViewById(R.id.black);
white = findViewById(R.id.white);
orange = findViewById(R.id.orange);
purple = findViewById(R.id.purple);
gray = findViewById(R.id.gray);
testFinalResult(result);
}
public void check() {
if (blue.isChecked()) {
result += 30;
} else if (red.isChecked()) {
result += 30;
} else if (green.isChecked()) {
result += 30;
} else ig (yellow.isChecked()) {
result += 20;
} else if (black.isChecked()) {
result += 20;
} else if (white.isChecked()) {
result +=20;
} else if (orange.isChecked()) {
result += 10;
} else if (purple.isChecked()) {
result += 10;
} else if (gray.isChecked()) {
result +=10;
}
int testResult = check();
public void finalTestResult(int testResult) {
if (testResult <= 60) {
finalResultText.setText("low value");
} else if (testResult <= 120) {
finalResultText.setText("medium value");
} else if (testResult<= 180) {
finalResultText.setText("high value");
}
}
}
But as the code contains one more error that when you click a button on the home screen, the application is not going to the next screen for some reason, I would like to show you all the code (which is too much big to insert here on Stack OverFlow) for you to check out what is going wrong, that I'm not able to run the code normally at all, ok?
Please, could you check?
I've been working on this project for a long time, and I'm not getting the code to run normally on the smartphone.
The code link is:
https://github.com/AvaianoFC1/OficialTestProject
/Checked out your code on github here is my initial findings
hopefully it will help you resolve your issue in your Calc.java class you need to setcontentview to entries layout instead of referring back to main layout*/
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView(R.layout.activity_main);
setContentView(R.layout.entries);
you are referring back to main activity layout which only has the layout for one button, so your button is working but its taking you back to activity main - you need to display entries layout and get all your values from that layout and then move ahead accordingly
another error from my inital lookup that i noticed is this
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#android:color/holo_blue_bright"
android:onClick="sendMessage2"
android:text="OPEN PAGE 2"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.647"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.237" />
android:onClick="sendMessage2" -> you are calling the sendMessage2 function which you have not defined you need to call the openPage2 function
also you do not need the openPage1 and openPage2 in your main activity class
Let me know if this helps you these are the few errors that are flatout - also i would recommend adding a snackbar or a toast message on button click before implementing the functionality just to make sure thats its working as intended
I have a listview and in each cell it has a RelativeLayout with 7 buttons.
before the list is scrolled all the buttons work fine (all trigger when clicked) for all visible listView items, but after listView was scrolled some items turn to not clickable (no matter which button in the item I click), and it's random, after another scroll the same item can turn clickable, and other which was before turns to not clickable.
I have noticed that it usually happens (item turns not clickable) after scrolling all the way up.
Another thing that i have noticed that seldom (after 4-5 unsuccessful clicks in a row) the button triggers a few times in a row (like it was delayed). But usually it's not happening after a number of unsuccessful clicks.
In my original code I created an arrayList of RelativeLayouts (each for listView Item), and put the arrayList into adapter. For every 7 buttons (for each cell) I set 7 ids corresponding to their's place in arraylist.
In that way I implemented the OnClick event in the main class.
Here is 3 buttons (out of 7):
for (int i = 0; i < EXPEND_BUTTONS.length; i++) {
if (view.getId() == EXPEND_BUTTONS[i]) {
handleEmojiPanel(i);
break;
}
if (view.getId() == BUTTONS[i] || view.getId() == IMAGES[i]) {
ShowTopItem item = new ShowTopItem(getActivity(), i);
item.show();
break;
}
}
Because of the problem I change the code.
I handled the OnClick event for the buttons in the adapter itself in the getView method (for 2 buttons only):
public View getView(final int position, View convertView, ViewGroup parent) {
pos = position;
Button btn = (Button) listOfObjects.get(position).getChildAt(0);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
ShowTopItem item = new ShowTopItem(getActivity(), position + listChosen);
item.show();
}
});
Button imageBtn = (Button) listOfObjects.get(position).getChildAt(2);
imageBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
ShowTopItem item = new ShowTopItem(getActivity(), position + listChosen);
item.show();
}
});
return listOfObjects.get(position);
}
I have the same result. Nothing changed.
I have looked all over the internet, and it seems that I'm the only one who encountered such issue.
Id anybody knows what can be the issue here?
If some other code is needed, please feel free to ask.
I did not find the reason, but I changed ListView to ScrollView, and all works fine now.
Maybe there is some kind of bug in ListView, but in this case, I wonder why I did not find any complains regarding it.
Anyway, works perfect with ScrollView.
I am doing bubble display of selected contacts. Each telephone bubble is a LinearLayout, which contains ImageView and TextView. These bubbles are then displayed in another LinearLayout which is child of HorizontalScrollView.
It child/parent tree looks like this:
- HorizontalScrollView
|- LinearLayout (id="#+id/telField")
|- LinearLayout (id="#+id/telBox") <- is programmatically added to parent
|- TextView (id="#+id/telNumber")
|- ImageView (id="#+id/delNumber")
In my .java class I call this method to display "telBox" LinearLayout in "telField" LinearLayout:
public void createAdapter() {
telList = new ArrayAdapter<>(this, R.layout.text_buble, R.id.telNumber, telNumList);
telField = (LinearLayout) findViewById(R.id.telField);
telField.removeAllViews();
final int adapterCount = telNumList.size();
for (ik = 0; ik < adapterCount; ik++) {
final View item = telList.getView(ik, null, null);
telField.addView(item);
item.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
telField.removeView(item);
telNumList.remove(ik-1);
telList.notifyDataSetChanged();
refresh();
}
});
}
}
Method refresh(); – is custom method which helps to "reload" Activity: it gets App values, refreshes warning ImageViews and cals createAdapter() method.
Big button "SELECT" calls an Intent which returns a selected phone number from a contacts book. I call this code to update my LinearLayout with id "telField":
telNumList.add(cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.NUMBER)));
createAdapter();
Problem I face is:
After I click on LinearLayout with id "telNumber" it one by one deletes every bubble (no matter which I clicked) until it reaches first added bubble. It also crashes 50/50 when reaches first added element, I have not figured out a dependency. The error it returns is "out of bounds error", so I think it is connected with ik - 1 line.
My question is: How do I better construct my ArrayAdapter?
In your code you are trying to remove the view by ik which is getting change continuously because of which your coding is removing last view I have modified your code as given below
for (ik = 0; ik < adapterCount; ik++) {
final int position=ik;
final View item = telList.getView(ik, null, null);
telField.addView(item);
item.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
telField.removeView(item);
telNumList.remove(position);
telList.notifyDataSetChanged();
//refresh();
createAdapter();
}
});
}
Here position will help you to remove the particular view which you want to remove. I hope this is what you are asking for.
Inside your for loop, write these line of code:
for (ik = 0; ik < adapterCount; ik++) {
final View item = telList.getView(ik, null, null);
item.setTag(ik);
telField.addView(item);
item.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
int index = Integer.parseInt(v.getTag().toString());
telNumList.remove(index);
refresh();
}
});
}
Hope it will help you out.
I'm creating a chat application. I want to show the time when user clicks on message
. This feature is working with this code in getView() of BaseAdapter
holder.txt_msg_user.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v)
{
if (holder.txt_date_user.isShown() && holder.txt_AM_user.isShown())
{
holder.usertimelayout.setVisibility(View.GONE);
holder.txt_date_user.setVisibility(View.GONE);
holder.txt_AM_user.setVisibility(View.GONE);
}
else
{
holder.usertimelayout.setVisibility(View.VISIBLE);
holder.txt_date_user.setVisibility(View.VISIBLE);
holder.txt_AM_user.setVisibility(View.VISIBLE);
}
}
});
But i want to add one more feature, like in the below image if i click on every message every message is showing its time, but i want to achieve one thing like skype, if user sees the time of any message then first it will check for the previous messages. If any previous message is showing the time then it should be hide & recently tapped on message by the user should be shown.Please help how it can be possible. This is the problem: (see image)
Since you're achieving the time already and it's being added to each list item, perhaps you could just set the last item in your listview to View.VISIBLE and the rest to View.GONE.
You keep a property like:
View selectedMessage;
Where you set when the user clicks in some message, and verify it:
...
#Override
public void onClick(View v) {
if (selectedMessage != null && v != selectedMessage) {
Holder h = (Holder) selectedMessage.getTag();
h.hideTime();
h = (Holder) v.getTag();
h.showTime();
selectedMessage = v;
}
...
}
...
It worth noting that this Holder interface would be something that you have created, implementing the methods showTime and hideTime. This is not Android built-in.
I have a custom adapter that renders two (2) checkboxes, a picture and the name of the client. All the information needed for the adapter is fetched from an ArratList that contains the Client class.
Every row needs to have both checkboxes checked (selected) for the client in order to process the purchase order, in case that a particular client has one checkbox checked-off and the other checkbox not, that raises a flag as MISMATCH. To make a valid order both checkboxes need to be checked-off.
We are implementing a button for verification, which will find any mismatch in the adapter and then hightlight the mismatches.
EDITION: After pressing the verificationBtn I am able to identify if any row has mismatch on checkboxes, for example, if checkbox1 was checked and checkbox2 not. that will mark the row as mismatch. I am using the position of my checkboxes based on clientList that is an arraylist of List clientList.
QUESTION: How can I get the position that the viewHolder has in order compare against the clientList position? Is there any way I can force the viewHolder to store the position and get it back and make the comparison with cli.getClient_number() ?
So far I have tested two different ways with no luck:
Method 1:
viewHolder.clientName.setBackgroundColor((Interger.parseInt(cli.getClient_number()) ) == position ? Color.GREEN : Color.TRANSPARENT);
Method 2
viewHolder.clientName.setBackgroundColor(Color.GREEN);
here the code that I am implementing.
// This goes in my main Client Activity
Button verificationBtn = (Button) findViewById(R.id.verificationBtn);
verificationBtn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
buffer.setLength(0);
mismatchTv.setText("");
for (Client cli : clientList) {
if (cli.isCheckboxOneSelected() != cli.isCheckboxTwoSelected()) {
//We had defined above the following buffer = new StringBuffer();
buffer.append((ah.parseInt(cli.getClient_number(), 0) - 1) + ", ");
cli.setMismatch(true);
//We are passing here the ID that correspond to the client mismatch
list_adapter.setBackgroundColor(Integer.parseInt(cli.getClient_number()) - 1);
setListAdapter(list_adapter);
Log.w("cli.getClient_number() ", String.valueOf(Integer.parseInt(cli.getClient_number()) - 1));
}
}
// We display any mismatch on a TextView on top of the screen
if (buffer.length() != 0) {
//This is a TextView on top of the screen
mismatchTv.setText("Error en Client(s) "
+ buffer.toString());
}
// This goes inside of the ClientArrayAdapter
public void setBackgroundColor(int position) {
Log.w("inside of setBackgroundColor method", "True");
switchIndex = 1;
positionFetched = position;
}
// This goes inside of the ClientArrayAdapter
// and inside the body of public View getView(int position, View convertView, ViewGroup parent) {
switch (switchIndex) {
case 1:
viewHolder.cbone
.setButtonDrawable(R.drawable.btn_checkbox_selector);
viewHolder.cbtwo
.setButtonDrawable(R.drawable.btn_checkbox_selector);
Log.w("switch 1 was called ", "True");
for (Client cli : clientList) {
if (cli.isCheckboxOneSelected() != cli.isCheckboxTwoSelected()) {
Client cli = getItem(positionFetched);
if (cli.isMismatch()) {
cli.setColor(Color.BLACK);
Log.e("if (cli.isMismatch()) ", "");
//HERE WE ARE TRYING TO HIGHLIGHT THE ROW WITH MISMATCH
//WHY THIS LINE DOES NOT WORK?
//THE ISSUE THAT I AM GETTING IS THAT I CANNOT CONTROL WHAT ROW TO AFFECT
//IN THE VIEW HOLDER
viewHolder.clientName.setBackgroundColor(Color.GREEN);
}
}
}
break;
default:
viewHolder.cbone.setButtonDrawable(R.drawable.disabled_cb);
viewHolder.cbtwo.setButtonDrawable(R.drawable.disabled_cb);
break;
}
// This goes inside of the ClientArrayAdapter
static class ViewHolder {
public TextView clientName;
public TextView clientNumber;
public ImageView imageView;
public CheckBox cbtwo;
public CheckBox cbone;
public int position;
}
After three days trying to figure out what's wrong with this code, I finally found the solution moving the Method #1 just at the very end of the getView method. :-)