My animation I'snt working on click, here is my code:
Edit: can now click the button, but nothing happens on click
animation: bottom_up.xml:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:fromYDelta="75%p" android:toYDelta="0%p"
android:fillAfter="true"
android:duration="500"/>
here is my java MusicPlayerActivity.java:
btnPlayList.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Animation bottomUp = AnimationUtils.loadAnimation(MusicPlayerActivity.this, R.anim.bottom_up);
ViewGroup hiddenPanel = (ViewGroup) v.findViewById(R.id.hidden_panel);
hiddenPanel.startAnimation(bottomUp);
hiddenPanel.setVisibility(View.VISIBLE);
}
});
}
Thank you!
check if you initialed btnPlayList
like this btnPlayList=(Button)findViewById(R.id.btnPlayList);
or hiddenPanel not exist in this view that you click on;
The error you are getting is a very common one. It's called NullPointerException because you try to call a method of an object whose reference is null, so the system can't resolve the method. You cand read more about this exception in this link.
The line that is probably causing this exception is:
ViewGroup hiddenPanel = (ViewGroup) v.findViewById(R.id.hidden_panel);
In order to fix it, I would suggest two things:
Check that the id R.id.hidden_panel is correct.
If you are contemplating that R.id.hidden_panel might not be in your layout, wrap its usage with a null check:
if (hiddenPanel != null) {
hiddenPanel.startAnimation(bottomUp);
hiddenPanel.setVisibility(View.VISIBLE);
}
EDIT:
To fix the animation not showing, try calling startAnimation after you set the hiddenPanel to Visible, and not before.
java.lang.NullPointerException -> some variables not initialized.
check all variables
Related
I am currently making a chat app and i want to add a feature like telegram. As you can see here, there is a animation in the recycler view as if i=t zooming or decreasing its size. You can also notice that animation while opening and closing the navigation drawer. I too tried to do that using this but i get no animation in the recycler view. See this. Actually not this but like this. I took this gif from the README of the repo. So is that possible? If so, can you please tell me a simple implementation ?
Thanks in advance and i will be waiting 😀
Add overridePendingTransition in onBackPressed() method:
#Override
public void onBackPressed() {
super.onBackPressed();
overridePendingTransition(0, R.anim.slide_out_right);
}
res->anim->slide_out_right.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="#android:integer/config_mediumAnimTime"
android:fromXDelta="0"
android:toXDelta="100%p" />
</set>
To me it looks like an Activity transition where the translateZ value is modified. But since the Telegram Android client is open source, we can just take a look at it.
It looks like the Telegram devs use a lot of custom code instead of relying on the android SDK.
The DialogsActivity which extends a BaseFragment but is neither an Android Activity nor an Android Fragment since BaseFragmentdoesn't extend any of them, has the following method for the transition:
private void setSlideTransitionProgress(float progress) {
if (SharedConfig.getDevicePerformanceClass() == SharedConfig.PERFORMANCE_CLASS_LOW) {
return;
}
slideFragmentProgress = progress;
if (fragmentView != null) {
fragmentView.invalidate();
}
if (filterTabsView != null) {
float s = 1f - 0.05f * (1f - slideFragmentProgress);
filterTabsView.getListView().setScaleX(s);
filterTabsView.getListView().setScaleY(s);
filterTabsView.getListView().setTranslationX((isDrawerTransition ? AndroidUtilities.dp(4) : -AndroidUtilities.dp(4)) * (1f - slideFragmentProgress));
filterTabsView.getListView().setPivotX(isDrawerTransition ? filterTabsView.getMeasuredWidth() : 0);
filterTabsView.getListView().setPivotY(0);
filterTabsView.invalidate();
}
}
I am trying to get an instance of a SeekBar using the findViewById() and attaching a listener to it.
The code looks like this:
SeekBar seek_bar = findViewById(R.id.audio_volume);
seek_bar.setOnSeekBarChangeListener(listener);
And the listener:
private SeekBar.OnSeekBarChangeListener listener = new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
log.d("YO HERE");
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
};
Followed this SO post, and invoked the findViewById inside the onCreate method after these two lines:
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
But the problem persists. I am still getting:
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.SeekBar.setOnSeekBarChangeListener(android.widget.SeekBar$OnSeekBarChangeListener)' on a null object reference
And the app fails to open. How can I solve this issue? Also it would be very helpful if the process of debugging a null object reference error is shared.
Layout xml:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<ListPreference
android:key="PREF_MIN_REC"
android:title="#string/min_rec"
android:summary="#string/min_rec_desc"
android:entries="#array/min_rec_options"
android:entryValues="#array/min_rec_values"
android:dialogTitle="#string/min_rec"
android:defaultValue="15"
/>
<ListPreference
android:key="PREF_AUDIO_SOURCE"
android:title="#string/audio_source"
android:summary="#string/audio_source_desc"
android:entries="#array/audio_source_options"
android:entryValues="#array/audio_source_values"
android:dialogTitle="#string/audio_source"
android:defaultValue="4"
/>
<ListPreference
android:key="PREF_AUDIO_FORMAT"
android:title="#string/audio_format"
android:summary="#string/audio_format_desc"
android:entries="#array/audio_format_options"
android:entryValues="#array/audio_format_values"
android:dialogTitle="#string/audio_format"
android:defaultValue="1"
/>
<org.anasthase.androidseekbarpreference.SeekBarPreference
android:id="#+id/volume"
android:key="PREF_MIC_VOLUME"
android:title="#string/mic_volume"
android:summary="#string/mic_volume_desc"
app:maxValue="100"
app:minValue="1"
app:stepValue="1"
android:defaultValue="20"
app:format="%.1f"
app:displayDividerValue="5"/>
<org.anasthase.androidseekbarpreference.SeekBarPreference
android:id="#+id/audio_volume"
android:key="PREF_AUDIO_VOLUME"
android:title="#string/audio_volume"
android:summary="#string/audio_volume_desc"
app:maxValue="100"
app:minValue="1"
app:stepValue="1"
android:defaultValue="20"
app:format="%.1f"
app:displayDividerValue="5"/>
<EditTextPreference
android:key="PREF_URL_SUBDOMAIN"
android:title="#string/server_subdomain"
android:summary="#string/server_subdomain_desc"
android:dialogTitle="#string/server_subdomain"
android:defaultValue=".telforce.biz"
/>
<ListPreference
android:key="PREF_UPLOAD_NETWORK"
android:title="#string/upload_network"
android:summary="#string/upload_network_desc"
android:entries="#array/upload_network_options"
android:entryValues="#array/upload_network_values"
android:dialogTitle="#string/upload_network"
android:defaultValue="1"
/>
<CheckBoxPreference
android:key="save_term_enable_key"
android:title="#string/save_term_check_title"
android:summary="#string/save_term_check_summary"/>
<EditTextPreference
android:key="save_term_key"
android:title="#string/save_term_title"
android:summary="#string/save_term_summary"
android:dependency="save_term_enable_key"
android:defaultValue="#string/save_term_default_value"
android:dialogTitle="#string/save_term_dialog"/>
</PreferenceScreen>
In your XML your audio_volume view belongs to class
org.anasthase.androidseekbarpreference.SeekBarPreference
In your Java code, it is SeekBar only, so if it is not crashing, I assume Seekbar extends SeekBarPreference.
Ignore the casting comments and responses since it's not needed since last year if you're using the latest versions of java, android sdk and target api.
The null pointer exception means the seekbar was not found on the layout. Check if you have more than 1 xml with the same name (for different screen resolutions).
do this way if its preference fragment:
SeekBarPreference skp = (SeekBarPreference) findPreference("PREF_AUDIO_VOLUME");
Cheers ;)
I simply want to change the backgroundTint of a normal button from inside the java code. I tried many different approaches like ColorStateList or setColorFilter, but nothing worked. I am purposefully not using setBackgroundColor since I want to keep the original shape of the button.
Furthermore, the colors I want to use are already defined in my resources. After lots of trial and error I managed to access these colors with this code:
int colorBtnDeactivated = ContextCompat.getColor(this, R.color.colorBtnDeactivated);
So basically I only need this one line of java code which enables me to access the background tint. The rest I can do myself.
I would really appreciate help, I have been stuck on this problem for hours. Thanks!
Edit: Using a selector-xlm didn't work, since it only changed the color of the button while being pressed. Also the buttons will influence each other, so by pressing one button I will need to be able to change the background tint of another button.
Edit 2: I tried again with setColorFilter:
//this is all inside the onCreate-method
int colorBtnActiveTest= ContextCompat.getColor(this, colorBtnActive);
int colorBtnDeactivatedTest=ContextCompat.getColor(this, colorBtnDeactivated);
Button btnKnockOne = (Button)findViewById(R.id.btnKnockOne);
boolean stateBtnKnockOne = false;
btnKnockOne.getBackground().setColorFilter(colorBtnDeactivatedTest, PorterDuff.Mode.SRC_IN);
btnKnockOne.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (stateBtnKnockOne==false){
btnKnockOne.getBackground().setColorFilter(colorBtnActiveTest, PorterDuff.Mode.SRC_IN);
stateBtnKnockOne=true;
}
else if (stateBtnKnockOne==true){
btnKnockOne.getBackground().setColorFilter(colorBtnDeactivatedTest, PorterDuff.Mode.SRC_IN);
stateBtnKnockOne=false;
}
}
});
This is the result:
When I open the activity, the button is displayed in the default grey button color, not in my custom color colorBtnDeactivatedTest
When I press the button, it briefly changes its color to colorBtnActiveTest, but then goes back to its grey color
I finally found a solution! This post helped me find it: https://stackoverflow.com/a/8748112/8952749
Now I basically have two buttons of which only one can be selected at one time. For this to work I created a selector-xlm:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_selected="true"
android:drawable="#drawable/button_active" />
<item
android:state_selected="false"
android:drawable="#drawable/button_deactivated" />
</selector>
This is the java code, which enables me to change state_selected of the buttons:
stateBtn1=false;
stateBtn2=false;
btnTest1=(Button)findViewById(R.id.button);
btnTest2=(Button)findViewById(R.id.button2);
btnTest1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (stateBtn1==false){
btnTest1.setSelected(true);
stateBtn1=true;
btnTest2.setSelected(false);
stateBtn2=false;
}
else if (stateBtn1==true){
btnTest1.setSelected(false);
stateBtn1=false;
}
}
});
btnTest2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (stateBtn2==false){
btnTest2.setSelected(true);
stateBtn2=true;
btnTest1.setSelected(false);
stateBtn1=false;
}
else if (stateBtn2==true){
btnTest2.setSelected(false);
stateBtn2=false;
}
}
});
On a specific view I am adding a global layout listener:
myView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
// code
LinearLayout linearLayout = (LinearLayout)findViewById(R.id.someView);
if(linearLayout != null && linearLayout.getVisibility() == View.VISIBLE) {
linearLayout.setVisibility(View.GONE);
}
LinearLayout otherLayout = (LinearLayout) findViewById(R.id.someOtherView);
otherLayout.setVisibility(View.GONE);
//other code
}
});
In some cases but I don't know exactly how, during rotation some times it happens that there is NPE for the line otherLayout.setVisibility(GONE)
To be honest I am not sure why the code checks for null in the lines above for the linearLayout and not for the otherLayout but both are defined in the same resource file and are not e.g. removed programmatically anywhere.
The only difference is that the otherLayout is not visible.
So my question is: Are there any things I should look out for on rotation with global layout listeners? Why am I getting NPE in some random cases?
Update:
Both views are part of the same xml file. And actually one defined is after the other. The only difference is that someView is defined as visible and otherView as not visible. Having said that though, there can be such a case where someView is already visible/rendered while otherView has not been yet rendered/made visible when the rotation is happening depending on the current width
I suspect there is a difference between the layouts of findViewById(R.id.someView) and findViewById(R.id.someOtherView). The difference is the timing, for sure, and possibly the layout xml file that it is inflating. With R.id.someOtherView, it is done immediately while R.id.someView, it is executed when the layout is drawn OR at any other time as in screen orientation change since the width/height of the screen changed.
NEW:
final LinearLayout linearLayout = (LinearLayout)findViewById(R.id.someView);
myView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
//LinearLayout linearLayout = (LinearLayout)findViewById(R.id.someView);
if(linearLayout != null && linearLayout.getVisibility() == View.VISIBLE) {
linearLayout.setVisibility(View.GONE);
}
...
}
});
Notes:
I commented out the findViewById() inside onGlobalLayout(), basically removing it.
I think it's not safe to call findViewById() inside the listener since layouts cannot be cached, similar to views and anything related to UI objects. This is what I meant above on my last sentence. I know it's not obvious. I think this explains that the issue is not consistent or strange, as you said essentially.
EDIT: Based on evolution of the problem, I edited this question.
First of all, I know there some similar questions, but each one of them has a specific difference, that makes the answer useless for me...
I really really appreciate if anyone can help me, I'm getting really desperate with this problem...
So the problem is: I want to populate a ListView with checkboxes, and can't use simple_multiple_choice_mode (something like that) because I need to manually build my XML layout - so I'm using the list_item.xml file:
<?xml version="1.0" encoding="utf-8"?>
<CheckBox
xmlns:android="http://schemas.android.com/apk/res/android" android:paddingLeft="8mm"
android:layout_width="fill_parent"
android:layout_height="wrap_content" android:id="#+id/nomeAPP" style="?listItem">
</CheckBox>
Problem is, if I use the simple_multiple (..) mode option, getCheckedItemPositions works fine. If don't (as I have in the code below) getCheckedItemPositions comes null. So from what I read, it's a common bug, that needs an handler as workaround. But I cant get the handler to work, I get an exception with java.lang.NullPointerException in the logcat.
Can anyone please help me?
I have this little pretty code:
this.setListAdapter(new ArrayAdapter<String>(this,
R.layout.list_item, aux));
list.setItemsCanFocus(false);
list.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
list.setSelected(true);
list.setClickable(true);
list.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// Auto-generated method stub
Log.d("checked",""+arg2);
}
public void onNothingSelected(AdapterView<?> arg0) {
// Auto-generated method stub
}
});
CheckBox c = (CheckBox) findViewById(R.id.nomeAPP);
c.setOnCheckedChangeListener(new OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
CheckBox checkbox = (CheckBox)arg0;
boolean isChecked = checkbox.isChecked();
if(isChecked == true)
Log.v("TAG", "true");
else
Log.v("TAG", "false");
}
});
try this code
list = (ListView) findViewById(R.id.listagem);
list.setAdapter(new ArrayAdapter<String>(this, R.layout.list_item,R.id.nomeAPP,aux));
list.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
//get the checked Items like this;
int count=list.getChildCount();
for(int i=1;i<=count;i++){
if(list.isItemChecked(i)){
//do your task/work
}
}
You have to set the choiceMode attribute on your ListView in order to get an object back from this method:
http://developer.android.com/reference/android/widget/AbsListView.html#getCheckedItemPositions%28%29
Your problem is that the custom row should implement Checkable.
Read this question.
Looks that was a bug in the use of setAdapter, which is solved using setListAdapter.
Let's see, documentation states that ListActivity should use
list=getListView();
<ListView android:id="#android:id/list"
so forget about = "+#id(....
now we can our activity knows "where" is the List and now we can use
setListAdapter(new ArrayAdapter<String>(this,
R.Layout.List_item, aux));
and everything works fine.