Change Android Drawable Button Icon Programmatically - java

How would one change the android:src XML property programatically of a FloatingActionButton or Button widget? I have a button defined as following within an activity:
FloatingActionButton floatingActionButton = (FloatingActionButton) this.findViewById(R.id.start_button);
It has an initial android:src set as follows:
android:src="#android:drawable/ic_media_play"
I'm trying to utilize the setImageResource(int id) to change the android:src XML property, but how to I access a Drawable icon such as ic_media_pause after a button click that occurs for example. When I try to pass in R.drawable.ic_media_pause to the setImageResource() method I received an cannot resolve symbol error.
floatingActionButton.setImageResource(R.drawable.ic_media_pause);
How do I get at the available R.drawable resources to pass it in as an argument.
Any help would be great appreciated. Thank you!

You're trying to access a default icon included in the SDK. You need to prefix your resource identifier with "android." For example:
floatingActionButton.setImageResource(android.R.drawable.ic_media_pause);
this will allow you to reference the default icons.
:)

Related

How and Where to define this ImageView in xml?

In this Android Navigation Icon - Profile picture instead of hamburger icon stackoverfolw post
ImageView profileImage = (ImageView)
binding.navView.getHeaderView(0).findViewById(R.id.iv_profile_image);
How an Where to define R.id.iv_profile_image in which xml file ?
I couldn't find the XML code in the example you linked. However, I noticed the following line in the Activity's onCreate() method. This would indicate that the XML file being used is called activity_maps.xml. Most likely this is where the ImageView was placed.
binding = DataBindingUtil.setContentView(this, R.layout.activity_maps);
Assuming this assumption is correct, the ImageView can be declared something like this. The answer to "where" within the XML depends as XML is a declarative language.
<ImageView android:id="#+id/iv_profile_image"
android:layout_width="60dp"
android:layout_height="60dp"
...
android:contentDescription="#string/to_replace" />

How to change the action bar color and open a file on click?

I'm working now on an app for Android and there are two things that I don't know how to do.
I want to change the colour of the action bar at the request of the user, so I want to change it dynamically. I see some answers that say that I need to use:
ActionBar bar = getActionBar();
bar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#004D40")));
But for some reason, it does not work for me.
I've added to my app option to download a file, but I do it in an unusual way so it does not create a notification when the download is complete. I want to give my user an easy way to get to the file. Can I create a button that opens the file when clicked?
Thanks!
1) For that I would recommend you having Example here Toolbar View and set it as your Actionbar. Just add this view at the top:
<android.support.v7.widget.Toolbar
android:id="#+id/my_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:elevation="4dp"
android:theme="#style/ThemeOverlay.AppCompat.ActionBar"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"/>
And then in Activity
Toolbar myToolbar = (Toolbar) findViewById(R.id.my_toolbar);
setSupportActionBar(myToolbar);
After this you can change everything you want to the view myToolbar. Set background color etc. It will work. Using just Actionbar is old and depricated approach.
2) Yes you can, it is like opening new Activity with intent. Android has some inbuilt Intent Action Type which helps you to Open or View specific files, but for this you need to know which type of file you are going to handle.
Suppose If you have file type which categorized in document type you can use,
ACTION_OPEN_DOCUMENT with specific MIME_TYPE (Android 4.4 or Higher)
or If you going to handle some media file (Audio/Video)
you can use,
ACTION_VIEW
To identify MIME_TYPE of specific file you can use function
guessContentTypeFromName (String url)Link
Or getMimeTypeFromExtension(String extension)Link
Hope this helps :)
You can't change the color because you should call getSupportActionBar() and it's also for api >11;
getSupportActionBar().setBackgroundDrawable(new ColorDrawable(getResources().getColor(android.R.color.black)‌​));
or
getSupportActionBar().setBackgroundDrawable(new ColorDrawable(Color.parseColor("#004D40")​));
About your second question, we need more info and your code where you download file. You totally can access files onClick but you need to know the file type and the directory where you store the files and target api to check runtime permissions for example
I suggest you ask another question with all your code and this info

How to change background of the app?

Is there a way to change background directly in the app? (!not homescreen wallpaper)
You need to specify the layout that its background will be changed. I guess you have a LinearLayout, so add an id to the layout (if it is not exist):
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id=#+id/parentLayout>
Then you can change the background in Java with:
LinearLayout linearLayout = (LinearLayout) findViewById(R.id.parentLayout);
linearLayout.setBackgroundResource(R.drawable.new_background);
Hope it helps you!
i have a link for you is easy to change background app.
Try something like that https://www.youtube.com/watch?v=mDKuWIlSNJE

How to choose a view group in my android app

I am developing an app and have successfully added a floating action button using the library shown here. The floating button displays well but when i navigate to another fragment through the navigation drawer the button still displays instead i want the button to only display in the activity i created it. I checked for those that had similar issues online and i saw comments...saying that i have to set the View by modifying this line of code found within the method show below.
ViewGroup root = (ViewGroup) activity.findViewById(android.R.id.content);
root.addView(button, params);
Please can tell me how to achieve this, thanks in advance.
Library Method
public FloatingActionButton create() {
final FloatingActionButton button = new FloatingActionButton(activity);
button.setFloatingActionButtonColor(this.color);
button.setFloatingActionButtonDrawable(this.drawable);
params.gravity = this.gravity;
ViewGroup root = (ViewGroup) activity.findViewById(android.R.id.content);
root.addView(button, params);
return button;
}
if you can access the FAB view using findViewById you can simply do:
button.setVisibility(VISIBILITY.GONE);
to hide it, that way you can set visibility back to visible when you go back to that activity.
or if you can access view called 'root' (or can access button via getActivity().findViewById for Fragments)
root.removeView(button);
Set the FloatingActionButton to be invisible by default in the XML file:
<ImageButton android:id="#+id/button" android:visibility="invisible"/>
and make it visible by using the following code in the required activity class:
button.setVisibility(View.VISIBLE);

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

Categories

Resources