Can I lock the position of an imageview? - java

EDIT: For those like me who just want an image locked in the upper left-hand corner of their screen, you can set a logo to replace the app launcher icon in the corner and that accomplished this easily.
I want to have an image in the upper left-hand corner of my screen in the ActionBar instead of the icon and name of the app. So far I have gotten it there but it moves and changes size depending on what other icons are in the action bar (I have icons appear and disappear). It is also centered on the remaining space and not simply in the center.
Is there a way I can lock this image to the lefthand corner? This is how fit it into the ActionBar below.
public void ActionBarCreation(){
getActionBar().setDisplayShowHomeEnabled(false);
getActionBar().setDisplayShowTitleEnabled(false);
ImageView img = new ImageView(getApplicationContext());
img.setImageResource(R.drawable.my_image);
// 7 padded on the bottoms so it does not cover the dividing line.
img.setPadding(0, 0, 0, 7);
getActionBar().setCustomView(img);
getActionBar().setDisplayShowCustomEnabled(true);
}

Put your theme for application as
<android:theme="#android:style/Theme.NoTitleBar">
-> this will get rid off your default toolbar with app name.
Next add your own toolbar layout in your activity layout
<android.support.v7.widget.Toolbar
android:id="#+id/my_awesome_toolbar"
android:layout_height="256dp"
android:layout_width="match_parent"
android:minHeight="?attr/actionBarSize"
android:background="?attr/colorPrimary"
>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left|center_horizontal"
android:id="#+id/image"
//your drawable and customizations here
android:background="#android/color/transparent"
/>
</android.support.v7.widget.Toolbar>
Now set this toolbar as your activity's toolbar:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.my_awesome_toolbar);
if (toolbar != null) {
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowTitleEnabled(false);
getSupportActionBar().setDisplayShowHomeEnabled(false);
}}

Related

How to add a back navigation icon to an appBarLayout in java

Add back navigation to an appBarLayout, I want to add a toolbar inside an appBarLayout in xml, and then add a back arrow to that toolbar.
I have followed this and tried to adjust it for my needs, with no luck.
Display Back Arrow on Toolbar
According to Android Docs
AppBarLayout is a vertical LinearLayout
So you will need to include your toolbar and other components inside it, something like:
<android.support.design.widget.AppBarLayout
android:layout_height="wrap_content"
android:layout_width="match_parent">
<android.support.v7.widget.Toolbar
...
app:layout_scrollFlags="scroll|enterAlways"/>
<android.support.design.widget.TabLayout
...
app:layout_scrollFlags="scroll|enterAlways"/>
</android.support.design.widget.AppBarLayout>
Then you can add your back navigation button to the toolbar:
Toolbar toolbar = (Toolbar) findViewById(R.id.Your_Toolbar);
setSupportActionBar(toolbar);
And then calls to:
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
Override method:
#Override
public boolean onSupportNavigateUp() {
onBackPressed();
return true;
}
In your activity do the following in onCreate
ActionBar bar = getSupportActionBar();
if(bar != null){
bar.setDisplayHomeAsUpEnabled(true);
}

Android Multiline Toolbar Title

I have a Toolbar that when in landscape mode isn't as wide as usual, but it has more height than usual and for that reason I want to set the title to be multiline (2 lines to be precise) when it is in landscape. I have tried some things where I put a TextView inside of the Toolbar, but when I try to access the TextView to set it's title (since the the title is variable) I get a NullPointer after using findViewById.
Here is some code of the current situation (without multiline):
#Override
protected void onResume() {
super.onResume();
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar2);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = (int)( size.x * 0.37);
int height = Math.round(size.x * 0.63f * 0.25f);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(width,height);
toolbar.setLayoutParams(params);
}
if (executeOnResume) {
currentProject = getIntent().getParcelableExtra("Project");
getSupportActionBar().setTitle("This is a pretty long piece of text, I hope it fits");
// A lot of irrelevant code...
//...
//...
} else { loadStuff();}
}
And here is the toolbar xml:
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar2"
android:title="#string/menuLabel1a"
app:layout_widthPercent="37%"
app:layout_heightPercent="26%"
android:gravity="top"
app:titleTextAppearance="#style/toolbarTitleText"
android:background="#color/greyLight"
android:theme="#style/AppTheme.AppBarOverlay"
app:popupTheme="#style/AppTheme.PopupOverlay" />
Try this :
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar2"
android:layout_width="match_parent"
android:layout_height="#dimen/double_height_toolbar"
android:gravity="top"
app:titleTextAppearance="#style/toolbarTitleText"
android:background="#color/greyLight"
android:theme="#style/AppTheme.AppBarOverlay"
app:popupTheme="#style/AppTheme.PopupOverlay">
<TextView
android:id="#+id/product_details_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="top"
android:title="#string/menuLabel1a"
android:paddingTop="#dimen/padding_normal"
style="#android:style/TextAppearance.Holo.Widget.ActionBar.Title.Inverse"
android:maxLines="2" />
</android.support.v7.widget.Toolbar>
Other Solution is for ActionBar
The default TextView of the ActionBar does not support line wrapping, and there is no exposed method to set it to wrap. So you can create a flexible layout, like this: action_bar_title_layout.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="#+id/action_bar_title"
android:layout_height="match_parent"
android:layout_width="wrap_content"
android:gravity="center_vertical"
android:ellipsize="end"
android:maxLines="2"/>
</LinearLayout>
Then set this as the custom layout on your ActionBar:
ActionBar actionBar = getActionBar();
actionBar.setDisplayShowCustomEnabled(true);
actionBar.setCustomView(R.layout.action_bar_title_layout);
((TextView) findViewById(R.id.action_bar_title)).setText(
"This is a long text title that will wrap to multiple lines, when necessary.");
Found this solution who was work for me :
fun Toolbar.disableTitleSingleLine() {
for (i in 0..childCount) {
val child = getChildAt(i)
if (child is TextView) {
child.isSingleLine = false
}
}
}
According Toolbar implementation, the title TextView is created when text been set.
It's important to call this extension after title set.

LayoutParams using AddRules

I know there are related questions but please try my code 1st
I think my code is correct but I don't know why I can't get want I want
I'm trying to Replace the image into a RelativeLayout with the same LayoutParams dimension and placement(programmatically) then inside that RelativeLayout put a WebView in the center of it.
It was almost done but I'm having trouble for the placement of Webview.
see the images below
activity_main.java
#Override
protected void onCreate(Bundle savedInstanceState) {
.........
// Image
ImageView image = (ImageView)findViewById(R.id.image);
// Container of the image
RelativeLayout rl = (RelativeLayout) findViewById(R.id.rl_imageView);
loader(rl , image);
}
public void loader(RelativeLayout rl,View view){
// Creating RelativeLayout
RelativeLayout rlnew = new RelativeLayout(this);
// Getting the Layout Parameters of the image such as (position and dimension)
RelativeLayout.LayoutParams lp1 = (RelativeLayout.LayoutParams) view.getLayoutParams();
// Coloring the background of the new Relative Layout into blue
rlnew.setBackgroundColor(getResources().getColor(R.color.colorPrimary));
// passing the parameters to the new relative layout from the image
rlnew.setLayoutParams(lp1);
// image turns into invisible
view.setVisibility(View.INVISIBLE);
// Creating a webView
WebView webView = new WebView(this);
// load the loading.gif
webView.loadDataWithBaseURL("file:///android_asset/", "<center><img src='loading.gif' style=''/></center>", "text/html", "utf-8", null);
// setting up dimension(height and weight) for the webview
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(100, 60);
// adding position(Center) for the webview
lp.addRule(RelativeLayout.CENTER_IN_PARENT);
// setting up Layout parameters for the webiview
webView.setLayoutParams(lp);
// adding the webview to the new Relative Layout
rlnew.addView(webView);
// adding the new relative layout into the container
rl.addView(rlnew);
}
activity_main xml
<RelativeLayout
android:layout_width="250dp"
android:layout_height="100dp"
android:background="#ec0c0c"
android:id="#+id/rl_imageView"
android:layout_marginTop="47dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true">
<!--Desired Output-->
<!--<RelativeLayout-->
<!--android:layout_width="70dp"-->
<!--android:layout_height="70dp"-->
<!--android:background="#010e6e"-->
<!--android:layout_alignParentTop="true"-->
<!--android:layout_alignParentEnd="true">-->
<!--<WebView-->
<!--android:layout_width="60dp"-->
<!--android:layout_height="30dp"-->
<!--android:id="#+id/imageView"-->
<!--android:layout_centerVertical="true"-->
<!--android:layout_centerHorizontal="true" />-->
<!--</RelativeLayout>-->
<!--Default Image-->
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/sample1"
android:id="#+id/image"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true">
</ImageView>
</RelativeLayout>
Output Images
I'm making a method that will ask for two parameters a RelativeLayout(that contains the second parameter of the method) and View so it can be anything like(Image, TextView, RelativeLayout , etc.,).
This code can replace a specific View like a relativeLayout or textView or Image
and replace it with a relativeLayout with the same size and position of the given View(mine was the image)(It was already Done if you move that image anywhere in the given container the code will replace the image into a relativeLayout no matter where it is as long inside of the given container).
The only problem is the WebView position. . why is webView there?. .I want it in the center of the new RelativeLayout
Can someone tell where did I go wrong
If you have another way just submit answer
Your code is correct, but your xml is not. Try to change your xml below and test again:
<ImageView
android:layout_width="your dimen in dp < rl_imageView. width"
android:layout_height="your dimen in dp < rl_imageView. height"
android:src="#drawable/sample1"
android:id="#+id/image"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true">
</ImageView>
P/S: I suggest create this Imageview programmatically also.

How to set a little arrow next to icon in toolbar?

I want something like this: link . A little arrow and icon but in a toolbar.
EDIT:
I have tried this:
mToolbar = (CustomToolbar) findViewById(R.id.toolbar);
setSupportActionBar(mToolbar);
mToolbar.setNavigationIcon(R.drawable.ic_ab_back_holo_light);
mToolbar.setTitle(mMatchName);
mToolbar.setLogo(R.mipmap.ic_options);
getSupportActionBar().setHomeButtonEnabled(true);
mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
finish();
overridePendingTransition(0, R.anim.slide_right);
}
});
But this makes a big distance between arrow icon and Logo.
Question:
How do I put an arrow and a logo next to each other?
Your link takes you to an example of the old ActionBar, but you seem to be using the new toolbar. The new toolbar design doesn't have icons like the old version, only text, so the extra space is by design. If you want to use something like you linked then you need to use the ActionBar not a toolbar.
Here is a link on the new toolbar:
https://blog.xamarin.com/android-tips-hello-toolbar-goodbye-action-bar/
vs. the old ActionBar
https://developer.android.com/training/basics/actionbar/index.html
After some experiments I've found how to achieve in a toolbar the Up Navigation like this one:
We can create a custom layout and put it in a toolbar.
1) in onCreate method:
mToolbar = (CustomToolbar) findViewById(R.id.toolbar);
setUpCustomUpNavigation();
setSupportActionBar(mToolbar);
// to disable title in a toolbar
getSupportActionBar().setDisplayShowTitleEnabled(false);
2) setUpCustomUpNavigation() method.
private void setUpCustomUpNavigation() {
LayoutInflater mLayoutInflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View customUpNavView = mLayoutInflater.inflate(R.layout.up_navigation_layout, null);
// "ic_ab_back_holo_light.png" Located in ...android-sdk\platforms\android-14\data\res\drawable-xhdpi
ImageView backArrowImageView = (ImageView) customUpNavView.findViewById(R.id.backArrowImageView);
ImageView logoToolbarImageView = (ImageView) customUpNavView.findViewById(R.id.logoToolbarImageView);
TextView titleTextView = (TextView) customUpNavView.findViewById(R.id.titleTextView);
// set logo
logoToolbarImageView.setImageResource(R.drawable.help_toolbar_ic_selector);
// set title
titleTextView.setText(R.string.title_activity_help);
mToolbar.addView(customUpNavView);
}
3) toolbar.xml
<?xml version="1.0" encoding="utf-8"?>
<net.stargorod.dating.toolbar.CustomToolbar xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?actionBarSize"
android:background="#color/colorPrimary"
android:minHeight="?actionBarSize"
style="#style/MyToolbar" />
4) in style.xml create the style: MyToolbar.
<!-- Toolbar style -->
<style name="MyToolbar" parent="Widget.AppCompat.ActionBar">
<item name="contentInsetStart">0dp</item>
<item name="contentInsetEnd">0dp</item>
<item name="android:padding">0dp</item>
</style>
Result:
That's it!
P.S. I'm sorry for my grammar mistakes.

Show activity's view overlay FrameLayout with Fragment

I have the following activity:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="es.xxx.xxx.MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:background="#CCFF0000"
android:id="#+id/lyNetworkError">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="No hay conexión a internet"
android:textAlignment="center"/>
</LinearLayout>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/container"/>
</RelativeLayout>
In its FrameLayout the app will load other fragments.
This is the onCreate code of activity
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Constants.setAppContext(this);
setContentView(R.layout.activity_main);
Log.d("LoadFragment", "1 "+ loadFragment);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction().replace(R.id.container, new MainFragment()).commit();
}
IntentFilter filter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);
registerReceiver(networkStateReceiver, filter);
fragmentManager = getSupportFragmentManager();
lyNetworkError = (LinearLayout) findViewById(R.id.lyNetworkError);
}
The problem is that LinearLayout (That contains TextView) doesn't show (is posible that fragment render over LinearLayout, because if I remove getSupportFragmentManager().beginTransaction().replace(R.id.container, new MainFragment()).commit(); the LinearLayout appears)
So, how can I show the LinarLayout over fragment (loaded inside FrameLayout)?
If the LinearLayout and your Fragments are displaying in the correct location on screen when each is shown individually, then you can simply reverse the order of of the FrameLayout and LinearLayout in your XML.
The problem is that RelativeLayout allows its children to overlap. The last item in the RelativeLayout will appear "above" or "on top" of other items in the layout. Since you haven't specified any layout constraints for your views, the RelativeLayout puts them both in the default position, which is the top left corner. Since your FrameLayout is set to fill the parent view's width and height, it will overlay everything else.
If you actually want the LinearLayout to appear above the FrameLayout, then you can use RelativeLayout's positioning properties (explained very well here) to position your views.
Specifically, you would be looking for something like this:
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#id/lyNetworkError"
android:id="#+id/container"/>
The android:layout_below attribute tells the FrameLayout that you want it to always be below the view with ID lyNetworkError (below as with text on a piece of paper, not in 3-dimensional space).

Categories