android change imageview programmatically - java

I am making an android app where the same imageview is displayed every time the user enters a value. but I want the imageview to be changed every time the user enters a value. I wrote a code to do this but the problem is after the first image is displayed I face black screen and it takes me to the first page in the app, it doesn't crash just shows black screen.
here is the imageview code in xml file:
<ImageView
android:id="#+id/ImageSuccess"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:layout_marginBottom="20dip"
android:layout_marginTop="20dip"
android:scaleType="centerInside"
android:src="#drawable/image1" />
and here where I changed the imageview in java file:
ImageView myImage= (ImageView) findViewById(R.id.ImageSuccess);
if(userVlue.equals("SCHOOL")){
myImage.setImageResource(R.drawable.image1);
}
else if(userVlue.equals("CAR")){
myImage.setImageResource(R.drawable.image2);
}
else if(userVlue.equals("TOY")){
myImage.setImageResource(R.drawable.image3);
}

One of myImage or userVlue is not set properly and probably is null.
Debug and share logs. Your partcular activity is getting distroyed probably because of nullpointexception hence returning to main activity in backtrack.
I tried with a sample app and your logic is working perfectly.
You are handling user action on some UI element, when you do findViewById, cotext is that UI element, so this method searches ImageSuccess in children of it. Try giving page layout as rootlayout and do rootlayout.findViewById("ImageSuccess");

I found the solution finally, the black screen appeared because the images were only in the drawable file, so I needed to add the images in all drawbridge folders such as drawable-ldp, drawable-ldpi etc...., so that the app can work in different phone sizes.

Related

Open TextView links at another activity - custom ClickableSpan

I have a TextView with links and other text. I need to links to be clickable & I need to capture the click and open the link in a webview in my activity. I followed this this answer. My code looks like this:
actitivity_main.xml
<TextView android:id="#+id/textlinktest"
android:layout_width="wrap_content"
android:layout_height="50dp"/>
And this is the way I invoke it:
textLinkTest.setText(Html.fromHtml("Something else google"));
textLinkTest.setText(RichTextUtils.replaceAll(
(Spanned) textLinkTest.getText(),
URLSpan.class,
new URLSpanConverter()));
The links show up fine, but they are not clickable! The onclick in my customeclickablespan never gets called. I tried toggling android:linksClickable & also tried different values for android:autoLink. But the links remain unclickable. Any thoughts?
Add
textLinkTest.setMovementMethod(LinkMovementMethod.getInstance());
before
textLinkTest.setText(RichTextUtils.replaceAll((Spanned) textLinkTest.getText(), URLSpan.class, new URLSpanConverter()));

No visible button feedback after setEnabled(false) -> (true)/ selectableItemBackground as drawable?

With one button i'm disabling three other button's with button.setEnabled(false).
That works fine.
When this button is pressed again, the tree button's will be enabled with button.setEnabled(true).
They are still doing what they shoud and are fine responding to the onClickListener.
But since the enabling they do not visible respond when they get pressed.
How do i reactivate them right?
(i already searcht in google but didn't find anything).
private void startSleeping()
{
editorState.putBoolean("SLEEPING", true);
editorState.commit();
buttonDrink.setEnabled(false);
buttonEat.setEnabled(false);
buttonWash.setEnabled(false);
buttonDrink.setBackgroundColor(getResources().getColor(R.color.darkgray));
buttonEat.setBackgroundColor(getResources().getColor(R.color.darkgray));
buttonWash.setBackgroundColor(getResources().getColor(R.color.darkgray));
buttonSleep.setBackgroundColor(getResources().getColor(R.color.orange));
buttonWash.setTextColor(getResources().getColor(R.color.lightgray));
buttonDrink.setTextColor(getResources().getColor(R.color.lightgray));
buttonEat.setTextColor(getResources().getColor(R.color.lightgray));
buttonSleep.setTextColor(getResources().getColor(color.black));
}
private void stopSleeping()
{
editorState.putBoolean("SLEEPING", false);
editorState.commit();
buttonDrink.setEnabled(true);
buttonEat.setEnabled(true);
buttonWash.setEnabled(true);
buttonDrink.setBackgroundColor(getResources().getColor(R.color.transparent));
buttonEat.setBackgroundColor(getResources().getColor(R.color.transparent));
buttonWash.setBackgroundColor(getResources().getColor(R.color.transparent));
buttonSleep.setBackgroundColor(getResources().getColor(R.color.transparent));
buttonWash.setTextColor(getResources().getColor(R.color.white));
buttonDrink.setTextColor(getResources().getColor(R.color.white));
buttonEat.setTextColor(getResources().getColor(R.color.white));
buttonSleep.setTextColor(getResources().getColor(R.color.white));
}
<Button
android:id="#+id/buttonEat"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?android:attr/selectableItemBackground"
android:paddingBottom="#dimen/padding_size"
android:paddingTop="#dimen/padding_size"
android:text="#string/button_eat"
android:textColor="#color/white"
android:textColorHint="#color/white"
android:textSize="#dimen/text_size" />
This is because you are setting the background resource as transparent instead of what you applied in the layout as the background drawable to those buttons. Re-apply those background drawables and you'll get the touch feedback again. That should solve your problem.
On a side note for future implementations you can directly provide the enabled/disabled background resource in the drawable's list-selector itself. use state_enabled = true/false. This eliminates the need of doing all of that in the code.

How to change the color of intern Android drawables?

I've tried to put the intern icons of Android (anrdoid.R.drawable.bla) into an ImageButton
and I wanted to change the color of Icon (not the Background!), but it doesn't work like I want to.
Here is what I've tried:
my ImageButton from the Layout:
<ImageButton
android:id="#+id/imageButton1"
android:layout_marginTop="100dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/background"
android:src="#android:drawable/ic_lock_silent_mode" />
what I've tried in my Activity:
Drawable myIcon = getResources().getDrawable( android.R.drawable.ic_lock_silent_mode);
ColorFilter filter = new LightingColorFilter( R.color.blue, R.color.blue);
myIcon.setColorFilter(filter);
No matter what Values I've tried for the LightingColorFilter it always gives the same result. The icon inside the Imagebutton gets dark. But thats not what I wnated. I just wanted to apply a color from my colors.xml, it somehow doesnt work out.
Is this even the right direction I'm going? Or is there another opertunity (And I don't mean coloring myself in Photoshop and putting them into the drawables folder)
this worked for me, for example it will paint in dark gray:
ImageButton imgBtn = (ImageButton) findViewById(R.id.imageButton11); // image button
imgBtn.setColorFilter(getResources().getColor(android.R.color.darker_gray), Mode.SRC_ATOP);

paste option for edittext

I have an edittext and I would like to paste some text in it. I can copy the text from some web page but I am not able to paste the text in my edittext control.How can I enable my edittext to paste some text.Here is my main.xml for edittext ;
enter code here
<EditText
android:id="#+id/enter"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight= "2"
android:scrollbars="vertical"
android:textColor="#color/black"
/>
Thanks
This is on Android 4.4.2 Samsung S4;
Documentation for TextView says that:
To allow users to copy some or all of the TextView's value and paste
it somewhere else, set the XML attribute android:textIsSelectable to
"true" or call setTextIsSelectable(true). The textIsSelectable flag
allows users to make selection gestures in the TextView, which in turn
triggers the system's built-in copy/paste controls.
There is also another Textview attribure called android:cursorVisible which determines if the system should be invoked about the copy/paste callbacks.
By default I believe both of these are true and selection/copy/paste mechanics are already enabled. I could not change that behaviour by using android:textIsSelectable="false" but if I set android:cursorVisible="false" initially you can't paste anything inside the EditText. Only after you type something in, cursor and selection behaviour becomes enabled again. Maybe this should be handled inside the code rather than in the layout xmls, or it might be related to android:inputType which also did not make a difference for me.
So try setting android:cursorVisible="true" in your EditText's layout xml if paste is not enabled by default.
According to your problem if you copied some data any where in your system and you want to paste it in some specific variable, like Edit TextBox, Textview etc, then this code will surely help you.
ClipboardManager clipMan = (ClipboardManager)getSystemService(v.getContext().CLIPBOARD_SERVICE);
myEdtTxt.setText(clipMan.getText());
Note:- here the clipMan object will store the data whenever copied process take place and we will return that data from that object and will set it,
To enable the standard copy/paste for TextView, U can choose one of the following:
Change in layout file:
Either add below property to your TextView
android:textIsSelectable="true"
and In your Java class write this line to set it programmatically.
myTextView.setTextIsSelectable(true);
if fragment try with
mContext.myTextView.setTextIsSelectable(true);
And long press on the TextView you can see copy/paste action bar.
Try setting the inputType="text" for the EditText field

Android - Set Application Background

I am trying to set my applications background to a user chosen image, but I am having some trouble. Is it possible for someone to give me an example of how to do this? I can set the image ok from my resources, but when I try to use an image on the phone I can't seem to make it work.
Assuming you have created a method to let the user choose the path to the image, use this:
// Variable with the path to the background
String bg_path = "/sdcard/bg/background.png" // <-- This path can be whatever you like
//Change background of Activity
getWindow().setBackgroundDrawable(Drawable.createFromPath(bg_path));
Oh Don't forget to set you Layout background color to transparent in the XML file or you won't see the image. (this is valid to anything that fills parent window like a listview, for instance)
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout="#+id/m_layout"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android.background="Color.TRANSPARENT"
>

Categories

Resources