I have a custom PrintDocumentAdapter and draw pages on my own, so I should draw xml layout on the page canvas:
private void drawPage(PdfDocument.Page page, List<Object> objects,
int pageNumber) {
Canvas canvas = page.getCanvas();
...
}
there is an TableLayout in my xml. I want table width to fill the entire page but it exceed the page if the width bigger than page width or is smaller than the page if its width is smaller than the page width. I used both wrap_content and match_parent but none of them worked. I even tried fix width and height.
private void drawPage(PdfDocument.Page page, List<Payment> payments,
int pageNumber) {
Canvas canvas = page.getCanvas();
PdfDocument.PageInfo pageInfo = page.getInfo();
canvas.save();
canvas.translate(leftMargin , topMargin);
FrameLayout frameLayout = new FrameLayout(context);
frameLayout.setLayoutParams(new ViewGroup.LayoutParams(200, 200));
LayoutInflater li = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = li.inflate(R.layout.payments_table_layout, null);
v.setLayoutParams(new
FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
TableLayout tableLayout = (TableLayout) v.findViewById(R.id.payments_table_layout);
for(int i= 0 ; i< objects.size(); ++i){
//... some code
}
frameLayout.addView(v);
frameLayout.measure(200 , 200);
frameLayout.layout(100, 100, 100, 100);
frameLayout.draw(canvas);
canvas.restore();
}
R.layout.payments_table_layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="50dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="some text"
style="#style/CustomFont.Wave"/>
</FrameLayout>
<TableLayout
android:id="#+id/payments_table_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal"
android:stretchColumns="*"
android:layout_weight="1">
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:background="#ccc">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="some text"
android:layout_gravity="center"
android:background="#drawable/table_border"
android:padding="4dp"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="some text"
android:layout_gravity="center"
android:background="#drawable/table_border"
android:padding="4dp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="some text"
android:layout_gravity="center"
android:background="#drawable/table_border"
android:padding="4dp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="some text"
android:layout_gravity="center"
android:background="#drawable/table_border"
android:padding="4dp"/>
</TableRow>
</TableLayout>
</LinearLayout>
Now the Result:
I shouldn't stretch all columns of TableLayout so I changed this attribute to :
android:stretchColumns="1,3,7"
android:shrinkColumns="5"
and set the layout width to width of the page like this ( we should care the dimension unit): in my customPrintDocumentAdapter
#Override
public void onLayout(PrintAttributes oldAttributes, PrintAttributes newAttributes, CancellationSignal cancellationSignal, LayoutResultCallback callback, Bundle extras) {
myPdfDocument = new PrintedPdfDocument(context, newAttributes);
pageHeight =
newAttributes.getMediaSize().getHeightMils()/1000 * 72;
pageWidth =
newAttributes.getMediaSize().getWidthMils()/1000 * 72;
...
}
and my new drawPage method:
private void drawPage(PdfDocument.Page page, List<Payment> payments,
int pageNumber) {
...
frameLayout.setLayoutParams(new ViewGroup.LayoutParams(pageWidth, pageHeight));
...
}
please try below xml , in which I used weightsum and layout weight in TableRow and its child textviews
android:weightSum="4"
android:layout_width="0dp"
android:layout_weight="1"
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="50dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="some text"
/>
</FrameLayout>
<TableLayout
android:id="#+id/payments_table_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal"
android:stretchColumns="*"
android:layout_weight="1">
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="4"
android:layout_gravity="center_horizontal"
android:background="#ccc">
<TextView
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="some text"
android:layout_gravity="center"
android:background="#color/colorAccent"
android:padding="4dp"
/>
<TextView
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="some text"
android:layout_gravity="center"
android:background="#color/colorAccent"
android:padding="4dp"/>
<TextView
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="some text"
android:layout_gravity="center"
android:background="#color/colorAccent"
android:padding="4dp"/>
<TextView
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="some text"
android:layout_gravity="center"
android:background="#color/colorAccent"
android:padding="4dp"/>
</TableRow>
</TableLayout>
</LinearLayout>
Related
how to create this kind of layout in android
i have tried using table layout. but getting error in creating this.
please check my code i am able to display images. 1 image in 1 row. but i want 2 small images in 1 row. and continues.
here is my code
private void getBannerList() {
final Call<BannerList_Model> itemMainResponse = APIHandler.getApiService().bannerList(getFiledMapForItemList());
itemMainResponse.enqueue(new Callback<BannerList_Model>() {
#Override
public void onResponse(Response<BannerList_Model> response, Retrofit retrofit) {
BannerList_Model itemResponse = null;
itemResponse = response.body();
if (itemResponse == null) {
UIUtils.showToast(getContext(), "Server Error !");
} else {
String msg = itemResponse.getMsg().toString();
if (msg.equals("success")) {
userList = itemResponse.getBannerList();
if(userList != null) {
TableLayout table = (TableLayout) view.findViewById(R.id.tblBanner_list);
table.setColumnStretchable(0,true);
table.setColumnStretchable(1,true);
int tsize = userList.size();
TextView[] t1 = new TextView[tsize];
ImageView[] img = new ImageView[tsize];
for(int i=0; i<userList.size();i++)
{
TableRow row = new TableRow(getContext());
String banner_size = userList.get(i).getBannerSize().toString();
img[i] = new ImageView(getContext());
Picasso.with(getContext())
.load(Constants.VIDEO_URL + userList.get(i).getBannerUrl())
.into(img[i]);
row.addView(img[i]);
table.addView(row);
}
}
}
}
}
}
});
}
here is the video how i want layout
http://shrivivah.com/layout.mp4
if there is 2 small images than it will display in 1 row.
if there is 2 medium or full size image than it will display in 1 row each.
if 1 small and 2 medum than 1 image per row to display.
layout image
Your mistake is that you are adding TextView to TableRow(row) but you didn't add row to TableLayout. Here is an example for what you need. You can set some margins and add button.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TableLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<TableRow
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<TextView
android:id="#+id/textView3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="TextView" />
<TextView
android:id="#+id/textView2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="TextView" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<TextView
android:id="#+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
</TableRow>
</TableLayout>
<TableLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/textView5"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="TextView" />
</TableRow>
</TableLayout>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.tops.stackoverflow.MainActivity">
<LinearLayout
android:id="#+id/llShortcutParent"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:duplicateParentState="true"
android:elevation="20dp"
android:padding="10dp"
android:orientation="horizontal">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button1"
android:layout_weight="1"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button1"
android:layout_weight="1"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:duplicateParentState="true"
android:elevation="20dp"
android:padding="10dp"
android:orientation="horizontal">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button1"
android:layout_weight="1"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:duplicateParentState="true"
android:elevation="20dp"
android:padding="10dp"
android:orientation="horizontal">
<Button
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="Button1"
android:layout_weight="1"/>
<Button
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="Button1"
android:layout_weight="1"/>
</LinearLayout>
</LinearLayout>
I am inflating a layout and converting it into and then i will share it using the as usual intents.
inflating the layout using -
LayoutInflater inflater2 = LayoutInflater.from(this);
qr_sheetview = inflater2.inflate(R.layout.dummy_layout, mainlayout, false);
I tried three methods -
convert the inflated view into bitmap using this method
qr_sheetview.setDrawingCacheEnabled(true);
qr_sheetview.measure(View.MeasureSpec.makeMeasureSpec(0,
View.MeasureSpec.UNSPECIFIED),
View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
qr_sheetview.layout(0, 0, qr_sheetview.getMeasuredWidth(), qr_sheetview.getMeasuredHeight());
qr_sheetview.buildDrawingCache(true);
sharedbmap = Bitmap.createBitmap(qr_sheetview.getDrawingCache());
But using this method my layout gets shrinked and overlapped.
width was 1107 height was 448
I am using Framelayout as rootlayout and relative layout for inner views.
Second method i am using is this - >
sharedbmap = Bitmap.createBitmap(qr_sheetview.getWidth(), qr_sheetview.getHeight(), Bitmap.Config.ARGB_8888);
canvas = new Canvas(sharedbmap);
qr_sheetview.draw(canvas);
in this method error occurred : height and width must be > 0
using this also gives me the same error
qr_sheetview.post(new Runnable() {
#Override
public void run() {
sharedbmap = loadBitmapFromView(qr_sheetview);
shareBitmap(adjustOpacity(sharedbmap), "temporary");
}
});
the third method is this - >
public static Bitmap loadBitmapFromView(View v) {
int specWidth = View.MeasureSpec.makeMeasureSpec(0 /* any */, View.MeasureSpec.UNSPECIFIED);
v.measure(specWidth, specWidth);
Log.d("TAG", "height" + v.getMeasuredHeight());
Log.d("TAG", "width" + v.getMeasuredWidth());
Bitmap b = Bitmap.createBitmap(v.getMeasuredWidth(), v.getMeasuredHeight(), Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(b);
v.layout(v.getLeft(), v.getTop(), v.getRight(), v.getBottom());
v.draw(c);
return b;
}
in this i am getting the same result as in first.
the Layout is ->
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff"
>
<RelativeLayout
android:id="#+id/mainqrlayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff">
<RelativeLayout
android:id="#+id/layout001"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp">
<TextView
android:id="#+id/appname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="5dp"
android:gravity="center|bottom"
android:text="Some text"
android:textColor="#212121"
android:textStyle="bold"
android:textSize="24sp" />
<ImageView
android:id="#+id/logo"
android:layout_width="64dp"
android:layout_height="64dp"
android:layout_centerHorizontal="true"
android:layout_below="#+id/appname"
android:layout_marginTop="10dp"
android:src="#drawable/logomain" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/layout003"
android:layout_below="#+id/layout001">
<ImageView
android:id="#+id/qrimage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true" />
<ProgressBar
android:id="#+id/progressBar"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_centerInParent="true"
android:visibility="gone" />
<ImageView
android:id="#+id/qr_logo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:visibility="gone"
card_view:srcCompat="#drawable/kj_qr_logo_svg" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/layout003"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true">
<TextView
android:id="#+id/vpaname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:gravity="center"
android:text="kdvkjadsfkasmd"
android:textColor="#212121" />
<TextView
android:id="#+id/qr_vpa"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/vpaname"
android:layout_centerHorizontal="true"
android:text="skdmfkjasdf"
android:textColor="#444444" />
<TextView
android:id="#+id/text3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/qr_vpa"
android:layout_centerHorizontal="true"
android:layout_marginTop="15dp"
android:gravity="center"
android:padding="10dp"
android:text="ckasfdowmdmwkmdwkdmkwmdklmekld \n sdafasdfasdfaasdf"
android:textColor="#727272" />
<TextView
android:id="#+id/companyname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/text3"
android:layout_centerHorizontal="true"
android:layout_marginBottom="10dp"
android:layout_marginTop="20dp"
android:gravity="bottom"
android:text="#string/companyname"
android:textColor="#727272"
android:textSize="10sp" />
</RelativeLayout>
</RelativeLayout>
</FrameLayout>
I would like to create a Seekbar, above which there will be text label on each Seekbar step, what it should look like is shown in below image
this is the expected result, for which what i have done,
<RelativeLayout
android:id="#+id/layout_font_size"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/txtFont_size_hint"
style="#style/textStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/Aa" />
<LinearLayout
android:id="#+id/layout_seekbar_interval_holder"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/txtFont_size_hint"
android:layout_marginLeft="16dp"
android:layout_marginRight="#dimen/margin_16dp"
android:orientation="horizontal"
android:weightSum="5">
<TextView
android:id="#+id/txtSize_14"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:focusable="false"
android:gravity="center"
android:text="#string/setting_font_text"
android:textSize="#dimen/font_size_14" />
<TextView
android:id="#+id/txtSize_18"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:focusable="false"
android:gravity="center"
android:text="#string/setting_font_text"
android:textSize="#dimen/font_size_18sp" />
<TextView
android:id="#+id/txtSize_24"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:focusable="false"
android:gravity="center"
android:text="#string/setting_font_text"
android:textSize="#dimen/font_size_24sp" />
<TextView
android:id="#+id/textSize_30"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:focusable="false"
android:gravity="center"
android:text="#string/setting_font_text"
android:textSize="#dimen/font_size_30sp" />
<TextView
android:id="#+id/txtSize_36"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:focusable="false"
android:gravity="center"
android:text="#string/setting_font_text"
android:textSize="#dimen/font_size_36sp" />
</LinearLayout>
<SeekBar
android:id="#+id/seekBarSetting_font_size"
android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_below="#id/layout_seekbar_interval_holder"
android:layout_marginLeft="#dimen/margin_16dp"
android:layout_marginRight="#dimen/margin_16dp"
android:max="4"
android:theme="#style/seekBarYelloStyle" />
</RelativeLayout>
Output of this is
the Layout looks similar to what is expected but, while moving the seekbar thumb, the thumb is not vertically aligned with the TextView so the problem here is the seekbar thumb is not vertically aligend with the text indicating the steps of seekBar.
I had also tried calculating the exact position of TextView by dividing the seekbars width by steps (Here we have 5 steps but first one is on 0th position so i divided the total seekbar Width by 4 and then placed each text accordingly) but didn't got the expected solution.
I am bit confused here, and want a simple solution as soon as possible.
P.S: ScreenShot of output of view created dynamically is also attached for reference
layout for this :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="android.sample.MainActivity">
<TextView
android:id="#+id/txtFont_size_hint"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Sizes"/>
<LinearLayout
android:id="#+id/ll"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:weightSum="5">
<LinearLayout
android:id="#+id/ll"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="5">
<TextView
android:id="#+id/txtSize1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="#string/setting_font_text"
android:textSize="15sp"/>
<TextView
android:id="#+id/txtSize_18"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="#string/setting_font_text"
android:textSize="20sp"/>
<TextView
android:id="#+id/txtSize_24"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:focusable="false"
android:gravity="center"
android:text="#string/setting_font_text"
android:textSize="25sp"/>
<TextView
android:id="#+id/textSize_30"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:focusable="false"
android:gravity="center"
android:text="#string/setting_font_text"
android:textSize="30sp"/>
<TextView
android:id="#+id/txtSize_36"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:focusable="false"
android:gravity="center"
android:text="#string/setting_font_text"
android:textSize="35sp"/>
</LinearLayout>
<SeekBar
android:id="#+id/seekBarSetting_font_size"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="4"/>
Now what i have done is aligned the width of Seekbar till the center of the last TextView which in this case is txtSize_36
and have set the ems as android:max="4" so there are five possible values(you can change this to as much as you want)
Code for Activity is :
public class MainActivity extends AppCompatActivity {
SeekBar seekBar;
private LinearLayout bar, ll;
TextView txtSize_14, txtSize_36;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
seekBar = (SeekBar) findViewById(R.id.seekBarSetting_font_size);
txtSize_14 = (TextView) findViewById(R.id.txtSize1);
txtSize_36 = (TextView) findViewById(R.id.txtSize_36);
ll = (LinearLayout) findViewById(R.id.ll);
}
float density;
#Override
protected void onResume() {
super.onResume();
ViewTreeObserver vto = ll.getViewTreeObserver();
//****old code (may not work on all orientations)****
/*vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
ll.getViewTreeObserver().removeGlobalOnLayoutListener(this);
} else {
ll.getViewTreeObserver().removeOnGlobalLayoutListener(this);
}
int width = ll.getMeasuredWidth();
int height = ll.getMeasuredHeight();
ViewGroup.LayoutParams params = seekBar.getLayoutParams();
density = getResources().getDisplayMetrics().density;
int newwidth = (int) (txtSize_14.getLeft() / density);
newwidth = newwidth+ (txtSize_36.getLeft() + txtSize_36.getRight()) / 2;
params.width = newwidth;
seekBar.setLayoutParams(params);
}
});*/
//****new code (should work on all orientations)****
vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
ll.getViewTreeObserver().removeGlobalOnLayoutListener(this);
} else {
ll.getViewTreeObserver().removeOnGlobalLayoutListener(this);
}
seekBar.setPadding(15, 0, 15, 0);
seekBar.setX(((txtSize_14.getLeft() + txtSize_14.getRight()) / 2) - 15);
ViewGroup.LayoutParams params = seekBar.getLayoutParams();
int centerwidth = ((txtSize_36.getLeft() + txtSize_36.getRight()) / 2) - ((txtSize_14.getLeft() + txtSize_14.getRight()) / 2) + 15;
params.width = centerwidth;
seekBar.setLayoutParams(params);
}
});
}
}
Here is screenshot for reference (I have clubbed for all positions of seekbar):
Frankly speaking it took me a while to sort this out using XML only.
The first and last label are differently positioned than the others....
In case you will change the size of the thumb from 15dp - you must also change the margin of first and last TextView and size of Spaces (margin + Space = thumb size)
preview
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:paddingRight="0dp"
android:paddingLeft="0dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:paddingLeft="0dp">
<TextView
android:text="7 days"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:textColor="#color/colorPrimaryBackground"
android:gravity="left|center"
android:textSize="#dimen/textSize_smallest"
android:layout_weight="0.5"
android:layout_marginLeft="5dp"/>
<Space
android:layout_width="10dp"
android:layout_height="match_parent"/>
<TextView
android:text="1 month"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:textColor="#color/colorPrimaryBackground"
android:gravity="center"
android:textSize="#dimen/textSize_smallest"
android:layout_weight="1"
android:layout_marginRight="0dp"
android:id="#+id/textView" />
<TextView
android:text="3 months"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:textColor="#color/colorPrimaryBackground"
android:gravity="center"
android:textSize="#dimen/textSize_smallest"
android:layout_weight="1"
android:layout_marginRight="0dp"/>
<TextView
android:text="1 year"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:textColor="#color/colorPrimaryBackground"
android:gravity="center"
android:textSize="#dimen/textSize_smallest"
android:layout_weight="1"
android:layout_marginLeft="0dp"/>
<TextView
android:text="3 years"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:textColor="#color/colorPrimaryBackground"
android:gravity="center"
android:textSize="#dimen/textSize_smallest"
android:layout_weight="1"
android:layout_marginLeft="0dp"/>
<Space
android:layout_width="10dp"
android:layout_height="match_parent"/>
<TextView
android:text="5 years"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:textColor="#color/colorPrimaryBackground"
android:gravity="right|center"
android:textSize="#dimen/textSize_smallest"
android:layout_weight="0.5"
android:layout_marginRight="5dp"/>
</LinearLayout>
</RelativeLayout>
I'm drawing lines in relative Layout.To do this, I add line views add to relative layout. But when I add drawed lines to relative layouts the Relative Layout's height changes. Why ? Thanks in advance for your help ! :)
layout.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent"
android:layout_height="fill_parent"
tools:context="com.mobisoft.kelimebul.Local.Oyun"
android:backgroundTint="#ffffcd8d"
android:backgroundTintMode="multiply"
android:background="#ffff9f63"
android:padding="7dp"
android:id="#+id/s"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/relative"
android:touchscreenBlocksFocus="true"
android:nestedScrollingEnabled="true"
android:layout_weight="1">
<GridView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/gameBoard"
android:listSelector="#00000000"
android:background="#color/tahta_rengi"
android:scrollbars="none"
android:fastScrollAlwaysVisible="false"
android:fastScrollEnabled="false"
android:focusable="false"
android:focusableInTouchMode="false"
android:drawSelectorOnTop="true" />
</RelativeLayout>
<HorizontalScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/horizontalScrollView"
android:background="#color/tahta_rengi"
android:layout_weight="1">
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/scrolinear">
</LinearLayout>
</HorizontalScrollView>
<RelativeLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#color/backgroundColor"
android:backgroundTint="#color/backgroundColorTint"
android:visibility="visible"
android:layout_weight="1">
<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginRight="5dp"
android:layout_centerHorizontal="true">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/timeIcon"
android:src="#drawable/clock"
android:scaleType="fitCenter"
android:layout_gravity="center_horizontal"
android:layout_marginRight="4dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:id="#+id/time"
android:gravity="center_vertical|center|right|center_horizontal"
android:textSize="#dimen/score_textsize"
android:layout_gravity="right"
android:textAlignment="center"
android:layout_alignParentRight="true" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="#string/RemainingWordN"
android:id="#+id/textView5"
android:textSize="#dimen/score_textsize" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:id="#+id/remainingNumber"
android:textSize="#dimen/score_textsize" />
</LinearLayout>
</RelativeLayout>
</LinearLayout>
Activity.java // Some part code of this activity
DrawView drawView;
drawView = new DrawView(getApplicationContext(), locs, view.getWidth(), colors.get(lineColor));
RelativeLayoutOfLines.addView(drawView); // Add view to relativeLayouts
DrawView.java
public class DrawView extends View {
Paint paint = new Paint();
float locs[];
int strokeWidth;
int color;
public DrawView(Context context, float locs[], int strokeWidth, int color) {
super(context);
this.locs = locs;
this.strokeWidth = strokeWidth;
this.color = color;
}
#Override
public void onDraw(Canvas canvas) {
paint.setColor(getContext().getResources().getColor(color));
paint.setAlpha(100);
paint.setStrokeWidth(strokeWidth);
canvas.drawLine(locs[0], locs[1], locs[2], locs[3], paint); // left, top, right, bottom
canvas.save();
canvas.restore();
}
}
Before Drawing:
https://www.dropbox.com/s/oo79d8rvp4sa2nj/s.PNG?dl=0
After Drawing :
https://www.dropbox.com/s/vawjsxcbi6gd2go/s2.PNG?dl=0
I am trying to draw lines and rectangles and the line works perfectly, but when I try to draw the rectangles they doesn't show, so I commented the method and put the rectangles in the background of the layouts, but even they didn't show.
MainActivity.class
protected void onCreate (Bundle savedInstanceState {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
line = (LinearLayout)findViewById(R.id.line);
doLine DoLine = new doLine(line.getContext (),0.2,0.4,0.4);
line.addView(DoLine);
}
DoLine.class
public DoLine (Context context, double first, double second, double third){
super (context);
one = first;
two = second;
three = three;
}
#SuppressLink("DrawAllocation")
public void onDraw (Canvas canvas){
canvas.drawRGB (255,255,255);
Paint pincel1 = new Paint;
pincel1.setColor(getResources().getColor(R.color.yellow));
canvas.drawRect(0,10, getWidth(),40,pincel1);
pincel1.setColor(getResources().getColor(R.colot.green));
canvas.drawRect((float)(getWidth()*one),10, getWidth(),40,pincel1);
pincel1.setColor(getResources().getColor(R.color.blue));
canvas.drawRect((float)(getWidth()*one),10, (float)(getWidth()-(getWidth()*three)),40, pincel1);
activity_main.xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#dcdcdc"
android:orientation="vertical"
tools:context=".MainActivity" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="5dp"
android:background="#fdfdfd"
android:orientation="vertical"
android:padding="3dp" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="35dp"
android:background="#90ffb200"
android:padding="5dp" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginRight="5dp"
android:gravity="right"
android:text="#string/title"
android:textSize="17sp" />
</LinearLayout>
<LinearLayout
android:id="#+id/line"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingLeft="5dp"
android:paddingRight="5dp" >
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingLeft="5dp">
<LineraLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal
<LinearLayout
android:id="#+id/rectyellow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="#drawable/yellow
</LinearLayout>
<TextView
android:id="#+id/oneText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="one" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
<LinearLayout
android:id="#+id/rectblue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="#drawable/blue"
</LinearLayout>
<TextView
android:id="#+id/twoText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="two" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
<LinearLayout
android:id="#+id/rectgreen"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="#drawable/green"
</LinearLayout>
<TextView
android:id="#+id/threeText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Three" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>
I have been researching for many many days to find a solution and today i found it, I just needed to change the height of the layout to 40dp
android:layout_height="wrap_content"// change this to 40dp
mainActivity.xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#dcdcdc"
android:orientation="vertical"
tools:context=".MainActivity" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="5dp"
android:background="#fdfdfd"
android:orientation="vertical"
android:padding="3dp" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="35dp"
android:background="#90ffb200"
android:padding="5dp" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginRight="5dp"
android:gravity="right"
android:text="#string/title"
android:textSize="17sp" />
</LinearLayout>
<LinearLayout
android:id="#+id/line"
android:layout_width="match_parent"
android:layout_height="wrap_content"// change this to 40dp
android:orientation="vertical"
android:paddingLeft="5dp"
android:paddingRight="5dp" >
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingLeft="5dp">
<LineraLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal
<LinearLayout
android:id="#+id/rectyellow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="#drawable/yellow
</LinearLayout>
<TextView
android:id="#+id/oneText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="one" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
<LinearLayout
android:id="#+id/rectblue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="#drawable/blue"
</LinearLayout>
<TextView
android:id="#+id/twoText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="two" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
<LinearLayout
android:id="#+id/rectgreen"
android:layout_width="wrap_content"
android:orientation="horizontal"
android:background="#drawable/green"
</LinearLayout>
<TextView
android:id="#+id/threeText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Three" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>
https://www.dropbox.com/s/8dvmgv1k37tnm9f/Sin%20t%C3%ADtulo.png
it's right:
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LinearLayout line = (LinearLayout)findViewById(R.id.line);
DoLine doLine = new DoLine(line.getContext (),0.2,0.4,0.4);
line.addView(doLine);
}
public class DoLine extends View {
double one;
double two;
double three;
public DoLine(Context context, double first, double second, double third) {
super(context);
one = first;
two = second;
three = third;
}
#SuppressLint("DrawAllocation")
public void onDraw(Canvas canvas) {
canvas.drawRGB(255, 255, 255);
Paint pincel1 = new Paint();
pincel1.setColor(Color.YELLOW);
canvas.drawRect(0, 10, getWidth(), 40, pincel1);
pincel1.setColor(Color.GREEN);
canvas.drawRect((float) (getWidth() * one), 10, getWidth(), 40, pincel1);
pincel1.setColor(Color.BLUE);
canvas.drawRect((float) (getWidth() * one), 10, (float) (getWidth() - (getWidth() * three)), 40, pincel1);
}
}
}