Android "Header" Text - java

Is there a way to modify an Android app's header programatically? Maybe even adding a custom view with an image in it? I'm talking about the gray bar at the top of an app, whose text can be modified via an app's Mainfest.xml with the label attribute of the activity:
<activity android:name=".ProgramTracks" android:label="#string/app_name">
</activity>
Is there some way to, for a lack of a better word, mess with that?

Already discussed here.

You can add a custom header inside your activitys onCreate().
Write an usual xml layout and call
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.window_title);
where R.layout.window_title is your custom layout.

Related

Landscape orientation not working correctly

I have a problem when I rotate my device, my activity is re-executed and the values ​​change.For example, suppose that you have an activity with a button, when this button is clicked, it will show the increased value in a text view (a counter). And when you put your device in landscape mode, the activity will run again, and you will see (again) the value increased. My question is, is there a way to not run the activity again when you rotate the device ?
Please read this. You will see that there are two solutions for handling configuration changes:
You can declare that you are handling the change on your own by updating activity in the manifest. This way your activity will not be restarted and onConfigurationChanged() will be called:
<activity ...
android:configChanges="orientation|screenSize"
...>
You can pass your data objects to the new activity via onSaveInstanceState() function. Passing large objects can slow down the app. It is better you separate data from the view for example by using ViewModels, which are retained in these kind of changes.
First way may seem easy, but it is not recommended as you can see in the link provided at the beginning:
Caution: Handling the configuration change yourself can make it much
more difficult to use alternative resources, because the system does
not automatically apply them for you. This technique should be
considered a last resort when you must avoid restarts due to a
configuration change and is not recommended for most applications.
Add android:configChanges in your activity
<activity name= ".YourActivity" android:configChanges="orientation|screenSize"/>
In the manifest file add android:configChanges = "orientation|screenSize"
to the activity tag.
For ex:
<activity
android:name=".dummyActivity"
android:configChanges = "orientation|screenSize"/>
If you want to handle orientation changes manually you can do so by overriding the onConfigurationChanged() method in Activity.
For more info refer Handling Configuration Changes
add this to the manifest
android:configChanges="orientation|screenSize|screenLayout|keyboardHidden"
If configChanges contains uiMode, remove it. If you don't want to uninstall uiMode, you can update these libraries.
androidx.appcompat:appcompat:1.2.0-rc01
androidx.core:core:1.3.2

How to create an additional input method service without changing default selected one?

I want to make a new input method with no actual keyboard.
It should stay active (after being enabled by user) always but not be the default keyboard. Because it will not have any visual keyboard and user will still be able to use the default keyboard as he/she selects.
I will trigger it on demand from a network command. Is this possible ?
It should be possible, because for example Android TV remote or Shield TV remote can do this.
But I couldn't find how to do it. When I create a normal input method, it only works if I select it as the default input method.
<service android:name="SoftKeyboard"
android:permission="android.permission.BIND_INPUT_METHOD">
<intent-filter>
<action android:name="android.view.InputMethod" />
</intent-filter>
<meta-data android:name="android.view.im" android:resource="#xml/method" />
</service>
This is the part from manifest file. Is it possible to make it work without selecting it as the default input method ?
As a second, additional input method …
I could not find a solution which could live together with the original leanback keyboard.
So, I found an example Leanback keyboard code , injected my rest API in it.
Now I have the leanback keyboard modified to make what I want.

How to hide Title Bar of a pop-up activity

I created an activity which is shown as pop up/ Dialog by following answers given in this question.
I did it by adding these lines in AndroidManifest
<activity
android:name=".package.Activity"
android:theme="#style/Theme.AppCompat.Light.Dialog.Alert" />
It works well as intended but the problem it shows Appname as title. I want to make it appear as AlertDialog. How to fix this?
I also cant use setTitle("") or label="" because empty space is left where appname was before.
Add this code to the top of the onCreate() method of the Activity:
setTheme(R.style.{style}
The {style} should be ended with NoActionBar. i.e. R.style.Theme_AppCompat_NoActionBar
And most importantly, add the code before these two lines of code (add it to the top of the method):
super.onCreate(savedInstanceState);
setContentView(R.layout.{your layout});
Hope this answer helped!
You can simply call the setTitle("") function from within your Activity.

Android custom title bar, does not work with android:label from manifest

I have a custom title bar that I would like to apply to all my activities. I have searched online and found a few ways of doing that however I have ran into a problem.
It seems that despite the fact I have a TextView in my titlebar with id #android:/title, Android will not use the value defined in the manifest in android:label for each activity.
I tried also calling setTitle(R.string.myTitle) but it still will not render it! If I manually put some text in my TextView in the xml it shows up fine.
How can I have a custom title bar that is linked and makes Android use my manifest values for labels? I do not want to lose the setTitle() or the XML attributes functionality.
Thanks.
Try using #android:id/title instead of #android:/title.

Android add item to global context menu

When you long press on something in Android, a context menu comes up. I want to add something to this context menu for all TextViews in the system.
For example, the system does this with Copy and Paste. I would want to add my own, and have it appear in every application.
Currently Android does not support this, you cannot override or hook functionality globally at the system level without the particular activity implementing an intent or activity that you expose. Even in the case of publishing an intent it wouldn't matter unless the application running is a consumer... and all the base system applications and obviously all applications prior to yours would not be without updating the app to consume.
Basically as it stands, this is not possible.
What exactly are you trying to accomplish with this global context menu, some sort of global "Search For" or "Send To" functionality that runs through your application?
Add intent-filter in your file android-manifest.xml:
<activity
android:name=".ProcessTextActivity"
android:label="#string/process_text_action_name">
<intent-filter>
<action android:name="android.intent.action.PROCESS_TEXT" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="text/plain" />
</intent-filter>
</activity>
Get highlighted by user text in activity your app in method onCreate():
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.process_text_main);
CharSequence text = getIntent()
.getCharSequenceExtra(Intent.EXTRA_PROCESS_TEXT);
// process the text
}
More information in the article on medium.
Thanks for user: YungBlade
Him answer on ru.stackoverflow.com
This might be a little bit hacky, but you can trap the menu key at the application/activity level, check to see if your current active view is a text entry view, and then build and display your own custom popup menu.
It is possible, it's just a little tricky.
If you create/inflate a TextView, call setFocusable(false) on it, and then set it as the active view your Activity will still receive key events.
You will have to forward all those events (trackball, touch, key) to your View tree by hand. (Inside your "onKeyDown" function you'd have to call the appropriate "onKeyDown" method for the top level View) You effectively have to trap the notion of 'focus' and dole it out to the correct view yourself.
While a little ugly, it may give you the desired results.
This would, however, only work in your own application. Previous answers are correct about it being impossible across the entire phone.
Push the Share button (tree of three dots) within selection menu. Then you should select your own app. Does't work for input field content, unfortunately.
Hmm; I don't know if you can extend built in types like in example. Ruby (my Java knowledge is not so deep enough).
However you can derive your own MyTextView from TextView. Then substitute all your TextView in layouts like this:
<TextView android:id="#+id/TextView01" />
to
<com.mydomain.mypackage.MyTextView android:id="#+id/TextView01" />
to automatically change type of all these fields.
Then you need to override all constructors
(especially TextView(Context context, AttributeSet attrs) ).
After that all layout inflaters (automatic or manual) will create this type with no fuss.
Then you can create/register context menu callbacks as you wish.

Categories