Android Button with Drawable - Padding - java

I want to add a Drawable Image to my Button on the left and tried the following:
button = new Button(this);
button.setWidth(screen_dimens(0.25, "w")); //25% of screen width
button.setPadding(5, 5, 0, 5);
Drawable img = getResources().getDrawable(R.drawable.image);
button.setCompoundDrawablesWithIntrinsicBounds(img, null, null, null);
But I want to have a padding on the left between the drawable and the border of the button. Unfortunately, there is none.
Any help?

This will generate text + image button ( Image on Left & Text After That )
<Button
android:gravity="center_vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/button2"
android:paddingLeft="20dp"
style="#style/whiteText"
android:onClick="openWhyAshoka"
android:drawableLeft="#drawable/arrow"
android:drawablePadding="50dp"
android:text="Ankit" />

You have to add the padding to you drawable, by changing the 9 slices content area or by changing your drawable padding:
<padding
android:bottom="10dp"
android:left="10dp"
android:right="10dp"
android:top="10dp" />
If you are using selector you have to add the padding rule to each one of the drawable states.

Use this API call:
public void setCompoundDrawablePadding (int pad)

Use this Code:
Drawable image = (Drawable)getResources().getDrawable(R.drawable.onstar_mute_unmute_button_selector);
ImageButton button=(ImageButton)findViewById(R.id.imageButton);
button.setPadding(0,10,5,0);
button.setGravity(Gravity.RIGHT);
button.setBackground(image);

<Button
android:id="#+id/otherapps"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/btn_background"
android:text="#string/other_apps"
android:layout_weight="1"
android:fontFamily="cursive"
android:layout_marginBottom="10dp"
android:drawableLeft="#drawable/ic_more"
android:paddingLeft="8dp" //for drawable padding to the left
android:textColor="#android:color/holo_red_light" />

Related

How to set text to the imageview like gmail inbox?

I want to design
like this. how to set first charecter to that circle and should circle be in random color.
You can't directly set a text to image view but you can use the Relative layout and Text-View to do so.
let's see how.
This is the result:-
This is the code
here is the code for your xml layout file
<androidx.cardview.widget.CardView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:cardCornerRadius="100dp"
app:cardBackgroundColor="#color/transparent">
<RelativeLayout
android:layout_width="150dp"
android:layout_height="150dp">
<ImageView
android:id="#+id/img"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="#null"
android:background="#color/grey"
/>
<TextView
android:id="#+id/person_name_first_letter_txt"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="K or whatever your person name is"
android:maxLength="1"
android:textColor="#color/white"
android:textSize="80dp"
android:gravity="center"
/>
</RelativeLayout>
</androidx.cardview.widget.CardView>
here is the array of colors for picking a random color in colors.xml
<array name="random_color_array">
<item>#37BFA8</item>
<item>#F50057</item>
<item>#3D5AFE</item>
<item>#FF3D00</item>
<!-- add more colors according to your need-->
</array>
Now here is the code for the activity in java
TextView name_text = findViewById(R.id.person_name_first_letter_txt);
ImageView background_image = findViewById(R.id.img);
String user_name = "John Doe";
String[] colorsTxt = getApplicationContext().getResources().getStringArray(R.array.allcolors);
List<Integer> colors = new ArrayList<Integer>();
for (int i = 0; i < colorsTxt.length; i++) {
int newColor = Color.parseColor(colorsTxt[i]);
colors.add(newColor);
}
Random random = new Random();
int color = colors[random.nextInt(colors.length)];
background_image.setBackgroundColor(getResources().getColor(color));
name_text.setText(user_name);
TRY this
Change color as per your requirement.
you will add colors programmatically.
and text add pragmatically using this link
Textview -
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="#drawable/ic_baseline_circle_24"
android:gravity="center"
android:maxLength="1"
android:padding="10dp"
android:text="M"
android:textColor="#android:color/white"
android:textSize="12sp"
android:textStyle="normal" />
add in res/drawable/ic_baseline_circle_24.xml
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:tint="#41B582"
android:viewportWidth="24"
android:viewportHeight="24">
<path
android:fillColor="#android:color/white"
android:pathData="M12,2C6.47,2 2,6.47 2,12s4.47,10 10,10 10,-4.47 10,-10S17.53,2 12,2z" />
</vector>

How to align buttons based on phone screen in a GridLayout?

So I have a ScrollView inside of a GridLayout and I'm trying to change it so that the buttons will resize based off the size of the phone, but so far I can't get a button to appear. I have a set height and a width of 0 as a style for each ImageView
<android.support.v7.widget.GridLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
grid:alignmentMode="alignBounds"
grid:columnCount="2"
grid:rowOrderPreserved="false"
grid:useDefaultMargins="true">
<com.pkmmte.view.CircularImageView
app:srcCompat="#android:drawable/alert_dark_frame"
android:id="#+id/bucksImageView"
grid:layout_columnWeight="1"
grid:layout_gravity="fill_horizontal"
android:gravity="center"
app:border="true"
app:border_color="#EEEEEE"
app:border_width="4dp"
app:shadow="true"
style="#style/buttonStyle"
/>
<com.pkmmte.view.CircularImageView
app:srcCompat="#android:drawable/alert_dark_frame"
android:id="#+id/heatImageView"
grid:layout_columnWeight="1"
grid:layout_gravity="fill_horizontal"
android:gravity="center"
app:border="true"
app:border_color="#EEEEEE"
app:border_width="4dp"
app:shadow="true"
style="#style/buttonStyle" />
<com.pkmmte.view.CircularImageView
app:srcCompat="#android:drawable/alert_dark_frame"
android:id="#+id/hawksImageView"
grid:layout_columnWeight="1"
grid:gravity="center"
grid:layout_gravity="fill_horizontal"
app:border="true"
app:border_color="#EEEEEE"
app:border_width="4dp"
app:shadow="true"
style="#style/buttonStyle" />

Change BackgroundTint of ImageView on Android

I have an ImageButton which have a drawable background resource which is oval shape.
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<solid
android:angle="270"
android:color="#FFFF0000" />
</shape>
Here is the ImageButton in XML:
<ImageButton
android:id="#+id/c1"
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_columnSpan="1"
android:layout_rowSpan="1"
android:background="#drawable/circle"
android:layout_margin="10dp"
/>
I need to change the color of the circle shape dynamically, this will be done by either change the backgroundTint property in the ImageButton or change the circle shape color.
NOTE:
I have array of strings that stores a list of RGB colors i need to use these RGB colors.
You can do it like this:
mImageView.setBackgroundTintList(getResources().getColorStateList(R.color.my_color));
Or you can do better, to support versions pre LOLLIPOP:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
{
ColorStateList stateList = ColorStateList.valueOf(getResources().getColor(R.color.my_color));
mImageView.setBackgroundTintList(stateList);
}
else
{
mImageView.getBackground().getCurrent().setColorFilter(
new PorterDuffColorFilter(getResources().getColor(R.color.my_color),
PorterDuff.Mode.MULTIPLY));
}
More about PorterDuffColorFilter here.
i just found the answer, it works as follow:
In my changeColors(int id) function
Create ImageButton variable.
Assign the passed id to the ImageButton variable.
Define GradientDrawable variable to store the ImageButton background.
Update the color of the background using GradientDrawable variable.
Update the ImageButton to the new background.
This is the code:
ImageButton circle;
circle = (ImageButton) findViewById(id);
GradientDrawable drawable = (GradientDrawable) getResources().getDrawable(R.drawable.circle);
drawable.setColor(Color.parseColor("color in format #FFFFFF");
circle.setBackground(drawable);
You may use ImageView instead and use app:tint to set the tint color for the background drawable.
<ImageView
android:layout_width="20dp"
android:layout_height="20dp"
android:src="#drawable/circle"
app:tint="#android:color/holo_red_dark"/>

text view on the image view?

the text should be saved on the image view ?the text should not be defined in the layout folder and should be saved on the image view on the accordingly from the key board ?
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fullimage);
// get intent data
Intent i = getIntent();
// Selected image id
int position = i.getExtras().getInt("id");
ImageAdapter imageAdapter = new ImageAdapter(this);
ImageView imageView = (ImageView) findViewById(R.id.fullimage);
imageView.setImageResource(imageAdapter.mThumbIds[position]);
}
}
use framelayout for that .. define both ImageView and TextView and set text on TextView
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/thumbHolder">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/thumbImage"
android:scaleType="fitXY" />
<TextView
android:layout_width="match_parent"
android:layout_height="24dp"
android:id="#+id/titletxt"
android:gravity="center_vertical"
android:maxLines="2"
android:paddingRight="8dp"
android:paddingLeft="8dp"
android:textSize="16sp"
android:layout_gravity="bottom"
android:background="#80000000"
android:textColor="#ffffffff"
android:text="" />
</FrameLayout>
for setting custom font :-
create "assets\fonsts\" folder and then place your font there
then create a typeface by loading that font :-
Typeface tfBold = Typeface.createFromAsset(getActivity().getAssets(), "fonts/Raleway-Bold.otf");
then set your typeface in textView :-
yourTextView.setTypeface(tfBold);
try this-
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:background="#drawable/image">
<LinearLayout
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/white"
android:textSize="18sp"
android:textStyle="bold"
android:singleLine="true"
android:text="text"/>
</LinearLayout>
</LinearLayout>
You can either draw the image on a canvas, and then draw the text on the image or else, create a layout via code and set the textview inside the layout and then place the layout on top of the image view with background of the textview set to transparent
You can do it using TextView only. Using drawableTop, drawableBottom, drawableLeft, drawableRight in the following way.
<TextView
style="#style/layout_wrap"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:paddingRight="5dp"
android:drawableTop="#drawable/flags_00"
android:textColor="#color/text_color_tabs"
android:textStyle="bold"
android:maxLength="3"
android:gravity="center"
android:text="Team B" />
and in the code by the following way
battingTeamFlag.setCompoundDrawablesWithIntrinsicBounds(left, top, right, bottom);
You can use Button and use button background to set image.and use text element for adding text on button.
<Button
android:layout_width="wrap_parent"
android:layout_height="wrap_parent"
android:text="YourText"
android:background="#drawable/image"
>
</Button>
According to your requirement you can get text from button and pass it with intent

How to write Text on ImageView in android coding?

Hi I've been trying to write Numbers on Imageview. Images for N number of questions. If the user entered answer is correct, then it should be displayed with question number with tick mark image else question number with wrong mark image. I need to write the question number on the image. My code is here:
LinearLayout l_layout = (LinearLayout) findViewById(R.id.linear_view_report);
LayoutParams param = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT, 1.0f);
ImageView[] imgview=new ImageView[questions.length];
for(int i=0;i<no_of_questions;i++)
{
if(userEnteredAnswers[i]==correct_answer[i]){
Bitmap bm=BitmapFactory.decodeResource(getResources(),R.drawable.correct);
Canvas canvas = new Canvas(bm);
Paint paint = new Paint();
paint.setColor(Color.BLACK);
paint.setTextSize(10);
canvas.drawText(i, 5, 5, paint);
imgview[i]=new ImageView(this);
imgview[i].setImageDrawable(new BitmapDrawable(bm));
l_layout.addView(imgview[i]);
}
else {
Bitmap bm=BitmapFactory.decodeResource(getResources(),R.drawable.wrong);
Canvas canvas = new Canvas(bm);
Paint paint = new Paint();
paint.setColor(Color.BLACK);
paint.setTextSize(10);
canvas.drawText(i, 5, 5, paint);
imgview[i]=new ImageView(this);
imgview[i].setImageDrawable(new BitmapDrawable(bm));
l_layout.addView(imgview[i]);
}
}
I get this warning:
The constructor BitmapDrawable(Bitmap) is deprecated
Image doesn't showing at run time.
What am I doing wrong?
I believe the easiest workaround without overriding anything would be to have a TextView and set its background with a drawable resource.
For instance:
TextView t = (TextView)findViewById(R.id.my_text_view);
// setting gravity to "center"
t.setGravity(Gravity.CENTER);
t.setBackgroundResource(R.drawable.my_drawable);
t.setText("FOO");
From the Android documentation :
BitmapDrawable(Bitmap bitmap)
This constructor was deprecated in API level 4. Use BitmapDrawable(Resources, Bitmap) to ensure that the drawable has correctly set its target density.
You can also create your own view (see Android: Creating Custom Views tutorial for an example) and put you image and the text in this view.
1). Make your own layout Say Image_TextView.xml like this :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="150dp"
android:layout_height="150dp"
android:gravity="center" >
<ImageView
android:id="#+id/imgview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:adjustViewBounds="true"
android:cropToPadding="true"
android:scaleType="center"
android:src="#drawable/box" />
<RelativeLayout
android:layout_width="150dp"
android:layout_height="35dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_marginBottom="0dp"
android:background="#80666666" >
<TextView
android:id="#+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="Hello"
android:textColor="#FFFFFF"
android:textStyle="bold"/>
</RelativeLayout>
</RelativeLayout>
</LinearLayout>
This will make Image with Text over it.
2). Then use layout inflator to inflate this view in your parent view.
Note : Using layout inflator you can add other view to your parent view.
its not possible to write text on imageview, but you can extend it to do so. Alternatively,
you can add an element overlapping the ImageView in the layout, and make it visible only when your condition becomes true.
or, you can use textView to display image, as a background or as a drawable to left/right..
Ps: however, the option 1 is easier to implement, but it will have additional rendering. so consider the other option first.

Categories

Resources