I am trying to use a badge in android by exploiting https://github.com/jgilfelt/android-viewbadger
To this end I am inflating a TextView like
TextView target = (TextView) rowView.findViewById(R.id.menu_item_badge);
BadgeView badge = new BadgeView(this.getContext(), target);
badge.setText("1");
int droidGreen = Color.parseColor("#A4C639");
badge.setBadgeBackgroundColor(droidGreen);
badge.show();
The problem is I can't see the badge
Related
I'm testing my android app with espresso and want to test the ZoomControls like this:
onView(withId(R.id.zoomIn)).perform(click());
But I don't get the zoomIn/zoomOut id, from the Zoom_Controls. In the Layout Component Tree, the zoomIn and zoomOut are visible.
Can someone help me?
Thanks in advance.
Resources res = Resources.getSystem();
int id = res.getIdentifier("btn_zoom_down", "drawable", "android");
Didn't work.
Got it working with:
Resources res = Resources.getSystem();
int id = res.getIdentifier("#id/zoomOut", "drawable", "android");
onView(withId(id)).perform(click());
I'm currently finishing my game app for android, but I have encountered one problem that keeps crashing my app.
I was trying to place the text from strings.xml inside the toast message and my code looks like this:
// THIS IS THE PART THAT DOES NOT WORK:
int stringrank = getResources().getIdentifier("rank"+points, "string", this.getPackageName());
rank = getString(stringrank);
// I want to get different rank String depending on the collected points,
// so it would be rank1, rank2, rank3 and so on - thats why "rank"+points.
// It worked for me in the different parts of the code, but when I want to
// use it in the Toast Message it does not.
LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.done_toast, (ViewGroup) findViewById(R.id.done_toast));
TextView text = (TextView) layout.findViewById(R.id.text);
// APP CRASHES when I use "rank" String here, if I place other text it works just fine.
text.setText(rank);
final Toast donetoast = new Toast(getApplicationContext());
donetoast.setGravity(Gravity.BOTTOM|Gravity.CENTER_HORIZONTAL, 0, 35);
donetoast.setDuration(Toast.LENGTH_SHORT);
donetoast.setView(layout);
// Rest of the Toast Message code is below, not important.
(...)
When I place the normal text in the Toast Message (does not matter whether in Java or in the done_toast.xml) it works perfectly, but if I want to call the String from strings.xml the app crashes.
Thank you for your help in advance!
You can't create a Toast object like that. It must be done using Toast.makeText().
Try this
final Toast donetoast=Toast.makeText(getApplicationContext());
donetoast.setDuration(Toast.LENGTH_SHORT);
donetoast.setView(layout);
I am currently creating an Android app that I want to support multiple screen sizes/densities. When I set up layouts in xml, everything looks fine across different screen sizes. However, there are rows I need to add programatically.
Whenever I add the rows, I can't seem to get things to look consistent across different devices. I believe using the dp unit when settings up heights, widths in xml, along with wrap_content and match_parent when appropriate, have allowed things to easily translate between different devices.
Programatically, when I try to set a layout height/width, I have been trying to convert the anticipated dp value to a pixel value. I've done so using this:
public static int convertToPixels(int value) {
Resources resources = mContext.getResources();
int x = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,value,resources.getDisplayMetrics());
return x;
}
Overall, the heights look ok, but the widths don't look good. For instance, the width of the row will look such that on a smaller device, the information neatly displays across the row, which is what I want. However,when I try to run the app on a tablet, for instance, the information will only stretch to half of the row, which doesn't look good. I want the sizes to scale and neatly display just like on the smaller device.
If anyone has any idea what my problem might be, I would greatly appreciate it. Below is the source for adding a row using java:
LinearLayout row = new LinearLayout(mContext);
row.setOrientation(LinearLayout.HORIZONTAL);
row.setId(Integer.parseInt(txn.getId().toString()));
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, convertToPixels(60));
params.setMargins(0, convertToPixels(1), 0, 0);
row.setLayoutParams(params);
row.setBackgroundColor(mContext.getResources().getColor(R.color.white));
LinearLayout imageLayout = new LinearLayout(mContext);
LinearLayout.LayoutParams imageParams = new LinearLayout.LayoutParams(convertToPixels(40), convertToPixels(40));
imageParams.gravity = Gravity.CENTER;
imageLayout.setLayoutParams(imageParams);
ImageView image = new ImageView(mContext);
if (txn.getTransactionStateID() == Constants.TXN_STATUS_OK) {
image.setImageDrawable(mContext.getResources().getDrawable(R.drawable.ok));
} else if (txn.getTransactionStateID() == Constants.TXN_STATUS_SUSPICIOUS) {
image.setImageDrawable(mContext.getResources().getDrawable(R.drawable.alert));
} else if (txn.getTransactionStateID() == Constants.TXN_STATUS_RED_FLAG) {
image.setImageDrawable(mContext.getResources().getDrawable(R.drawable.flag));
}
imageLayout.addView(image);
row.addView(imageLayout);
LinearLayout txnMiddleLayout = new LinearLayout(mContext);
txnMiddleLayout.setOrientation(LinearLayout.VERTICAL);
LinearLayout.LayoutParams txnTopParams = new LinearLayout.LayoutParams(convertToPixels(400), convertToPixels(60));
txnTopParams.setMargins(convertToPixels(10), 0, 0, 0);
txnMiddleLayout.setLayoutParams(txnTopParams);
TextView txnTopContents = new TextView(mContext);
txnTopContents.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, convertToPixels(30)));
txnTopContents.setText(txn.getTopLineContents());
txnTopContents.setTextColor(Color.BLACK);
// txnTopContents.setTextSize(convertToPixels(16));
TextView txnBottomContents = new TextView(mContext);
txnBottomContents.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, convertToPixels(30)));
txnBottomContents.setText(txn.getBottomLineContents());
// txnBottomContents.setTextSize(convertToPixels(12));
txnMiddleLayout.addView(txnTopContents);
txnMiddleLayout.addView(txnBottomContents);
row.addView(txnMiddleLayout);
LinearLayout txnBottomLayout = new LinearLayout(mContext);
txnBottomLayout.setOrientation(LinearLayout.VERTICAL);
LinearLayout.LayoutParams txnBottomParams = new LinearLayout.LayoutParams(convertToPixels(120), convertToPixels(60));
txnBottomLayout.setLayoutParams(txnBottomParams);
TextView amount = new TextView(mContext);
amount.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, convertToPixels(30)));
amount.setText(txn.getAmountStr());
amount.setTextColor(Color.BLACK);
// amount.setTextSize(convertToPixels(16));
TextView date = new TextView(mContext);
date.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, convertToPixels(30)));
date.setText(txn.getDateStr());
// date.setTextSize(convertToPixels(12));
txnBottomLayout.addView(amount);
txnBottomLayout.addView(date);
row.addView(txnBottomLayout);
txnList.addView(row);
I eventually found a solution. Instead of trying to set an exact width value, I set the width of the layout or textview to 0, and used layout_weight instead.
https://stackoverflow.com/a/3996104/4056947
I am making an android app and currently stuck on signup page.
Trying to enter values in database using parse and storing the database on parse.com
The values are entered in EditText box and are having some id say "name"
On using that for database, my java file includes command like
parseObject text=new parseObject("Signup");
text.put("Key_id_name", R.id.name);
//Due to this, id of EditText box is coming in database.
Kindly help me how to get the value entered in that.
You should do something like this:
TextView view = (TextView) findViewById(R.id.name);
parseObject text=new parseObject("Signup");
text.put("Key_id_name", view.getText().toString());
I guess your view has a TextView with the id R.id.name?
In that case you should use findViewById(R.id.name) in your activity code
See these docs:
http://developer.android.com/reference/android/app/Activity.html#findViewById(int)
http://developer.android.com/reference/android/widget/TextView.html#getText()
Something like this:
TextView idText = (TextView) findViewById(R.id.name);
String idString = idText.getText().toString();
text.put("Key_id_name", idString);
Good luck =)
Use getText() method of EditText class as below.
EditText field = (EditText) findViewById(R.id.name);
String fieldValue = field.getText().toString();
Then add it to your map
text.put("Key_id_name", fieldValue);
R.id.name will simply give you the generated Id and not the value.
I have an imageView that i want to display a little icon of the country that you are currently in. I can get the country code, but problem is i can't dynamically change the imageView resource. My image files are all lowercase
String lowerCountryCode = countryCode.toLowerCase();
String resource = "R.drawable." + lowerCountryCode;
img.setImageResource(resource);
int id = getResources().getIdentifier(lowerCountryCode, "drawable", getPackageName());
setImageResource(id);
abve is my code.
Now, of course this will not work because set Image Resource wants an int, so how can i do this? Thanks in advance
I mean you know the name of files(Drawable) which you wanna use for one and other country.
ImageView imgView=(ImageView)findViewById(R.id.imageView1);
int id = getResources().getIdentifier(lowerCountryCode.toLowerCase(), "drawable", getPackageName());
imgView.setImageDrawable(res.getDrawable(id));
The following code should work well (assuming that you really have a file with name lowerCountryCode in your res/drawable/ folder)
String lowerCountryCode = countryCode.toLowerCase();
int id = getResources().getIdentifier(lowerCountryCode, "drawable", getPackageName());
ivCard[0].setImageResource(id);
i use that code....
ImageView[] ivCard = new ImageView[1];
#override
protected void onCreate(Bundle savedInstanceState)
ivCard[0]=(ImageView)findViewById(R.id.imageView1);