Adding buttons,textviews from layout to java class - java

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

Related

Android studio: How to auto-generate views variables from xml to java class?

I have the following activity xml file:
I would like to generate all the views variables in the java file base on their name.
For this example:
public class RegisterActivity extends AppCompatActivity {
private EditText editText_firstname;
private TextView label_lastname;
// .. So on
private void setup_views() {
editText_firstname = (EditText) findViewById(R.id.editText_firstname);
label_lastname = (TextView) findViewById(R.id.label_lastname);
// .. So on
}
}
It very frustrating to do it all over again for any new activity, and I seems to basic to not be in android studio.
So, how do I do it?
Thanks in advance.
in java for did not developfindviewByid() you can use dataBinding library like Butter Knife
or use android native DataBinding
DataBinding as mentioned above or switch to Kotlin and you won't need findViewById
In java it may be frustating but if you switch to Kotlin it will be much easier to draw a lyaout using this great Anko Library
Android Studio has a plugin which allows you to do that, please refer the below link to get the plugin:
https://plugins.jetbrains.com/plugin/7595-android-code-generator
In the recent release of Android API, ViewBinding is the recommended way to manipulate view components.
However, if you really need the classic findViewById(), here are several plugins that you can use in Android Studio:
https://plugins.jetbrains.com/plugin/11204-android-findviewbyid-support
https://plugins.jetbrains.com/plugin/7595-android-code-generator
https://github.com/laobie/FindViewByMe
https://github.com/dotnet-ad/AutoFindViews

How can I show image set in android java?

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

how can I right-align activity titles?

I'm new to android development and I need proper instructions to right-align activity titles in a min-sdk:8 android application. I can do so by enabling rtl support for my app, but that only takes effect when the user has chosen an rtl language on his/her device. is there a way to force rtl even when the device language is ltr? or any other instructions to right-align titles in ltr mode? thx.
You can set a custom view in your Toolbar like this:
(copied from How to align center the title or label in activity?)
TextView customView = (TextView)
LayoutInflater.from(this).inflate(R.layout.actionbar_custom_title_view_centered,
null);
ActionBar.LayoutParams params = new ActionBar.LayoutParams(
ActionBar.LayoutParams.MATCH_PARENT,
ActionBar.LayoutParams.MATCH_PARENT, Gravity.RIGHT );
customView.setText("Some centered text");
getSupportActionBar().setCustomView(customView, params);
You can do that^. If you were do that multiple times in your app, you can create a customToolbar class that extends Toolbar and overwrite this method and add all that code there^ (and use that custom toolbar in your xml as well)
https://developer.android.com/reference/android/widget/Toolbar.html#generateLayoutParams(android.util.AttributeSet)

Android cardView click callback

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

Android development toggling TextView visibility

Im having some trouble with setting textview to invisible/visible.
basicly i want this to happen when an on/off button has been clicked.
what i did is kind of like
textview.setVisibility(TextView.VISIBLE);
textview.setVisibility(TextView.INVISIBLE);
when i try executing this the emultor says that the app has stopped unexcpetedly
Are you building this from XML or programmatically?
I would make it with an XML file then when the Activity runs change the property. Be sure to use setContentView(R.layout.main); before you try to get the TextView with findViewById(...).
Call .setVisibility(View.GONE); on the TextView to hide it.
Call .setVisibility(View.VISIBLE); to on the TextView to show it.
I have an example that does something like this. You can see the code here: https://github.com/ethankhall/Morse-Messenger/blob/master/src/com/kopysoft/MorseMessenger/Translate.java
Without more code or a stack trace, it's hard to say, but it sounds like you haven't initialized the text view. Here's how to do it:
TextView myTextView = (TextView) findViewById(R.id.tv_text);
Where 'tv_text' is the id of the textview as defined in the xml layout file.
Hope that helped!
Read about DDMS and logcat to obtain a stacktrace and to see what the problem is: http://developer.android.com/guide/developing/debugging/debugging-projects.html
This is what you are looking for:

Categories