I am working on one project where I need to display Image Slider with use of ViewPager.
I had done all code and working very fine but now I want to add some animation to my dots indicator as display in this Image.
Here is my code which I write to make indicator.
private fun initLauncherScreen() {
landingScreens = arrayListOf()
landingScreens.add(
LauncherData(
R.drawable.ic_launch_screen,
getString(R.string.get_latest_trends),
getString(R.string.loram)
)
)
landingScreens.add(
LauncherData(
R.drawable.ic_launch_screen,
getString(R.string.get_latest_trends),
getString(R.string.loram)
)
)
landingScreens.add(
LauncherData(
R.drawable.ic_launch_screen,
getString(R.string.get_latest_trends),
getString(R.string.loram)
)
)
landingScreens.add(
LauncherData(
R.drawable.ic_launch_screen,
getString(R.string.get_latest_trends),
getString(R.string.loram)
)
)
val launcherPagerAdapter = LauncherPagerAdapter(this, landingScreens)
binding.launcherPager.adapter = launcherPagerAdapter
setupIndicatorDots(0)
binding.launcherPager.addOnPageChangeListener(object : OnPageChangeListener {
override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {}
override fun onPageSelected(position: Int) {
setupIndicatorDots(position)
}
override fun onPageScrollStateChanged(state: Int) {}
})
}
private fun setupIndicatorDots(position: Int) {
indicators = arrayOfNulls(landingScreens.size)
indicatorLayout = binding.indicatorLayout
indicatorLayout.removeAllViews()
for (i in indicators.indices) {
indicators[i] = TextView(this)
indicators[i]?.setPadding(10, 10, 10, 10)
indicators[i]!!.text = Html.fromHtml("●")
indicators[i]!!.textSize = 18f
indicators[i]!!.setTextColor(ContextCompat.getColor(this, android.R.color.darker_gray))
indicatorLayout.addView(indicators[i])
}
indicators[position]!!.setTextColor(ContextCompat.getColor(this, R.color.colorPrimary))
}
In My Layout Design I have pager like below:
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<androidx.viewpager.widget.ViewPager
android:id="#+id/launcherPager"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHeight_percent="0.6"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<LinearLayout
android:id="#+id/indicatorLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginHorizontal="#dimen/_30sdp"
android:layout_marginVertical="#dimen/_5sdp"
android:gravity="center|center_vertical"
android:orientation="horizontal"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/launcherPager" /></androidx.constraintlayout.widget.ConstraintLayout>
I need to do animation on scrolling exactly like the image.
Any answer from your side will make me helpful.
Thanks in advance !!
Related
Failed to add data into PieEntry. All the dependencies and repositories are added correctly. It can run, but the output is not as same as what I code in. I have no idea about how to solve it.
Main Activity:
private void ShowPieChart() {
ArrayList<PieEntry> pieEntries = new ArrayList<>();
String label = "type";
//initializing data
Map<String, Integer> typeAmountMap = new HashMap<>();
typeAmountMap.put("Toys",200);
typeAmountMap.put("Snacks",230);
typeAmountMap.put("Clothes",100);
typeAmountMap.put("Stationary",500);
typeAmountMap.put("Phone",50);
//initializing colors for the entries
ArrayList<Integer> colors = new ArrayList<>();
colors.add(Color.parseColor("#304567"));
colors.add(Color.parseColor("#309967"));
colors.add(Color.parseColor("#476567"));
colors.add(Color.parseColor("#890567"));
colors.add(Color.parseColor("#a35567"));
colors.add(Color.parseColor("#ff5f67"));
colors.add(Color.parseColor("#3ca567"));
//input data and fit data into pie chart entry
for(String type: typeAmountMap.keySet()){
pieEntries.add(new PieEntry(typeAmountMap.get(type).floatValue(), type));
}
PieDataSet pieDataSet = new PieDataSet(pieEntries,label);
pieDataSet.setValueTextSize(12f);
pieDataSet.setColors(colors);
PieData pieData = new PieData(pieDataSet);
pieData.setDrawValues(true);
pieChart.setData(pieData);
pieChart.invalidate();
pieChart.setUsePercentValues(true);
pieChart.getDescription().setEnabled(false);
pieChart.setRotationEnabled(true);
pieChart.setDragDecelerationFrictionCoef(0.9f);
pieChart.setRotationAngle(0);
pieChart.setHighlightPerTapEnabled(true);
pieChart.animateY(1400, Easing.EasingOption.EaseInOutQuad);
pieChart.setHoleColor(Color.parseColor("#000000"));
}
XML:
<com.github.mikephil.charting.charts.PieChart
android:id="#+id/pieChart"
android:layout_above="#+id/bottom_navi"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:layout_marginStart="160dp"
android:layout_marginLeft="160dp"
android:layout_marginTop="287dp"
android:layout_marginEnd="144dp"
android:layout_marginRight="144dp"
android:layout_marginBottom="391dp" />
I'm trying to make a program which will display color that you choose. So you would slide on picture with color shades and application would display you current color. Under that there is text view that will display RGB value of current color. But when I tried to write code for color picker I get this error:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.leds/com.example.leds.MainActivity}:
java.lang.NullPointerException: color_picker must not be null
I tried everything but i can't fix it.
My kotlin code:
class MainActivity : AppCompatActivity() {
private lateinit var bitmap:Bitmap
#SuppressLint("ClickableViewAccessibility")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
color_picker.isDrawingCacheEnabled = true
color_picker.buildDrawingCache(true)
val wheel = findViewById<ImageView>(R.id.color_picker)
wheel.setOnTouchListener { v, event ->
if (event.action == MotionEvent.ACTION_DOWN || event.action == MotionEvent.ACTION_MOVE) {
bitmap = color_picker.drawingCache
val pixel = bitmap.getPixel(event.x.toInt(), event.y.toInt())
val r = Color.red(pixel)
val g = Color.green(pixel)
val b = Color.blue(pixel)
selected_color_view.setBackgroundColor(Color.rgb(r,g,b))
color_value.text = "RGB: $r, $g, $b"
}
true}
And my xml code:
<?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"
android:gravity="center">
<ImageView
android:id="#+id/color_picker"
android:layout_width="350dp"
android:layout_height="450dp"
android:src="#drawable/colour_wheel"/>
<View
android:id="#+id/selected_color_view"
android:layout_width="100dp"
android:layout_height="100dp" />
<TextView
android:id="#+id/color_value"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="RGB:"
android:textColor="#000"
android:textSize="18sp"/>
It's because you did not initialize variable color_picker before using it
color_picker.isDrawingCacheEnabled = true
color_picker.buildDrawingCache(true)
You have to initialize it like you did in val wheel = findViewById<ImageView>(R.id.color_picker) and then you can modify it like
wheel.isDrawingCacheEnabled = true
#EDIT You have to initialize variable wheel first, then you can call it's properties.
val wheel = findViewById<ImageView>(R.id.color_picker)
wheel.isDrawingCacheEnabled = true
wheel.buildDrawingCache(true)
I am developing a recycler view with Kotlin. Let's look at my code, when I click on the orderProduct button, the orderRecyclerview is visible and on the contrary, when I click again, the visible is gone. But sometimes when I click on it, the recycler view is shown and sometimes it is not shown.
So how can I do this anytime? How can I solve this bug?
orderProduct.setOnClickListener{
orderProduct.setCompoundDrawablesWithIntrinsicBounds(0, 0, if (!isClicked) R.drawable.btn_down else R.drawable.btn_up, 0)
if (isClicked) {
var r = Runnable {
try {
orderRecyclerview.visibility=View.VISIBLE
paymentList= paymentDb?.paymentDao()?.getAll()!!
mAdapter = PaymentRecylcerViewAdapter(this, paymentList)
mAdapter.notifyDataSetChanged()
orderRecyclerview.adapter = mAdapter
orderRecyclerview.layoutManager = LinearLayoutManager(this)
orderRecyclerview.setHasFixedSize(false)
}catch (e: Exception) {
}
}
val thread = Thread(r)
thread.start()
}else {
orderRecyclerview.visibility=View.GONE
}
isClicked = !isClicked
}
Firstly, you can move this code above the onclickListner.
paymentList= paymentDb?.paymentDao()?.getAll()!!
mAdapter = PaymentRecylcerViewAdapter(this, paymentList)
mAdapter.notifyDataSetChanged()
orderRecyclerview.adapter = mAdapter
orderRecyclerview.layoutManager = LinearLayoutManager(this)
orderRecyclerview.setHasFixedSize(false)
Then inside the onClickListner handle visibility of orderRecyclerview.
For better user experience, You can also add animation in this.
Hope, it will help.
add Dependecy
compile 'net.cachapa.expandablelayout:expandablelayout:2.9.2'
<net.cachapa.expandablelayout.ExpandableLayout
android:id="#+id/expandable_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:el_duration="1000"
app:el_expanded="true"
app:el_parallax="0.5">
<RecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp"
android:text="Fixed height" />
To trigger the animation, simply grab a reference to the ExpandableLayout from your Java code and and call either of expand(), collapse() or toggle().
I have a TextView in the navigation drawer of my app. When initially loading the relevant xml, the TextView (displaying users name and surname) runs fine without an error. However, after navigating to the next activity and returning immediately back to the previous activity the app crashes with a NullPointerException. Why does it load it successfully initially but crashes when navigating back? I have put in a null check but this just sets the TextView's to blank but prevents the crashing.
My navigation header xml:
<?xml version="1.0" encoding="utf-8"?>
<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="wrap_content"
android:orientation="vertical">
<ImageView
android:id="#+id/imageView2"
android:layout_width="match_parent"
android:layout_height="117dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
app:srcCompat="#drawable/logo" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal">
<TextView
android:id="#+id/textView15"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="20dp"
android:textSize="24sp" />
<TextView
android:id="#+id/textView40"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="24sp" />
</LinearLayout>
</LinearLayout>
Part of the layout xml where i am referencing the navigation header:
<com.google.android.material.navigation.NavigationView
android:id="#+id/mydeals_seller_nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:headerLayout="#layout/navigation_header"
android:fitsSystemWindows="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:menu="#menu/seller_menu"/>
Referencing the TextView in the activity:
docref = FirebaseFirestore.getInstance()
docref.collection("Users").whereEqualTo("uid", userid).get()
.addOnSuccessListener { value->
if (value != null ) {
for (document in value!!) {
val user_name = document.getString("name")!!
val user_surname = document.getString("surname")!!
if(textView15 != null || textView40 != null){
menu_name = findViewById(R.id.textView15)!!
menu_surname = findViewById(R.id.textView40)!!
menu_name.setText("$user_name ")
menu_surname.setText(user_surname)
}
}
} else {
Log.d("Firestore_error", "No Data")
}
}
Do I need a separate navigation header and TextView for each activity?
Edit
Testing this further on API 22, this issue doesnt present itself. Any suggestions?
Instead of calling the findViewById on the activity view you should call it on the header view like so:
docref.collection("Users").whereEqualTo("uid", userid).get()
.addOnSuccessListener { value->
if (value != null ) {
for (document in value!!) {
val user_name = document.getString("name")!!
val user_surname = document.getString("surname")!!
// This gets the first header that is attached to the
// navigation view
val header = mydeals_seller_nav_view.getHeaderView(0)
menu_name = header.findViewById(R.id.textView15)!!
menu_surname = header.findViewById(R.id.textView40)!!
menu_name.setText("$user_name ")
menu_surname.setText(user_surname)
}
}
} else {
Log.d("Firestore_error", "No Data")
}
}
Hope these fixies your issue
menu_name = findViewById(R.id.textView15)!!
menu_surname = findViewById(R.id.textView40)!!
menu_name.setText("$user_name ")
menu_surname.setText(user_surname)
Replace those lines with these:
val headerView = mydeals_seller_nav_view.getHeaderView(0)
val menu_name = headerView.textView15
val menu_surname = headerView.textView40
menu_name.setText("$user_name ")
menu_surname.setText(user_surname)
Please try this...
You have to store that values in prefernces and set preferences values to textview or initialize firbase.instance in onResume() method.
After lots of trial and error its seems like the error was a Firestore query issue. based on my query above i was using a loop where userid id matched the authenticated user. for some reason when moving between activities it resulted in a null pointer. i have no idea why but changing the query above to a single read as below fixed the issue:
docref = FirebaseFirestore.getInstance()
docref.collection("Users").document(userid).get()
.addOnSuccessListener { value->
if (value != null ) {
val user_name = value.getString("name")!!
val user_surname = value.getString("surname")!!
if(textView15 != null || textView40 != null){
menu_name = findViewById(R.id.textView15)!!
menu_surname = findViewById(R.id.textView40)!!
menu_name.setText("$user_name ")
menu_surname.setText(user_surname)
}
} else {
Log.d("Firestore_error", "No Data")
}
}
Just like that, no more NullPointerException. it would be great if a Firestore expert could explain why this was happening.
I have a button and an EditText with input type numbers only in my main xml. I want to check all values entered. What i did until now and works fine is that i can check if starts with zero and then toast an attention msg, if value is zero also a toast msg and if the value entered is bigger then 25. This work perfectly into my onClick procedure from the button in xml.
What i cant solve is when you delete all the numbers in the edit text and will be empty. I tryed a lot of codes and all the time my app crashes.
My code from xml for the Edit Text:
<EditText
android:id="#+id/numarJocuri"
android:background="#drawable/decor_fundal_la_edittext"
android:paddingTop="6dp"
android:paddingLeft="32dp"
android:paddingRight="32dp"
android:paddingBottom="6dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:inputType="number"
android:maxLength="2"
android:textColor="#be9e51"
android:textColorHint="#c8e6c9"
android:textSize="14sp"
android:text="1"/>
My code from xml for the button:
<Button
android:layout_width="match_parent"
android:layout_height="38dp"
android:background="#drawable/buton_incepeti"
android:drawableLeft="#drawable/ok1"
android:gravity="center"
android:layout_marginBottom="4dp"
android:onClick="beginClick"
android:paddingLeft="30dip"
android:paddingRight="26dip"
android:text="#string/incepeti"
android:textColor="#ffffff" />
What i did on my java main code at the button click procedure:
//BEGIN CLICK
public void beginClick(View view) {
//initialize the edit text
final EditText edMeu = (EditText) findViewById(R.id.numarJocuri);
String s1;
s1 = edMeu.getText().toString().trim();
int numar1;
numar1 = Integer.parseInt(s1);
//the value entered to not be bigger then 25
if (numar1 > 25) {
Toast.makeText(getApplicationContext(), getString(R.string.atentie_numar_maxim), Toast.LENGTH_SHORT).show();
return;
}
//to not start with zero
if (edMeu.getText().toString().startsWith("0")) {
Toast.makeText(getApplicationContext(), getString(R.string.nu_incepe_cu_zero), Toast.LENGTH_SHORT).show();
return;
}
}//SFARSIT BEGIN CLICK
So, until now all of this works.
I tryed this but not worked for me here on button click. In other activity at an alertdialog this code works fine.
//check to not be empty
String verif_text1 = edMeu.getText().toString().trim();
if(verif_text1.isEmpty() || verif_text1.length() == 0 || verif_text1.equals("") || verif_text1 == null)
{
Toast.makeText(getApplicationContext(), getString(R.string.msg_null_values), Toast.LENGTH_LONG).show();
return;
} //end checking
Also i tryed to check the lenght of that edit text, not worked.
you may try isEmpty() to check the text is empty or not
edMeu.getText().toString().isEmpty()
Finally i solved it. This is how:
The main problem was that me tryed to check that edit text after i was transforming his value into integer, so after i readed more carefully the errors displayed on android console i figured where the problem was.
Code into my java button
//begin click for the button
public void beginClick(View view) {
//initialize
final EditText edMeu = (EditText) findViewById(R.id.numarJocuri);
String s1;
s1 = edMeu.getText().toString().trim();
//here is the verification for not to be empty or no value entered
if (s1.toString().isEmpty()) {
Toast.makeText(getApplicationContext(), getString(R.string.games_not_entered), Toast.LENGTH_SHORT).show();
return;
}
//transform into integer values
//after the string was checked above
int numar1;
numar1 = Integer.parseInt(s1);
//to not be bigger then 25, after is transformed in integer
if (numar1 > 25) {
Toast.makeText(getApplicationContext(), getString(R.string.atentie_numar_maxim), Toast.LENGTH_SHORT).show();
return;
}
//to not start with zero values
if (edMeu.getText().toString().startsWith("0")) {
Toast.makeText(getApplicationContext(), getString(R.string.nu_incepe_cu_zero), Toast.LENGTH_SHORT).show();
return;
}
} //end of begin click for the button
That is all, was not hard, but requires more attention when writing codes.