Android Tablayout: 3 Tabs, let center tab take 70% of available width - android

I'm currently using a TabLayout in my application.
I set GRAVITY_FILL and MODE_FIXED but that does not accomplish what I want.
I have 3 tabs all the time. The left and the right tab header only show a small Icon while the middle tab has a long text.
This is what I want:
[________TOTAL WIDTH__________]
[Icon][A very long tab header][Icon2]
This is how it looks like:
[__________________________TOTAL WIDTH__________________________]
[_________Icon________][A very long tab header][_________Icon2________]
So as you can hopefully see from my poor illustration I want to have the middle tab take about 70% of the space and give the rest to the 2 tabs with the icons.
Now I have already done some research and people suggest that I extend some of the layout classes but I could not get it to work.
Can someone please be so kind to let me know if this is possible and how I would achieve it.

this is the only solution of your problem
int padding_in_dp = 2; // 6 dps
final float scale = getResources().getDisplayMetrics().density;
int padding_in_px = (int) (padding_in_dp * scale + 0.5f);
for (int i = 0; i < 3; i++) {
TabLayout.Tab tab = mTabLayout.newTab();
mTabLayout.addTab(tab);
LinearLayout layout = ((LinearLayout) ((LinearLayout) mTabLayout.getChildAt(0)).getChildAt(i));
LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) layout.getLayoutParams();
layoutParams.width = LinearLayout.LayoutParams.WRAP_CONTENT;
layout.setLayoutParams(layoutParams);
layout.setPadding(padding_in_px, 0, padding_in_px, 0);
tab.setText(array[i]);
}
It will surely help you

Related

How to add many buttons dynamically giving a line jump before arriving at the edge in Android?

I am trying to add many button into Relativelayout or Linearlayout,
Layout
<Relativelayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
>
</Relativelayout>
then in the class
_ll_layout = (RelativeLayout) findViewById(R.id.container);
I only know how add the button dynamically with code.
JSONArray jsonArray = new JSONArray(tmp.getString("productos"));
Button bt[] = new Button[jsonArray.length()]; // size of product
for (int i = 0; i < jsonArray.length(); i ++){
int padding_40dp = (int) (40 * scale + 0.5f);
int margin_10dp = (int) (10 * scale + 0.5f);
int padding_90dp = (int) (90 * scale + 0.5f);
LinearLayout.LayoutParams params = new Relativelayout.LayoutParams(padding_90dp, padding_40dp);
params.setMargins(margin_10dp, 0 , 0, 0);
bt[i] = new Button(DetalleServicioActivity.this);
bt[i].setText(jsonArray.getJSONObject(i).getString("nombre"));
bt[i].setTag(new TagInfo(jsonArray.getJSONObject(i).getString("id_producto")));
bt[i].setTextColor(Color.parseColor("#FFFFFF"));
bt[i].setBackgroundColor(Color.parseColor("#D8D8D8"));
bt[i].setEnabled(false);
bt[i].setId(Integer.parseInt(jsonArray.getJSONObject(i).getString("id_producto")));
bt[i].setLayoutParams(params);
_ll_layout.addView(bt[i]);
}
but the result is
One on another one, but I need something like this:
Edit
If I use LinearLayout with orientation horizontal and gravity center, this happend
Instead of using Relative Layout or Linear Layout I would rather suggest you to create custom flow layout.Custom flow layout will adjust child views accordingly in rows, and will jump the button in new row according to screen width.
Please have a look here : Flow layout example
Happy Coding :)
Instead of RelativeLayout, make use of LinearLayout with orientation as horizontal and add the button in them at run time
As per your design requirement, make sure you have two linear layouts here.
you can use griedlayout for solved your problem

Android how to programmatically create scrollview and add programmatically created views into it

Alright I'm trying to build an activity that has a horizontal scrollview, that the user can swipe through, to view different "pages". My train of thought is these "pages" will be views. The following is a mockup of my idea (to mess around to see if it works)
I've experimented with this as follows:
My content view is set to the the scrollview. (unsure if this is an incorrect approach)
I create the scrollview, and place a view into it as follows:
private void setupScrollView()
{
Display display = getWindowManager().getDefaultDisplay();
DisplayMetrics outMetrics = new DisplayMetrics ();
display.getMetrics(outMetrics);
float density = getResources().getDisplayMetrics().density;
float dpHeight = outMetrics.heightPixels / density;
float dpWidth = outMetrics.widthPixels / density;
int width = (int)MeasureUtils.convertDpToPixel(dpWidth, getApplicationContext());
int height = (int)MeasureUtils.convertDpToPixel(dpHeight, getApplicationContext());
_scrollView = new HorizontalScrollView(getApplicationContext());
_scrollView.setBackgroundColor(Color.CYAN);
_scrollView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
Log.i("DEBUG", "Screen dp width = " + dpWidth + " screen dp height = " + dpHeight);
TextView view = new TextView(getApplicationContext());
view.setBackgroundColor(Color.RED);
view.setText("TEST");
view.setX(0); // Start at the left of the scrollview.
view.setWidth(width); // Size it so that it fills to the right of the scrollview.
TextView view2 = new TextView(getApplicationContext());
view2.setBackgroundColor(Color.GREEN);
view2.setText("TEST2");
view2.setX(width); // Start the second "page/view" offscreen to the right where i can scroll to it
view.setWidth(width); // Fill the screen width
LinearLayout layout = new LinearLayout(getApplicationContext());
layout.setBackgroundColor(Color.MAGENTA);
layout.addView(view);
layout.addView(view2);
_scrollView.addView(layout);
}
The idea above is that I will see a view, that takes up the screen, representing a page. This view should be "RED" in color. I can then scroll horizontally to the right and see the second view (view2) representing the next page. This view should be "GREEN" in color. This does not happen. I end up seeing what looks like 1/3rd or 1/2 of my screen being view1, the linearlayout taking up almost the whole screen (a bit of a gap to the right edge where the CYAN from the scrollview bleeds through).
Am I approaching this the wrong way, and/or is it possible to make this work the way I'm going at it?
You probably do not want to use a horizontalscroll view to create "pages".
Try looking at PageViewer
This automatically builds in all the sywpe and inflating logic for you.
Basically you will get a call to inflate a certain page. There you can then create your view (dynamically if you wish) and then just return the root to be rendered.
Alright I've figured out what I was doing wrong, and it turned out to be something very small...
The complete code is here:
private void setupScrollView()
{
Display display = getWindowManager().getDefaultDisplay();
DisplayMetrics outMetrics = new DisplayMetrics ();
display.getMetrics(outMetrics);
float density = getResources().getDisplayMetrics().density;
float dpHeight = outMetrics.heightPixels / density;
float dpWidth = outMetrics.widthPixels / density;
int width = (int)MeasureUtils.convertDpToPixel(dpWidth, getApplicationContext());
int height = (int)MeasureUtils.convertDpToPixel(dpHeight, getApplicationContext());
_scrollView = new HorizontalScrollView(getApplicationContext());
_scrollView.setBackgroundColor(Color.CYAN);
_scrollView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
Log.i("DEBUG", "Screen dp width = " + dpWidth + " screen dp height = " + dpHeight);
TextView view = new TextView(getApplicationContext());
view.setBackgroundColor(Color.RED);
view.setText("TEST");
view.setX(0);
view.setWidth(width);
view.setHeight(height - 50);
TextView view2 = new TextView(getApplicationContext());
view2.setBackgroundColor(Color.GREEN);
view2.setText("TEST2");
view2.setX(0);
view2.setWidth(width);
view2.setHeight(height - 50);
LinearLayout layout = new LinearLayout(getApplicationContext());
layout.setBackgroundColor(Color.MAGENTA);
layout.addView(view);
layout.addView(view2);
_scrollView.addView(layout);
}
This creates a horizontal scrollview programmatically, as I had, but the problem was that I was setting the second view to be "width" away, when it should be set to "0"as can be seen by:
view2.setX(0);
With that, I get 2 "views" that resemble pages in my scrollview that I can swipe through. Each taking up the whole page.
Hate having the code close and it being a simple fix that I missed :|
Hope this helps anyone else that tries to do it this way. I'm going to look into the PageViewer as Frank suggested.

Android - Dynamic textViews - setMargins not applying?

I'm trying to make a dynamic grid layout, it being API 10+ is the part that's been making it slow going. I tried to make it wrap automatically.. but in the end found it easier just to try to force it into a grid pattern using coordinates. This script was working by itself when I did the positioning at time of creation, but now I am trying to loop through each item as a sort. So if one item is deleted, they all float back into a grid without a hole in the middle.
Problem is, it seems the layout parameters are only applying to the last object.
Here's some base variables and onCreate setup:
int screenWidth;
int screenHeight;
int distStep = 130;
int leftPad = 20;
int numCols;
int baseID = 0;
android.util.DisplayMetrics metrics = this.getResources().getDisplayMetrics();
screenWidth = metrics.widthPixels;
screenHeight = metrics.heightPixels;
numCols = (int) (screenWidth - leftPad) / distStep;
int scrRemain = screenWidth - ((numCols * distStep) + leftPad);
distStep += (int) scrRemain / numCols;
Then on to the main function for adding:
public void addObjToLayout() {
RelativeLayout relLay = (RelativeLayout) this.findViewById(R.id.mainWindow);
for(int i = 1; i <= currQuantity; i++){
TextView tv=new TextView(this);
tv.setTextSize(40);
tv.setId(baseID + i);
tv.setPadding(24, 4, 24, 4);
tv.setBackgroundColor(0x110000FF);
tv.setText(String.valueOf(baseID + i)); //Val for debugging
tv.setTextColor(0xFFFFFFFF);
relLay.addView(tv);
}
baseID += currQuantity;
sortLayout();
}
Then the sorting:
public void sortLayout() {
int leftNum = 20;
int topNum = 0;
for(int i = 1; i <= baseID; i++){
TextView tv= (TextView) this.findViewById(baseID);
MarginLayoutParams mp = new MarginLayoutParams(tv.getLayoutParams());
mp.setMargins(leftNum, topNum, 0, 0);
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(mp);
tv.setLayoutParams(lp);
leftNum += distStep;
if(leftNum >= distStep * numCols){
leftNum = leftPad;
topNum += distStep;
}
}
}
What I am getting is all the textViews pile up in the top left corner, except the last one which is positioned exactly where it should be. So it seems in my head, the params object isn't applying until the loop ends or something.. but logically I don't see why.
As I said, this worked when I set the params at the get go, problem is mass updating them all at once. I am pretty new to android, so I hope I'm not just doing something stupid.
Thanks for your time
Margin means it will set a gap between the previous view and current view.
When you add view1, view2 and view3 to grid layout and if you remove view2 at some point of time, then the margin for view3 is set according to view1. So, it won't leave empty space in place of view2. Instead of removing view2 at run time, set the background for view2 as null and set the text as empty as below.
textView.setBackground(null);
textView.setText("");
So that the view is still available but looks as deleted.
Started looking into GridView using an extended baseAdapter. Looks promising:
For more (see #2):
http://www.mkyong.com/android/android-gridview-example/

How to set a desired layout for an animating fragment?

I have a fragment which is animating from right to left. I want to set it to stop at like 100 units from the left part of the screen. This is what i did so far.
RelativeLayout tempLi;
Display display = MyActivity.context.getWindowManager().getDefaultDisplay();
final int width = display.getWidth();
tempLi.setLayoutParams(new LayoutParams(width-100, LayoutParams.FILL_PARENT));
This line of code is getting the layout 100 units short from the right side. I tried doing -width + 100 , it didn't work.
Any suggestions will be appreciated. TIA
You can set the left-margin to the layout as follows:
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
layoutParams.setMargins(100, 0, 0, 0);
tempLi.setLayoutParams(layoutParams);

How to add space between tab buttons in Android?

I need to separate tab buttons with space, I tried to set margin to views and then add them as tabs, but it does not work, I also thought of adding empty view as divider, but haven't tried it yet, is there any standard way of doing this, or any tweak that can achieve same effect?
Thanks!
Here's the way:
TabWidget tabWidget = (TabWidget) findViewById(android.R.id.tabs);
final int tabChildrenCount = tabWidget.getChildCount();
View currentView;
for (int i = 0; i < tabChildrenCount; i++) {
currentView = tabWidget.getChildAt(i);
LinearLayout.LayoutParams currentLayout =
(LinearLayout.LayoutParams) currentView.getLayoutParams();
currentLayout.setMargins(0, 5, 5, 0);
}
tabWidget.requestLayout();
This is really a good solution even for my problem! Many thanks for that! I used it to implement space before the first and after the last item in the widget to have the possibility to scroll them visible to the center without adding additional (and disturbing, because the widget does not excpect such silly things) invisible buttons.
//pump up space for first entry on the left and last entry on the right!
Display display = getWindowManager().getDefaultDisplay();
//Point size = new Point();
int width = display.getWidth();
View currentView = mTabHost.getTabWidget().getChildAt(0);
LinearLayout.LayoutParams currentLayout = (LinearLayout.LayoutParams) currentView.getLayoutParams();
currentLayout.setMargins(currentLayout.leftMargin + width/2, currentLayout.topMargin, currentLayout.rightMargin, currentLayout.bottomMargin);
currentView = mTabHost.getTabWidget().getChildAt(mTabHost.getTabWidget().getChildCount()-1);
currentLayout = (LinearLayout.LayoutParams) currentView.getLayoutParams();
currentLayout.setMargins(currentLayout.leftMargin, currentLayout.topMargin, currentLayout.rightMargin + width/2, currentLayout.bottomMargin);
mTabHost.getTabWidget().requestLayout();

Categories

Resources