how can I right-align activity titles? - java

I'm new to android development and I need proper instructions to right-align activity titles in a min-sdk:8 android application. I can do so by enabling rtl support for my app, but that only takes effect when the user has chosen an rtl language on his/her device. is there a way to force rtl even when the device language is ltr? or any other instructions to right-align titles in ltr mode? thx.

You can set a custom view in your Toolbar like this:
(copied from How to align center the title or label in activity?)
TextView customView = (TextView)
LayoutInflater.from(this).inflate(R.layout.actionbar_custom_title_view_centered,
null);
ActionBar.LayoutParams params = new ActionBar.LayoutParams(
ActionBar.LayoutParams.MATCH_PARENT,
ActionBar.LayoutParams.MATCH_PARENT, Gravity.RIGHT );
customView.setText("Some centered text");
getSupportActionBar().setCustomView(customView, params);

You can do that^. If you were do that multiple times in your app, you can create a customToolbar class that extends Toolbar and overwrite this method and add all that code there^ (and use that custom toolbar in your xml as well)
https://developer.android.com/reference/android/widget/Toolbar.html#generateLayoutParams(android.util.AttributeSet)

Related

How to create a cardview with a specific layout using only Java (android studio)

I am making an app similar to facebook where i have a news feed which shows all the posts. However, the number of post in this feed varies with each user, depending on the amount of friends they have and how many posts they have made. I want to create a CardView for every post item which will show the name of the user who posted it, the time it was posted and the actual post itself. The layout will be split horizontally once with the post in the bottom half and the name of the person with the time it was posted on the top half.
Currently i can only find ways to do this by altering the xml text, but i want to create these CardViews as and when i need them, in my Java code.
I know i can alter TextViews by using such functions as setText() & setTextSize(). Is there a way for me to use similar functions on a CardView which can alter the base layout.
for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
CardView TVcard = new CardView(Home.this);
TextView TVposts = new TextView(Home.this);
TextView test = new TextView(Home.this);
TVcard.addView(TVposts);
TVcard.addView(test);
test.setText("test");
test.setBackgroundColor(Color.GREEN);
TVposts.setText(snapshot.getValue().toString());
TVposts.setTextColor(Color.WHITE);
TVposts.setTextSize(25);
TVposts.setBackgroundColor(Color.BLACK);
TVposts.setHeight(200);
linearLayout.addView(TVcard);
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) TVcard.getLayoutParams();
params.setMargins(0, 20, 0, 0);
TVcard.setLayoutParams(params);
}
Thanks

Android Studio overlays

I want to start creating long running applications. For instance, you choose some settings and then they are used while you browsing or playing a game. For example shortcut apps. You click somewhere in the corner, and something pop ups, with list of applications you want to go to.
My problem: I don't know how to create buttons, which would be on phone screen, like triggers, when you touch, something happens. I heard it's called overlays, but I couldn't manage to find tutorials about it.
Application example: Pie Control on Google Play. https://play.google.com/store/apps/details?id=jun.ace.piecontrol
Thanks for helping me out.
use system alert window .
example
View mView = new View(this);
WindowManager.LayoutParams params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY,
WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH,
PixelFormat.TRANSLUCENT);
params.gravity = Gravity.RIGHT | Gravity.TOP;
WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);
wm.addView(mView, params);

Android change textview content

Hi I tried to change the content of a textview of another xml.
Unfortunaly it does not work, my code looks like this:
View inflatedView = getLayoutInflater().inflate(R.layout.fragment_main, null);
TextView testView= (TextView) inflatedView.findViewById(R.id.section_label);
TextView test = (TextView) inflatedView.findViewById((R.id.testext));
testView.setText("adasd");
test.setText("adjhsajdH");
Does anyone know a solution.
When you are inflating a layout, it creates a new layout. It means your first line of code does not retrieve the layout of your fragment, but creates a new one.
That' why you don't see the change in your fragment view.
If you want to change the values displayed by the fragment, create a new method in which fragment that will do the job internally.
try this. remove the old layout from the parent view.
parentView.removeView(urtextview)
then add the new textview u just edited
parentView.add(urTextView)

Nested Android SlidingMenus possible?

I'm using the sliding menu library here: https://github.com/jfeinstein10/SlidingMenu/
and I have an activity that inherits from SlidingMenuActivity with a sliding menu that works perfectly, but I also want to add SlidingMenus to each row in a list fragment that is shown as part of this SlidingMenuActivity subclass. It seems that the way I'm doing it doesn't work at all; the touches get intercepted and they don't let me click on a list row, but I can't swipe the sliding menu into appearance, nor can I see the SlidingMenu when it's closed.
This is the code that I'm using to add the sliding menu to each list row:
private void makeSlidingMenu(View view) {
FrameLayout menuClosedFrame = // ... the above view
RelativeLayout menuLayout = // ... the behind view
SlidingMenu slidingMenu = new SlidingMenu(view.getContext());
slidingMenu.setContent(menuClosedFrame);
slidingMenu.setMenu(menuLayout);
slidingMenu.setBackgroundColor(Color.RED);
slidingMenu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
slidingMenu.setTouchModeBehind(SlidingMenu.TOUCHMODE_FULLSCREEN);
slidingMenu.setBehindScrollScale(1.0f);
slidingMenu.setFadeDegree(0.0f);
RelativeLayout layout = (RelativeLayout)view;
layout.addView(slidingMenu, new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT) {{
addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE);
addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE);
}});
}
It's not pretty, but it seems to get the job done on a normal activity that doesn't have a list view and is not a SlidingMenuActivity. It just doesn't work in a nested scenario with a ListView for me. Is there anything else I could be doing wrong? If posting more code would help let me know. Thanks!
Figured it out; the way I was adding the sliding menu to the row was causing it to be sized improperly I think; I fixed it by adding it to a framelayout instead that also contains the contents I want to show since i want the cell's main content to remain static.

Having application running above other app

I want to make an activity that can be opened above ANY app.
Normally, even when the activity is set as dialog, when you switch to my app, you see my app, and in the background you see the launcher:
BUT, I want the app will go above any app like this: (made in photoshop):
I did see this question Creating a system overlay window (always on top), but in ICS there is no functionallity to the layout.
Furthermore, I want to give a dialog box from my app without minimizing the other app...
there are plenty of apps that show a floating view on top of everything like : airbrowser , LilyPad , Stick it , AirTerm , Smart Taskbar , aircalc ...
anyway , in order to achieve this feature , you must have a special permission called "android.permission.SYSTEM_ALERT_WINDOW" , and use something like that:
final WindowManager.LayoutParams param=new WindowManager.LayoutParams();
param.flags=WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
final View view=findViewById(R.id.my_floating_view);
final ViewGroup parent=(ViewGroup)view.getParent();
if(parent!=null)
parent.removeView(view);
param.format=PixelFormat.RGBA_8888;
param.type=WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;
param.gravity=Gravity.TOP|Gravity.LEFT;
param.width=parent!=null?LayoutParams.WRAP_CONTENT:view.getLayoutParams().width;
param.height=parent!=null?LayoutParams.WRAP_CONTENT:view.getLayoutParams().height;
final WindowManager wmgr=(WindowManager)getApplicationContext().getSystemService(Context.WINDOW_SERVICE);
wmgr.addView(view,param);
// TODO handle overlapping title bar and/or action bar
// TODO you must add logic to remove the view
// TODO you must use a special permission to use this method :android.permission.SYSTEM_ALERT_WINDOW
// TODO if you wish to let the view stay when leaving the app, make sure you have a foreground service running.
I'm one of the developers of the Tooleap SDK, and we also dealt with this issue.
Basically, you don't need to use the SYSTEM_ALERT_WINDOW to display an activity on top of another one. You can just display a regular "shrinked" Activity with a transparent background.
To make a "shrinked Activity, change the activity window layout params of height and width:
WindowManager.LayoutParams params = getWindow().getAttributes();
params.x = ...;
params.y = ...;
params.width = ...;
params.height = ...;
this.getWindow().setAttributes(params);
To make a transparent background add to your activity definition in the manifest file:
android:theme="#android:style/Theme.Translucent"
That way, you can create the illusion of a floating activity:
Note that only the foreground activity will be resumed, while the background one is paused. But for most apps this shouldn't be an issue.
Now all that remains is when to launch the floating activity.
Here is an example of a "floating" calculator app using a regular activity. Note that the activity below the calculator belongs to another app.

Categories