I'm trying to get all edit text views to be the same size without using a hard definition like px or dp, but rather to use a wrap_content and get them all to be the same size. This is what I have, but it doesn't work:
enterAge = (EditText) findViewById(R.id.enterAge);
enterAge.addTextChangedListener(new TextWatcher1());
enterWeight = (EditText) findViewById(R.id.enterWeight);
enterWeight.addTextChangedListener(new TextWatcher2());
enterWeight.setWidth(enterAge.getWidth());
enterHeight = (EditText) findViewById(R.id.enterHeight);
enterHeight.addTextChangedListener(new TextWatcher3());
enterHeight.setWidth(enterAge.getWidth());
enterMealFrequency = (EditText) findViewById(R.id.enterMealFrequency);
enterMealFrequency.addTextChangedListener(new TextWatcher4());
enterMealFrequency.setWidth(enterAge.getWidth());
Any suggestions on why this isn't working the way I want it to?
wrap_content is fundamentally the opposite of what you are saying -- it specifies to make the view only as large as it needs to be, regardless of the parent size.
You need to write your UI as a view hierarchy with layouts describing how to position the views. It is very simple to create a vertical row of text views that are all given them same width by putting them in a LinearLayout.
It depends entirely on when you are executing this code. The values for the width and height of a View will not return valid data in Java code until layout is complete (this happens sometime later than when the layout is inflated from XML). If you are doing this in onCreate() or even onResume(), this is too early and getWidth() will likely return zero.
You need to work with the layout system to determine when you will have valid view sizes to work with.
Hope that Helps!
I have solved similar problem introducing my own class which extends EditText, e.g.
public class SimpleEditText extends EditText
{
private static float textSize=14.0;
public SimpleEditText(Context context)
{
super(context);
this.setTextSize(textSize);
}
public SimpleEditText(Context context, AttributeSet attrs)
{
super(context, attrs);
this.setTextSize(textSize);
}
public SimpleEditText(Context context, AttributeSet attrs, int defStyle)
{
super(context, attrs, defStyle);
this.setTextSize(textSize);
}
}
In resource you can directly use reference to SimpleEditText instead of EditText (for sure one have to keep in mind that all size related XML attributes will be ignored...)
Related
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.
I am trying to make an application, which is meant for two people and both see one half of it, so I need to flip one half vertically. I am using a LinearLayout with two RelativeLayouts inside it with layout_weight="1".
Thing is, I am not sure how to do this flip. Apparently android:rotate is only available in version 11+ (3.0+), but I would like it to support at least 2.2.
After reading other related questions on SO, I tried various things, none of which seem to work. I tried to extend the RelativeLayout and override the onDraw function, but it doesn't seem to do anything. Here's my code:
public class FlippedRelativeLayout extends RelativeLayout
{
public FlippedRelativeLayout(Context context)
{
super(context);
}
public FlippedRelativeLayout(Context context, AttributeSet attrs)
{
super(context, attrs);
}
public FlippedRelativeLayout(Context context, AttributeSet attrs,
int defStyle)
{
super(context, attrs, defStyle);
}
#Override
protected void onDraw(Canvas canvas)
{
canvas.save();
canvas.rotate(180);
super.onDraw(canvas);
canvas.restore();
}
}
I will be glad for any help, thanks!
Try this:
public class MyRelativeLayout extends RelativeLayout {
public MyRelativeLayout(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
public MyRelativeLayout(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public MyRelativeLayout(Context context) {
super(context);
init();
}
private void init() {
setStaticTransformationsEnabled(true);
}
#Override
protected boolean getChildStaticTransformation(View child, Transformation t) {
t.setTransformationType(Transformation.TYPE_MATRIX);
Matrix m = t.getMatrix();
m.reset();
m.postRotate(180, child.getWidth() / 2.0f, child.getHeight() / 2.0f);
return true;
}
}
The result is:
Very interesting question!
You could perhaps try to create two partly transparant Activity-s, showing their own copy of the same layout xml and then switching the "z-order" of the active Activity depending on whos turn it is to make a move.
Activity A would be "your own" activity and it would have a transparent top half and the RelativeLayout as it's bottom half. It would also have a normal screen orientation, like: setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT).
Activity B would be "your opponents activity". It would also have a transparent top half and a copy of the very same RelativeLayout as it's bottom part. It would however have an inverted screen orientation, like: setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT). This would mean that the transparent part of Activity B would overlap the RelativeLayout part of Activity A, and the transparent part of Activity A would overlap the RelativeLayout part of Activity B.
You could also put the corresponding launch mode of the Activity-s to "single top" or some other suitable value, so you don't create a new instance of your Activity when "starting it again", i.e. passing on the "make-a-move-ticket" to the opponent.
Unfortunately the ...REVERSE_PORTRAIT orientation wasn't added until API level 9 (Android 2.3.something) and you explicitely request API level 8.
The neat part about this approach would be that since only one Activity can have focus (and, hence, take input) at a time, you would automatically get a statemachine for the user input: the opponent wouldn't have the possibility to interact with his/her board until you've made your move and vice versa.
Hope this gives you some ideas at least.
Cheers!
I have a texSwitcher to which I add two text views (created dynamically using TextView class). I am switching between the child text views using gesture detector. But when the text is large to fit in the current viewable area, the scrolling doesn't work for textswitcher.
When I tried using setTextMovement method of child text views, then the TextSwitcher stopped listening to horizontal swipe gestures.
Has anybody been successful in showing scrollable text views inside a TextSwitcher.
I solved this problem with creating my own TextSwitcher.
public class MyOwnSwitcher extends ViewSwitcher {
public MyOwnSwitcher (Context context) {
super(context);
}
public MyOwnSwitcher (Context context, AttributeSet attrs) {
super(context, attrs);
}
}
I moved my "onTouchEvent"-Method into that new Class. Then I had to Override the "onInterceptTouchEvent"-Method like that:
#Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
onTouchEvent(ev);
return super.onInterceptTouchEvent(ev);
}
I also had to move some of my Fields and Variables from my Activity to that new class.
But you can also use methods of your activity too with:
Activity ac = (Activity) this.getContext();
That should return your Activity.