I am making my first android app in Android Studio. I have an ImageButton that I want to change on click. For that I made a new class called Field that extends ImageButton class. so, I use that instead of ImageButton. And in the class field I redefined a constructor so that I added that it sets OnClickListener when it creates Field object.
Here is Java code I used for that: `
public Field(Context context) {
super(context);
init();
}
public Field(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public Field(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
private void init(){
setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
setImageResource(R.drawable.open);
}
});
}`
Than, when I create Field object in my activity, OnClickListener is set.
And, it works. However, the new image called open is bigger that my previous image called pic01, but both images have the same size 185x185.
Here is my activity xml:
<asd.clicker.Field
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/asd.clicker.Field1"
android:background="#drawable/pic01"
android:layout_above="#+id/asd.clicker.Field4"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginBottom="25dp" />
I noticed that, even when I just change pic01 to open in my xml, the open image is bigger than pic01, both are .png. How can I get image of the same size when I click?
Change the two lines
android:layout_width="wrap_content"
android:layout_height="wrap_content"
to
android:layout_width="48dp"
android:layout_height="48dp"
or some other size you wan't your view to have. wrap_content will change the view's size based on the displayed drawable.
Related
I'm working on a Japanese Vocabulary App in Android Studio in Java.
I want to make it possible for the user to select a Font that best fits there preferences.
Currently, I have a Font added to res and defined a font family using the font file. All japanese text (Textbox, Buttons etc.) in the App uses this font family.
Is there a way to change the font family itself or change the font family tag-value pair in xml for every element using this font family while the app is running.
This is my current font family definition
<font-family xmlns:app="http://schemas.android.com/apk/res-auto">
<font app:fontStyle="normal" app:fontWeight="400" app:font="#font/kosugimaru_regular"/>
<font app:fontStyle="italic" app:fontWeight="400" app:font="#font/kosugimaru_regular" />
</font-family>
And this is how i use this font family in xml:
<TextView
android:id="#+id/TV_KanjiChar"
android:fontFamily="#font/kanji_default"
android:text="学"
android:textSize="60sp"
...
/>
if there is no way of changing that binding while running the app is there another elegant way of solving this problem? I Would like to avoid using update functions in every activity to change the font family dynamically onCreate.
I'm going ahead and answer my question.
A quite elegant solution is to create a subclass of Textview and Button and add a UpdateFont function to the constructors.
public class KanjiTextView extends AppCompatTextView {
public KanjiTextView(Context context) {
super(context);
updateFont();
}
public KanjiTextView(Context context, AttributeSet attrs) {
super(context, attrs);
updateFont();
}
public KanjiTextView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
updateFont();
}
public void updateFont()
{
int font_res = get_user_selected_font();
Typeface typeface = getFont(getContext(), font_res.get_Res());
setTypeface(typeface);
}
}
_____________________________________________________________________________
<com.[package].KanjiTextView
android:id="#+id/TV_Kanjitext"
android:text="学校"
android:textAlignment="center"
android:textSize="30sp"
...
/>
So i've been working on this inventory management system in Android Studio. In the fragment for Product Taking i have a search view and i want to make this search views whole body to be clickable. Part of this problem is solved here: Android - Make whole search bar clickable. But i want the search view to have a visible query hint. So basically i want it to be a button with search icon and a text. I want that because it is supposed to open a dialog where the user actually going to search for products. Not from this search view. When i add the setIconified(true) whole body is clickable but query hint is not visible. Like this:
When i add setIconified(false) query hint is visible but only search icon is clickable. Like this:
You can intercept all touches before SearchView consume them. I've created a simple class that intercept all touch events.
Kotlin:
class TouchInterceptorLayout #JvmOverloads constructor(
context: Context,
attrs: AttributeSet? = null,
defStyleAttr: Int = 0
) : FrameLayout(context, attrs, defStyleAttr) {
// You need override this method.
override fun onInterceptTouchEvent(ev: MotionEvent?): Boolean {
return true
}
}
Java:
public class TouchInterceptorLayout extends FrameLayout {
public TouchInterceptorLayout(Context context) {
super(context);
}
public TouchInterceptorLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
public TouchInterceptorLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
#Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
return true;
}
}
See how xml looks:
<com.example.testci.temp.TouchInterceptorLayout
android:id="#+id/interceptorLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<SearchView
android:id="#+id/searchView"
android:queryHint="#string/app_name"
android:iconifiedByDefault="false"
android:layout_height="wrap_content"
android:layout_width="wrap_content"/>
</com.example.testci.temp.TouchInterceptorLayout>
Now you need just set OnClickListener to interceptorLayout.
Full code with my experiment you can find here.
So far I have just been putting all my code in one main Activity class. I am now trying to improve this and have different classes.
I am making a very simple app that is like a story.There are 8 pages and 4 characters. Each character has an image, a button with their name and a textView. When you click their button their text is shown.
I have made a class for each character and in this I have put all the methods relating to that character in there. ie,
public void johnSpeak1 (View view) {
johnText1.setVisibility(View.VISIBLE)
markText1.setVisibility(View.INVISIBLE)
}
This should mean when the John button is pressed his text becomes visible and the other persons disappears.
It works fine when I had it all in the main activity class but now I have put it in it's own class my app just closes when I run it and press the button.
I have declared all the buttons and textviews in the main activity. In the oncreate on the main activity I have given all the findviewbyID for the first page and also set the textViews to invisible.
Everything I have learnt so far has been from an Udemy video which was great but didn't ever mention classes!!
I have set the other classes I have made to extends mainActivity
What I have done so far could all be garbage. I have been trying to do it on my own looking on google for help but I seem to be stumped on this.
Thanks for any advice!
BTW I am using android studio
Danny
First: Always share your error log for quick and clear answer please.
Second: I think your problem is that some view is null in your activity. You can find out in view>toolwindows>logcat, after app crash you will see a list of errors, some part of them has links that lead you to the error cause!
EDIT:
for example you create a class for each character:
public class John extends LinearLayout
{
Context context;
public John(Context context) {
super(context);
this.context = context;
init();
}
public John(Context context, #Nullable AttributeSet attrs) {
super(context, attrs);
this.context = context;
init();
}
public John(Context context, #Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
this.context = context;
init();
}
TextView dialog;
private void init()
{
View view = LayoutInflater.from(context).inflate(R.layout.john_layout, this);
dialog = view.findViewById(R.id.dialog);
}
public void setShowHideDialog(boolean mustShow)
{
if(mustShow)
dialog.setVisibility(VISIBLE);
else
dialog.setVisibility(GONE);
}
}
in john_layout xml put ImageView and TextView and set them with your image and dialog of your character
Then in your Activity xml add john_layout like this:
<John android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/ch_john"/>
And at last in your activity use John this way:
final John john = findViewById(R.id.ch_john);
john.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
mark.setShowHideDialog(false);
john.setShowHideDialog(true);
}
});
I need a Button or TextView that changes the color of its CompoundDrawable (Top) and Text when it is clicked to show whether or not is on.
It needs to be either a Button or TextView
There can only be 1 drawable resource per button
The Text color needs to change
DrawableTop color need to change, NOT BE REPLACED WITH A DIFFERENT DRAWABLE
The background of the view needs to remain transparent
The state of the button needs to be saved
I tried different things, but I think my best shot is to create a new Java file which extends Button or TextView. I was thinking something like this:
public class CustomButton extends android.support.v7.widget.AppCompatButton {
int colorOn;
int colorOff;
boolean isOn = false;
public CustomButton(Context context) {
super(context);
}
public CustomButton(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CustomButton(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
private void onButtonClicked(){
if(isOn){
setCompoundDrawablesColors(colorOn);
setTextColor(colorOn);
} else {
setCompoundDrawablesColors(colorOff);
setTextColor(colorOff);
}
isOn = !isOn;
}
}
My questions are:
Where do I insert onButtonClicked? onTouchEvent? onPressed? onFocused? Implement an OnClickListener?
How do I change the color of the Drawable so it updates UI immediately? I know changing the color is done with setCompoundDrawablesTint(color, mode). Does that update the UI asap? I haven't been able to get it to work.
How do I do this without any XML code?
In fact, you can customize the view to draw their own, you can dispatchTouchEvent (MotionEvent event) to listen to the key state call OnClickListener, this will be easier to achieve.
I have made a very simple Custom TextView. Everything works fine in android device. However on graphical layout of eclipse All I can see is the class name written instead of TextView original Text. How Can I test it in eclipse graphical layout?
Below is my code
public class MyTextView extends TextView
{
public MyTextView(Context context, AttributeSet attrs, int defStyle)
{
super(context, attrs, defStyle);
init();
}
public MyTextView(Context context, AttributeSet attrs)
{
super(context, attrs);
init();
}
public MyTextView(Context context)
{
super(context);
init();
}
private void init()
{
//if (!isInEditMode())
{
Typeface tf = Typeface.createFromAsset(getContext().getAssets(), "HelveticaLTStd-Bold.otf");
setTypeface(tf);
this.setTextColor(Color.parseColor("#FFD200"));
this.setShadowLayer(1, 1, 1, Color.BLACK);
}
}
i had the same problem with custom fonts on custom textViews.
the graphical editor is quite buggy and lacks many features that work fine on real devices.
this is one example of such a thing.
in order to fix it , just don't load fonts when isInEditMode() returns true, and ignore how the text looks like.
in fact, maybe the shadow feature also doesn't work well, so you might want to add it too.