I just want to animate my button.
I am getting:
W/PropertyValuesHolder: Method setAlpha() with type int not found on target class class android.support.v7.widget.AppCompatButton
Mainactivity.java:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
ObjectAnimator objectAnimator;
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView mListView= (ListView) findViewById(R.id.listView);
Button button= (Button) findViewById(R.id.button);
objectAnimator=ObjectAnimator.ofInt(button,"alpha",0,1).setDuration(1000);
objectAnimator.setTarget(button);
objectAnimator.start();
adapter myAdapter = new adapter(this);
AlphaInAnimationAdapter animationAdapter = new AlphaInAnimationAdapter(myAdapter);
animationAdapter.setAbsListView(mListView);
mListView.setAdapter(animationAdapter);
}
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="autogenie.mp.MainActivity">
<ListView
android:layout_width="wrap_content"
android:layout_height="300dp"
android:id="#+id/listView"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="#+id/button"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="50dp" />
</RelativeLayout>
Why is it not animating?
What comes under property name parameter of method ofint() of objectanimator class?
The alpha property is of type float. You are trying to animate it with int values. The object animator tries to find a setAlpha(int) method in the View class and can't do it - hence the exception.
You either need to use ObjectAnimator.ofFloat() or the simpler button.animate().alpha() variant. I would personally recommend the second option - it's much cleaner and removed some boilerplate code.
Related
as my title says I'm here with a problem in Android studio (JAVA) with my app, how to call class as an Activity when class has extends LinearLayout?
My class is:
public class CustomCalendar extends LinearLayout {
My code where I try to call it:
Intent customCalendar = new Intent(MainActivity.this, CustomCalendarActivity.class);
startActivity(customCalendar);
I tried to make this:
public class CustomCalendarActivity extends AppCompatActivity {
CustomCalendar customCalendar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
customCalendar = (CustomCalendar) findViewById(R.id.kalendoriaus_virsus);
customCalendar.SetUpCalendar();
}
}
My crash is this:
java.lang.NullPointerException: Attempt to invoke virtual method 'void com.example.kalendorius.CustomCalendar.SetUpCalendar()' on a null object reference
at com.example.kalendorius.MainActivity$2.onClick(MainActivity.java:75)
75 Line is:
startActivity(customCalendar);
How I setup my calendar:
My kalendorius.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/kalendoriaus_virsus"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/soft_blue_green"
android:orientation="horizontal"
android:paddingTop="8dp"
android:paddingBottom="8dp">
<ImageButton
android:id="#+id/atgalBtn"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_margin="10dp"
android:background="#drawable/back" />
<TextView
android:id="#+id/dabartineData"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
android:layout_weight="3"
android:gravity="center"
android:text="Data"
android:textColor="#ffffff"
android:textSize="18sp"
android:textStyle="bold" />
That's just a bit code where I use that kalendorius_virsus.
DONE. I figure out what was the problem. I just forgot to generate for database constructor:D Thank you for everyone who tried to help.
The problem is
customCalendar = (CustomCalendar) findViewById(R.id.kalendoriaus_virsus);
The groupView LinearLayout with id kalendorias_virsus in the kalendorias.xml is not a CustomCalendar so always return null.
You need to understand how to properly user a costum view
basic example CustomView ->
public class CustomView extends LinearLayout {
public CustomView(Context context, #Nullable AttributeSet attrs) {
super(context, attrs);
View view = LayoutInflater.from(getContext()).inflate(
R.layout.costum, null);
this.addView(view);
}
public void setCustomText(String text){
TextView textview = (TextView) findViewById(R.id.textViewId);
textview.setText(text);
}
}
layout custom.xml ->
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/textViewId"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="#string/app_name"
android:gravity="center">
</TextView>
</LinearLayout>
activity.xml->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<com.example.myapplication.CustomView
android:id="#+id/customViewId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
MainActivity class->
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
CustomView customView = findViewById(R.id.customViewId);
customView.setCustomText("Welcome ");
}
}
You have many errors in your code. We still need more information to determine why you are getting a NullPointerException (NPE) in the MainActivity.onClick() method. However, this will for sure crash:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
customCalendar = (CustomCalendar) findViewById(R.id.kalendoriaus_virsus);
customCalendar.SetUpCalendar();
}
because customCalendar will be null because you have not called setContentView() before calling findBiewById().
You need to add
setContentView(R.layout.XXXXXX);
before you call findViewById(). The XXXXXX above must be the name of your layout XML file (without the .xml file extension).
I put the photo of the account in the navigation header. The border is rectangular, I would like to make it rounded, how can I do that?
In this code I used Glide to display the image.
my_header_navigation.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="140dp"
android:background="#color/common_google_signin_btn_text_dark_focused"
android:id="#+id/navigation_header">
<ImageView
android:id="#+id/photoImageView"
android:layout_width="56dp"
android:layout_height="56dp"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true"
android:layout_marginBottom="16dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="18dp"
android:background="#color/colorAccent"
android:visibility="invisible" />
</RelativeLayout>
and in the Main Activity:
private ImageView photoImageView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list);
View hView = navigationView.getHeaderView(0);
photoImageView = hView.findViewById(R.id.photoImageView);
if(user != null){
Glide.with(List.this).load(user.getPhotoUrl()).into(photoImageView);
photoImageView.setVisibility(View.VISIBLE);
}
else{
photoImageView.setVisibility(View.INVISIBLE);
}
Your code i'ts right, you need implement CircleImageView, I show you how to implement.
CircleImageView - GitHub
nav_header_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="#dimen/nav_header_height"
android:background="#drawable/side_nav_bar"
android:gravity="bottom"
android:orientation="vertical"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:theme="#style/ThemeOverlay.AppCompat.Dark">
<com.mikhaellopez.circularimageview.CircularImageView
android:id="#+id/profile_image"
android:layout_width="150dp"
android:layout_height="150dp"
android:src="#drawable/profile_image"
android:layout_gravity="center_horizontal"
android:layout_marginTop="20dp"
android:layout_marginBottom="0dp"
app:civ_border_color="#EEEEEE"
app:civ_border_width="4dp"
app:civ_shadow="true"
app:civ_shadow_radius="10"
app:civ_shadow_color="#8BC34A"/>
</LinearLayout>
MainActivity
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
CircleImageView circleImageView = (CircleImageView) findViewById(R.id.profile_image);
Glide.with(List.this).load(user.getPhotoUrl()).into(circleImageView);
}
My NavigationDrawer with CircleImageView
Link Image
NOTE
Remember, your image need proportional dimensioned, example (100x100, 200x200), like a perfect square, I hope I have help you.
I am having a layout with a single editText in it,below the editText I added a add button. Now my question is when I click the add button I need to get another editText below the editText and has to repeat the same.Please anyone help, Thank you.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:id="#+id/ll_edit_texts_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<EditText
android:id="#+id/et1"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<Button
android:id="#+id/buttonAdd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ADD" />
</LinearLayout>
and put following code in your Activity
final LinearLayout llEditTextsContainer = (LinearLayout) view.findViewById(R.id.ll_edit_texts_container);
Button buttonAdd = (Button) view.findViewById(R.id.buttonAdd);
buttonAdd.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
EditText editText = new EditText(getActivity());
//editText.setId(); you should set id smartly if you wanted to use data from this edittext
llEditTextsContainer.addView(editText);
}
});
First of all create a container view in your main layout which is going to hold the new Edittexts.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.vuclip.dynamictextedit.MainActivity">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/addTextView"
android:text="Add EditText"
android:onClick="onClick"
/>
<TableLayout
android:id="#+id/containerLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
</TableLayout>
Here, on pressing the button, new Edittexts will be created and added into the TableLayout.
Now create a new Layout which will hold your edittext(I called this file new_layout.xml).
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<EditText
android:id="#+id/newEditText"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
Now add this layout into your main activity.
public class MainActivity extends AppCompatActivity {
TableLayout container;
static int rowIndex = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
container = (TableLayout) findViewById(R.id.containerLayout);
}
public void onClick(View view) {
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View newRowView = inflater.inflate(R.layout.new_layout,null);
container.addView(newRowView,rowIndex);
rowIndex++;
}}
I am trying to make a custom media player where when ever the music start a loader will start and when the music is paused or ends the the loader goes invisible .This is what i am trying to get when the button is clicked ..How to set the desired output.I am using the following gradel
compile 'com.leo.simplearcloader:simplearcloader:1.0.+'
compile 'de.hdodenhof:circleimageview:2.0.0'
my xml code
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:custom="http://schemas.android.com/apk/res-auto"
tools:context="com.union.playerfront1.MainActivity"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="invisible"
android:id="#+id/load1">
<com.leo.simplearcloader.SimpleArcLoader
android:visibility="visible"
android:id="#+id/loader"
android:layout_centerInParent="true"
android:layout_width="200dp"
android:layout_height="200dp"
custom:arc_style="simple_arc"
custom:arc_speed="medium"
custom:arc_margin="3dp"
>
</com.leo.simplearcloader.SimpleArcLoader>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<de.hdodenhof.circleimageview.CircleImageView
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/profile_image"
android:layout_width="150dp"
android:layout_height="150dp"
android:src="#drawable/profile"
app:civ_border_width="2dp"
app:civ_border_color="#FF000000"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="#+id/button"
android:layout_marginBottom="44dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
my java code
public class MainActivity extends AppCompatActivity {
boolean isPressed = false;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final MediaPlayer mediaPlayer =MediaPlayer.create(this,R.raw.song);
final RelativeLayout real=(RelativeLayout)findViewById(R.id.load1);
final Button button=(Button)findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (isPressed){
button.setBackgroundResource(R.drawable.profile);
real.setVisibility(View.VISIBLE);
mediaPlayer.start();
}
}
});
}
}
How I can change the visibility of the loader on the base that a song is playing or it is stopped I am able to set the button visibility properly.How i can set the visibility on the basis of song pause,song start,song stop?
I've seen other people who have had the same issue, but they were missing setContentView().
Any ideas as to why the below isn't working? NPE occurs when I call setAdapter because listView is null.
public class DisplayMessageActivity extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_message);
ArrayList<String> message = getArray();
ListView listView = (ListView) findViewById(R.id.listView);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, message);
listView.setAdapter(adapter);
};
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:orientation="horizontal"
tools:context=".MainActivity" >
<EditText android:id="#+id/edit_message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="#string/edit_message" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/button_send"
android:onClick="sendMessage" />
<ListView android:id="#+id/listView"
android:layout_height="match_parent"
android:layout_width="match_parent"/>
<TextView
android:id="#+id/rowTextView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:textSize="16sp" >
</TextView>
</LinearLayout>
You are referencing R.layout.activity_display_message when you call setContentView meaning that (ListView) findViewById(R.id.listView); will return null
Instead of setContentView(R.layout.activity_display_message) call setContentView(R.layout.activity_main)