Android ImageButton with background, source and text - android

I want to create ImageButton with custom background, custom Icon and text below that icon
What I have so far is
<ImageButton
android:id="#+id/imageButton1"
android:layout_width="140dp"
android:layout_height="120dp"
android:layout_marginLeft="3dp"
android:layout_marginBottom="6dp"
android:background="#drawable/btn_rectangle_background"
android:src="#drawable/ic_action_search"
/>
However, If I put there android:text="blablabla" it won't shows up :/
ic_action_homework is .PNG icon, but btn_rectangle_background is XML file, which defines shape
That's what I would like to achieve

1st answer:
Must be structure of layout likes here:
<LinearLayout
android:widht_layout="80dp"
android:height_layout="80dp"
android:padding="10dp"
android:gravity="center"
android:layout_gravity="center"
android:bacgkground="your_color ARGB"
>
<ImageView />
<TextView />
</LinearLayout>
or 2nd answer:
Create custom view
public class customView extends View{
public customView(Context context){
super(context);
}
public customView(Context context, String s, Drawable d){
super(context);
// Set Width&Height for this view
this.measure(80,80);
// or layout params with specified height&width for this view
Resources r = getResources();
int width = (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, **Your width**,
r.getDisplayMetrics());
int height = (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, **your height**,
r.getDisplayMetrics());
ViewGroup.LayoutParams pp = new ViewGroup.LayoutParams(width,height);
this.setLayoutParams(pp);
TextView _text = new TextView(context);
ImageView _image = new ImageView(context);
_text.setText(s);
_image.setBackground(d);
this.addView(_image);
this.addView(_text);
}
public customView(Context context, String s, Bitmap b){
....
_image.setImageBitmap(b);
...
}
}
also add view into root view #id=content of layout from activity:
findByView(R.id.content).addView(new customView((Context)this,"Your Text",getResources().getDrawable(R.drawable.icon));
or with parametr bitmap by path:
findByView(R.id.content).addView(new customView((Context)this,"Your Text",BitmapFactory.decodeFile("/sdcard/file.png"));

Related

Custom LinearLayout - childs not visible

I build customView extending LinearLayout and having simple 3 childrens(2 TextView and ImageView). I create this view dynamically in code and adding it to parent LinearLayout. This view has background, so I can easily spot on the screen, that it is inflated correctly in its place, but any of child is not visible. I checked LayoutInspector and it shows that everything is setted correctly(text values to TextViews and picture to ImageView), but somehow when I try to locate them on inspector they are shown as little dot over my customView:
My CustomView is called DayTileView and this is square with gray background. As you can see on inspector on the left childrens are filled with content. Layout of View:
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<merge>
<TextView
android:id="#+id/day"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:textColor="#android:color/white"
/>
<TextView
android:id="#+id/dayName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:textColor="#android:color/white"
/>
<ImageView
android:id="#+id/padlock"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:src="#drawable/ic_padlock"
/>
</merge>
</layout>
And its code:
public class DayTileView extends LinearLayout {
private DayTileBinding mBinding;
public DayTileView(Context context) {
super(context);
init();
}
public DayTileView(Context context, #Nullable AttributeSet attrs) {
super(context, attrs);
init();
}
public DayTileView(Context context, #Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
private void init() {
mBinding = DataBindingUtil.inflate(LayoutInflater.from(getContext()), R.layout.day_tile, this, true);
setOrientation(VERTICAL);
}
#Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
final int width = getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec);
setMeasuredDimension(width, width);
}
public void setDay(int day, int month, int year) {
DateTime settedDay = new DateTime().withYear(year).withMonthOfYear(month).withDayOfMonth(day);
mBinding.day.setText(String.valueOf(day));
String dayName = settedDay.dayOfWeek().getAsText();
mBinding.dayName.setText(dayName);
boolean isWeekend = settedDay.dayOfWeek().get() == 6 || settedDay.dayOfWeek().get() == 7;
setBackgroundColor(ContextCompat.getColor(getContext(), isWeekend ? R.color.weekend_bg : R.color.weekday_bg));
}
}
Its use in another CustomView which is also LinearLayout but wiht horizontal orientation (PlannedDayView on inspector):
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<merge>
<*.customViews.DayTileView
android:id="#+id/dayTile"
android:layout_width="0dp"
android:layout_weight="0.2"
android:layout_height="wrap_content"/>
<LinearLayout
android:id="#+id/container"
android:orientation="vertical"
android:layout_weight="1.2"
android:layout_width="0dp"
android:layout_height="wrap_content"/>
</merge>
</layout>
Has anyone any idea what could be casuing this (childs out of view)? When I replace merge for LinearLayout with vertical orientation and same background everything in Design mode of layout is visible correctly, so it should work.
EDIT:
I found out, that if I set during View initalization Padding Top to 10px then dot is moving down. So it looks like from some reasons Android didn't made to inflate correctly TextViews and ImageView
I found out what was the problem:
I overrided onMeasure and didn't measure child Views. Earlier I was using such code to make square View not square ViewGroup.
Corrected code:
final int width = getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec);
setMeasuredDimension(width, width);
super.onMeasure(MeasureSpec.makeMeasureSpec(width, MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(width, MeasureSpec.EXACTLY));
After setting correct width and height for View I must measure whole view with new MeasureSpec

How can I make the Views defined in Android XML customs view file visible

I've the below code, that is having one element as shown in the XML file below it, and a call of the custom view named card
package tk.zillion.app1;
import tk.zillion.app1.CustomViews.Card;
public class EmptyActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_empty);
RelativeLayout rl;
rl = (RelativeLayout) findViewById(R.id.activity_empty);
rl.addView(new Card(this));
}
}
Below is the XML file, and how the layout appear in the Android studio:
The Card custom view is as below code and XML file:
package tk.zillion.app1.CustomViews;
import tk.zillion.app1.R;
public class Card extends RelativeLayout {
public Card(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init(context);
}
public Card(Context context, AttributeSet attrs){
super(context, attrs);
init(context);
}
public Card(Context context) {
super(context);
init(context);
}
private void init(Context context) {
WindowManager windowmanager = (WindowManager) this.getContext()
.getSystemService(Context.WINDOW_SERVICE);
Display display = windowmanager.getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);;
RelativeLayout rl = new RelativeLayout(this.getContext());
rl.setBackground(context.getDrawable(R.drawable.layer_card_background));
rl.setMinimumWidth(width);
Button btn = new Button(this.getContext());
// btn.setId(R.id.titleId);
btn.setId(View.generateViewId());
TextView one = new TextView(this.getContext());
one.setText("Device width is: "+String.valueOf(width)+", Device height is: "+String.valueOf(height));
one.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
btn.setText(R.string.custom);
btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
int duration = Toast.LENGTH_SHORT;
CharSequence text = "Hello from another toast!";
Toast toast = Toast.makeText(v.getContext(), text, duration);
toast.show();
}
});
rl.addView(btn);
lp.addRule(RelativeLayout.BELOW, btn.getId());
rl.addView(one, lp);
// or one.setLayoutParams(lp); rl.addView(one);
// lp.removeRule(RelativeLayout.BELOW);
this.addView(rl);
}
}
and the XML file is:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/custom_view"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<tk.zillion.app1.CustomViews.Card
android:id="#+id/custom_card"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView android:id="#+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/app_name"
/>
</tk.zillion.app1.CustomViews.Card>
</RelativeLayout>
and appeared in Android studio as:
Below is how the app looks at execution (Right side), and how it should be (Left side).
The View defined in the XML file, that is the My First App TextView, is not appearing at the execution, while all other elements defined pragmatically are visible.
I tried to use inflate but did not work with me
My Question is how can I the View, the ones defined in the XML file are visible?
Try this xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/custom_view"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<TextView android:id="#+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/app_name"
/>
<tk.zillion.app1.CustomViews.Card
android:id="#+id/custom_card"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
I found the answer, and will detailed it below, may some one in the future get help from it:
1 . there was no need for using <tk.zillion.app1.CustomViews.Card> so I restructured my view_customs.xml to be:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/custom_view"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<TextView android:id="#+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/app_name"
/>
</LinearLayout>
2 . It looks I was wrongly using the inflater, so re-wote it to match the:
inflate(XmlPullParser parser, ViewGroup root, boolean attachToRoot)inflation statement, where itInflates a new view hierarchy from the specified XML node.
3 . The final structure of my Card class init method, became:
private void init(Context context) {
// 1. Define the main layout of the CARD class
RelativeLayout cardLayout = new RelativeLayout(this.getContext());
// 2. (Optional) set the require specs for this Layout
cardLayout.setBackground(
context.getDrawable(R.drawable.layer_card_background));
cardLayout.setMinimumWidth(width);
// 3. Inflate the custom view
LayoutInflater inflater = LayoutInflater.from(context);
View inflatedLayout= inflater.inflate(R.layout.view_customs, null, false);
// 4. Set ID for the inflated view
inflatedLayout.setId(View.generateViewId());
// 5. (Optional) pick and change the elements in the custom view
TextView otv = (TextView) inflatedLayout.findViewById(R.id.tv);
otv.setText("Welcome to my first app");
/*
You can avoid point 3, 4 and 5, if you do not want to infalte from existing layout, simply use:
View inflatedLayout = new View(this.getContext());
*/
// 6. (Optional) create new elements
Button btn = new Button(this.getContext());
btn.setId(View.generateViewId());
btn.setText(R.string.custom);
btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
int duration = Toast.LENGTH_SHORT;
CharSequence text = "Hello from another toast!";
Toast toast = Toast.makeText(v.getContext(), text, duration);
toast.show();
}
});
TextView one = new TextView(this.getContext());
one.setText("Device width is: ");
one.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
// 7. Locate the relative locations between the inflated element and the main Layout
RelativeLayout.LayoutParams p = new RelativeLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
p.addRule(RelativeLayout.BELOW, inflatedLayout.getId());
cardLayout.setLayoutParams(p);
// 8. In the same way as of 7, locate the relative locations of the elements inside the main Layout itself.
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
lp.addRule(RelativeLayout.BELOW, btn.getId());
// 9. Add the inflated Layout, and all other elements to the main Layout
cardLayout.addView(inflatedLayout);
cardLayout.addView(btn);
cardLayout.addView(one, lp);
// 10. Add the main Layout to the class
this.addView(cardLayout);
// 11. In the same way, you need to locate the returned CARD
// in relative location in the view that calling it
RelativeLayout.LayoutParams p1 = new RelativeLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
// ViewGroup.LayoutParams.WRAP_CONTENT
150);
p1.addRule(RelativeLayout.BELOW, R.id.checkBox);
this.setLayoutParams(p1);
}
(Optional) for the custom card drawable background, I used the below code:
<?xml version="1.0" encoding="UTF-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<stroke
android:width="2dp"
android:color="#FFFFFFFF" />
<solid
android:color="#FFFFFFFF"
/>
<corners
android:bottomRightRadius="7dp"
android:bottomLeftRadius="7dp"
android:topLeftRadius="7dp"
android:topRightRadius="7dp" />
</shape>

How can I prevent an ImageView from resizing its parent? [duplicate]

This question already has answers here:
Scale Image to fill ImageView width and keep aspect ratio
(16 answers)
Closed 5 years ago.
Note: I have checked this question, but none of the answers helped. I'm restating the question to specify my specific problems and needs.
I've made a custom view in my android app that displays data for an upcoming event (title, location, price, description, etc). In addition to this data, there are also icon and cover photos displayed on this view (the icon is displayed in an ImageView in the upper-left corner of the view and the cover is modified to have an alpha of 128, then displayed behind the content of the view. Here's my layout so far:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/eventview_main" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_marginLeft="#dimen/activity_horizontal_margin"
android:layout_marginRight="#dimen/activity_horizontal_margin"
android:layout_marginTop="#dimen/activity_vertical_margin"
android:background="#color/color_black" android:elevation="16dp">
<!-- OBJECT IN QUESTION -->
<ImageView
android:id="#+id/eventview_cover"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:adjustViewBounds="false"
android:cropToPadding="false"
android:scaleType="centerCrop" />
<ImageView android:id="#+id/eventview_icon"
android:layout_width="100dp" android:layout_height="100dp"
android:layout_marginLeft="#dimen/half_margin"
android:layout_marginRight="#dimen/half_margin"
android:layout_marginTop="#dimen/half_margin" android:elevation="8dp" />
<TextView android:id="#+id/eventview_title"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_alignLeft="#+id/eventview_price"
android:layout_alignParentEnd="true" android:layout_alignParentRight="true"
android:layout_alignParentTop="true" android:layout_marginTop="#dimen/half_margin"
android:layout_toEndOf="#+id/eventview_icon" android:layout_toRightOf="#+id/eventview_icon"
android:text="Large Text" android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#color/color_white" />
<TextView android:id="#+id/eventview_location"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_alignParentEnd="true" android:layout_alignParentRight="true"
android:layout_below="#+id/eventview_price" android:layout_toEndOf="#+id/eventview_icon"
android:layout_toRightOf="#+id/eventview_icon" android:text="Small Text"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#color/color_white" />
<TextView android:id="#+id/eventview_price"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_alignParentEnd="true" android:layout_alignParentRight="true"
android:layout_below="#+id/eventview_title" android:layout_toEndOf="#+id/eventview_icon"
android:layout_toRightOf="#+id/eventview_icon" android:text="Small Text"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#color/color_white" />
<TextView android:id="#+id/eventview_shortdesc"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_alignLeft="#+id/eventview_title"
android:layout_alignParentEnd="true" android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentStart="true" android:layout_alignStart="#+id/eventview_title"
android:layout_below="#+id/eventview_icon"
android:layout_marginBottom="#dimen/half_margin"
android:layout_marginLeft="#dimen/half_margin" android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#color/color_white" android:width="0dip" />
</RelativeLayout>
When an Activity instantiates my view, it must supply a source for the data (a custom class). It is then (under setSource()) that a method updateLayout() is called, which sets the icon image, and the various TextViews. Additionally, it sets the eventview_cover's source to a BitmapDrawable (to modify the original photo's alpha). That, in turn, extends the height of the view (which I want to avoid). The height of the view should only be governed by the heights of the TextViews and the ImageView for the icon. Here's the code for EventView:
public class EventView extends RelativeLayout {
private MergeEvent src = null;
public MergeEvent getSource() {
return src;
}
protected void setSource(MergeEvent e) {
src = e;
//The data was updated, so we need to update the view!
updateLayout();
}
public EventView(MergeEvent src, Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
setSource(src);
}
public EventView(MergeEvent src, Context context, AttributeSet attrs) {
super(context, attrs);
setSource(src);
}
public EventView(MergeEvent src, Context context) {
super(context);
setSource(src);
}
protected void updateLayout() {
if (getSource() != null) {
//Get source data.
MergeEvent src = getSource();
//Set the layout.
View.inflate(getContext(), R.layout.eventview, this);
//Get references to the TextViews and ImageViews
TextView title = (TextView)findViewById(R.id.eventview_title),
price = (TextView)findViewById(R.id.eventview_price),
location = (TextView)findViewById(R.id.eventview_location),
shortdesc = (TextView)findViewById(R.id.eventview_shortdesc);
ImageView icon = (ImageView)findViewById(R.id.eventview_icon),
cover = (ImageView)findViewById(R.id.eventview_cover);
icon.setImageBitmap(src.getIcon());
title.setText(src.getTitle());
price.setText("$" + src.getPrice());
location.setText(src.getLocation());
shortdesc.setText(src.getShortDescription());
//Create a new BitmapDrawable from the source's cover (a Bitmap)
BitmapDrawable bd = new BitmapDrawable(getResources(), src.getCover());
bd.setAlpha(128);
bkg.setImageDrawable(bd);
requestLayout();
}
}
}
This all works, but it resizes the view to make room for the cover photo, which again, I want to avoid. I tried using getHeight() before I set the drawable, then set the drawable, and set the cover's ImageView's height with bkg.getLayoutParams().height = height, but the call to getHeight() returned 0 because the view is technically not visible at this point, so the cover photo was invisible. The view should look like this, where the background image is cropped and centered:
tl;dr/summary:
How can I prevent an ImageView from resizing its parent?
I found this answer on another question very similar to mine:
This needs to be done using code. You need to call those size APIs a few milliseconds after the screen renders. So, if you call it 100 milliseconds after, using postDelayed method of any view that has been rendered, you will get the sizes.
Using that, I modified my updateLayout() method to this:
protected void updateLayout() {
if (getSource() != null) {
MergeEvent src = getSource();
View.inflate(getContext(), R.layout.eventview, this);
TextView title = (TextView)findViewById(R.id.eventview_title);
TextView price = (TextView)findViewById(R.id.eventview_price);
TextView location = (TextView)findViewById(R.id.eventview_location);
TextView shortdesc = (TextView)findViewById(R.id.eventview_shortdesc);
ImageView i = (ImageView)findViewById(R.id.eventview_icon);
i.setImageBitmap(src.getIcon());
title.setText(src.getTitle());
price.setText("$" + src.getPrice());
location.setText(src.getLocation());
shortdesc.setText(src.getShortDescription());
//use postDelay to set the ImageView's source and max size after 100 miliseconds (plenty of time for the UI to be drawn)
this.postDelayed(new Runnable() {
#Override
public void run() {
BitmapDrawable bd = new BitmapDrawable(getResources(), src.getCover());
bd.setAlpha(128);
ImageView bkg = (ImageView)findViewById(R.id.eventview_cover);
bkg.setMaxHeight(getHeight());
bkg.setImageDrawable(bd);
}
}, 100);
requestLayout();
}
}

Custom Button with two TextView

I'm trying to Customize button with two TextView with different typeface within a single button. For that I just extended Button and with have written the following code inside the constructor,
LayoutInflater layoutInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = layoutInflater.inflate(R.layout.custom_button,
(ViewGroup) findViewById(R.id.custom_button_view));
TextView firstTextView = (TextView) layout
.findViewById(R.id.firstTextView);
TextView secondTextView = (TextView) layout
.findViewById(R.id.secondTextView);
in the layout custom_button I have placed two TextView with different typeface and text and custom_button_view is the ID of that LinearLayout, what I got is an empty button with no text.
Any Ideas, Thanks.
You can use Layout as a button by setting ur custom button style to layout and can add two textViews to it, in this way:
<LinearLayout android:id="#+id/customButtonLayout"
android:layout_height="wrap_content" style="#android:style/Widget.Button"
android:layout_width="wrap_content">
<TextView android:text="First" android:id="#+id/firstTextView"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:textColor="#000"></TextView>
<TextView android:textColor="#000" android:text="Second"
android:layout_height="wrap_content" android:id="#+id/secondTextView"
android:layout_width="wrap_content" android:layout_marginLeft="10dp"></TextView>
</LinearLayout>
and in Activity you can have this to set different typeface:
Typeface font=Typeface.createFromAsset(getAssets(),"ARIALN.TTF") ;
Typeface font2=Typeface.createFromAsset(getAssets(), "COMPCTAN.TTF");
TextView firstTextView = (TextView)findViewById(R.id.firstTextView);
TextView secondTextView = (TextView)findViewById(R.id.secondTextView);
firstTextView.setTypeface(font);
secondTextView.setTypeface(font2);
LinearLayout btnLayout=(LinearLayout) findViewById(R.id.customButtonLayout);
btnLayout.setOnClickListener(this);
You can derive a new class from Button and override the onDraw(Canvas canvas) method. I did it for a button with an icon and a text, and it works without any xml. The main problem will be to write the text at the good place on the button. For this you can use the Paint.getTextBounds() function to get the text dimensions.
Using a LayoutInflater is probably a better practice, but I didn't manage to make it work.
public class CustomButton extends Button {
private int mWidth;
private int mHeight;
private Bitmap mBitmap;
private String mText;
private Paint mPaintIcon;
private Rect mRectIconSrc;
private Rect mRectIconDst;
private Paint mPaintText;
public CustomButton(Context context, Bitmap bitmap, int width, int height, String text) {
super(context);
mBitmap = bitmap;
mWidth = width;
mHeight = height;
mText = text;
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(width, height);
setLayoutParams(params);
mPaintIcon = new Paint();
mRectIconSrc = new Rect(0, 0, mBitmap.getWidth(), mBitmap.getHeight());
mRectIconDst = new Rect(0, 0, mHeight, mHeight);
mPaintText = new Paint();
mPaintText.setColor(0xFF778800);
mPaintText.setTextSize(30);
}
#Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawBitmap(mBitmap, mRectIconSrc, mRectIconDst, mPaintIcon);
canvas.drawText(mText, mWidth/4, mHeight*2/3, mPaintText);
}
}
You can surround the button with a FrameLayout and then add a textview within the FrameLayout. You can manage the typeface in the activity. If the text doesn't show try using bringToFront()
layout:
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
android:id="#+id/button_frame"
>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/button_border"
android:gravity="center"
android:onClick="onClick"
android:text="#string/get_more"
android:id="#+id/get_more"
android:stateListAnimator="#null"
/>
<TextView
android:id="#+id/linearTimer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_gravity="right"
android:padding="10dp"
android:text="123"
>
</FrameLayout>
Activity:
countDownView = (TextView) findViewById(R.id.linearTimer);
Typeface tf = Typeface.createFromAsset(getAssets(), "fonts/digital-7.ttf");
countDownView.setTypeface(tf);
countDownView.bringToFront();

How to put a horizontal divisor line between edit text's in a activity

I'm making an activity to configure my app, and I have to divide the sections of my configuration window with a line. I used this: divider_horizontal_bright, from this example:
http://android.cryx.li/doku.php?id=know:settings:start
However it doesn't work! When I test on my android phone, it doesn't show a horizontal line. Why?
I am using Android 2.1
Try this link....
horizontal rule
That should do the trick.
The code below is xml.
<View
android:layout_width="fill_parent"
android:layout_height="2dip"
android:background="#FF00FF00" />
If this didn't work:
<ImageView
android:layout_gravity="center_horizontal"
android:paddingTop="10px"
android:paddingBottom="5px"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:src="#android:drawable/divider_horizontal_bright" />
Try this raw View:
<View
android:layout_width="fill_parent"
android:layout_height="1dip"
android:background="#000000" />
For only one line, you need
...
<View android:id="#+id/primerdivisor"
android:layout_height="2dp"
android:layout_width="fill_parent"
android:background="#ffffff" />
...
How about defining your own view? I have used the class below, using a LinearLayout around a view whose background color is set. This allows me to pre-define layout parameters for it. If you don't need that just extend View and set the background color instead.
public class HorizontalRulerView extends LinearLayout {
static final int COLOR = Color.DKGRAY;
static final int HEIGHT = 2;
static final int VERTICAL_MARGIN = 10;
static final int HORIZONTAL_MARGIN = 5;
static final int TOP_MARGIN = VERTICAL_MARGIN;
static final int BOTTOM_MARGIN = VERTICAL_MARGIN;
static final int LEFT_MARGIN = HORIZONTAL_MARGIN;
static final int RIGHT_MARGIN = HORIZONTAL_MARGIN;
public HorizontalRulerView(Context context) {
this(context, null);
}
public HorizontalRulerView(Context context, AttributeSet attrs) {
this(context, attrs, android.R.attr.textViewStyle);
}
public HorizontalRulerView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
setOrientation(VERTICAL);
View v = new View(context);
v.setBackgroundColor(COLOR);
LayoutParams lp = new LayoutParams(
LayoutParams.MATCH_PARENT,
HEIGHT
);
lp.topMargin = TOP_MARGIN;
lp.bottomMargin = BOTTOM_MARGIN;
lp.leftMargin = LEFT_MARGIN;
lp.rightMargin = RIGHT_MARGIN;
addView(v, lp);
}
}
Use it programmatically or in Eclipse (Custom & Library Views -- just pull it into your layout).
Use This..... You will love it
<TextView
android:layout_width="fill_parent"
android:layout_height="1px"
android:text=" "
android:background="#anycolor"
android:id="#+id/textView"/>

Categories

Resources