I've followed a tutorial to animate text change on text view in order to implement (Read more) in comments and such, so far it animates when extending the text but not when collapsing text.
How can i animate on collapse?
gif
fragment
private var extended = false
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
val testTextView = view.findViewById<TextView>(R.id.test_text)
val container = view.findViewById<ConstraintLayout>(R.id.text_view_container)
val comment = /*Long comment*/
//animate text change
container.layoutTransition.enableTransitionType(
LayoutTransition.CHANGING
)
if ((comment.length ?: 0) <= 200) {
testTextView.text = comment
} else {
testTextView.text =
comment.substring(
0, if (comment.length < 200) comment.length else 200
)
}
testTextView.setOnClickListener {
extended = !extended
if (extended) {
testTextView.text = comment
} else {
testTextView.text = comment.substring(
0, (if (comment.length < 200) comment.length else 200)
)
}
}
}
XML
<androidx.constraintlayout.widget.ConstraintLayout 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:id="#+id/fragment_blank_root"
tools:context=".BlankFragment">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/text_view_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:animateLayoutChanges="true"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<TextView
android:id="#+id/test_text"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="#string/hello_blank_fragment" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
I tried setting other layout transition but it did not work.
I am expecting it to animate on collapse
Related
I am trying to make a drawing app but when I am setting the brush size seek and try to make a text view to show its size it is always returning null I tried multiple ways but it didn't succeed so please can any one help me fix the poblems in this code so the text shows the seekbar progress text
class MainActivity : AppCompatActivity() {
private var ibBrush: ImageView? = null
private var tvBrushSize: TextView? = null
private var sbBrushSize: SeekBar?= null
override fun onCreate(savedInstanceState: Bundle?) {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO)
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
ibBrush = findViewById(R.id.ib_brush)
ibBrush?.setOnClickListener{
changeBrushSize()
}
}
private fun changeBrushSize(){
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO)
val brushDialog = Dialog(this)
brushDialog.setContentView(R.layout.brush_size_dialog)
tvBrushSize = findViewById(R.id.tv_brush_size)
sbBrushSize = findViewById(R.id.sb_brush_size)
tvBrushSize?.text = sbBrushSize?.progress.toString()
brushDialog.show()
}
}
Why tvBrushSize is returning null while I am setting it to findViewById() to the correct id in xml
*here is the 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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:padding="10dp"
android:orientation="vertical"
>
<TextView
android:id="#+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/brush_size"
android:gravity="center"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toTopOf="#+id/linear"/>
<LinearLayout
android:layout_width="350dp"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:orientation="horizontal"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView">
<SeekBar
android:id="#+id/sb_brush_size"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="10"
android:max="30"
/>
<TextView
android:id="#+id/tv_brush_size"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:text="#string/_0" />
</LinearLayout>
</LinearLayout>
You should inflate the dialog view first using LayoutInflater.inflate(layoutId) and then use view.findViewById(id) like this snippet:
private fun changeBrushSize(){
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO)
val brushDialog = Dialog(this)
val dialogView = LayoutInflater.inflate(R.layout.brush_size_dialog, null)
brushDialog.setContentView(dialogView)
tvBrushSize = dialogView.findViewById(R.id.tv_brush_size)
sbBrushSize = dialogView.findViewById(R.id.sb_brush_size)
tvBrushSize?.text = sbBrushSize?.progress.toString()
brushDialog.show()
}
you need to bind views from dialog, so manage it like this;
brushDialog.setContentView(R.layout.brush_size_dialog)
val dialogView = layoutInflater.inflate(R.layout.brush_size_dialog, null)
brushDialog.setContentView(dialogView)
tvBrushSize = dialogView.findViewById(R.id.tv_brush_size)
the dialog with your view is not yet existed, when you are trying to refer it with findViewById()
You can inflate view first, then access it as follows
val brushDialog = Dialog(this)
val view = layoutInflater.inflate(R.layout.brush_size_dialog, null)
brushDialog.setContentView(view)
tvBrushSize = view.findViewById(R.id.tv_brush_size)
sbBrushSize = view.findViewById(R.id.sb_brush_size)
tvBrushSize?.text = sbBrushSize?.progress.toString()
brushDialog.show()
or call brushDialog.show() first and then access views with findViewById()
that also should work
You should inflate the view first
val dialogView = LayoutInflater.inflate(R.layout.brush_size_dialog, null)
brushDialog.setContentView(dialogView)
then try to use
dialogView.findViewById(...)
I was using ViewPager with FragmentStatePagerAdapter. Since they deprecated whole class of FragmentStatePagerAdapter, I had to migrate ViewPager2. I could successfully migrated ViewPager2, using FragmentStateAdapter, and so on.
Everything works fine, connecting ViewPager2 with TabLayout also works smoothly, I can slide ViewPager2, and I also can control it via TabLayout. But now I can't fast scroll ListView, even though I touch cursor to control scrolling, it won't scroll down nor up. Then if I just scroll down/up via touching ListView normally, then cursor suddenly moves to listview's current position. Testers reported that in some device, it works only in landscape mode while my emulator and device doesn't work neither on portrait nor landscape.
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout 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:background="?attr/backgroundPrimary">
<com.google.android.material.appbar.AppBarLayout
android:id="#+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.google.android.material.appbar.CollapsingToolbarLayout
android:id="#+id/cscollapse"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:contentScrim="?attr/colorPrimary"
app:layout_scrollFlags="scroll|enterAlways"
app:toolbarId="#+id/cstool">
<androidx.appcompat.widget.Toolbar
android:id="#+id/cstool"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:contentInsetStart="0dp"
app:contentInsetStartWithNavigation="0dp">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="#+id/csbck"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:backgroundTint="?attr/colorPrimary"
android:clickable="true"
android:contentDescription="#null"
android:focusable="true"
app:backgroundTint="?attr/colorPrimary"
app:borderWidth="0dp"
app:elevation="0dp"
app:fabSize="mini"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#drawable/back_button"
tools:ignore="TouchTargetSizeCheck" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.appcompat.widget.Toolbar>
</com.google.android.material.appbar.CollapsingToolbarLayout>
<com.google.android.material.tabs.TabLayout
android:id="#+id/cslisttab"
style="#style/CounterStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabGravity="fill"
app:tabMaxWidth="0dp"
app:tabMode="auto"
app:tabTextAppearance="#style/CounterStyle"
app:tabTextColor="?attr/UnitinfoName" />
</com.google.android.material.appbar.AppBarLayout>
<androidx.core.widget.NestedScrollView
android:id="#+id/cslistscroll"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
app:layout_behavior="com.google.android.material.appbar.AppBarLayout$ScrollingViewBehavior">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".CastleList">
<androidx.viewpager2.widget.ViewPager2
android:id="#+id/cslistpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ProgressBar
android:id="#+id/prog"
style="#style/Widget.AppCompat.ProgressBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/status"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="#string/load_process"
android:textColor="?attr/TextPrimary"
app:layout_constraintEnd_toEndOf="#+id/prog"
app:layout_constraintStart_toStartOf="#+id/prog"
app:layout_constraintTop_toBottomOf="#+id/prog" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.core.widget.NestedScrollView>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
This is layout of list screen activity, as you can see, I'm using AppBarLayout with ViewPager2.
<?xml version="1.0" encoding="utf-8"?>
<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="match_parent"
android:layout_height="match_parent">
<ListView
android:id="#+id/entitylist"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fastScrollAlwaysVisible="false"
android:fastScrollEnabled="true"
android:nestedScrollingEnabled="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
</ListView>
<TextView
android:id="#+id/entitynores"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/filter_nores"
android:textColor="?attr/TextPrimary"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
This is what layout ViewPager2 is using, it's just simple layout which contains one TextView and ListView. As you can see, fast scroll is activated for ListView, also with nest scrolling allowed because it won't scroll in view pager if I disable this.
val tab = findViewById<TabLayout>(R.id.cslisttab)
val pager = findViewById<ViewPager2>(R.id.cslistpager)
pager.adapter = CsListTab()
pager.offscreenPageLimit = getExistingCastle()
val keys = getExistingPack()
TabLayoutMediator(tab, pager) { t, position ->
val def = getString(R.string.pack_default) ?: "Default"
t.text = when(position) {
0 -> "$def - RC"
1 -> "$def - EC"
2 -> "$def - WC"
3 -> "$def - SC"
else -> StaticStore.getPackName(keys[position])
}
}.attach()
if(getExistingCastle() == 1) {
tab.visibility = View.GONE
val collapse = findViewById<CollapsingToolbarLayout>(R.id.cscollapse)
val param = collapse.layoutParams as AppBarLayout.LayoutParams
param.scrollFlags = 0
collapse.layoutParams = param
}
val bck = findViewById<FloatingActionButton>(R.id.csbck)
bck.setOnClickListener {
activity.finish()
}
Above is code in OnCreate method, it lacks some references, but I think you guys will be able to understand what I did, other things aren't that important.
inner class CsListTab : FragmentStateAdapter(fm, lc) {
private val keys = getExistingPack()
override fun getItemCount(): Int {
return keys.size
}
override fun createFragment(position: Int): Fragment {
return CsListPager.newInstance(keys[position])
}
}
This is FragmentStateAdapter I made. This one also contains some unexplained/unneeded methods/variables, but I think it can explain what I did.
class CsListPager : Fragment() {
companion object {
fun newInstance(pid: String) : CsListPager {
val cs = CsListPager()
val bundle = Bundle()
bundle.putString("pid", pid)
cs.arguments = bundle
return cs
}
}
private var pid = Identifier.DEF
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val c = context ?: return null
val view = inflater.inflate(R.layout.entity_list_pager, container, false)
pid = arguments?.getString("pid") ?: Identifier.DEF
val list = view.findViewById<ListView>(R.id.entitylist)
val nores = view.findViewById<TextView>(R.id.entitynores)
val p: PackData
var index = -1
if(pid.startsWith(Identifier.DEF)) {
val d = pid.split("-")
p = UserProfile.getPack(d[0])
index = if(d.size == 1)
0
else
d[1].toInt()
} else {
p = UserProfile.getPack(pid)
}
if(p is PackData.DefPack) {
nores.visibility = View.GONE
val csList = CastleList.defset().toList()[if(index == -1) 0 else index]
val names = ArrayList<String>()
val data = ArrayList<Identifier<CastleImg>>()
for(i in csList.list.indices) {
names.add(StaticStore.generateIdName(csList.list[i].id, c))
data.add(csList.list[i].id)
}
val adapter = ArrayAdapter(c, R.layout.list_layout_text, names.toTypedArray())
list.adapter = adapter
list.onItemClickListener = AdapterView.OnItemClickListener { _, _, posit, _ ->
if(SystemClock.elapsedRealtime() - StaticStore.cslistClick < StaticStore.INTERVAL)
return#OnItemClickListener
StaticStore.cslistClick = SystemClock.elapsedRealtime()
val intent = Intent(c, ImageViewer::class.java)
intent.putExtra("Data", JsonEncoder.encode(data[posit]).toString())
intent.putExtra("Img", ImageViewer.CASTLE)
c.startActivity(intent)
}
} else if(p is PackData.UserPack && p.castles.list.isNotEmpty()) {
nores.visibility = View.GONE
val csList = p.castles
val names = ArrayList<String>()
val data = ArrayList<Identifier<CastleImg>>()
for(i in csList.list.indices) {
names.add(StaticStore.generateIdName(csList.list[i].id, c))
data.add(csList.list[i].id)
}
val adapter = ArrayAdapter(c, R.layout.list_layout_text, names.toTypedArray())
list.adapter = adapter
list.onItemClickListener = AdapterView.OnItemClickListener { _, _, posit, _ ->
if(SystemClock.elapsedRealtime() - StaticStore.cslistClick < StaticStore.INTERVAL)
return#OnItemClickListener
StaticStore.cslistClick = SystemClock.elapsedRealtime()
val intent = Intent(c, ImageViewer::class.java)
intent.putExtra("Data", JsonEncoder.encode(data[posit]).toString())
intent.putExtra("Img", ImageViewer.CASTLE)
c.startActivity(intent)
}
}
return view
}
}
I don't think I need to post Fragment code too, but just in case I will post it.
I don't know why this is happening. I looked into issue tracker either, but couldn't find good answer from there. I don't know if this is my fault or android ViewPager2's bug
Is this because I coded ViewPager2 incorrectly?
If not, can this be bug of ViewPager2?
If yes for one of 1 or 2, is there any workaround?
Both my emulator and device is Android 11 (API 30), and I'm using Android Studio Canary (Picture below)
Below gif describes the problem I'm having currently
EDIT :
I don't know if this can be the hint of this problem, but I noticed that one screen can fast scroll. Fast scroll works only in landscape mode. When I try to fast scroll on portrait mode, this debug message is printed
D/AbsListView: in onLayout changed
But if I try to fast scroll in landscape, such debug message won't be printed, but this message got printed.
D/DecorView: semSetRoundedCorners: 5
In portrait mode, I can't see debug message above (which is from DecorView) while in landscape mode, I can't see debug message which is from AbsListView. The thing is that this screen has different layout file for each orientation. But it's just font size difference, I put same view with same ID in there, so each layout are actually identical each other.
I'm trying to find what difference each layout has for now, I will update question when I found one
<androidx.viewpager2.widget.ViewPager2
android:id="#+id/cslistpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
Okay, I finally found a workaround. I still don't know why this is happening, and I'm guessing that this is either my fault or bug of ViewPager2 itself. As you can see, layout_width and layout_height properties are set as match_parent. If any of these are set as match_parent, fast scroll won't work. Below is the way to fix fast scroll bug.
<androidx.viewpager2.widget.ViewPager2
android:id="#+id/cslistpager"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
Setting layout_width, and layout_height to 0dp fixed problem. I noticed that I must not use match_parent in ConstraintLayout (Even though IDE lets you). I could find this while finding difference between 0dp and match_parent. I still don't know if this is the real cause of problem, but for now, problem solved.
I am using tab layout with custom views.
The issues I am facing are the following -
1) The text is being cut off with small screen devices as you can see here -
small device -
big device -
2) the second issue I am facing is as you can see, the background does not go wrap content on "Chats" tabs. It does through wrap the "Market" tab title as it is longer. I need it to wrap quallity with short and long texts.
here is my xml -
<androidx.appcompat.widget.Toolbar
android:id="#+id/activity_dashboard_toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="none"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:titleTextColor="#color/transparent">
<de.hdodenhof.circleimageview.CircleImageView
android:layout_width="35dp"
android:layout_height="35dp"
android:src="#drawable/user_default_image"
app:civ_border_color="#color/black"
app:civ_border_width="1dp" />
</androidx.appcompat.widget.Toolbar>
<com.google.android.material.tabs.TabLayout
android:id="#+id/activity_dashboard_tablayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="#+id/activity_dashboard_toolbar"
app:tabIndicator="#null" />
<androidx.viewpager2.widget.ViewPager2
android:id="#+id/activity_dashboard_viewpager"
android:layout_width="match_parent"
android:layout_height="600dp"
app:layout_constraintTop_toBottomOf="#+id/activity_dashboard_tablayout" />
private fun initTabLayoutAndViewPager() {
setSupportActionBar(toolbar)
supportActionBar?.title = null
viewpager.adapter = DashboardViewPagerAdapter(this)
TabLayoutMediator(tabLayout, viewpager, TabLayoutMediator.TabConfigurationStrategy { _, _ -> }).attach()
val chatsView = View.inflate(this, R.layout.dashboard_activity_cusom_tab, null)
val callsView = View.inflate(this, R.layout.dashboard_activity_cusom_tab, null)
val walletView = View.inflate(this, R.layout.dashboard_activity_cusom_tab, null)
val marketView = View.inflate(this, R.layout.dashboard_activity_cusom_tab, null)
(chatsView as TextView).text = pageTitles[0]
(callsView as TextView).text = pageTitles[1]
(walletView as TextView).text = pageTitles[2]
(marketView as TextView).text = pageTitles[3]
chatTextView = chatsView
callsTextView = callsView
walletTextView = walletView
marketTextView = marketView
tabLayout.getTabAt(0)?.customView = chatTextView
tabLayout.getTabAt(1)?.customView = callsTextView
tabLayout.getTabAt(2)?.customView = walletTextView
tabLayout.getTabAt(3)?.customView = marketTextView
viewpager.registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback() {
override fun onPageSelected(position: Int) {
super.onPageSelected(position)
when(position) {
DashboardTabs.CHATS.type -> {
ViewPagerUtils.setSelectedTab(chatTextView, callsView, walletView, marketView)
}
DashboardTabs.CALLS.type -> {
ViewPagerUtils.setSelectedTab(callsView, chatTextView, walletView, marketView)
}
DashboardTabs.WALLET.type -> {
ViewPagerUtils.setSelectedTab(walletView, callsView, chatTextView, marketView)
}
DashboardTabs.MARKET.type -> {
ViewPagerUtils.setSelectedTab(marketView, callsView, walletView, chatTextView)
}
}
}
})
}
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="#font/roboto_medium"
android:paddingStart="5dp"
android:paddingEnd="5dp"
android:textStyle="bold"
android:textSize="18sp"
android:maxLines="1"
tools:text="Chats">
</TextView>
edit -
After resolving the issue with the answer provided here I am stuck a gravity issue -
how can I make all tabs in the center? tried both gravity and layout_gravity and none helped me.
make tab layout mode scrollable app:tabMode="scrollable" or app:tabMode="auto" like below
<com.google.android.material.tabs.TabLayout
android:id="#+id/activity_dashboard_tablayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabMode="scrollable"
app:layout_constraintTop_toBottomOf="#+id/activity_dashboard_toolbar"
app:tabIndicator="#null" />
I need to draw dynamic buttons inside a foreach loop that retrieve data from my anko sqlite, the foreach only enter once and breaks and only draw one button in my layout, what I doing wrong? my code is this:
fun loadZones (ctx: Context, update: String, view: View, layout: LinearLayout) {
val zonesParser = rowParser{idzone: Int, zone: String -> Pair(idzone, zone)}
for (it in ctx.database.use {
select("tableplan")
.distinct()
.column("idzone")
.column("zone")
.orderBy("zone")
.parseList(zonesParser)
}) {
val layoutParams = LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT)
val btnZone = layoutInflater.inflate(R.layout.zones_item, null) as MaterialButton
btnZone.text = it.second
btnZone.id = it.first
layout.addView(btnZone, layoutParams)
Log.e("PAIR", "FIN DEL CICLO")
continue
}
}
The data that retrieves from my query is this:
(2, LARRY)
(1, MADISON)
That's my activity, I need to draw the buttons in "lytZonesButtons" id
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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" tools:context=".TablePlanFragment">
<com.google.android.material.appbar.AppBarLayout android:layout_width="match_parent"
android:layout_height="wrap_content" android:elevation="2dp"
tools:targetApi="lollipop" app:liftOnScroll="true">
<androidx.appcompat.widget.Toolbar
android:id="#+id/toolbarTablePlan"
style="#style/com.madison.Toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:title="#string/table_title_module">
</androidx.appcompat.widget.Toolbar>
</com.google.android.material.appbar.AppBarLayout>
<LinearLayout
android:layout_marginTop="56dp"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:orientation="horizontal"
android:background="#color/orangeLighter"
android:gravity="center_vertical"
android:padding="5dp" android:id="#+id/lytZonesButtons" />
<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="112dp"
android:padding="5dp">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/rc_tableplan"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</androidx.core.widget.NestedScrollView>
</FrameLayout>
and that's my button template that I called "zones_item":
<?xml version="1.0" encoding="utf-8"?>
<com.google.android.material.button.MaterialButton
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" style="#style/com.madison.AppButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="#style/TextAppearance.MaterialComponents.Subtitle2"
tools:text="MADISON"
tools:targetApi="lollipop"
android:layout_margin="5dp"
/>
EDIT: I found the solution!
I don't now why my layout instance in the twice iteration of my loop throws NullPointerException but not shows in the log cat, my solution was put the loop code in onCreateView function, this is the code:
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val view = inflater.inflate(R.layout.activity_tableplan, container, false)
val iActivity = (activity as AppCompatActivity)
iActivity.setSupportActionBar(view.toolbarTablePlan)
iActivity.supportActionBar?.setDisplayShowTitleEnabled(true)
// view.rc_tableplan.setHasFixedSize(true)
// val gridLayoutManager = GridLayoutManager(context, 2, GridLayoutManager.HORIZONTAL, false)
// view.rc_tableplan.layoutManager = gridLayoutManager
val response = loadTablePlan(this.context!!, "no")
if (response.trim().toUpperCase() == "SUCCESS") {
val zonesParser = rowParser{idzone: Int, zone: String -> Pair(idzone, zone)}
for (zone in this.context!!.database.use {
select("tableplan")
.distinct()
.column("idzone")
.column("zone")
.orderBy("zone")
.parseList(zonesParser)
}) {
val layout:LinearLayout = view.lytZonesButtons
layout.let {
val btnZone = layoutInflater.inflate(R.layout.zones_item, layout, false) as MaterialButton
btnZone.text = zone.second
btnZone.id = zone.first
btnZone.requestLayout()
layout.addView(btnZone)
Log.e("PAIR", "FIN DEL CICLO")
}
}
}
return view
}
Thanks a lot for all people that tried help me, some admin can close my question please.
The hint is only one button is showing. Your trying to inflate the same view twice in the same spot.
You need to add an empty linearlayout in your xml. And in your loop change the buttonz..
var btnZone = findViewById(R.layout.btnZone)
button.text = "Pair"
btnZone.addView(button, layoutParams)
That's not the exact code (and probably not even the right syntax) but it shows you how you need to modify your loop.
Basicly you were attempting to inflate the same instance of the same view. When really your not inflating any views this way your just adding views.
Note
If you have a linearlayout in your xml when you add another button view to it it will add it below it. If you set the layout orientation to horizontal the button view then gets added beside the other one.
here's a link to an example.
Sorry I would make sure my code matched your code and variables with proper syntax but I am at work.
I am doing a really simple activity, but I can't get the tool bar to extend all the way across the screen. Here is my 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:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="#+id/linearLayout"
android:background="#drawable/backgroundgradient"
tools:context="com.webnation.text2email.DisplayTextActivity">
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:local="http://schemas.android.com/apk/res-auto"
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?attr/actionBarSize"
android:background="#color/colorPrimary"
local:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
local:popupTheme="#style/ThemeOverlay.AppCompat.Light" />
<ScrollView
android:id="#+id/widget54"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1"
>
<TextView
android:id="#+id/displayText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:padding="10dip"
android:text="#string/eula"
tools:context=".DisplayText" />
</ScrollView>
</LinearLayout>
My activity:
open class DisplayTextActivity : AppCompatActivity() {
private var nameOfFile = ""
lateinit var androidText: AndroidText
public override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.displaytext)
linearLayout.background = ContextCompat.getDrawable(this, Globals.getBackground(this))
val toolbar = findViewById(R.id.toolbar) as Toolbar
setSupportActionBar(toolbar)
val actionBar = supportActionBar
val preferences = PreferenceManager.getDefaultSharedPreferences(applicationContext)
val theme = preferences.getString(Globals.KEY_THEME, Globals.THEME_PRIMARY)
val iTheme = Globals.getTheme(theme)
val extras = intent.extras
if (extras != null) {
nameOfFile = extras.getString(Globals.keyFileName, Globals.FILE_EULA)
try {
androidText = AndroidText(nameOfFile, this#DisplayTextActivity) //Get the text of the eula from text file
displayText.text = Html.fromHtml(androidText.androidText)
} catch (e: Resources.NotFoundException) {
Timber.e(e, e.toString())
val dialog = WebNationAlertDialog(this#DisplayTextActivity)
dialog.setTitle("Error!")
dialog.setDialogText(resources.getString(R.string.resource_not_found))
dialog.setNeuText(resources.getString(R.string.ok)) {
dialog.dismiss()
setResult(Activity.RESULT_OK)
finish()
}
dialog.show()
}
Globals.setToolBarColor(iTheme, toolbar)
try {
assert(actionBar != null)
actionBar?.setDisplayHomeAsUpEnabled(true)
actionBar?.setHomeButtonEnabled(true)
actionBar?.setDisplayShowTitleEnabled(true)
var title = resources.getString(R.string.eulaTitle)
when (nameOfFile) {
Globals.FILE_EULA -> {
title = resources.getString(R.string.eulaTitle)
}
Globals.FILE_HELP -> {
title = resources.getString(R.string.fileHelpTitle)
}
}
actionBar?.setTitle(title)
} catch (ignored: Exception) {
Timber.e(ignored.toString())
}
} else {
setResult(Activity.RESULT_OK)
finish()
}
}
override fun onOptionsItemSelected(menuItem: MenuItem): Boolean {
if (menuItem.itemId == android.R.id.home) {
finish()
}
return super.onOptionsItemSelected(menuItem)
}
}
Here is what the screenshot looks like. I'm not sure what is wrong since I'm using a version of this layout in several other activities.
I suspect that you have padding within your android:background="#drawable/backgroundgradient" in your parent LinearLayout. Either remove that padding from the backgroundgradient or apply padding to other views but the toolbar. You have multiple options. Good Luck!