Getting information from classes to run in an app - java

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);
}
});

Related

Android - Make whole search view clickable with query hint

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.

Android ListView and Child selection

I am trying to implement a list view for chat messages (like WhatsApp)
I need to implement selection mechanism on the list view such that
i ) Long press on any item should start the list view action mode (Should show 1 item selected, 5 item selectd etc...)
ii) If the action mode on the list view is on , subsequent clicks on the child views should be discarded the list item should be selected...(Clicking on the image should not open or download the image)
iii) If not in Action mode, clicking on the attachments should open the attachments (Click on the attachment should open or download attachment if no items are selected.. )
Please help...
You can create a ClickInterceptor class which can handle both the view and the list item click listener
public class ClickInterceptor implements
View.OnClickListener,
ListView.OnItemClickListener,
ListView.OnItemLongClickListener
which results in implementing all the different click methods you'll need.
This way, the view and your listview clicklistener/itemclicklistener can accept objects of this class.
Now in the ClickInterceptor class, you can write the code that can keep track and decide what to happen, according to the ActionMode state.
public class ChatAttachment extends LinearLayout
{
private Context mContext ;
public ChatAttachment(Context context, AttributeSet attrs) {
super(context, attrs);
mContext = context;
init();
}
public ChatAttachment(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
mContext = context;
init();
}
private void init(){
}
#Override
public boolean onInterceptTouchEvent(MotionEvent ev){
if(ChatListAdapter.mActionModeEnabled){
return true;
}
return false;
}
}

Showing CustomViews properly in Graphical Layout of android xml

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.

How to rotate a RelativeLayout by 180 degrees?

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!

Android Scroll Text in TextSwitcher

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.

Categories