I am trying to use this library for card view
https://github.com/DenisMondon/material-design-library
<com.blunderer.materialdesignlibrary.views.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:mdl_title="CardView"
app:mdl_description="A Left Image CardView"
app:mdl_normalButton="Normal"
app:mdl_highlightButton="Highlight"
app:mdl_imagePosition="left"
app:mdl_image="#drawable/image" />
I can not figure out how to add a onClickListener for the labels normalButton and highlightButto.
Can someone please give me a hand. thank you
Also how to set an Image programmatically into this cardView?
It has following two listeners
private OnClickListener mOnNormalButtonClickListener;
private OnClickListener mOnHighlightButtonClickListener;
You need to implement CardView.mOnNormalButtonClickListener, CardView.mOnHighlightButtonClickListener in your activity and override their methods to implement the click listeners for these buttons and set them using these functions.
setOnNormalButtonClickListener(OnClickListener onNormalButtonClickListener)
setOnHighlightButtonClickListener(OnClickListener onHighlightButtonClickListener)
#Joolah
With the latest version of the library, you can have a cardview url image.
Just write this:
myCardView.setImageUrl("http://your_image_url");
Related
I am trying to add an image into my app with java so I googled how to do this and seen I had to add
Context mContext;
Drawable myImage = mContext.getDrawable(R.drawable.my_image);
(That was all of the code they said I had to add) But when I run it I don't see my image I set to it. So how I would be able to show the image I set to it?
For a image to be displayed, you need to give a view for that . ie - ImageView
Add a ImageView in your main-layout.
Initialize that ImageView in the on create of your activity like
ImageView appImage = findViewById(R.id.your_image_View_id);
3.Then set an image to the image view like
appImage.setImageResource(R.drawable.your_image);
Hope this helps.
You just initialize the imageview from layout like below,
ImageView appImage = findViewById(R.id.your_image_View_id);
appImage.setImageDrawable(ContextCompat.getDrawable(getActivity(),R.drawable.ic_downarrow));
With new android API 22 getResources().getDrawable() is now deprecated. So now the best approach is to use only getDrawable() is using ContextCompat
Please follow the steps:
Step1: In your main_layout.xml, add an <ImageView ... tag give an id, let's say id is "imageView"
Step2: In your MainActivity.class onCreate() method, Initialize that ImageView as:
ImageView image = findViewById(R.id.imageView)
then,
Step3: assign your drawable as:
im.setImageDrawable(ContextCompat.getDrawable(MainActivity.this, R.drawable.your_image_drawable));
If you are using a fragment, use:
im.setImageDrawable(ContextCompat.getDrawable(getActivity(), R.drawable.your_image_drawable));
Hope it helps. Please update if it does.
ImageView appImage = findViewById(R.id.your_image_View_id);
In activity simply use setImageResource
appImage.setImageResource(R.drawable.ic_avatar);
I'm struggling to get my android app to show and hide imageView objects programmatically. Actually I'm struggling to get expected behavior from imageView objects full stop.
Following the answers to this question,
Here's what I'm testing with:
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback{
#Override
protected void onCreate(Bundle savedInstanceState) {
ImageView warn = (ImageView)findViewById(R.id.gpsStatusWarning);
warn.setImageResource(R.drawable.gps_error);
warn.getLayoutParams().height = 64;
warn.getLayoutParams().width = 64;
}
}
The above code is called in the parent activity's OnCreate method, and it does exactly what I expect: It changes the image of the object to be the one designated, and it sets the height and width of said object. However, what I can't seem to do is to set the object as INVISIBLE or GONE. I just can't make it vanish at all, in fact. I've tried both:
warn.setVisibility(View.INVISIBLE);
warn.setVisibility(View.GONE);
But the image is still visible. I've even tried changing it in the XML to
android:visibility="gone"
But even that hasn't helped. The image is still visible.
What am I doing wrong? Am I missing a call to some update method? Does setting the image resource force the image to be drawn?
Try :
warn.setImageResource(0);
Or : warn.setImageResource(android.R.color.transparent);
This is how you set the visibility of ImageView objects on Android programatically:
yourImage.setVisibility(View.VISIBLE);
yourImage.setVisibility(View.INVISIBLE);
yourImage.setVisibility(View.GONE);
You can also set the initial states of ImageView objects in XML layout files like this:
visibility="visible"
visibility="gone"
visibility="invisible"
You can follow the official documentation about ImageView controls to try it on yourself on this link below. Learn how to set the visibility state of a view.
imageView.setVisibility(View.GONE);
imageView.setVisibility(View.VISIBLE);
let's have a use case that I create some buttons and textviews in layout and I'm too lazy to write something like
private Button sendMessage;
private TextView nameOfPerson;
.
.
.
Button sendMessage = (Button) findViewById(R.id.ButtonSendMessage);
is there some kind of shortcut or macro that will generate it for me in Android Studio ?
Thank you
You can use one of the following plugins:
ButterKnife
AndroidAnnotations
I'm not sure if this is bad practice or not, or what errors I could encounter in the future if I do this.
Basically I'm trying to switch two fragments with a FragmentManager, and I need to do this when I press a button that is inside my first fragment. I declare the fragment in the main.xml file like this:
<fragment
class="com.example.MyApp.ButtonFragmentOne"
android:id="#+id/button_fragment_one"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:layout="#layout/button_fragment_one"
/>
I can access my button from my first fragment just fine in my Main.java file, but I'm not sure if I should do this or not.. Someone told me that it's a bad idea, but they couldn't really explain why. Should I be doing the onClick listener from my Main.java file, or inside the ButtonFragmentOne.java file? Does it matter at all?
Any documentation or help would be appreciated! Thanks!
Your problem is easy to solve. There is a documentation from Google.
To communicate between fragments you have to call your activity via an interface and the activity has to manage the switch between the fragments.
You can call below code on click of button of first fragment,
SecondFragment secFrag = new SecondFragment();
FragmentTransaction fragTransaction = getFragmentManager().beginTransaction();
fragTransaction.replace(R.id.fragment_container,secFrag );
fragTransaction.addToBackStack(null);
fragTransaction.commit();
Official Documents :
http://developer.android.com/training/basics/fragments/fragment-ui.html
http://developer.android.com/training/basics/fragments/communicating.html
I'm making a game using AndEngine which uses the soft keyboard of the device.
I'm NOT using EditText, but rather my own. I'm trying to detect presses on the soft keyboard,
I've already succeeded in showing and hiding the keyboard over the AndEngine scene.
My activity implements the OnKeyboardActionListener, which I read is used as a soft-keyboard listener, but I'm not sure how to register the keyboard with this class (the activity).
Obviously, at the moment, the code inside onPress() is useless..
I couldn't find any examples, most of them refers to EditText, which I'm not using..
Is it possible?
Maybe a service or something?
Thanks in advance.
EDIT:
I'm trying to create my own keyboard using KeyboardView and a custom Keyboard,
This way I could do
KeyboardView kbView = new KeyboardView(this,null);
kbView.setKeyboard(new Keyboard(this, R.xml.keyboard);
kbView.setOnKeyboardActionListener(new OnKeyboardActionListener() {
....
}
Has anyone done this? works, doesn't?
Thanks.
I ended up creating my own Keyboard and KeyboardView`, then I used this code
CustomKeyboardView kbView = (CustomKeyboardView) findViewById(R.id.keyboard_view);
kbView.setKeyboard(new Keyboard(this, R.xml.myCustomKeyboard);
kbView.setOnKeyboardActionListener(new OnKeyboardActionListener() {
#Override
public void onPress(int primaryCode) {
// A Key was pressed
}
....
}
And created a KeyboardView in the xml layout
<pathToCustomKeyboardView.CustomKeyboardView
android:id="#+id/keyboard_view"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:visibility="gone"
android:layout_alignParentBottom="true" />
CustomKeyboardView only extends KeyboardView, dunno why, but it works with the custom and not with the original
EDIT:
Also, The activity extends SimpleLayoutGameActivity, And then I selected the layout which contains the CustomKeyboardView