How to specify Custom Tab View without an image? - android

I have created a layout XML and called setCustomTabView in the right sequence and verified that it's pretty much as described here.
How can custom tab view be implemented using setCustomTabView method in SlidingTabColor sample?
I don't want an image. I only want to control the text size. I want the default on a small phone, but twice the size on a tablet. The default is very small on a tablet. The app is used outdoors and visibility and large buttons is important. I have two "buckets" so far. The following examples are for the default.
If I use this xml layout, I only see the text for the first tab, and no colorizer.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- Default sized TextView for tab title.
This allows us to specify a larger one for tablets in layout-w720dp. -->
<TextView
android:id="#+id/tab_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textSize="18dp"/>
</LinearLayout>
If I add the xml for an image and provide a source, it works. If I leave out the source, the result is the same as for a layout with only a TextView.
<TextView
android:id="#+id/tab_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textSize="18dp"/>
<ImageView
android:layout_width="wrap_content"
android:src="#drawable/ic_launcher"
android:layout_height="wrap_content" />
In one of my experiments I increased the text size (very large) and without an image I got all four tab titles.
How can I replicate the default tab titles with only a TextView?
(I had a working solution where I detected the metrics at run time and doubled the text size, but I want to control this through different layout subfolders.)
I see that the default tabView is a simple, instantiated TextView and tabTitleView is equated to it. If I could do a findViewByIdon the TextView within my layout I could do the same. But attempts to do that resulted in a null TextView - probably because a ViewGroup is not yet established. But if I use my layout the child already has a parent and can't be used in the tab strip's addView().

This worked.
<TextView
android:id="#+id/tab_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textSize="12sp"
android:gravity="center_vertical|center_horizontal"
android:padding="16dp" />
Using the debugger (and looking at the source where the default tabView is created) I saw that gravity was 0x800033 for my TextView and 0x000011 for theirs. Changing the android:gravity and adding the same padding as the default fixed it. I think center would also work. It's an alias for vertical|horizontal (1<<4|1<<0).
I also called setAllCaps(true) on the tabTitleView for consistency.

Related

How to fix imageview positions but have variable textview above them in Android

I am currently working on an app and my layout is as follows:
I have a recyclerview holding all of my items, each item has a textview above an imageview. So the text view can expand up to 3 lines but only if needed. The recyclerview is using a gridlayout manager to display all of my items.
The problem is that currently when the text view expands to have more than 1 line it presses down the image instead of keeping it aligned with the other images in the row
What I currently have
What I want is to prevent the text view from pressing down on the image but instead build it's way up so that way the images stay normal and the text view will grow upwards like the image below.
What I want
Is there any way to achieve this? I would think it shouldn't be hard but I can't seem to find a way to accomplish this, or more likely, can't google the right terms to find the solution I'm looking for.
So I found an answer, I had to set two properties for my textview:
android:lines="3"
android:gravity="bottom"
so in the end each item had a layout below:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:paddingStart="31dp"
android:paddingEnd="32dp">
<TextView
android:id="#+id/app_name"
android:layout_width="#dimen/product_list_image_dimensions"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:text="#string/app_card_temp_name"
android:textSize="#dimen/product_list_text_size"
android:fontFamily="sans-serif-condensed"
android:lines="3"
android:textAlignment="center"
android:textColor="#color/app_name_product_list_color"
android:typeface="normal"
android:gravity="bottom" />
<ImageView
android:id="#+id/app_thumbnail"
android:layout_width="#dimen/product_list_image_dimensions"
android:layout_height="#dimen/product_list_image_dimensions"
android:layout_below="#id/app_name"
android:paddingTop="#dimen/product_list_text_bottom_padding"
android:scaleType="centerCrop"
android:layout_centerHorizontal="true" />
</RelativeLayout>

AutoResizeTextView does not resize after other Views are hidden (View.GONE)

Context:
I have two AutoResizeTextViews inside a LinearLayout. I would like to have the first one about 1/3 of the size of the second one, and both as large as possible. Therefore I use layout_weight=1 and layout_weight=3 as can be seen in the XML code below.
Problem:
When I hide other TextViews in the View of the Fragment (stored as mView), using:
textView7.setVisibility(View.GONE);
(..many more to GONE..)
more space gets available for the LinearLayout, and so I expect the AutoResizeTextViews to grow. However the two AutoResizeTextViews do not grow, and seem to keep their canvas size. They are moved to align to the new 1/3 ratio of the LinearLayout, so there really is space inside the LinearLayout.
When the Activity and the View is restarted upon device reorientation (I do not retain the Fragment) they DO change as expected, apparently the canvas is redrawn correctly then. How can I make the AutoResizeTextView to be redrawn correctly without have to turn the device?
Already tried:
mView.forceLayout();
mView.refreshDrawableState();
mView.requestLayout();
mView.invalidate();
linearLayoutAutoFit.invalidate();
tvTimer.invalidate();
tvTimer.setTextsize(999);
is all not working, the Text is not adjusted to the new size.
XML code:
<LinearLayout
android:id="#+id/linearLayoutAutoFit"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/viewline2"
android:layout_below="#id/progressBar3"
android:layout_alignParentLeft="true"
>
<AutoResizeTextView
android:layout_weight="3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="#string/Ready"
android:id="#+id/textViewAction"
android:textSize="999sp"
android:textColor="#ADFF2F"
android:singleLine="true" />
<AutoResizeTextView
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:id="#+id/tvTimer"
android:textColor="#fff"
android:text="#string/init_time"
android:textSize="999sp"
android:visibility="visible"
android:singleLine="true" />
</LinearLayout>
instead :
android:layout_height="wrap_content"
type :
android:layout_height="0dp"
Although the answer by Kastriot was very close, and also part of the solution, I found the solution today by testing the values inside the AutoResizableTextView class with Log outputs. The mistake I made was that I also had
android:layout_width="wrap_content"
Because of that, upon layout change, the width of the AutoResizableTextView was not adjusted! As a consequence, the text could not grow in size. Therefore the setting for the width should be:
android:layout_width="fill_parent"
such that the width of the AutoResizableTextView is large enough to fit a larger text.

Changing the Size of my ListView by one Pixel messes up Formatting

Let me show you two links to demonstrate my point:
http://i.snag.gy/QP1i2.jpg (The ListView is 60 pixels)
http://i.snag.gy/DvXsL.jpg (The ListView is 61 pixels)
The whole file is done with an outer Vertical LinearLayout to provide weighted percentages (e.g. the ListView is 75%, the search part is 4%, so on and so forth) independent of device screen size. Within that outer LinearLayout I have a nested Layout (Linear or Relative) depending on my needs for the particular row.
It works beautifully, except for the last part. And it seems a lot of the items I try adding end up messing the formatting so I'm not sure if the problem is how I'm doing the ListView. Anyways, here's the relevant code:
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="0dip"
android:background="#null"
android:layout_weight="0.75"
android:paddingLeft="0dip" >
<ImageView
android:id="#+id/searchdivider"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="2dp"
android:contentDescription="#string/desc"
android:src="#drawable/searchdivider" />
<ListView
android:id="#+id/listView1"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_alignParentLeft="true"
android:layout_below="#+id/searchdivider" >
</ListView>
</RelativeLayout>
Thank you for your help!
EDIT: I should add that I don't want the height to be 60dp, obviously. I want it to fill_parent; however I picked the arbitrary value of 60 and 61 to figure out exactly what change makes the layout mess up.
2nd EDIT: I think I figured out the problem. It seems when I make the Theme AppCompat, it looks and acts fine. But when I make the Theme NoTitleBar (with or without fullscreen) then it acts all screwy. I haven't changed it in the manifest but rather the place in the graphical layout that lets you modify it for that one activity only.
Any suggestions?
android:gravity="left" on imageView and android:gravity="right" on the other listView

Autoresizing of text in Android

In iOS there is a notion of UIView's being able to resize themselves based on constraints given by the parent View. For example, if the parent View enlarges itself, then the child Views might expand or shrink to fit the available space. This is built in to the platform and makes development trivial.
So, my problem is that I am developing a smartphone application that makes use of the ActionBar in ICS. I have a custom view in there that I set using:
actionBar.setCustomView(R.layout.my_custom_title);
Note that I'm just inflating a view from the 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/title"
style="#style/TextAppearance.Title.Theme"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="end"
android:scrollHorizontally="true"
android:singleLine="true"
android:text="Title" />
<TextView
android:id="#+id/subtitle"
style="#style/TextAppearance.SubTitle.Theme"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="end"
android:scrollHorizontally="true"
android:singleLine="true"
android:text="Subtitle" />
</LinearLayout>
Now, in ICS, the ActionBar changes its height during an orientation change. It is narrower in landscape mode than in portrait mode. This causes my "subtitle" text to be cut off in landscape mode since the height of the ActionBar has shrunk, while my custom title view text has not resized itself.
Is it possible to resize the text on orientation change without programmatically doing so?
I'm overring
onConfigurationChanged()
so I can't just have a separate landscape and portrait custom title view.
SIDE NOTE:
This reminds me... I wish in the onConfigurationChanged() we can just supply a new xml layout that basically just adjusts the positioning of the views on the screen. The portrait and xml layouts would have to contain the same views of course, but there would be different layout information. This would make life much easier and would be more efficient than having onCreate() called again.
Your TextView has changed its size but the text size has not changed. There is no way to change the text size without some extra effort but you have different options:
you define different text size values for portrait and landscape mode and dont override onConfigurationChanged. This can by done by creating two dimensions.xml. One is inside res/values-land and the other in res/values-port
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="font_size">16dp</dimen>
</resources>
And in your layout assign that value to your TextView
<TextView
android:textSize="#dimen/font_size"
Second option is to compute the font size programatically. This was already covered in another question

Draw an Image beside an Edit text in android

I want to draw an image on the left of an EditText. I don't want the image appear insde the EditText though.
I use this:
<EditText
android:id="#+id/firstNameTxt"
style="#style/UserInfoInputs"
android:drawablePadding="20dp"
android:drawableLeft="#drawable/first_name" >
</EditText>
It displays the image inside of the EditText. However I use this on TextView and it works fine:
<TextView
android:id="#+id/positionValue"
style="#style/userInfo"
android:drawableLeft="#drawable/position" />
How this can be done for an EditText?
try this
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/go_image"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:src="#drawable/icon"
/>
<EditText
android:id="#+id/url"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:lines="1"
android:layout_weight="1.0" />
</LinearLayout>
let me know if this works.
The difference between the two use-cases you describe are simple. There is no difference. With an EditText, there are lines that are easily discernible. With a TextView there is not. Try setting the background property of the textview, and you'll see that the drawable is, in fact, drawn on the left side, but still 'inside', the TextView.
The simplest way to accomplish your task (as described) is to utilize an ImageView. Depending on what ViewGroup (LinearLayout, RelativeLayout, etc) you're using, the code may be a bit different; so, update your question with the appropriate info and I'll make my answer a bit more specific.
I should note that, another method you could use is to create your own custom component, which is really very easy to do. See this article Custom Components | Android Developer, be sure to scroll down to the Compound Controls heading title "Compound Controls". This would be especially helpful when this is a 'common' format of controls that you'll use often (I.E. you have an image next to a TextView throughout your app).
That is because the background part of the EditText stretches behind the entire contents of the view, including the drawables.
If you are using a RelativeLayout you can just add a separate ImageView:
<ImageView
layout_width="wrap_content"
layout_height="wrap_content"
layout_toLeftOf="#+id/firstNameTxt"
src="#drawable/first_name"
other imageview attributes as neccessary...
/>
<EditText
android:id="#+id/firstNameTxt"
style="#style/UserInfoInputs"
/>
Or if you use another kind of layout, create a LinearLayout-wrapper:
<LinearLayout
layout_width="wrap_content"
layout_height="wrap_content">
<ImageView
layout_width="wrap_content"
layout_height="wrap_content"
layout_toLeftOf="#+id/firstNameTxt"
src="#drawable/first_name"
other imageview attributes as neccessary...
/>
<EditText
android:id="#+id/firstNameTxt"
style="#style/UserInfoInputs"
/>
</LinearLayout>

Categories

Resources