i am begginer into kotlin development and i am doing a simple TicTacToe game with kotlin and i want to check if the first three button texts are equal and at the same time one of them is not empty but when i use !button1.text.toString().isEmpty it doesn't work. don't know why. help me. here are my kotlin and xml codes
package com.example.tictac
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import android.widget.Toast
import kotlinx.android.synthetic.main.activity_tic_tac.*
class TicTacActivity : AppCompatActivity() {
var isFirstPlayer = true;
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_tic_tac)
init()
}
fun init() {
buttonClick(button1)
buttonClick(button2)
buttonClick(button3)
buttonClick(button4)
buttonClick(button5)
buttonClick(button6)
buttonClick(button7)
buttonClick(button8)
buttonClick(button9)
}
fun buttonClick(button: Button) {
button.setOnClickListener() {
if (isFirstPlayer) {
button.text = "X"
isFirstPlayer = false
} else {
button.text = "O"
isFirstPlayer = true
}
button.isClickable = false
}
if (!button1.text.toString().isEmpty() &&
button1.text.toString().equals(button2.text.toString()) &&
button2.text.toString().equals(button3.text.toString())) {
winner(button1)
}
}
fun winner(button: Button) {
Toast.makeText(this, button.text.toString(), Toast.LENGTH_LONG).show()
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".TicTacActivity"
android:orientation="vertical"
>
<LinearLayout android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1">
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:textSize="49sp"
/>
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:textSize="49sp"
/>
<Button
android:id="#+id/button3"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:textSize="49sp"/>
</LinearLayout>
<LinearLayout android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1">
<Button android:id="#+id/button4"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:textSize="49sp"/>
<Button android:id="#+id/button5"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:textSize="49sp"/>
<Button android:id="#+id/button6"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:textSize="49sp"/>
</LinearLayout>
<LinearLayout android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1">
<Button android:id="#+id/button7"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:textSize="49sp"/>
<Button android:id="#+id/button8"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:textSize="49sp"/>
<Button android:id="#+id/button9"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:textSize="49sp"/>
</LinearLayout>
</LinearLayout>
You must include the code that you think it does not work inside button.setOnClickListener so it is executed every time you click on a button.
Of course you have to add more code to check other cases.
fun buttonClick(button: Button) {
button.setOnClickListener() {
if (isFirstPlayer) {
button.text = "X"
isFirstPlayer = false
} else {
button.text = "O"
isFirstPlayer = true
}
button.isClickable = false
if (!button1.text.toString().isEmpty() &&
button1.text.toString().equals(button2.text.toString()) &&
button2.text.toString().equals(button3.text.toString())) {
winner(button1)
}
}
}
Related
Problem:
Video is not playing in the custom_playback_control.xml like image1
instead it is playing in a separate black screen like image2
Iam using exoplayer 2.16.1 as my dependency in build.gradle
I want to play the video with the playback_control.
This is my videoplayer.java
import androidx.appcompat.app.AppCompatActivity;
import android.net.Uri;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;
import com.google.android.exoplayer2.C;
import com.google.android.exoplayer2.ExoPlaybackException;
import com.google.android.exoplayer2.Player;
import com.google.android.exoplayer2.source.ConcatenatingMediaSource;
import com.google.android.exoplayer2.source.MediaSource;
import com.google.android.exoplayer2.source.ProgressiveMediaSource;
import com.google.android.exoplayer2.util.Util;
import com.google.android.exoplayer2.SimpleExoPlayer;
import com.google.android.exoplayer2.ui.PlayerView;
import com.google.android.exoplayer2.upstream.DefaultDataSourceFactory;
import java.io.File;
import java.util.ArrayList;
public class videoplayer extends AppCompatActivity {
PlayerView playerView;
SimpleExoPlayer player;
int position;
String video_title;
ArrayList<MediaFiles> videosFiles = new ArrayList<>();
ConcatenatingMediaSource concatenatingMediaSource;
TextView title;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_videoplayer);
playerView = findViewById(R.id.exoplayer_view);
getSupportActionBar().hide();
position = getIntent().getIntExtra("Position", 1);
video_title = getIntent().getStringExtra("Title");
videosFiles = getIntent().getExtras().getParcelableArrayList("videoArrayList");
title = findViewById(R.id.video_title);
title.setText(video_title);
playVideo();
}
private void playVideo() {
String path = videosFiles.get(position).getPath();
Uri uri = Uri.parse(path);
player = new SimpleExoPlayer.Builder(this).build();
DefaultDataSourceFactory dataSourceFactory = new DefaultDataSourceFactory(this, Util.getUserAgent(this, "app_name"));
concatenatingMediaSource = new ConcatenatingMediaSource();
for (int i = 0; i < videosFiles.size(); i++) {
new File(String.valueOf(videosFiles.get(i)));
MediaSource mediaSource = new ProgressiveMediaSource.Factory(dataSourceFactory)
.createMediaSource(Uri.parse(String.valueOf(uri)));
concatenatingMediaSource.addMediaSource(mediaSource);
}
playerView.setPlayer(player);
playerView.setKeepScreenOn(true);
player.prepare(concatenatingMediaSource);
player.seekTo(position, C.TIME_UNSET);
playError();
}
private void playError() {
player.addListener(new Player.EventListener() {
public void onPlayerError(ExoPlaybackException error) {
Toast.makeText(videoplayer.this, "Error in Playing Video", Toast.LENGTH_SHORT).show();
}
});
player.setPlayWhenReady(true);
}
}
This is my activity_videoplayer.xml
<RelativeLayout 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=".videoplayer">
<com.google.android.exoplayer2.ui.PlayerView
android:id="#+id/exoplayer_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/black"
app:controller_layout_id="#layout/custom_playback_control"
app:hide_on_touch="true"
app:player_layout_id="#layout/exo_player_view"
app:resize_mode="fit"
app:show_timeout="40000"
app:shutter_background_color="#color/black"
app:surface_type="surface_view"
app:use_controller="true" />
</RelativeLayout>
This is my custom_playback_control.xml
<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="match_parent"
android:background="#color/black"
android:orientation="vertical">
<RelativeLayout
android:id="#+id/root_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginTop="10dp"
android:gravity="center_vertical"
android:orientation="horizontal"
android:visibility="visible">
<ImageView
android:id="#+id/video_back"
android:layout_width="60dp"
android:layout_height="60dp"
android:padding="10dp"
android:src="#drawable/ic_back_button" />
<TextView
android:id="#+id/video_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ellipsize="end"
android:maxLines="2"
android:text="File Name"
android:textColor="#color/white"
android:textSize="20sp" />
<ImageView
android:id="#+id/video_playlist"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_gravity="center_vertical"
android:src="#drawable/ic_baseline_playlist_play_24" />
<ImageView
android:id="#+id/video_more"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_gravity="center_vertical"
android:src="#drawable/ic_menu_vertical" />
</LinearLayout>
<RelativeLayout
android:id="#+id/progress"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="#id/bottom_icons"
android:visibility="visible">
<TextView
android:id="#+id/exo_position"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_centerVertical="true"
android:layout_marginStart="10dp"
android:text="00:00"
android:textColor="#color/white" />
<TextView
android:id="#+id/exo_duration"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_centerVertical="true"
android:layout_marginEnd="10dp"
android:text="23:34"
android:textColor="#color/white" />
<com.google.android.exoplayer2.ui.DefaultTimeBar
android:id="#+id/exo_progress"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toStartOf="#id/exo_duration"
android:layout_toLeftOf="#id/exo_duration"
android:layout_toEndOf="#id/exo_position"
android:layout_toRightOf="#id/exo_position"
app:scrubber_drawable="#drawable/scrubber"
app:played_color="#color/purple_200"/>
</RelativeLayout>
<LinearLayout
android:id="#+id/bottom_icons"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="20dp"
android:gravity="center_vertical"
android:orientation="horizontal"
android:weightSum="7">
<ImageView
android:id="#+id/unlock"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_weight="1"
android:src="#drawable/ic_lock_close" />
<ImageView
android:id="#+id/exo_prev"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_weight="1"
android:src="#drawable/ic_rewind_button" />
<ImageView
android:id="#+id/exo_rew"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_weight="1"
android:src="#drawable/ic_replay_10" />
<ImageView
android:id="#+id/exo_play"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_weight="1"
android:src="#drawable/ic_play_icon" />
<ImageView
android:id="#+id/exo_pause"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_weight="1"
android:src="#drawable/ic_pause_button" />
<ImageView
android:id="#+id/exo_ffwd"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_weight="1"
android:src="#drawable/ic_forward_10" />
<ImageView
android:id="#+id/exo_next"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_weight="1"
android:src="#drawable/ic_fast_forward_round" />
<ImageView
android:id="#+id/exo_more"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_weight="1"
android:src="#drawable/ic_menu_vertical" />
</LinearLayout>
</RelativeLayout>
</RelativeLayout>
I've got a bunch of onclicklisteners just like this, but this one just doesn't want to work for some reason, I'm sure I'm missing something obvious lol. All the button tryna do is logout.
I know at this point that it's not that the startActivity thats not working, rather the whole listener, as I added the Toast make text alert that isn't set off when I tap the logout button.
I've also tried "binding = FragmentProfileBinding.inflate(layoutInflater)" with "binding!!.btnLogout.setOnClickListener" but the onClickListener still isn't working or activating the make text alert when I tap logout.
But yeah any help would be greatly appreciated.
Here is my kotlin fragment code:
package com.example.nonutsinmybasket.fragments
import android.content.Intent
import android.content.SharedPreferences
import android.view.LayoutInflater
import android.view.ViewGroup
import android.os.Bundle
import android.view.View
import android.widget.Toast
import androidx.fragment.app.Fragment
import com.example.nonutsinmybasket.R
import com.example.nonutsinmybasket.activity.Login
import com.example.nonutsinmybasket.databinding.FragmentProfileBinding
import com.google.firebase.auth.FirebaseAuth
import kotlinx.android.synthetic.main.fragment_profile.view.*
class Profile(var userId: String?, var sharedPrefs: SharedPreferences) : Fragment() {
var binding: FragmentProfileBinding? = null
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
super.onCreate(savedInstanceState)
val view = inflater.inflate(R.layout.fragment_profile, container, false)
setupLogoutButton(view, sharedPrefs)
return view
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
// find views by id
}
private fun setupLogoutButton(view: View, sharedPrefs: SharedPreferences) {
view.btnLogout.setOnClickListener {
Toast.makeText(activity,
"Tap detected",
Toast.LENGTH_LONG).show()
sharedPrefs.edit().remove("Email").apply()
sharedPrefs.edit().remove("UserId").apply()
FirebaseAuth.getInstance().signOut()
startActivity(Intent(context, Login::class.java))
activity?.finish()
}
}
}
And here is my associated XML:
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/bg"
android:focusableInTouchMode="true"
tools:context=".fragments.Profile">
<LinearLayout
android:id="#+id/profileFooter"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginStart="#dimen/_10sdp"
android:layout_marginTop="#dimen/_10sdp"
android:layout_marginEnd="#dimen/_10sdp"
android:layout_marginBottom="#dimen/_10sdp"
android:background="#drawable/bg_rounded_ractangle_10"
android:backgroundTint="#color/colorPrimary"
android:orientation="vertical"
android:padding="#dimen/_10sdp"
app:layout_constraintBottom_toBottomOf="parent"
android:clickable="true">
<TextView
android:id="#+id/btnLogout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:fontFamily="#font/p_bold"
android:gravity="center"
android:text="Logout"
android:textColor="#color/white"
android:textSize="#dimen/_12sdp" />
</LinearLayout>
<LinearLayout
android:id="#+id/aboveFooter"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginStart="#dimen/_10sdp"
android:layout_marginTop="#dimen/_10sdp"
android:layout_marginEnd="#dimen/_10sdp"
android:layout_marginBottom="#dimen/_10sdp"
android:background="#drawable/bg_rounded_ractangle_10"
android:backgroundTint="#color/colorPrimary"
android:orientation="vertical"
android:padding="#dimen/_10sdp"
app:layout_constraintBottom_toTopOf="#id/profileFooter">
<TextView
android:id="#+id/updateInfo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:fontFamily="#font/p_bold"
android:gravity="center"
android:text="Update Information"
android:textColor="#color/white"
android:textSize="#dimen/_12sdp" />
</LinearLayout>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintTop_toTopOf="parent">
<LinearLayout
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:layout_marginLeft="#dimen/_5sdp"
android:layout_marginTop="#dimen/_5sdp"
android:layout_marginRight="#dimen/_5sdp"
android:layout_marginBottom="#dimen/_5sdp"
android:background="#drawable/bg_rounded_ractangle_10"
android:orientation="vertical"
android:padding="#dimen/_10sdp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<EditText
android:id="#+id/test"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#00ffffff"
android:fontFamily="#font/p_reguler"
android:hint=""
android:inputType="text"
android:textColor="#color/text1"
android:textColorHint="#color/text4"
android:textSize="#dimen/_10sdp"
android:visibility="gone" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/_10sdp"
android:layout_weight="1"
android:background="#drawable/bg_rounded_ractangle_5_stroke"
android:orientation="vertical"
android:padding="#dimen/_10sdp">
<EditText
android:id="#+id/etRegisterEmail"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#00ffffff"
android:fontFamily="#font/p_reguler"
android:hint="Email"
android:inputType="text"
android:maxLines="1"
android:textColor="#color/text1"
android:textColorHint="#color/text4"
android:textSize="#dimen/_11sdp" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/_10sdp"
android:orientation="horizontal">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#drawable/bg_rounded_ractangle_5_stroke"
android:orientation="horizontal"
android:padding="#dimen/_10sdp">
<EditText
android:id="#+id/etConfirmPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#00ffffff"
android:fontFamily="#font/p_reguler"
android:hint="Password"
android:inputType="textPassword"
android:maxLines="1"
android:textColor="#color/text1"
android:textColorHint="#color/text4"
android:textSize="#dimen/_11sdp" />
<ImageView
android:id="#+id/btnShowPass"
android:layout_width="#dimen/_25sdp"
android:layout_height="match_parent"
android:layout_gravity="center"
android:adjustViewBounds="true"
android:alpha=".5"
android:padding="#dimen/_2sdp"
android:src="#drawable/ic_eye" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/_10sdp"
android:orientation="horizontal">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#drawable/bg_rounded_ractangle_5_stroke"
android:orientation="horizontal"
android:padding="#dimen/_10sdp">
<EditText
android:id="#+id/confirmpassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#00ffffff"
android:fontFamily="#font/p_reguler"
android:hint="Confirm Password"
android:inputType="textPassword"
android:maxLines="1"
android:textColor="#color/text1"
android:textColorHint="#color/text4"
android:textSize="#dimen/_11sdp" />
<ImageView
android:id="#+id/btnShowConPass"
android:layout_width="#dimen/_25sdp"
android:layout_height="match_parent"
android:layout_gravity="center"
android:adjustViewBounds="true"
android:alpha=".5"
android:padding="#dimen/_2sdp"
android:src="#drawable/ic_eye" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>
</ScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>
Not sure if this helps but in when writing view.btnLogout I had to import "import kotlinx.android.synthetic.main.fragment_profile.view.*" otherwise the text was red.
onCreateView should only by used for view inflation. Any logic that operates on the Fragment's view should be written in onViewCreated, from onCreateView docs
It is recommended to only inflate the layout in this method and move
logic that operates on the returned View to onViewCreated(View,
Bundle).
So first thing to do in your case is move the setupLogoutButton(view, sharedPrefs) to onViewCreated, and second thing is I am not entirely sure that you are using the synthetics correctly, because view.btnLogout doesn't make much sense, usually with synthetics you specify the view id, I suggest you migrate to ViewBinding, or in this case simply use findViewById on Fragments's view as
val button = view.findViewById<TextView>(R.id.btnLogout)
button.setOnClickListener {
// your code here
}
Certified bruh moment - my scroll view was covering my buttons, knew it would be something like this
I'm adding view dynamically in fragment which works fine but when I'm navigating to another fragment and return back to first fragment all layout is set as initial one (dynamically added view is not present). So here I have to save UI state untill my fragment's parent activity is not closed.
Initial Fragment Class Code
package com.hp.billingapp.ui.billfragment;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.fragment.app.Fragment;
import androidx.navigation.Navigation;
import com.google.android.material.snackbar.Snackbar;
import com.google.android.material.textfield.TextInputEditText;
import com.hp.billingapp.R;
import com.hp.billingapp.ui.BillingActivity;
public class OrderFragment extends Fragment {
Button pay, buttonAdd;
LinearLayout itemLayout;
Integer item = 1, emptyChk;
Float total;
ConstraintLayout constraintLayout;
Integer layoutId;
public OrderFragment() {
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.fragment_order, container, false);
pay = root.findViewById(R.id.pay);
pay.setOnClickListener(v -> {
datafetch(root);
});
itemLayout = root.findViewById(R.id.itemLayout);
constraintLayout = root.findViewById(R.id.constraintLayout);
buttonAdd = root.findViewById(R.id.button);
buttonAdd.setOnClickListener(v -> addView());
return root;
}
private void datafetch(View root) {
Log.i("heetcount", String.valueOf(itemLayout.getChildCount()));
emptyChk = 0;
total = (float) 0;
for (int i = 0; i < itemLayout.getChildCount(); i++) {
View itemView = itemLayout.getChildAt(i);
TextView number = itemView.findViewById(R.id.number);
AutoCompleteTextView name = itemView.findViewById(R.id.namet);
TextInputEditText price = itemView.findViewById(R.id.pricet);
AutoCompleteTextView type = itemView.findViewById(R.id.typet);
if (name.getText().toString().isEmpty() || price.getText().toString().isEmpty() ||
type.getText().toString().isEmpty()) {
Snackbar.make(constraintLayout, "Please fill the required fields",
Snackbar.LENGTH_SHORT).show();
emptyChk += 1;
} else {
total = total + Float.parseFloat(price.getText().toString().trim());
}
}
if (emptyChk == 0) {
Log.i("heettotal", total.toString());
OrderFragmentDirections.ActionOrderFragmentToBillingFragment action =
OrderFragmentDirections.actionOrderFragmentToBillingFragment(total);
Navigation.findNavController(root).navigate(action);
((BillingActivity) getActivity()).heading("b");
}
}
private void addView() {
item = item + 1;
String text = "Item " + item;
View itemView = getLayoutInflater().inflate(R.layout.item_layout, null, false);
TextView number = itemView.findViewById(R.id.number);
ImageButton remove = itemView.findViewById(R.id.remove);
AutoCompleteTextView nametiet = itemView.findViewById(R.id.namet);
AutoCompleteTextView typetiet = itemView.findViewById(R.id.typet);
TextInputEditText pricetiet = itemView.findViewById(R.id.pricet);
number.setText(text);
remove.setOnClickListener(v -> removeView(itemView));
itemLayout.addView(itemView);
}
private void removeView(View itemView) {
Log.i("heetview", String.valueOf(itemView));
itemLayout.removeView(itemView);
}
}
Initial Fragment Layout Code
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/constraintLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/backdropcenter"
android:theme="#style/Theme.MaterialComponents.NoActionBar"
tools:context=".ui.billfragment.OrderFragment">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="80dp"
android:overScrollMode="never">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:id="#+id/itemLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<include layout="#layout/item_layout" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<Button
android:id="#+id/button"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="50dp"
android:background="#drawable/allcornerround"
android:fontFamily="#font/montserrat"
android:text="Add Item"
android:textAllCaps="false"
android:textSize="18sp"
android:textStyle="bold" />
<Button
android:id="#+id/pay"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="20dp"
android:background="#drawable/allcornerround"
android:fontFamily="#font/montserrat"
android:text="Pay"
android:textAllCaps="false"
android:textSize="18sp"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
</ScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>
Dynamically Added Layout Code
<?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"
android:theme="#style/Theme.MaterialComponents.NoActionBar">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:orientation="horizontal">
<TextView
android:id="#+id/number"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_weight="1"
android:text="Item 1"
android:textColor="#191b20"
android:textSize="18sp"
android:textStyle="bold" />
<ImageButton
android:id="#+id/remove"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="3"
android:background="#00000000"
android:src="#drawable/ic_close"
app:tint="#191b20" />
</LinearLayout>
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/nametil"
style="#style/InputDropDown"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginTop="10dp"
android:layout_marginEnd="10dp"
android:elevation="5dp"
android:hint="Item Name"
app:boxBackgroundMode="filled"
app:endIconMode="dropdown_menu"
app:endIconTint="#AA191b20"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/nametil">
<AutoCompleteTextView
android:id="#+id/namet"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="#font/montserrat"
android:gravity="center_vertical"
android:inputType="none"
android:textColor="#191b20"
android:textCursorDrawable="#drawable/cursor"
android:textSize="18sp" />
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/typetil"
style="#style/InputDropDown"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginTop="10dp"
android:layout_marginEnd="10dp"
android:elevation="5dp"
android:hint="Type"
app:boxBackgroundMode="filled"
app:endIconMode="dropdown_menu"
app:endIconTint="#AA191b20"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/nametil">
<AutoCompleteTextView
android:id="#+id/typet"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="#font/montserrat"
android:gravity="center_vertical"
android:inputType="none"
android:textColor="#191b20"
android:textCursorDrawable="#drawable/cursor"
android:textSize="18sp" />
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/pricetil"
style="#style/InputText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginTop="10dp"
android:layout_marginEnd="10dp"
android:elevation="5dp"
android:hint="Price"
app:boxBackgroundMode="filled"
app:endIconMode="clear_text"
app:endIconTint="#AA191b20">
<com.google.android.material.textfield.TextInputEditText
android:id="#+id/pricet"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fontFamily="#font/montserrat"
android:gravity="center_vertical"
android:inputType="numberDecimal"
android:textColor="#191b20"
android:textCursorDrawable="#drawable/cursor"
android:textSize="18sp" />
</com.google.android.material.textfield.TextInputLayout>
</LinearLayout>
Snaps of issue
Initial state when first fragment starts
Dynamically view added and information filled
First fragment after returning from another fragment
i try this:
val user = "false"
if (user == "true"){
navHead.visibility = View.VISIBLE
navLeft.inflateMenu(R.menu.nav_menu)
}else{
navHead.visibility = View.GONE
navLeft.setBackgroundColor(resources.getColor(R.color.colorGrey))
navLeft.itemTextColor = (ColorStateList.valueOf(resources.getColor(R.color.colorWhite)))
navLeft.inflateMenu(R.id.layout)
}
layout.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">
<LinearLayout
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<TextView
android:gravity="center"
android:layout_gravity="center"
android:id="#+id/textView3"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:text="DenemeDenemeDenemeDenemeDenemeDenemeDenemeDenemeDenemeDeneme" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
and error: Resource ID #0x7f090070 type #0x12 is not valid
if user is true i should do inflate menu but if user is false i should do menu visibility gone and include my layout.xml
i am a beginner in android programming.( i am using android studio for coding)
I am trying to design a dashboard for my android application taking guidance from the following link
and its working well, but i want to make it as per
Like This Image
i need 2 column layout with icon image, Title 1 and title 2 with a Background image.
can anyone help me.
thank you
you have to use customGridView using BaseAdapter. In customGridView show the ImageView with TextView for each list item.
main.xml
<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"
tools:context=".MainActivity" >
<GridView
android:id="#+id/gridViewCustom"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:columnWidth="80dp"
android:gravity="center"
android:numColumns="auto_fit"
android:stretchMode="columnWidth" />
</RelativeLayout>
grid_row.xml
<LinearLayout 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:orientation="vertical"
android:padding="5dp" >
<ImageView
android:id="#+id/imageView"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginRight="10dp"
android:src="#drawable/ic_launcher" >
</ImageView>
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:textSize="15sp" >
</TextView>
</LinearLayout>
CustomGridViewMainActivity.java
public class CustomGridViewMainActivity extends Activity
{
GridView gridView;
GridViewCustomAdapter grisViewCustomeAdapter;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
gridView=(GridView)findViewById(R.id.gridViewCustom);
// Create the Custom Adapter Object
grisViewCustomeAdapter = new GridViewCustomAdapter(this);
// Set the Adapter to GridView
gridView.setAdapter(grisViewCustomeAdapter);
// Handling touch/click Event on GridView Item
gridView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View v, int position, long arg3) {
String selectedItem;
if(position%2==0)
selectedItem="Facebook";
else
selectedItem="Twitter";
Toast.makeText(getApplicationContext(),"Selected Item: "+selectedItem, Toast.LENGTH_SHORT).show();
}
});
}
}
then set the adapter with your customized view
GridViewCustomAdapter.java
public class GridViewCustomAdapter extends ArrayAdapter
{
Context context;
public GridViewCustomAdapter(Context context)
{
super(context, 0);
this.context=context;
}
public int getCount()
{
return 24;
}
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
View row = convertView;
if (row == null)
{
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
row = inflater.inflate(R.layout.grid_row, parent, false);
TextView textViewTitle = (TextView) row.findViewById(R.id.textView);
ImageView imageViewIte = (ImageView) row.findViewById(R.id.imageView);
if(position%2==0)
{
textViewTitle.setText("Facebook");
imageViewIte.setImageResource(R.drawable.facebook);
}
else
{
textViewTitle.setText("Twitter");
imageViewIte.setImageResource(R.drawable.twitter);
}
}
return row;
}
}
Output:
At last, I found the solution...
your XML File`
<?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:background="#drawable/bg"
android:padding="16dp">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:orientation="horizontal">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#ededed"
android:drawableLeft="#drawable/ic1"
android:paddingBottom="5dp"
android:paddingLeft="16dp"
android:paddingRight="40dp"
android:paddingTop="8dp"
android:text="\nButton"
android:layout_marginBottom="5dp"
android:textColor="#000000" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginBottom="5dp"
android:background="#ededed"
android:drawableLeft="#drawable/ic1"
android:paddingBottom="5dp"
android:paddingLeft="5dp"
android:paddingRight="40dp"
android:paddingTop="8dp"
android:text="\nButton"
android:textColor="#000000" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:orientation="horizontal">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#ededed"
android:drawableLeft="#drawable/ic1"
android:paddingBottom="5dp"
android:paddingLeft="16dp"
android:paddingRight="40dp"
android:paddingTop="8dp"
android:text="\nButton"
android:layout_marginBottom="5dp"
android:textColor="#000000" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginBottom="5dp"
android:background="#ededed"
android:drawableLeft="#drawable/ic1"
android:paddingBottom="5dp"
android:paddingLeft="5dp"
android:paddingRight="40dp"
android:paddingTop="8dp"
android:text="\nButton"
android:textColor="#000000" />
</LinearLayout>
</LinearLayout>
`
then AndroidButtonWithIconAndText.java class
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
public class AndroidButtonWithIconAndText extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.android_button_with_icon_text);
}
}
and finally it may look like this
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/bg"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:layout_width="0dp"
android:layout_height="120dp"
android:layout_weight="1"
android:textColor="#000" />
<Button
android:layout_width="0dp"
android:layout_height="120dp"
android:layout_weight="1"
android:textColor="#fff" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:layout_width="0dp"
android:layout_height="120dp"
android:layout_weight="1"
android:textColor="#fff" />
<Button
android:layout_width="0dp"
android:layout_height="120dp"
android:layout_weight="1"
android:textColor="#000" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:layout_width="0dp"
android:layout_height="120dp"
android:layout_weight="1"
android:textColor="#000" />
<Button
android:layout_width="0dp"
android:layout_height="120dp"
android:layout_weight="1"
android:textColor="#fff" />
</LinearLayout>