I m using listfragment in my app. When the data fails to load I call setEmptyText with the failure message. This works fine on 4 inch phones but on 7 and 10 inch phones the size of the empty textview is very small and hard to read.
4 inch empty text view
7 inch empty text view
How can I increase the size of empty text view?
You can provide your own TextView to be used as the "empty text", it just needs to have android:id="#id/android:empty" and the ListFragment will use it instead of the standard one.
You could then customize the style of this TextView however you like (either statically in the xml layout):
<TextView android:id="#id/android:empty"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textSize="... />
or by calling findViewById() in onCreateView():
TextView emptyTextView = (TextView)view.findViewById(android.R.id.empty);
emptyTextView.setTextSize(...);
A (very good) alternative, as #Delblanco commented, is to call getListView().getEmptyView(). It will return either the TextView with id android.R.id.empty (if supplied in the layout), or the standard TextView that is used otherwise.
Here is a drop in extension for ListFragment which changes the size (and color) of the empty view programmatically:
/**
* Abstract list fragment that does customizations of the list style.
*/
public abstract class LoggingListFragment extends ListFragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = super.onCreateView(inflater, container, savedInstanceState);
TextView emptyView = null;
View possibleEmptyView = view.findViewById(0x00ff0001);
if (possibleEmptyView instanceof TextView) {
emptyView = (TextView) possibleEmptyView;
}
if (emptyView == null) {
possibleEmptyView = getListView().getEmptyView();
if (possibleEmptyView instanceof TextView) {
emptyView = (TextView) possibleEmptyView;
}
}
if (emptyView != null) {
emptyView.setTextColor(getActivity().getResources().getColor(R.color.list_subtitle_normal));
emptyView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 17);
}
return view;
}
}
Create different folders like res/values-sw720dp,res/values-sw600dp,
res/values-v11.
Create dimen.xml with different font size of different screen size.
Use values/dimen.xml:
<resources>
<dimen name="normal_font">16sp </dimen>
</resources>
4.Use in your widget in your xml.
android:textSize="#dimen/normal_font
I would do it a bit differently.
My idea: find out the dimensions of the screen and use a switch/case statement to adjust your textView text's size.
For example:
switch ( area ) {
case 7inch:
TextView emptyTextView = (TextView)view.findViewById(android.R.id.empty);
emptyTextView.setTextSize(...);
break;
case 4inch:
TextView emptyTextView = (TextView)view.findViewById(android.R.id.empty);
emptyTextView.setTextSize(...);
break;
default:
values_not_caught_above;
}
You can find the dimensions of the screen as follows:
If you want the display dimensions in pixels you can use getSize:
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;
int area = height*width;
If you're not in an Activity you can get the default Display via WINDOW_SERVICE:
WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
Before getSize was introduced (in API level 13), you could use the getWidth and getHeight methods that are now deprecated:
Display display = getWindowManager().getDefaultDisplay();
int width = display.getWidth(); // deprecated
int height = display.getHeight(); // deprecated
I like my way best because it's very easy to just add another case statement for an additional device(may be 10inch screen). Almost nothing in your code will need to change that way.
You may increase the size of the text by calling setEmptyText method with a Spannable, let me show you some code:
String yourEmptyTextMessage = getString(R.string.yourEmptyTextMessage);
Spannable spanText = new SpannableString(yourEmptyTextMessage);
spanText.setSpan(new RelativeSizeSpan(1.5f), 0, yourEmptyTextMessage.length(), 0);
setEmptyText(spanText)
You may want to change 1.5f multiplier value to desired ratio
Related
I have a ListView that every row shows different percentage.
and I wanna change one of inner layouts width (inside inflated counterView) for this purpose .
for example:
if first value item in listview is 10% then width set to 10px too
if second value item is 50% then width change to 50px too
something like this:
How can I do that ?
I tried these codes but it doesn't work and the width doesn't change:
(I defined match_parent width for layout inside XML and I wanna change it programmatically)
public class MyAdapterScores extends BaseAdapter {
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(R.layout.scores_layout,null,true);
//I wanna change 'relativeLayoutRightSqure' width inside 'scores_layout'
View layout = convertView.findViewById(R.id.relativeLayoutRightSqure);
ViewGroup.MarginLayoutParams p = (ViewGroup.MarginLayoutParams) layout.getLayoutParams();
//variable precentage has value
p.width=precentage ;
layout.requestLayout();
You can't just set the width to a percentage I think. I haven't tested this solution, but try it out! Might work!
One solution could be to set the width to a percentage of the total width of the display maybe:
WindowManager wm = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int layoutWidth = size.x * (percentage / 100)
Then you can use that variable "layoutWidth" to set the width on the layout you want.
please use the following code
RelativeLayout layout = convertView.findViewById(R.id.relativeLayoutRightSqure);
RelativeLayout.LayoutParams param=layout.getLayoutParams();
param.width=precentage ;
layout.setLayoutParams(param);
I found the problem !
the problem was defining android:layout_toRightOf and android:layout_toLeftOf for my inner counterView layout view simultaneously that causes setting width doesn't work !!
(
Referred to above code (relativeLayoutRightSqure):
View layout = convertView.findViewById(R.id.relativeLayoutRightSqure);
)
I am trying to create a bar chart in android where each bar consists of one Viewand one TextView.
I add these cells to a HorizontalScrollView. This works fine, all my bars appear.
Now I want to resize these bars(as they now all appear in full height) by multiplying them with a factor.
This is the activity class that i Use.
private ViewGroup linearScrollLayout;
private HorizontalScrollView hScrollView;
private ArrayList<NutrientCell> nutrientsData;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.box_plot);
this.hScrollView = (HorizontalScrollView) findViewById(R.id.horizontalScrollView1);
this.linearScrollLayout = (ViewGroup) this.hScrollView
.findViewById(R.id.linearScrollLayout);
try {
Intent intent = getIntent();
this.nutrientsData = (ArrayList<NutrientCell>) intent
.getSerializableExtra("nutrientsData");
//
} catch (Exception e) {
// TODO: handle exception
}
this.initChart();
}
private void initChart() {
for (NutrientCell c : this.nutrientsData) {
RelativeLayout rlt = (RelativeLayout) getLayoutInflater().inflate(
R.layout.hv_boxplot_cell, this.linearScrollLayout, false);
((TextView) rlt.findViewById(R.id.textView1)).setText(c
.getNutrientName().toString());
View v = ((View) rlt.findViewById(R.id.vv1));
v.setBackgroundColor(c.getColor());
this.linearScrollLayout.addView(rlt);
}
}
Now, in the loop found in initChart() I iterate over all elements in an ArrayList, each of which has a percentage variable. This is the variable I would like to multiply the height with.
I have tried to just multiply the height of the LayoutParams of the inflated RelativeLayout found in initChart but as this is called in onCreate(), its height property is not correct.
So how can I make sure that the inflated RelativeLayout I add to my View has a height corresponding to the percentage obtained from c(c.getPercentage())
What exactly is the percentage used for? If the percentage is used to scale the view based on it's initial size, you could probably use the view's setScaleY() method to change it's height.
In my app I display several text views containing text of various length that is loaded in at run time. I do not know the dimensions of the text view or the length of the text until run time. Sometimes, when the text is long and the textview small some of the text is partially visible, for example:
I want to remove the partially visible text as it looks a bit naff, but I can't find a way to do this. Any help would be appreciated!
Thanks,
Dave
You can hard code the TextView height in a way that the second row of text will not be visible.
Or use:
android:maxLines , Makes the TextView be at most this many lines tall.
as suggested above.
Put your textviews in a scrollview layout.And specify a specific width to your textview and make the height wrap content.So that your text doesn't get cut.
This is how I did it. I ran this code after the activity had loaded by posting the method CheckTextIsVisible to the parent relativelayout's handler queue, otherwise the height of the textviews will not be known:
m_eventsLayout.Post(new Action(CheckTextIsVisible));
Then the method CheckTextIsVisible finds each textview with text in it, calculates the height of the font, works out how many lines can fit in the textview, and sets the number of maximum lines accordingly:
private void CheckTextIsVisible()
{
View view;
TextView tView;
Android.Text.TextPaint tPaint;
float height;
int heightOfTextView;
int noLinesInTextView;
for (int i = 0; i < m_eventsLayout.ChildCount; i++)
{
view = m_eventsLayout.GetChildAt(i);
if (view is TextView)
{
tView = (TextView)view;
if (tView.Text != "")
{
//calculate font height
tPaint = tView.Paint;
height = CalculateTextHeight(tPaint.GetFontMetrics());
//calculate the no of lines that will fit in the text box based on this height
heightOfTextView = tView.Height;
noLinesInTextView = (int)(heightOfTextView / height);
//set max lines to this
tView.SetMaxLines(noLinesInTextView);
}
}
}
}
private float CalculateTextHeight(Android.Graphics.Paint.FontMetrics fm)
{
return fm.Bottom - fm.Top;
}
This results in no partially visible text!
I have created a basic RelativeLayout in my XML file. In my code, I want to dynamically create several ImageViews and place them at different locations within the RelativeLayout. Everything I've tried (ImageView.setX(), ImageView.setTranslationX(), ImageView.setPadding()) either says I need a higher API level (11+) or causes the ImageView to not appear.
If I do not try to do anything with the location of the ImageView, then the image does appear on the screen in the (0,0) position.
This simple app will layout 15 icons in three rows dynamically using RelativeLayout. There is no reason to use AbsoluteLayout - it is also deprecated.
The main activity.
public class MainActivity extends Activity {
private int mWidth;
private int mTile;
private int mColMax = 5;
private Context mContext;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mContext = this;
// the screen width is need to work out the tile size
mWidth = mContext.getResources().getDisplayMetrics().widthPixels;
// how wide (and high) each icon will be to fit the screen.
mTile = (mWidth / mColMax);
setContentView(R.layout.activity_main);
// layout the icons
initUI();
}
/**
* Layout 15 icon images in three rows dynamically.
*/
private void initUI() {
// this is the layout from the XML
ViewGroup layout = (ViewGroup) findViewById(R.id.main_layout);
ImageView iv;
RelativeLayout.LayoutParams params;
int i = 0;
int row = 0;
int col = 0;
do {
params = new RelativeLayout.LayoutParams(mTile,mTile);
params.setMargins((col * mTile), (row * mTile), 0, 0);
iv = new ImageView(mContext);
iv.setAdjustViewBounds(true);
iv.setScaleType(ScaleType.FIT_CENTER);
iv.setImageResource(R.drawable.ic_launcher);
iv.setLayoutParams(params);
layout.addView(iv);
if (col == mColMax) {
row++;
col = 0;
} else {
col++;
}
} while (++i <= 16);
}
}
And the layout XML.
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/main_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
</RelativeLayout>
RelativeLayouts are used to place items in relationship to other items. YOu don't use them to place layouts at specific positions like a setX. If you want to place the new item relative to existing items, look at RelativeLayout.LayoutParams- you can set layout_alignXXX and layout_toXXXOf type parameters through them.
If you need an exact pixel position, use the deprecated AbsoluteLayout. Just be aware its going to look ugly as heck on any device with a different aspect ratio or size screen without a ton of work.
My problem is that i want change width of my TEXTVIEW (INPUT TEXT).
In emulator that code work success but when i try to work with my app in mobile that crash it.
My answer is: Why my app crash when i try to change .setWidth? and how can i solve my problem?
A lot of thanks.
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.binomial);
EditText TextVA, TextVB, TextVC, EtiquetaA, EtiquetaB, EtiquetaC;
TextVA = (EditText)findViewById(R.id.editTextP);
TextVB = (EditText)findViewById(R.id.editTextK);
TextVC = (EditText)findViewById(R.id.editText3);
ArrayList<TextView> listaTexto = new ArrayList<TextView>();
listaTexto.add(TextVA);
listaTexto.add(TextVB);
listaTexto.add(TextVC);
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int y = size.y;
int x = size.x;
ResolucionPantalla respant = new ResolucionPantalla(y, x);
for (int i=0;i<listaTexto.size();i++)
//ResolInput METHOD return size in px according to the screen size
listaTexto.get(i).setWidth(respant.ResolInput());
Button BotonCalcular = (Button)findViewById(R.id.buttonCalcular);
BotonCalcular.setOnClickListener(this);
}
EDIT: TextView to EditText
I see several potential issues...
For starters:
TextVA = (TextView)findViewById(R.id.editTextP);
Is R.id.editTextP an EditText? You are casting it as a TextView
And also:
Calling setWidth here will have no effect. There is an onLayout method for each view that gets called after onCreate is finished. The width/height get set to whatever is specified in your XML layout during that call
You can either extend and create your own EditText and override the onLayout method, or you can figure out how to size your views correctly via XML.