How to align tab text at center in android - android

Can anybody tell me how to align tab text in the center in android? I had given android:gravity="center" for LinearLayout and TabWidget. It is still not working. Can anybody tell me how to do this?
Thanks

use this
int tabCount = tabHost.getTabWidget().getTabCount();
for (int i = 0; i < tabCount; i++) {
final View view = tabHost.getTabWidget().getChildTabViewAt(i);
if ( view != null ) {
// reduce height of the tab
view.getLayoutParams().height *= 0.66;
// get title text view
final View textView = view.findViewById(android.R.id.title);
if ( textView instanceof TextView ) {
// just in case check the type
// center text
((TextView) textView).setGravity(Gravity.CENTER);
// wrap text
((TextView) textView).setSingleLine(false);
// explicitly set layout parameters
textView.getLayoutParams().height = ViewGroup.LayoutParams.FILL_PARENT;
textView.getLayoutParams().width = ViewGroup.LayoutParams.WRAP_CONTENT;
}
}
}

Use android:layout_gravity="center"

Related

Custom Listview/Recyclerview

I want to implement custom listview in android that has row from smaller to bigger text as below.
If not listview/recyclerview then what other component of android can i use?
Any help will be appreciated.
For listview you will need something like this, after populating your list:
private void changeSizes() {
View v;
View v1;
TextView tv; // this one depends on your layout
TextView tv1;
int count = myList.getCount();
int middleRowIndex = (int)count/2 ;
TextView midRow = myList.getAdapter().getView(middleRowIndex, null, null).findViewById(R.id.textView);
midRow.setTextSize(TypedValue.COMPLEX_UNIT_SP,(int)(count * middleRowIndex));
int prevIndex = middleRowIndex - 1;
int nextIndex = middleRowIndex + 1;
while(prevIndex >=0 && nextIndex < count){
v = myList.getAdapter().getView(prevIndex, null, null);
tv = (TextView) v.findViewById(R.id.textview);
tv.setTextSize(TypedValue.COMPLEX_UNIT_SP,(int)(count * prevIndex));
v1 = myList.getAdapter().getView(nextIndex, null, null);
tv1 = (TextView) v.findViewById(R.id.textview);
tv1.setTextSize(TypedValue.COMPLEX_UNIT_SP,(int)(count * prevIndex));
prevIndex--;
nextIndex++;
}
}
v,v1,tv and tv1 are views inside your custom listview so you might need to change them, also the way of calculating font sizes and setting it you might want to change it.
P.S i didn't try this code on an app, but it should work let me know in case you encountered some error.

How to set icon size in TabLayout?

Here is my code:
private TabLayout tabLayout;
private int[] tabIcons = {
R.mipmap.ic_compass,
R.mipmap.ic_place,
R.mipmap.ic_passport,
R.mipmap.ic_setting
};
...
tabLayout.getTabAt(0).setIcon(tabIcons[0]);
tabLayout.getTabAt(1).setIcon(tabIcons[1]);
tabLayout.getTabAt(2).setIcon(tabIcons[2]);
tabLayout.getTabAt(3).setIcon(tabIcons[3]);
The size of icon is according to image size. How could I resize it?
set icons padding
for (int i = 0; i < tablayout.getTabWidget().getChildCount(); i++)
{
tablayout.getTabWidget().getChildAt(i).setPadding(10,10,10,10);
}
If you don't want to use xml for the custom view. Simply create imageview on runtime and add that in tablayout in a particular tab.
ImageView imgView= new ImageView(MainActivity.this);
imgView.setImageResource(drawableImage);
imgView.setPadding(10,10,10,10)
tabLayout.getTabAt(1).setCustomView(imgView);
It will look like this.
change of Icon size android.support.design.widget.TabLayout
for (int i = 0; i < view_bottom_tabLayout.getTabCount(); i++)
{
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(60, 60); //set new width & Height
params.gravity = Gravity.CENTER; //set gravity back to center
view_bottom_tabLayout.getChildAt(i).setLayoutParams(params);//set ur new params
}
To get total control of size I recommend using a custom view to render the tab.
First create a new layout - named "my_custom_tab" for this example
<?xml version="1.0" encoding="utf-8"?>
<!--Change the width, height, scaleType to whatever you like-->
<ImageView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="fitCenter"
android:id="#+id/icon"
android:layout_gravity="center_horizontal"
android:src="#mipmap/ic_launcher"/>
Now in your code set the custom image
private void setUpTabs (ViewPager viewPager) {
TabLayout tabs = (TabLayout) findViewById(R.id.tabs);
if (tabs != null) {
tabs.setupWithViewPager(viewPager);
int tabCount = tabAdapter.getCount(); //Assuming you have already somewhere set the adapter for the ViewPager
for (int i = 0; i < tabCount; i++) {
TabLayout.Tab tab = tabs.getTabAt(i);
if (tab != null){
ImageView myCustomIcon = (ImageView) LayoutInflater.from(tabs.getContext()).inflate(R.layout.my_custom_tab, null);
/*Here is where to set image if doing it dynamically
myCustomIcon.setImageBitmap(bitmap);
*/
tab.setCustomView(myCustomIcon);
}
}
}
}
you can do this:
LinearLayout ll = (LinearLayout) tabLayout.getChildAt(0);
for (int i = 0; i < ll.getChildCount(); i++) {
LinearLayout tabView = (LinearLayout) ll.getChildAt(i);
for (int j = 0; j < tabView.getChildCount(); j++) {
if (tabView.getChildAt(j) instanceof TextView) {
TextView textView = (TextView) tabView.getChildAt(j);
LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) textView.getLayoutParams();
layoutParams.topMargin = 0;
textView.setLayoutParams(layoutParams);
} else if (tabView.getChildAt(j) instanceof ImageView) {
ImageView imageView = (ImageView) tabView.getChildAt(j);
LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) imageView.getLayoutParams();
layoutParams.bottomMargin = 0;
imageView.setLayoutParams(layoutParams);
}
}
}
I decide to close this question. Since we actually cannot set specific size for icon in TabLayout but padding to it.
Ref:
https://developer.android.com/guide/practices/ui_guidelines/icon_design_tab
one code worked for me, the answer above of Deepak Sachdeva
just i edited it a litlle, try it 100% working
from first i add the tab with custom image
ImageView imgView= new ImageView(getApplicationContext());
imgView.setImageResource(R.drawable.icon);
imgView.setScaleType(ImageView.ScaleType.FIT_CENTER);
imgView.setLayoutParams(new LinearLayout.LayoutParams(100, 100));
tablayout1.addTab(tablayout1.newTab().setCustomView(imgView));
and do that to all your tabs, add them by this way.

TextView with too long Strings

I have this layout
<ScrollView>
<TextView android:id="#+id/textView" />
</ScrollView>
When I try to set a too long text, this Textview doesn't show that.
//OnCreate
//...
TextView textView = (TextView) findViewById(R.id.textView);
textView.setText("..."); //Here is a text with more than 2500
//chars and at least have 10 \n char
//(it means has at least 10 paragraph)
How can do I show that text?
Edit One :
Even I set a background to that TextView and the TextView does'nt show that background
you can set
android:maxLines="1"
android:ellipsize="end"
to your textview, so the text which long than your textview will be hide.
Because android:singleLine="true" attribute has been deprecated, set the following attributes to your TextView:
android:ellipsize="end"
android:maxLines="1"
The TextView will then show only the text that can fit to your TextView, followed by an ellipsis ...
i dont know if this will help you but saw this on developer.android...
With Android 8.0 (API level 26) and higher, you can instruct a TextView to let the text size expand or contract automatically to fill its layout based on the TextView's characteristics and boundaries. This setting makes it easier to optimize the text size on different screens with dynamic content.
android:autoSizeTextType="uniform"
There are three ways you can set up the autosizing of TextView:
Default
Granularity
Preset Sizes
=================================================
readMore
Use this :
EditText thumbnailView = (EditText) findViewById(R.id.enterBearingNo_editText_id);
TextView messageView = (TextView) findViewById(R.id.string2);
String text = "LargeText";
Display display = getWindowManager().getDefaultDisplay();
FlowTextHelper.tryFlowText(text, thumbnailView, messageView, display);
FlowTextHelper.class
class FlowTextHelper {
static boolean mNewClassAvailable;
static {
if (Integer.valueOf(Build.VERSION.SDK) >= 8) { // Froyo 2.2, API level 8
mNewClassAvailable = true;
}
// Also you can use this trick if you don't know the exact version:
/*
* try {
* Class.forName("android.text.style.LeadingMarginSpan$LeadingMarginSpan2"
* ); mNewClassAvailable = true; } catch (Exception ex) {
* mNewClassAvailable = false; }
*/
}
public static void tryFlowText(String text, View thumbnailView,
TextView messageView, Display display) {
// There is nothing I can do for older versions, so just return
if (!mNewClassAvailable)
return;
// Get height and width of the image and height of the text line
thumbnailView.measure(display.getWidth(), display.getHeight());
int height = thumbnailView.getMeasuredHeight();
int width = thumbnailView.getMeasuredWidth();
float textLineHeight = messageView.getPaint().getTextSize();
// Set the span according to the number of lines and width of the image
int lines = (int) Math.round(height / textLineHeight);
// For an html text you can use this line: SpannableStringBuilder ss =
// (SpannableStringBuilder)Html.fromHtml(text);
SpannableString ss = new SpannableString(text);
ss.setSpan(new MyLeadingMarginSpan2(lines, width), 0, ss.length(),
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
messageView.setText(ss);
// Align the text with the image by removing the rule that the text is
// to the right of the image
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) messageView
.getLayoutParams();
int[] rules = params.getRules();
rules[RelativeLayout.RIGHT_OF] = 0;
}
public static void tryFlowTextPrice(String text, TextView messageView,
Display display) {
// There is nothing I can do for older versions, so just return
if (!mNewClassAvailable)
return;
// Get height and width of the image and height of the text line
// thumbnailView.measure(display.getWidth(), display.getHeight());
// int height = thumbnailView.getMeasuredHeight();
// int width = thumbnailView.getMeasuredWidth();
float textLineHeight = messageView.getPaint().getTextSize();
// Set the span according to the number of lines and width of the image
// int lines = (int) Math.round(height / textLineHeight);
// For an html text you can use this line: SpannableStringBuilder ss =
// (SpannableStringBuilder)Html.fromHtml(text);
SpannableString ss = new SpannableString(text);
// ss.setSpan(new MyLeadingMarginSpan2(lines, width), 0, ss.length(),
// Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
messageView.setText(ss);
// Align the text with the image by removing the rule that the text is
// to the right of the image
// LinearLayout.LayoutParams params = (LinearLayout.LayoutParams)
// messageView
// .getLayoutParams();
// int[] rules = params.getRules();
// rules[RelativeLayout.RIGHT_OF] = 0;
}
}

HorizontalScrollView.getMeasuredWidth() returns 0

I am adding horizontalScrollView programatically , but when I try to do horizontalScrollView.getMeasuredWidth() it keeps returning 0.
void addCategory(String catTitle) {
mVideos = mShows.get(catTitle);
LinearLayout theLayout = (LinearLayout)findViewById(R.id.activitymain);
TextView textview=(TextView)getLayoutInflater().inflate(R.layout.categorytitle,null);
textview.setTextColor(Color.CYAN);
textview.setTextSize(20);
textview.setText(catTitle);
HorizontalScrollView horizontalScroll = new HorizontalScrollView (this,null);
LinearLayout LL = new LinearLayout(this);
LL.setOrientation(LinearLayout.HORIZONTAL);
LayoutParams LLParams = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
LL.setLayoutParams(LLParams);
HorizontalGalleryAdapter adapter = new HorizontalGalleryAdapter(this,mVideos);
for (int i = 0; i < adapter.getCount(); i++) {
View item = adapter.getView(i, null, null);
LL.addView(item);
}
horizontalScroll.addView(LL);
int maxScrollX = horizontalScroll.getChildAt(0).getMeasuredWidth()-horizontalScroll.getMeasuredWidth();
AlertDialog alertDialog = new AlertDialog.Builder(this).create();
alertDialog.setTitle("Reset...");
String max= String.valueOf(maxScrollX);
Ok, I see the problem. You create a HorizontalScrollView, add a child to it, and then immediately try to get its measured width.
You cannot do this. You must add the horizontal scroll view to an existing already-drawn view in your activity first, because otherwise it doesn't have set dimensions yet.
Think about how would it know how many pixels WRAP_CONTENT will set the dimension to before its laid out in your view? If you add it to an existing, already-laid-out view in your activity, then that WRAP_CONTENT will actually get converted to some height.
It looks like you kind-of have a loop - horizontalScroll's dimensions depend on its content (WRAP_CONTENT), yet the content's (LinearLayout's) dimensions depend on the horizontalScroll's dimensions. This does not make sense. Perhaps try MATCH_PARENT for at least the width dimensions of your horizontal scroll view. Then, make sure to not look at dimensions until the view has been drawn.
Have a look into typical usage example for HorizontalScrollView:
// read a view's width
private int viewWidth(View view) {
view.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
return view.getMeasuredWidth();
}
....
void getTableRowHeaderCellWidth(){
int tableAChildCount = ((TableRow)this.tableA.getChildAt(0)).getChildCount();
int tableBChildCount = ((TableRow)this.tableB.getChildAt(0)).getChildCount();;
for(int x=0; x<(tableAChildCount+tableBChildCount); x++){
if(x==0){
this.headerCellsWidth[x] = this.viewWidth(((TableRow)this.tableA.getChildAt(0)).getChildAt(x));
}else{
this.headerCellsWidth[x] = this.viewWidth(((TableRow)this.tableB.getChildAt(0)).getChildAt(x-1));
}
}
}
You can also check full details from this nice tutorial: The code of a Ninja.

Android: get height of tab bar?

Is there any way I can get the height of the tab bar in Android? Its getHeight() method seems to only return 0.
Thanks!
Try this in your code .
for (int i = 0; i < tabHost.getTabWidget().getChildCount(); i++) {
RelativeLayout relativeLayout =(RelativeLayout)tabHost.getTabWidget().getChildAt(i);
relativeLayout.getChildCount();
TextView tabhostText = (TextView) relativeLayout.getChildAt(1);
tabhostText.setTextSize(20.0f); // you can set the TextSize of your tab also
tabHost.getTabWidget().getChildAt(i).getLayoutParams().height = 30;
}
It will work .

Categories

Resources