Distorted App screen - Android - android

Our app looks like this on Huawei 8510, and android version is 2.3.5.
This screenshot came from our customer. We disabled screenshot on app, so our customer used another phone to take screenshot.
How is it possible?
this is layout code that i think there is no problem.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:layout_width="0px"
android:layout_height="0px"
android:focusable="true"
android:focusableInTouchMode="true" />
<com.asdasd.asdasd.controls.CMScrollView
android:id="#+id/scvAgreement"
style="#style/pm_ContentStyle"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:fadingEdge="none"
android:paddingLeft="0dp"
android:paddingRight="0dp">
<TextView
android:id="#+id/tvAgreement"
style="#style/pm_TextStyle"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal"
android:layout_marginLeft="#dimen/pm_content_padding_left"
android:layout_marginRight="#dimen/pm_content_padding_right"
android:gravity="center"
android:text=""
android:typeface="monospace" />
</com.asdasd.asdasd.controls.CMScrollView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0"
android:orientation="horizontal">
<Button
style="#style/pm_ButtonStyle_Standart"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#drawable/selector_btn_black"
android:gravity="center"
android:onClick="onBtnNegativeClicked"
android:text="#string/pgPrivacy_btnNotAccept"
android:textSize="#dimen/pm_middle_text_size"
android:singleLine="false"/>
<Button
android:id="#+id/btnSendAgreement"
style="#style/pm_ButtonStyle_Standart"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="#dimen/pm_button_margin_left"
android:layout_weight="1"
android:background="#drawable/selector_btn_black"
android:enabled="false"
android:gravity="center"
android:onClick="onBtnSendAgreement"
android:text="#string/pgPrivacy_btnSendAgreement"
android:textSize="#dimen/pm_middle_text_size"
android:singleLine="false"/>
<Button
android:id="#+id/btnPositive"
style="#style/pm_ButtonStyle_Standart"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="#dimen/pm_button_margin_left"
android:layout_weight="1"
android:background="#drawable/selector_btn_orange"
android:enabled="false"
android:gravity="center"
android:onClick="onBtnPositiveClicked"
android:text="#string/pgPrivacy_btnAccept"
android:textSize="#dimen/pm_middle_text_size"
android:singleLine="false"/>
</LinearLayout>
And also, our custom listView
public class CMScrollView extends ScrollView {
public static final String LOG_TAG = "com.asdasd.asdasd.controls.CMScrollView";
private OnScrollViewReachedToEnd onScrollViewReachedToEnd;
public CMScrollView(Context context, AttributeSet st) {
super(context, st);
}
public OnScrollViewReachedToEnd getOnScrollViewReachedToEnd() {
return onScrollViewReachedToEnd;
}
public void setOnScrollViewReachedToEnd(OnScrollViewReachedToEnd onScrollViewReachedToEnd) {
this.onScrollViewReachedToEnd = onScrollViewReachedToEnd;
}
#Override
protected void onScrollChanged(int l, int t, int oldl, int oldt) {
View view = (View) getChildAt(getChildCount() - 1);
int diff = (view.getBottom() - (getHeight() + getScrollY() + view.getTop()));// Calculate the scrolldiff
if (diff == 0) { // if diff is zero, then the bottom has been reached
if (ApplicationController.DEBUGMODE)
Log.d(CMScrollView.LOG_TAG, "MyScrollView: Bottom has been reached");
if (onScrollViewReachedToEnd != null) {
onScrollViewReachedToEnd.onReachedToEnd(this);
}
}
super.onScrollChanged(l, t, oldl, oldt);
}
public interface OnScrollViewReachedToEnd {
void onReachedToEnd(IngScrollView scrollView);
}}
on AndroidManifest GL Version:
<uses-feature
android:glEsVersion="0x00020000"
android:required="true" />

One of my friend found a promising Answer after a lot of time & search.
The same kind of issue (Flickering in GUI) is solved by simply Turn OFF the Developer Options
Because if you enable hardware overlay , GPU will be takes care of UI & sometimes there will be some conflict in GUI rendering.

Related

Fixed buttons between scrollable content

i'm struggling with scrollview.
I want to display content (1) then buttons (2) and finally content (3).
When i'm scrolling, i want the buttons to be fixed at the top of my activity.
I can't post image, so it would be :
1
2
3
I scroll
2 (fixed at top)
3 (this part start to scroll when the 1 disapear at top)
I scroll
2 (fixed at top)
3 (still scrollable).
I m curently using this :
<ScrollView
android:id="#+id/sv_entete"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/layout_bar"
android:background="#color/gris_blanc" >
<RelativeLayout
android:id="#+id/layout_conteneur"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
// Some image etc.
</RelativeLayout>
</ScrollView>
<Button
android:id="#+id/btn_presentation_has_to_stay_on_top"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/sv_entete" />
<Button
android:id="#+id/btn_analyze_has_to_stay_on_top"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="#+id/sv_entete" />
<ScrollView
android:id="#+id/sv_presentation"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/btn_presentation"
android:background="#color/gris_blanc" >
<RelativeLayout
android:id="#+id/layout_presentation"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
// Some image etc
</RelativeLayout>
</ScrollView>
But only the second part scroll (3), the first part (1) is (obviously) not linked with the second scrollview, but i can't figured out by myself.
Any help plz ?
Looks like there's no way to implement it right out of the box. So, You need to create custom components.
Unfortunately, there's not much details in the question, so I'm not able to suggest which way is better for your case.
However, below two variants I can think of:
Have two button panels in the same layout. Initially only one panel should be visible (one inside scroll view). During scrolling, it first panel reaches top, You'll need to make second one visible. And make it invisible, when scroll up (and first one become visible inside ScrollView). It would increase complexity of layout and coe little bit (e.g. You'll need to listen two sets of buttons instead of one). In simple test application I haven't observed any critical issues / UI artifacts using this implementation,
For that you need to create separate layout for buttons panel, e.g. buttons_panel.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="#+id/btn_presentation_has_to_stay_on_top"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="left button"/>
<Button
android:id="#+id/btn_analyze_has_to_stay_on_top"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="right button"/>
</LinearLayout>
Then main activity layout might look like the following (activity_main.xml):
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.alexstarc.testapp.ObservableScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/sv_entete">
<RelativeLayout
android:id="#+id/layout_conteneur"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:layout_width="match_parent"
android:layout_height="300dp"
android:id="#+id/image_1"
android:layout_alignParentTop="true"
android:background="#android:color/holo_blue_dark"
android:gravity="center"
android:textSize="30sp"
android:text="Image 1"/>
<ImageView
android:layout_width="match_parent"
android:layout_height="250dp"
android:id="#+id/image_1_1"
android:layout_centerHorizontal="true"
android:layout_below="#id/image_1"
android:src="#drawable/img1"/>
<include
layout="#layout/buttons_panel"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/middle_button_panel"
android:layout_below="#id/image_1_1" />
<TextView
android:layout_width="match_parent"
android:layout_height="100dp"
android:id="#+id/image_2"
android:layout_below="#id/middle_button_panel"
android:background="#android:color/holo_green_dark"
android:gravity="center"
android:textSize="30sp"
android:text="Image 2"/>
<ImageView
android:layout_width="match_parent"
android:layout_height="350dp"
android:id="#+id/image_3"
android:layout_below="#id/image_2"
android:src="#drawable/img3"/>
<TextView
android:layout_width="match_parent"
android:layout_height="250dp"
android:id="#+id/image_4"
android:layout_below="#id/image_3"
android:background="#android:color/holo_orange_dark"
android:gravity="center"
android:textSize="30sp"
android:text="Image 4 (1)"/>
<TextView
android:layout_width="match_parent"
android:layout_height="100dp"
android:id="#+id/image_5"
android:layout_below="#id/image_4"
android:background="#android:color/holo_purple"
android:gravity="center"
android:textSize="30sp"
android:text="Image 5"/>
<TextView
android:layout_width="match_parent"
android:layout_height="350dp"
android:id="#+id/image_6"
android:layout_below="#id/image_5"
android:background="#android:color/holo_blue_dark"
android:gravity="center"
android:textSize="30sp"
android:text="Image 6"/>
<TextView
android:layout_width="match_parent"
android:layout_height="250dp"
android:id="#+id/image_7"
android:layout_below="#id/image_6"
android:background="#drawable/img1"
android:gravity="center"
android:textSize="30sp"
android:text="Image 7"/>
<TextView
android:layout_width="match_parent"
android:layout_height="250dp"
android:id="#+id/image_8"
android:layout_below="#id/image_7"
android:background="#android:color/holo_orange_dark"
android:gravity="center"
android:textSize="30sp"
android:text="Image 8"/>
<TextView
android:layout_width="match_parent"
android:layout_height="100dp"
android:id="#+id/image_9"
android:layout_below="#id/image_8"
android:background="#android:color/holo_purple"
android:gravity="center"
android:textSize="30sp"
android:text="Image 9"/>
<TextView
android:layout_width="match_parent"
android:layout_height="350dp"
android:id="#+id/image_10"
android:layout_below="#id/image_9"
android:background="#android:color/holo_blue_dark"
android:gravity="center"
android:textSize="30sp"
android:text="Image 10"/>
<TextView
android:layout_width="match_parent"
android:layout_height="250dp"
android:id="#+id/image_11"
android:layout_below="#id/image_10"
android:background="#drawable/img1"
android:gravity="center"
android:textSize="30sp"
android:text="Image 11"/>
</RelativeLayout>
</com.alexstarc.testapp.ObservableScrollView>
<include
layout="#layout/buttons_panel"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/top_button_panel"
android:layout_alignParentTop="true"
android:visibility="gone" />
</RelativeLayout>
There's special custom ViewGroup - ObservableScrollView. It's needed, because ScrollView doesn't have any listeners for observing scroll position, but we need to detect then buttons panel inside it become visible. Here's its draft code (please note, I haven't done any optimization or full testing):
public class ObservableScrollView extends ScrollView {
private int mTrackViewId = 0;
private View mTrackView = null;
private int mTrackViewTop = 0;
private int mTrackViewDown = 0;
private OnViewReachTop mTopReachListener = null;
/* Helper interface to notify listener about */
public interface OnViewReachTop {
/**
* Called then View riches top
*/
void onReachTop();
/**
* Called then view leaves top position
*/
void onLeaveTop();
}
public ObservableScrollView(final Context context) {
super(context);
}
public ObservableScrollView(final Context context, final AttributeSet attrs) {
super(context, attrs);
}
public ObservableScrollView(final Context context, final AttributeSet attrs, final int defStyle) {
super(context, attrs, defStyle);
}
/**
* Sets view id to be tracked and corresponding callback
*
* #param viewId id of view to be trackked
* #param listener {#link com.alexstarc.testapp.ObservableScrollView.OnViewReachTop} to be called
*/
public void setViewTopTracking(final int viewId, final OnViewReachTop listener) {
mTrackView = findViewById(viewId);
if (mTrackView == null) {
throw new IllegalArgumentException("Passed view id should be from child of this scroll view");
}
mTrackViewId = viewId;
mTopReachListener = listener;
}
#Override
protected void onScrollChanged(final int l, final int t, final int oldl, final int oldt) {
if (mTrackViewDown == 0 && mTrackViewTop == 0) {
mTrackViewDown = mTrackView.getBottom();
mTrackViewTop = mTrackView.getTop();
}
super.onScrollChanged(l, t, oldl, oldt);
if (getScrollY() >= mTrackViewTop) {
mTopReachListener.onReachTop();
} else if (getScrollY() <= mTrackViewDown) {
mTopReachListener.onLeaveTop();
}
}
}
And activity code, which will observe current scroll position and show / hide second buttons panel:
public class MyActivity extends Activity implements ObservableScrollView.OnViewReachTop {
private static final String TAG = "MyActivity";
private View mButtonsPanel;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mButtonsPanel = findViewById(R.id.top_button_panel);
((ObservableScrollView)findViewById(R.id.sv_entete)).setViewTopTracking(R.id.middle_button_panel, this);
}
#Override
public boolean onCreateOptionsMenu(final Menu menu) {
getMenuInflater().inflate(R.menu.action_bar, menu);
return true;
}
#Override
public void onReachTop() {
mButtonsPanel.setVisibility(View.VISIBLE);
}
#Override
public void onLeaveTop() {
mButtonsPanel.setVisibility(View.GONE);
}
}
Second way: take ScrollView code, analyze it and make possible to fix some view on the top. This way is more complex, but possible to implement. Please, investigate it (if first one is not applicable to your code / layout) and ask additional questions if face some particular problems with it.

TextViewEx, (Textjustify)

my problem is this, using TextViewEx (https://github.com/bluejamesbond/TextJustify-Android) class texts to justify my android application works fine until the text over a carriage return appear \ n, when this happens the text is cut losing asin several lines of this, I need a solution to justify text without html injection, to change the font size if the application is open from mobile or from a tablet, if know what the problem is and can help me I thank you.
I will put the code in my application:
code xml:
<ScrollView
android:id="#+id/scrollView1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginLeft="25dp"
android:layout_marginRight="25dp"
android:layout_marginTop="45dp"
android:layout_below="#+id/view1" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingBottom="50dp"
android:background="#drawable/background_border" >
<TextView
android:id="#+id/txt_fecha_promociones"
style="#style/fechaDentroxlarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginLeft="20dp"
android:layout_marginTop="20dp"
android:text="24/05/2014" />
<TextView
android:id="#+id/txt_titulo_promociones"
style="#style/tituloDentroxlarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_below="#+id/comparte"
android:text="Titulo" />
**<com.xxxxx.tools.TextViewEx
android:id="#+id/txt_noticia_promociones"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/img_promo"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:layout_marginTop="20dp"
android:textSize="30sp"
android:text="noticia"
android:textColor="#color/txtBlanco" />**
<ImageView
android:id="#+id/comparte"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignParentRight="true"
android:layout_marginRight="15dp"
android:layout_marginTop="15dp"
android:layout_toLeftOf="#+id/txt_fecha_promociones"
android:onClick="compartir"
android:src="#drawable/share_icon_white" />
<ImageView
android:id="#+id/img_promo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/txt_titulo_promociones"
android:layout_marginLeft="40dp"
android:layout_marginRight="40dp"
android:layout_marginTop="20dp"
android:layout_marginBottom="20dp"
android:src="#drawable/promo1" />
</RelativeLayout>
</ScrollView>
code java:
public class xxxxx extends NavegacionActivity {
TextView txt_servicio;
TextViewEx txt_descripcion;
TextView prueba;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_servicios_dentro);
setBehindContentView(R.layout.menu);
getSlidingMenu().setBehindOffset(100);
txt_servicio = (TextView) findViewById(R.id.txt_titulo_servicio);
txt_descripcion = (TextViewEx) findViewById(R.id.txt_descripcion_servicios);
Intent i = getIntent();
txt_servicio.setText(i.getStringExtra("servicio"));
txt_descripcion.setText(i.getStringExtra("descripcion"),true);
}
#Override
public void onBackPressed() {
// TODO Auto-generated method stub
Intent i = new Intent(this, xxxx.class);
startActivity(i);
this.finish();
}
}
Import the classes TextJustifyUtils.java and TextViewEx.java in the root of your project
In your XML Replace TextView --->> com.example.myaplication.TextViewEx
Just go to TextViewEx.java and change to this code:
#Override
public void setPadding(int left, int top, int right, int bottom) {
DisplayMetrics metrics = getContext().getResources()
.getDisplayMetrics();
super.setPadding((int) ((left + (metrics.density * 10))), top,
(int) (right + (metrics.density * 10)),
(int) (bottom + (metrics.heightPixels * 1.5)));
Log.i("Log", "metrics : " + metrics);
}

Android screen resolution issue

I have two tablets. I have run a test resolution app to get screen parameters. Thouse are the results.
1) Coby MID7042 7": low density, scale: 0.75, 120dpi, 480 x 764,
layout for large screen, drawable LDPI.
2) BQ Elcano 7": high density, scale: 1.5, 240dpi, 800 x 1208, layout
for large screen, drawable HDPI.
I have developed an android application that fits good on Coby screen, but in BQ the screen is cut in the right and in the bottom. That is, the screen is bigger than you can see on the screen. I think BQ screen is better and bigger than Coby, but I don't know why it doesn't fit the screen.
In my developed android app, the xml file for the screen is into res/layout folder. I always use sp and dp.
I have read about "supporting multiple screen" and so on, but in this case, I think BQ should show the screen at right fit using the same layout. But it doesn't.
Some one could help me about this issue? I don't know how could I solve this, because two tablets are large for layouts.
Thanks.
Edited. This is a layout that is cut in the right (the latest EditText is cut):
<LinearLayout
style="#style/GroupBox3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:layout_weight="1"
android:baselineAligned="true" >
<LinearLayout
android:layout_width="130dp"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="#+id/TextView08"
style="#style/etstyle1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="#string/Box" />
<EditText
android:id="#+id/eBox"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="6dp"
android:layout_weight="1"
android:background="#drawable/roundedittext"
android:ems="10"
android:enabled="true"
android:gravity="right"
android:inputType="number|numberSigned|numberDecimal"
android:selectAllOnFocus="true" >
</EditText>
</LinearLayout>
<LinearLayout
android:layout_width="130dp"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:orientation="vertical" >
<TextView
android:id="#+id/TextView06"
style="#style/etstyle1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="#string/Uds" />
<EditText
android:id="#+id/euds"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="6dp"
android:background="#drawable/roundedittext"
android:ems="10"
android:enabled="true"
android:gravity="right"
android:inputType="number|numberSigned|numberDecimal"
android:selectAllOnFocus="true" />
</LinearLayout>
<LinearLayout
android:layout_width="130dp"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:orientation="vertical" >
<TextView
android:id="#+id/TextView09"
style="#style/etstyle1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal|left"
android:paddingLeft="10dp"
android:text="#string/Tar" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:paddingBottom="5dp" >
<EditText
android:id="#+id/etar"
android:layout_width="60dp"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:background="#drawable/roundedittext"
android:ems="10"
android:enabled="true"
android:gravity="center_vertical|right"
android:inputType="number"
android:selectAllOnFocus="true" />
<Button
android:id="#+id/btnTarifa"
style="#style/btnSer"
android:layout_width="50dp"
android:layout_height="40dp"
android:layout_gravity="center_vertical|center_horizontal|center"
android:layout_marginTop="2dp"
android:text="#string/Inter" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="135dp"
android:layout_height="match_parent"
android:layout_marginLeft="5dp"
android:orientation="vertical" >
<TextView
android:id="#+id/etit"
style="#style/etstyle1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="#string/price" />
<EditText
android:id="#+id/eprice"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="6dp"
android:background="#drawable/roundedittext"
android:ems="10"
android:enabled="true"
android:gravity="center_vertical|right"
android:inputType="numberDecimal"
android:selectAllOnFocus="true" />
</LinearLayout>
</LinearLayout>
DroidUsers answer is little bit confusing but method is effective...
look for more effective example:
http://www.vanteon.com/downloads/Scaling_Android_Apps_White_Paper.pdf
I have Used one approach where I design the Layout I wanted in a standard device, and store all dynamically apply all the layout dependent values(eg: padding, margin etc) using a seperate class. And by calculating a ratio with current device config to the standard i have stored,
I will give you my sample code. just go through it and find your way accordingly
public class ListViewDimensions
{
private Context _context;
private Configuration config;
private Display display;
private int _width;
private int _height;
private int _portOrLand;
private int _ORIENTATION_LAND=2;
private int _ORIENTATION_PORT=1;
private DisplayMetrics metrics;
private Dimension dimension;
//***************************** PORTRAIT ********************************\\
public int PlistLayoutPaddingLeft=16;
public int PlistLayoutPaddingRight=16;
public int PlistLayoutPaddingTop=16;
public int PlistLayoutPaddingBottom=15;
public int PsuraNoTextsize=10;
public int PsuraNameTextsize=20;
public int PayaNoTextsize=10;
public int PsuraNameWidth=180;
public int PpageNoTextsize=10;
public int PpageNoPadding=2;
public int PmalayalamFontsize;
public int ParabiFontsize;
public int PsuraHeadHeight=40;
//----------------------------------------------------------------------------------------
//***************************** LANDSCAPE IN TABS ********************************\\
public int LTlistLayoutPaddingLeft=35;
public int LTlistLayoutPaddingTop=30;
public int LTlistLayoutPaddingRight=35;
public int LTlistLayoutPaddingRightRightPage=35;
public int LTlistLayoutPaddingBottom=5;
public int LTpageNoTextsize=13;
public int LTpageNoPadding=0;
public int LTsuraNoTextsize=15;
public int LTsuraNameTextsize=25;
public int LTayaNoTextsize=15;
public int LTsuraNameWidth=375;
public int LTmalayalamFontsize;
public int LTarabiFontsize;
public int LTsuraHeadHeight=60;
//----------------------------------------------------------------------------------------
//***************************** LANDSCAPE IN NON-TABS ********************************\\
public int LlistLayoutPaddingLeft=21;
public int LlistLayoutPaddingRight=20;
public int LlistLayoutPaddingTop=16;
public int LlistLayoutPaddingBottom=17;
public int LsuraNoTextsize=10;
public int LsuraNameTextsize=20;
public int LayaNoTextsize=10;
public int LsuraNameWidth=330;
public int LpageNoTextsize=13;
public int LpageNoPadding=3;
public int LmalayalamFontsize;
public int LarabiFontsize;
public int LsuraHeadHeight=40;
//----------------------------------------------------------------------------------------
public float xScale;
private float yScale;
private double diagonalInches;
public class Dimension
{
public int listLayoutPaddingLeft;
public int listLayoutPaddingRight;
public int listLayoutPaddingTop;
public int listLayoutPaddingBottom;
public int pageNoTextsize;
public int pageNoPadding;
public int suraNoTextsize;
public int suraNameTextsize;
public int ayaNoTextsize;
public int suraNameWidth;
public int malayalamFontsize;
public int arabiFontsize;
public int suraHeadHeight;
public String bismi;
}
public ListViewDimensions.Dimension getDimensions(Context c)
{
PmalayalamFontsize=MyApp.appSettings.listMalayalamFontSize;
ParabiFontsize=MyApp.appSettings.listMalayalamFontSize;
LTmalayalamFontsize=MyApp.appSettings.listMalayalamFontSize*2;
LTarabiFontsize=MyApp.appSettings.listMalayalamFontSize*2;
LmalayalamFontsize=MyApp.appSettings.listMalayalamFontSize;
LarabiFontsize=MyApp.appSettings.listMalayalamFontSize;
dimension=new Dimension();
_context=c;
config =c.getResources().getConfiguration();
display=((Activity) c).getWindowManager().getDefaultDisplay();
_width = display.getWidth();
_height =display.getHeight();
if(config.orientation==config.ORIENTATION_LANDSCAPE)
_portOrLand=_ORIENTATION_LAND;
else
_portOrLand=_ORIENTATION_PORT;
metrics = new DisplayMetrics();
((Activity) c).getWindowManager().getDefaultDisplay().getMetrics(metrics);
int widthPixels = metrics.widthPixels;
int heightPixels = metrics.heightPixels;
int density=metrics.densityDpi;
float widthDpi = metrics.xdpi;
float heightDpi = metrics.ydpi;
if(density/widthDpi>1.2)
{
widthDpi=density;
heightDpi=density;
}
float widthInches = widthPixels / widthDpi;
float heightInches = heightPixels / heightDpi;
diagonalInches = Math.sqrt((widthInches * widthInches) + (heightInches * heightInches));
if(diagonalInches>=6.7 && _portOrLand==_ORIENTATION_LAND)
{
//TODO : for landscape in tabs
xScale = (_width/2)/(float)640 ;
yScale = _height/(float)752 ;
dimension.listLayoutPaddingLeft=LTlistLayoutPaddingLeft;
dimension.listLayoutPaddingRight=LTlistLayoutPaddingRight;
dimension.listLayoutPaddingTop=LTlistLayoutPaddingTop;
dimension.listLayoutPaddingBottom=LTlistLayoutPaddingBottom;
dimension.pageNoTextsize=LTpageNoTextsize;
dimension.pageNoPadding=LTpageNoPadding;
dimension.malayalamFontsize=LTmalayalamFontsize;
dimension.arabiFontsize=LTarabiFontsize;
dimension.suraNoTextsize=LTsuraNoTextsize;
dimension.suraNameTextsize=LTsuraNameTextsize;
dimension.ayaNoTextsize=LTayaNoTextsize;
dimension.suraNameWidth=LTsuraNameWidth;
dimension.suraHeadHeight=LTsuraHeadHeight;
dimension.bismi="&%$";
}
else if( _portOrLand==_ORIENTATION_PORT)
{
//TODO : for portrait in non-tab devices and tabs
xScale = _width/(float)320 ;
yScale = _height/(float)480 ;
dimension.listLayoutPaddingLeft=PlistLayoutPaddingLeft;
dimension.listLayoutPaddingRight=PlistLayoutPaddingRight;
dimension.listLayoutPaddingTop=PlistLayoutPaddingTop;
dimension.listLayoutPaddingBottom=PlistLayoutPaddingBottom;
dimension.pageNoTextsize=PpageNoTextsize;
dimension.pageNoPadding=PpageNoPadding; //TODO: check whether it is mandatory
dimension.malayalamFontsize=PmalayalamFontsize;
dimension.arabiFontsize=ParabiFontsize;
dimension.suraHeadHeight=PsuraHeadHeight;
dimension.suraNoTextsize=PsuraNoTextsize;
dimension.suraNameTextsize=PsuraNameTextsize;
dimension.ayaNoTextsize=PayaNoTextsize;
dimension.suraNameWidth=PsuraNameWidth;
if(diagonalInches<4.0)
dimension.bismi="รณ";
else
dimension.bismi="&%$";
}
else if(diagonalInches<6.7 && _portOrLand==_ORIENTATION_LAND)
{
//TODO : for land in non-tab devices
xScale = _height/(float)320 ;
yScale = _width/(float)480 ;
dimension.listLayoutPaddingLeft=LlistLayoutPaddingLeft;
dimension.listLayoutPaddingRight=LlistLayoutPaddingRight;
dimension.listLayoutPaddingTop=LlistLayoutPaddingTop;
dimension.listLayoutPaddingBottom=LlistLayoutPaddingBottom;
dimension.suraHeadHeight=LsuraHeadHeight;
dimension.suraNoTextsize=LsuraNoTextsize;
dimension.suraNameTextsize=LsuraNameTextsize;
dimension.ayaNoTextsize=LayaNoTextsize;
dimension.suraNameWidth=LsuraNameWidth;
dimension.pageNoTextsize=LpageNoTextsize;
dimension.pageNoPadding=LpageNoPadding; //TODO: check whether it is mandatory
dimension.malayalamFontsize=LmalayalamFontsize;
dimension.arabiFontsize=LarabiFontsize;
dimension.suraHeadHeight=LsuraHeadHeight;
dimension.bismi="&%$";
}
dimension.listLayoutPaddingLeft=(int) (dimension.listLayoutPaddingLeft*xScale);
dimension.listLayoutPaddingRight=(int) (dimension.listLayoutPaddingRight*xScale);
dimension.listLayoutPaddingTop=(int) (dimension.listLayoutPaddingTop*yScale);
dimension.listLayoutPaddingBottom=(int) (dimension.listLayoutPaddingBottom*yScale);
dimension.pageNoTextsize=(int) (dimension.pageNoTextsize*xScale);
dimension.pageNoPadding=(int) (dimension.pageNoPadding*yScale);
dimension.malayalamFontsize=(int) (dimension.malayalamFontsize*xScale);
dimension.arabiFontsize=(int) (dimension.arabiFontsize*xScale);
dimension.suraHeadHeight=(int) (dimension.suraHeadHeight*yScale);
dimension.suraNoTextsize=(int) (dimension.suraNoTextsize*xScale);
dimension.suraNameTextsize=(int) (dimension.suraNameTextsize*xScale);
dimension.ayaNoTextsize=(int) (dimension.ayaNoTextsize*xScale);
dimension.suraNameWidth=(int) (dimension.suraNameWidth*xScale);
return this.dimension;
}
public ListViewDimensions.Dimension updateFontSize()
{
if(diagonalInches>=6.7 && _portOrLand==_ORIENTATION_LAND)
{
xScale = (_width/2)/(float)640 ;
yScale = _height/(float)752 ;
dimension.malayalamFontsize=LTmalayalamFontsize;
dimension.arabiFontsize=LTarabiFontsize;
}
else if( _portOrLand==_ORIENTATION_PORT)
{
xScale = _width/(float)320 ;
yScale = _height/(float)480 ;
dimension.malayalamFontsize=PmalayalamFontsize;
dimension.arabiFontsize=ParabiFontsize;
}
else if(diagonalInches<6.7 && _portOrLand==_ORIENTATION_LAND)
{
xScale = _height/(float)320 ;
yScale = _width/(float)480 ;
dimension.malayalamFontsize=LmalayalamFontsize;
dimension.arabiFontsize=LarabiFontsize;
}
dimension.malayalamFontsize=(int) (dimension.malayalamFontsize*xScale);
dimension.arabiFontsize=(int) (dimension.arabiFontsize*xScale);
return dimension;
}
}
You should try to work with android:layout_weight instead of specifying your LinearLayout's android:layout_width="130dp". This way, the widths of your textviews and edittexts and what not can be set to fill_parent and Android will scale anything to the size of your screen, whatever the device you're working on might be.
With an exception for the linear layout where you have a new linearlayout for the edittext and a button, there you should use the weight attribute for this edittext and button again instead of their width parameters.
A slightly modified (I couldn't keep the style and text attributes when testing, for obvious reasons) example of how your xml would look:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:layout_weight="1"
android:baselineAligned="true" >
<LinearLayout
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_weight="1" >
<TextView
android:id="#+id/TextView08"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<EditText
android:id="#+id/eBox"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="6dp"
android:ems="10"
android:enabled="true"
android:gravity="right"
android:inputType="number|numberSigned|numberDecimal"
android:selectAllOnFocus="true" >
</EditText>
</LinearLayout>
<LinearLayout
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:orientation="vertical"
android:layout_weight="1" >
<TextView
android:id="#+id/TextView06"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal" />
<EditText
android:id="#+id/euds"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="6dp"
android:ems="10"
android:enabled="true"
android:gravity="right"
android:inputType="number|numberSigned|numberDecimal"
android:selectAllOnFocus="true" />
</LinearLayout>
<LinearLayout
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:orientation="vertical"
android:layout_weight="1" >
<TextView
android:id="#+id/TextView09"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal|left"
android:paddingLeft="10dp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:paddingBottom="5dp" >
<EditText
android:id="#+id/etar"
android:layout_weight="0.5"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:ems="10"
android:enabled="true"
android:gravity="center_vertical|right"
android:inputType="number"
android:selectAllOnFocus="true" />
<Button
android:id="#+id/btnTarifa"
android:layout_weight="0.5"
android:layout_height="40dp"
android:layout_gravity="center_vertical|center_horizontal|center"
android:layout_marginTop="2dp" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_weight="1"
android:layout_height="match_parent"
android:layout_marginLeft="5dp"
android:orientation="vertical" >
<TextView
android:id="#+id/etit"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal" />
<EditText
android:id="#+id/eprice"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="6dp"
android:ems="10"
android:enabled="true"
android:gravity="center_vertical|right"
android:inputType="numberDecimal"
android:selectAllOnFocus="true" />
</LinearLayout>
</LinearLayout>
After lot of sarch I haven't found why it happens, but a very quickly solution. I can scale all the application adding this code to the main activity:
public void changeDensity(float desiredDensity) {
//desiredDensity : ldpi = 0.75 (120dpi) , mdpi = 1 (160dpi), hdpi = 1.5 (240dpi), xhdpi = 2.0 (320dpi)
DisplayMetrics metrics = getResources().getDisplayMetrics();
metrics.density = desiredDensity;
metrics.xdpi = desiredDensity * 160;
metrics.ydpi = desiredDensity * 160;
metrics.densityDpi = (int) (desiredDensity * 160);
getResources().updateConfiguration(null, null);
}
Calling this function on creating main activity, with 1.25f as desiredDensity, the application is scaled to fit the right dimension. I know this isn't the rigth way. I should change all layout, but I can't do it with this device because both use the same layout (large) and then, perhaps, I should use w as parameter in res/layout. Briefly, scaling resolve the problem.
From this: https://stackoverflow.com/a/12077236/1616700

Heterogeneous GridLayout

I am trying to implement the layout below:
I guess GridLayout is suitable for my needs but after 2 hours of struggle I couldn't create even a similar layout.. The layout is resizing itself wrongly, it exceeds the screen of the phone and it also does not span the specified rows and columns.
Here I selected a button so you can see how it exceeds the boundaries:
and here is the associated xml code: https://gist.github.com/2834492
I have reached a similar layout with nested linearlayouts but it's not possible to properly resize it for different screen sizes.
UPDATE - approximate LinearLayout implementation:
The XML code: https://gist.github.com/cdoger/2835887
However, the problem is it does not resize itself properly here some screenshots with different screen configurations:
TLDR: Can someone show me a heterogeneous layout implementation with GridLayout like in the first picture?
The issue you are facing is due to inappropriate use of the GridLayout. The GridLayout is made to show its children in a grid and you are trying to override that without extending the GridLayout. While what you want may be accomplished in code (utilizing numcolumns and columnsize), it will not be useful for multiple screen sizes without a heck of a lot of code.
The only adequate solution that won't require a ton of hacking is judicious use of both LinearLayout and RelativeLayout. LinearLayout should not be used exclusively as it is made to drop items in a line (horizontally or vertically only). This becomes especially apparent when you try and do the bottom four buttons. While the buttons above may be done with LinearLayout with very little effort, RelativeLayout is what you need for the bottom four buttons.
Note:
RelativeLayout can be a little bit tricksy for those with little experience using them. Some pitfalls include: children overlapping, children moving off the screen, height and width rendering improperly applied. If you would like an example, let me know and I will edit my answer.
Final Note:
I'm all for utilizing the current framework objects in unique ways, and genuinely prefer to provide the requested solution. The solution, however, is not viable given the constraints of the question.
(Revision) Solution 1
After some careful thought last night, this may be accomplished with a pure LinearLayout. While I do not like this solution, it should be multi-screen friendly and requires no tooling around from me. Caution should be used with too many LinearLayouts, as according to Google's developers, it can result in slow loading UIs due to the layout_weight property. A second solution utilizing RelativeLayout will be provided when I return home. Now Tested This provides the desired layout parameters on all screen-sizes and orientations.
<?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" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal">
<Button
android:id="#+id/Button01"
android:layout_width="0"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="Button" />
<Button
android:id="#+id/Button02"
android:layout_width="0"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="Button" />
</LinearLayout>
<Button
android:id="#+id/button3"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:text="Button" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1.00"
android:orientation="horizontal">
<Button
android:id="#+id/button1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="Button" />
<Button
android:id="#+id/button2"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="Button" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal">
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical" >
<Button
android:id="#+id/button4"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:text="Button" />
<Button
android:id="#+id/button5"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="2"
android:text="Button" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical" >
<Button
android:id="#+id/button6"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="2"
android:text="Button" />
<Button
android:id="#+id/button7"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:text="Button" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
Solution 1 Explanation
The key to LinearLayouts is to define your imperatives as separate Layouts and nest the others in them. As you apply constraints to more dimensions, more LinearLayouts must be added to encapsulate the others. For yours, it was crucial to have two more parents in order to maintain the proportion. A great indicator of when you should add another level is when you have to utilize layout_weight using anything other than an integer value. It simply becomes to hard to calculate properly. From there it was relatively simple to break it into columns.
Solution 2 (Failed)
While I was able to achieve desirable results utilizing RelativeLayout and "struts", I could only do so with layouts that were multiples of 2 buttons in height. Such a trick would be awesome as the levels of layout are greatly reduced, so I will work on a pure XML solution and post the answer here if and when I achieve it. In the meantime, the LinearLayout above should suit your needs perfectly.
I read this thread and realised that I wanted a flatter solution than those with linear layout. After some research I ended up making my own layout. It is inspired by a GridLayout but differs a bit.
Please note that if you are going to copy-paste the code you'll need to change package names in some places.
This layout has 4 layout parameters that children use to position themselves.These are layout_left, layout_top, layout_right, layout_bottom. The ICGridLayout itself has two attributes: layout_spacing and columns.
Columns tells the layout how many columns you want it to contain. It will then calculate the size of a cell with the same height as width. Which will be the layouts width/columns.
The spacing is the amount of space you want between each child.
The layout_left|top|right|bottom attributes are the coordinates for each side. The layout does no calculations in order to avoid collision or anything. It just puts the children where they want to be.
If you'd like to have smaller squares you just have to increase the columns attribute.
Keep in mind that this is a quick prototype, I will continue working on it and when I feel that it's ready I'll upload it to Github and put a comment here.
All of my code below should produce the following result:
*****EDIT*****
Added the call to measure for the children, forgot that the first time around.
END EDIT
ICGridLayout.java:
package com.risch.evertsson.iclib.layout;
import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import com.risch.evertsson.iclib.R;
/**
* Created by johanrisch on 6/13/13.
*/
public class ICGridLayout extends ViewGroup {
private int mColumns = 4;
private float mSpacing;
public ICGridLayout(Context context) {
super(context);
}
public ICGridLayout(Context context, AttributeSet attrs) {
super(context, attrs);
init(attrs);
}
public ICGridLayout(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init(attrs);
}
private void init(AttributeSet attrs) {
TypedArray a = getContext().obtainStyledAttributes(
attrs,
R.styleable.ICGridLayout_Layout);
this.mColumns = a.getInt(R.styleable.ICGridLayout_Layout_columns, 3);
this.mSpacing = a.getDimension(R.styleable.ICGridLayout_Layout_layout_spacing, 0);
a.recycle();
}
#Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
if (changed) {
int width = (int) (r - l);
int side = width / mColumns;
int children = getChildCount();
View child = null;
for (int i = 0; i < children; i++) {
child = getChildAt(i);
LayoutParams lp = (LayoutParams) child.getLayoutParams();
int left = (int) (lp.left * side + mSpacing / 2);
int right = (int) (lp.right * side - mSpacing / 2);
int top = (int) (lp.top * side + mSpacing / 2);
int bottom = (int) (lp.bottom * side - mSpacing / 2);
child.layout(left, top, right, bottom);
}
}
}
#Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
measureVertical(widthMeasureSpec, heightMeasureSpec);
}
private void measureVertical(int widthMeasureSpec, int heightMeasureSpec) {
int widthMode = MeasureSpec.getMode(widthMeasureSpec);
int heightMode = MeasureSpec.getMode(heightMeasureSpec);
int width = 0;
int height = 0;
if (widthMode == MeasureSpec.AT_MOST || widthMode == MeasureSpec.EXACTLY) {
width = MeasureSpec.getSize(widthMeasureSpec);
} else {
throw new RuntimeException("widthMeasureSpec must be AT_MOST or " +
"EXACTLY not UNSPECIFIED when orientation == VERTICAL");
}
View child = null;
int row = 0;
int side = width / mColumns;
int childCount = getChildCount();
for (int i = 0; i < childCount; i++) {
child = getChildAt(i);
LayoutParams lp = (LayoutParams) child.getLayoutParams();
if (lp.bottom > row) {
row = lp.bottom;
}
int childHeight = (lp.bottom - lp.top)*side;
int childWidth = (lp.right-lp.left)*side;
int heightSpec = MeasureSpec.makeMeasureSpec(childHeight, LayoutParams.MATCH_PARENT);
int widthSpec = MeasureSpec.makeMeasureSpec(childWidth, LayoutParams.MATCH_PARENT);
child.measure(widthSpec, heightSpec);
}
height = row * side;
// TODO: Figure out a good way to use the heightMeasureSpec...
setMeasuredDimension(width, height);
}
#Override
public ViewGroup.LayoutParams generateLayoutParams(AttributeSet attrs) {
return new ICGridLayout.LayoutParams(getContext(), attrs);
}
#Override
protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
return p instanceof ICGridLayout.LayoutParams;
}
#Override
protected ViewGroup.LayoutParams
generateLayoutParams(ViewGroup.LayoutParams p) {
return new ICGridLayout.LayoutParams(p);
}
#Override
protected ViewGroup.LayoutParams generateDefaultLayoutParams() {
return new LayoutParams();
}
public static class LayoutParams extends ViewGroup.MarginLayoutParams {
int right = 1;
int bottom = 1;
int top = 0;
int left = 0;
int width = -1;
int height = -1;
public LayoutParams() {
super(MATCH_PARENT, MATCH_PARENT);
top = 0;
left = 1;
}
public LayoutParams(int width, int height) {
super(width, height);
top = 0;
left = 1;
}
public LayoutParams(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray a = context.obtainStyledAttributes(
attrs,
R.styleable.ICGridLayout_Layout);
left = a.getInt(R.styleable.ICGridLayout_Layout_layout_left, 0);
top = a.getInt(R.styleable.ICGridLayout_Layout_layout_top, 0);
right = a.getInt(R.styleable.ICGridLayout_Layout_layout_right, left + 1);
bottom = a.getInt(R.styleable.ICGridLayout_Layout_layout_bottom, top + 1);
height = a.getInt(R.styleable.ICGridLayout_Layout_layout_row_span, -1);
width = a.getInt(R.styleable.ICGridLayout_Layout_layout_col_span, -1);
if (height != -1) {
bottom = top + height;
}
if (width != -1) {
right = left + width;
}
a.recycle();
}
public LayoutParams(ViewGroup.LayoutParams params) {
super(params);
}
}
}
ICGridLayout.java is pretty straight forward. It takes the values provided by the children and lays them out.
attrs.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="ICGridLayout_Layout">
<attr name="columns" format="integer"/>
<attr name="layout_left" format="integer"/>
<attr name="layout_top" format="integer"/>
<attr name="layout_right" format="integer"/>
<attr name="layout_bottom" format="integer"/>
<attr name="layout_col_span" format="integer"/>
<attr name="layout_row_span" format="integer"/>
<attr name="layout_spacing" format="dimension"/>
</declare-styleable>
</resources>
example_layout.xml:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res/com.rischit.projectlogger"
android:id="#+id/scroller"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<com.risch.evertsson.iclib.layout.ICGridLayout
android:id="#+id/ICGridLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_spacing="4dp"
app:columns="4" >
<TextView
android:id="#+id/textView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_bottom="1"
app:layout_left="0"
app:layout_right="4"
app:layout_top="0"
android:background="#ff0000"
android:text="TextView" />
<TextView
android:id="#+id/textView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_bottom="3"
app:layout_left="3"
app:layout_right="4"
app:layout_top="1"
android:background="#00ff00"
android:text="TextView" />
<TextView
android:id="#+id/textView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_bottom="4"
app:layout_left="0"
app:layout_right="3"
app:layout_top="1"
android:background="#0000ff"
android:text="TextView" />
<TextView
android:id="#+id/textView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_bottom="4"
app:layout_left="3"
app:layout_right="4"
app:layout_top="3"
android:background="#ffff00"
android:text="TextView" />
<TextView
android:id="#+id/textView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_bottom="6"
app:layout_left="0"
app:layout_right="1"
app:layout_top="4"
android:background="#ff00ff"
android:text="TextView" />
<TextView
android:id="#+id/textView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_bottom="6"
app:layout_left="1"
app:layout_right="4"
app:layout_top="4"
android:background="#ffffff"
android:text="TextView" />
</com.risch.evertsson.iclib.layout.ICGridLayout>
</ScrollView>
-- Johan Risch
P.S
This is my first long answer, I've tried to do it in a correct way. If I've failed please tell me without flaming :)
D.S
Like this ?
<?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" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.54" >
<Button
android:id="#+id/Button01"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1.00"
android:text="Button" />
<Button
android:id="#+id/Button02"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1.00"
android:text="Button" />
</LinearLayout>
<Button
android:id="#+id/button3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="99dp" >
<Button
android:id="#+id/button1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="Button" />
<Button
android:id="#+id/button2"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="Button" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" >
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical" >
<Button
android:id="#+id/button4"
android:layout_width="match_parent"
android:layout_height="152dp"
android:text="Button" />
<Button
android:id="#+id/button5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical" >
<Button
android:id="#+id/button6"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button" />
<Button
android:id="#+id/button7"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Button" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
As many have said, nested linear layouts seem the only way to win here. Some of the solutions have not used the layout parameters in the most flexible manner. Code below seeks to do that, and in a way that's robust with aspect ratio changes. Details are in the comments.
<?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" >
<!-- First row. -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<!-- Equal weights cause two columns of equal width. -->
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="A" />
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="B" />
</LinearLayout>
<!-- Second row. -->
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="C" />
<!-- Third row. -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<!-- Equal weights cause two columns of equal width. -->
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="D" />
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="E" />
</LinearLayout>
<!-- Uneven fourth and fifth rows. -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:baselineAligned="false" >
<!-- Left column. Equal weight with right column gives them equal width. -->
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical" >
<!--
The use of weights below assigns all extra space to G. There
are other choices. LinearLayout computes sizes along its
axis as given, then divides the remaining extra space using
weights. If a component doesn't have a weight, it keeps
the specified size exactly.
-->
<!-- Fill width of layout and use wrap height (because there's no weight). -->
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="F" />
<!-- Fill width of layout and put all the extra space here. -->
<Button
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:text="G" />
</LinearLayout>
<!-- Right column. Equal weight with left column gives them equal width. -->
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical" >
<!-- Same as above except top button gets all the extra space. -->
<Button
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:text="H" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="I" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
So here is the solution I promised after one year =)
It basically uses the ViewTreeObserver to get the dimensions of the parent layout and create custom views accordingly. Since this code is one year old ViewTreeObserver might not be the best way to get the dimensions dynamically.
You can find the full source code here:
https://github.com/cdoger/Android_layout
I divided the screen into 8 equal widths and 6 equal heights. Here is a snapshot of how I laid out the views:
final RelativeLayout mainLayout = (RelativeLayout) findViewById(R.id.main_layout);
ViewTreeObserver vto = mainLayout.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
final int oneUnitWidth = mainLayout.getMeasuredWidth() / 8;
final int oneUnitHeight = mainLayout.getMeasuredHeight() / 6;
/**
* 1
***************************************************************/
final RelativeLayout.LayoutParams otelParams = new RelativeLayout.LayoutParams(
oneUnitWidth * 4, oneUnitHeight);
otelParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
otelParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
// otelParams.setMargins(0, 0, 2, 0);
View1.setLayoutParams(otelParams);
/***************************************************************/
/**
* 2
***************************************************************/
final RelativeLayout.LayoutParams otherParams = new RelativeLayout.LayoutParams(
oneUnitWidth * 4, oneUnitHeight);
otherParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
otherParams.addRule(RelativeLayout.RIGHT_OF, View1.getId());
otherParams.setMargins(2, 0, 0, 0);
View2.setLayoutParams(otherParams);
/***************************************************************/
//... goes on like this
Here is the final screenshot:
Embed your GridLayout in LinearLayout as below and try it worked for me.
<?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="horizontal" >
<GridLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnCount="2" >
<Button
android:layout_column="0"
android:layout_columnSpan="1"
android:layout_gravity="start|end"
android:layout_row="0"
android:text="ASDFASDF" />
<Button
android:layout_column="1"
android:layout_gravity="start|end"
android:layout_row="0"
android:text="SDAVDFBDFB" />
<Button
android:layout_column="0"
android:layout_columnSpan="2"
android:layout_gravity="fill|center"
android:layout_row="1"
android:text="ASDVADFBFDAFEW" />
<Button
android:layout_column="0"
android:layout_columnSpan="1"
android:layout_gravity="fill|center"
android:layout_row="2"
android:text="FWEA AWFWEA" />
<Button
android:layout_column="1"
android:layout_columnSpan="1"
android:layout_gravity="fill"
android:layout_row="2"
android:text="BERWEfasf" />
<Button
android:layout_width="94dp"
android:layout_column="0"
android:layout_columnSpan="1"
android:layout_gravity="fill|center"
android:layout_row="3"
android:text="SDFVBFAEVSAD" />
<Button
android:layout_column="0"
android:layout_columnSpan="1"
android:layout_gravity="fill|center"
android:layout_row="4"
android:layout_rowSpan="2"
android:text="GVBAERWEFSD" />
<Button
android:layout_column="1"
android:layout_columnSpan="1"
android:layout_gravity="fill|center"
android:layout_row="3"
android:layout_rowSpan="2"
android:text="VSDFAVE SDFASDWA SDFASD" />
<Button
android:layout_column="1"
android:layout_columnSpan="1"
android:layout_gravity="fill|center"
android:layout_row="5"
android:text="FWEWEGAWEFWAE"/>
</GridLayout>
</LinearLayout>

Custom components or regular composition

I'm up to creating a piece of reusable interface, namely a year-picker(just year). Essentially, this component should have a couple of buttons and a textview, sliding feature would be great as well.
As I've said I strive to make it reusable, but I'm not sure whether Android provides any means for achieving it. I've started reading the "Custom Components" article - is it what I really need ? Or should I make something like a regular composing class that has references to view objects, etc ?
Thanks.
Update: it was simple - here's an example if someone gets in the same situation
YearPicker.class
public class YearPicker extends LinearLayout {
protected int _minValue = 1980;
protected int _currentValue;
protected int _maxValue;
protected int _decrementValueView = R.id.decrement_value;
protected int _previousValueView = R.id.previous_value;
protected int _currentValueView = R.id.current_value;
protected int _nextValueView = R.id.next_value;
public YearPicker(Context context) {
super(context);
}
public YearPicker(Context context, AttributeSet attrs) {
super(context, attrs);
Calendar calendar = Calendar.getInstance();
_maxValue = calendar.get(Calendar.YEAR);
_currentValue = _maxValue - ((_maxValue - _minValue) / 2);
}
#Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
super.onLayout(changed, l, t, r, b);
TextView curView = (TextView) findViewById(_currentValueView);
curView.setText(Integer.toString(_currentValue));
findViewById(_decrementValueView).setOnClickListener(
_onDecrementValueClick);
}
protected OnClickListener _onDecrementValueClick = new OnClickListener() {
#Override
public void onClick(View v) {
_currentValue--;
TextView curView = (TextView) findViewById(_currentValueView);
curView.setText(Integer.toString(_currentValue));
}
};
}
main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/hello" />
<include
layout="#layout/year_picker"/>
</LinearLayout>
year_picker.xml
<view xmlns:android="http://schemas.android.com/apk/res/android"
class="localhost.yearpicker.YearPicker"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="#+id/decrement_value"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
<TextView
android:id="#+id/previous_value"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Small Text"
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:id="#+id/current_value"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/next_value"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Small Text"
android:textAppearance="?android:attr/textAppearanceSmall" />
<Button
android:id="#+id/increment_value"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</view>
In your case, I would just extend a preferable ViewGroup, inflate an XML - for styling and positioning Views - into it, grab an access to those View, add all relevant set/get methods, and that's it.
If relevant, you can also define attributes for your component to adjust its custom functionalities while using it in XMLs.
You might not need a custom view at all. I prefer to keep separate XML layouts that I can inflate as and when needed, then control them with custom OnClickListeners (or OnTouchListeners)
I'd only resort to a custom view if you need to override a base view's method (such as the onLayout or draw() methods)

Categories

Resources