I'm trying to remove a single view from layout but it is not being removed.
But, when i call removeAll, it does remove them all.
Here is the code:
for (int subspe = 0; subspe < totalSubSpe; subspe++) {
View childSubSpe =
BasicInfoUpdateDocProfile.ll_custom_for_supspecialist.getChildAt(subspe);
TextView subSpecialityId = (TextView) childSubSpe.findViewById(R.id.tv_speciality_id);
TextView specialistId = (TextView) child.findViewById(R.id.tv_id);
String speID = specialistId.getText().toString();
String subSepId = subSpecialityId.getText().toString();
Log.e("TAG", "the the the the id is: speciality " + speID);
Log.e("TAG", "the the the the id is: sub speiciality " + subSepId);
if (speID.equals(subSepId)) {
//removing sub qualificaiton
Log.e("TAG", "It is Equal subspecialist ");
ViewGroup par = (ViewGroup) childSubSpe.getParent();
par.removeView(childSubSpe);
//BasicInfoUpdateDocProfile.ll_custom_for_supspecialist.removeView(childSubSpe);
BasicInfoUpdateDocProfile.ll_custom_for_supspecialist.
removeView((View)childSubSpe.getParent());
// BasicInfoUpdateDocProfile.ll_custom_for_supspecialist.removeAllViews();
}
}
I try access to drawable resource that I put in hashmap object
this is my relevant code:
private void pairImagesCollection() {
mImages.put(R.drawable.img1, R.drawable.img1shadow);
mImages.put(R.drawable.img2, R.drawable.img2shadow);
mImages.put(R.drawable.img3, R.drawable.img3shadow);
mImages.put(R.drawable.img4, R.drawable.img4shadow);
}
private void checkMatch(View dragView, View view) {
ImageView target = (ImageView) view;
ImageView dragged = (ImageView) dragView;
Drawable image = dragged.getDrawable();
int imgId = mImages.get(Integer.valueOf(image)); // wrong, I don't know how to do it ?!
target.setImageResource(imgId);
}
Any help will be appraised!
You can use Iterator
Iterator itObj = hashMapOBJ.entrySet().iterator();
while (itObj .hasNext()) {
Map.Entry pair = (Map.Entry) itObj.next();
String Key = (String) pair.getKey();
Drawable Value = (Drawable) pair.getValue();
hashMapOBJ.put(Key, Value);
System.out.println("Key: " + Key + "------>" + Value);
}
int imgId = mImages.get(Integer.valueOf(image));
You cannot get Resource/Drawable Id from ImageView through Drawable. With codes above, You can set tag for ImageView dragged. After that get value with with your mImages.
Before you checkMatch, you set tag for dragView as Drawble, something like that:
dragged.setTag(R.drawable.img1);
And now you can getDrawableId and
imgId = getDrawableId(dragged);
private int getDrawableId(ImageView iv) {
return (Integer) iv.getTag();
}
You got a hashmap that stores references to an image and its shadow right?
Which image you want to put on the image view? You have to iterate through your map and put the image you want:
Iterator iterator = map.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry pair = (Map.Entry)iterator.next();
System.out.println(pair.getKey() + " = " + pair.getValue());
}
If you want to put the value image then you put the pair.getKey() else you put the pair.getValue().
target.setImageResource(R.drawable.xxx);
In my application I'am creating 10 EditText by dynamically. Now I want to give different value in run time and I want to add it to the list. I have assigned EditText object to the String variable like object.getText.toString(). But i cant get any value.I'am a beginner in android. Can anyone help me how to achieve this? Thanks in advance.
for(int i=0;i<=10;i++)
{
requirement = require.get(i);
RelativeLayout rl1 = new RelativeLayout(getActivity());
rl1.addView(req1);
req1estimate_value = new EditText(getActivity());
String value = req1estimate_value.getText().toString();
rl2.addView(req1estimate_value);
}
Try this. You should instantiate relative layout (rl1) at out of for loop, and should add child views with in that, so that all views could belongs to a parent layout. After that for accessing the values of all EditText you can use following:
String viewValue;
ViewGroup rootView = (ViewGroup) rl1;
int count = rootView.getChildCount();
for (int i = 0; i < count; i++) {
View view = rootView.getChildAt(i);
if (view instanceof EditText) {
viewValue = ((EditText) view).getText().toString();
Log.v("Value:: ", i + " " + viewValue);
} else if (view instanceof Spinner) {
viewValue = ((Spinner) view).getSelectedItem()
.toString();
Log.v("Value:: ", i + " " + viewValue);
}
}
Now after getting values you can put on a List or anywhere you want to use.
Here is my code :
private void setWeatherDialogInfoAndIcons(){
WeatherGoogle weather = WeatherGoogle.getInstance("istanbul");
String iconsPath = Environment.getExternalStorageDirectory().getAbsolutePath()+"/arla/images/hava/";
String[] allData = weather.getCityData().split("<>");
currentImage = (ImageView) customDialog.findViewById(R.id.imageViewcurrentWeatherImage );
currentInfo = (TextView) customDialog.findViewById( R.id.textViewCurrentInfo ) ;
currentInfo.setText( allData[0] +"\n"+allData[1]+"\n"+allData[2]+"\n"+allData[3] );
currentImage.setBackgroundResource(R.drawable.air);
currentImage.setBackgroundDrawable( UiHelper.getDrawableFromSdcard(iconsPath + weather.getIconName(0)) );
int j = 4;
for (int i = 0; i < dayInfos.size() ; i++) {
dayInfos.get(i).setText( allData[j] +"\nMin : "+ allData[j+1] +"\nMaks: "+ allData[j+2]
+ "\n" + allData[j+3] );
j+=4;
}
}
I can change the texts of textviews but when i want to set the background of imageviev(currentImage) it doesnt work , it shows the dialog but there is no background in Imageview.
currentImage.setBackgroundDrawable( UiHelper.getDrawableFromSdcard(iconsPath + weather.getIconName(0)) );
I am sure that my method getDrawableFromSdcard works because i used it somewhere else in my code lastly i checked png icon and its path. So what can be the problem?
currentImage.setBackgroundDrawable(getResources.getDrawable(R.drawable.air));
From SDcard :
Bitmap picture = BitmapFactory.decodeFile("file_path");
imageView.setImageBitmap(picture);
Try it. Good luck!
The method setBackgroundDrawable(Drawable background) is deprecated. You have to use setBackground(Drawable) instead. See the reference
Debugging my app with strange results on Samsung phones, which I don't have physical access to. I'd like to ask a user to run an instrumented App to help debug. My App gets a view that has an unknown (to me ;-) hierarchy in it (ViewGroups etc). Is there a way to "walk" a View and print out to a string / ddms all the components in the View (etc ViewGroups in it etc)?
This would be akin to HierarchyViewer tool - if I had adb level access to the device.
Update: I guess I could use the method
void dumpViewHierarchyWithProperties(Context context, ViewGroup group, BufferedWriter out, int level)
from the ViewDebug.java Android OS sources ...
Anyone have a better idea?
Thanks!
Here's the utility function I just made for this purpose:
public static void printViewHierarchy(ViewGroup vg, String prefix) {
for (int i = 0; i < vg.getChildCount(); i++) {
View v = vg.getChildAt(i);
String desc = prefix + " | " + "[" + i + "/" + (vg.getChildCount()-1) + "] "+ v.getClass().getSimpleName() + " " + v.getId();
Log.v("x", desc);
if (v instanceof ViewGroup) {
printViewHierarchy((ViewGroup)v, desc);
}
}
}
I've created utility method which returns hierarchy in a pretty printed way with human readable view ids. Here is an example of the output:
[LinearLayout] no_id
[CardView] com.example:id/card_view
[RelativeLayout] no_id
[LinearLayout] com.example:id/image_container
[AppCompatImageView] com.example:id/incident_icon
[CustomTextView] com.example:id/road_number
[RelativeLayout] no_id
[CustomTextView] com.example:id/distance_to_user
[CustomTextView] com.example:id/obstruction_title
[CustomTextView] com.example:id/road_direction
[CustomTextView] com.example:id/obstruction_description
[AppCompatImageView] com.example:id/security_related
Here is the utility method:
public static String getViewHierarchy(#NonNull View v) {
StringBuilder desc = new StringBuilder();
getViewHierarchy(v, desc, 0);
return desc.toString();
}
private static void getViewHierarchy(View v, StringBuilder desc, int margin) {
desc.append(getViewMessage(v, margin));
if (v instanceof ViewGroup) {
margin++;
ViewGroup vg = (ViewGroup) v;
for (int i = 0; i < vg.getChildCount(); i++) {
getViewHierarchy(vg.getChildAt(i), desc, margin);
}
}
}
private static String getViewMessage(View v, int marginOffset) {
String repeated = new String(new char[marginOffset]).replace("\0", " ");
try {
String resourceId = v.getResources() != null ? (v.getId() > 0 ? v.getResources().getResourceName(v.getId()) : "no_id") : "no_resources";
return repeated + "[" + v.getClass().getSimpleName() + "] " + resourceId + "\n";
} catch (Resources.NotFoundException e) {
return repeated + "[" + v.getClass().getSimpleName() + "] name_not_found\n";
}
}
Tip: we use this method to add view hierarchies to some crash reports. In some cases it is really helpful.
almost the same with this answer but with Kotlin:
fun getViewTree(root: ViewGroup): String{
fun getViewDesc(v: View): String{
val res = v.getResources()
val id = v.getId()
return "[${v::class.simpleName}]: " + when(true){
res == null -> "no_resouces"
id > 0 -> try{
res.getResourceName(id)
} catch(e: android.content.res.Resources.NotFoundException){
"name_not_found"
}
else -> "no_id"
}
}
val output = StringBuilder(getViewDesc(root))
for(i in 0 until root.getChildCount()){
val v = root.getChildAt(i)
output.append("\n").append(
if(v is ViewGroup){
getViewTree(v).prependIndent(" ")
} else{
" " + getViewDesc(v)
}
)
}
return output.toString()
}
The format of other outputs dissatisfied my eyes and some codes where an overhead. So, I improved the output with:
/**
* Prints the view hierarchy.
*/
public static void printViewHierarchy(ViewGroup parent, String intent) {
for (int i = 0, max = parent.getChildCount(), numLenght = (max + "").length(); i < max; i++) {
View child = parent.getChildAt(i);
String childString = child.getClass().getSimpleName() + " " + child.getId();
String format = "|— %0" + numLenght + "d/%0" + numLenght + "d %s";
Log.d("debug", intent + String.format(format, i + 1, max, childString));
if (child instanceof ViewGroup)
printViewHierarchy((ViewGroup) child, intent + " ");
}
}
to:
|— 1/4 ScrollView 2131296482
|— 2/4 MaterialTextView 2131296445
|— 3/4 FloatingActionButton 2131296374
|— 4/4 MaterialTextView 2131296363
|— 1/4 ScrollView 2131296482
|— 1/1 LinearLayout 2129243036
|— 01/74 RelativeLayout 2131296449
|— 1/4 MaterialCheckBox 2131296332
|— 2/4 MaterialTextView 2131296547
|— 3/4 MaterialTextView 2131296531
|— 4/4 AppCompatImageButton 2131296462
|— 02/74 RelativeLayout 2131296449
|— 1/4 MaterialCheckBox 2131296332
|— 2/4 MaterialTextView 2131296547
|— 3/4 MaterialTextView 2131296531
|— 4/4 AppCompatImageButton 2131296462
|— ...
|— 74/74 RelativeLayout 2131296449
|— 1/4 MaterialCheckBox 2131296332
|— 2/4 MaterialTextView 2131296547
|— 3/4 MaterialTextView 2131296531
|— 4/4 AppCompatImageButton 2131296462
|— 2/4 MaterialTextView 2131296445
|— 3/4 FloatingActionButton 2131296374
|— 4/4 MaterialTextView 2131296363
These Views are not mine, they are coming from other Apps, so I cannot touch code related to them (I'm inflating them from a RemoteView)
If you are receiving a RemoteViews, and you are applying it to something in your activity, you have direct access to the resulting Views, no different than if you had inflated them from XML yourself. Given the root View, it is simply a matter of walking the children (probably depth-first, but that's your call) and logging whatever information you want.
Just find menu item from BottomNavigationView and register long click.
View itemBag = bottomNavigationView.findViewById(R.id.action_bag);
if (itemBag != null) {
itemBag.setOnLongClickListener(BottomNavigationActivity.this::onLongClick);
}
private boolean onLongClick(View v) {
switch (v.getId()) {
case R.id.action_bag: {
showToastMessage(R.string.menu_shopping_bag);
}
break;
}
return false;
}