How to change a layout in same activity by clicking a Button? - java

Change a layout in same Activity when click on play button then layout show pause Button at the same place in Android Studio music player app.
How it is possible?
I'm a beginner so I don't know how to use method about it?
I cannot get an answer.

I understand from your question that when your user taps pause button, you should hide pause button and show play button and vice-versa. This is relatively simple to achieve. Just create a layout with a linear layout with two buttons like this:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_gravity="center_horizontal"
android:padding="5dp">
<Button
android:id="#+id/btnPlay"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginRight="5dp"
android:layout_marginBottom="2dp"
android:layout_marginLeft="5dp"
android:layout_marginTop="2dp"
android:layout_weight="1"
android:text="Play"
android:padding="5dp"/>
<Button
android:id="#+id/btnPause"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginBottom="2dp"
android:layout_marginTop="2dp"
android:layout_marginRight="5dp"
android:layout_weight="1"
android:text="Pause"/>
</LinearLayout>
Now set on click listner to both of the buttons and handle button visibility within that like this :
Button btnPlay = barcodeDialog.findViewById(R.id.btnPlay);
Button btnPause = barcodeDialog.findViewById(R.id.btnPause);
btnPlay.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
btnPlay.setVisibility(View.GONE);
btnPause.setVisibility(View.VISIBLE);
}
});
btnPause.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
btnPlay.setVisibility(View.VISIBLE);
btnPause.setVisibility(View.GONE);
}
});
This way you can handle the visibility of your Play and Pause buttons. Reply if you need more information.

Related

onClick method not working in android studio

I made a side navigation drawer and it works fine but whenever I click any option in navigation drawer nothing happens.It's like it is not taking any input. Onclicking any option I want to redirect user to a new activity but unfortunately it doesn't happens.
XML code is as follows
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center_vertical"
android:clickable="true"
android:onClick="ClickTournament_info"
android:focusable="true">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Tournament Info"
android:padding="12dp"
android:layout_marginStart="16dp"/>
<ImageView
android:layout_marginTop="-10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="48dp"
android:src="#drawable/tournament_info"/>
</LinearLayout>
Java code is as follows
public void ClickTournament_info(View view){
redirectActivity(this,TournamentInfo.class);
}
public static void redirectActivity(Activity activity,Class aClass) {
//Initialize intent
Intent intent = new Intent(activity,aClass);
//set flag
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//start activity
activity.startActivity(intent);
}
I think you need to use the onNavigationItemSelected method when you want to handle click events on the navigation drawer.
Learn more: https://developer.android.com/reference/com/google/android/material/navigation/NavigationView.OnNavigationItemSelectedListener#onNavigationItemSelected(android.view.MenuItem)
Firstly, try to always create an Onclicklistener instead of adding an onclick attribute in xml.
In your xml, add an ID attribute to your linear layout
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/linearlayout"
android:orientation="horizontal"
android:gravity="center_vertical"
android:clickable="true"
android:onClick="ClickTournament_info"
android:focusable="true">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Tournament Info"
android:padding="12dp"
android:layout_marginStart="16dp"/>
<ImageView
android:layout_marginTop="-10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="48dp"
android:src="#drawable/tournament_info"/>
</LinearLayout>
Now in your classfile inside the Oncreate Method,
LinearLayout LinearLayout = (LinearLayout )findViewById(R.id.linearlayout);
LinearLayout.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
redirectActivity(this,TournamentInfo.class);
}
});
You already have an onClick method. start navigating from that method only.
Try like this,

How can I add different ids to buttons recyclerview

My project is based on a recycler view. The layout of each elements is this :
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="4dp">
<ImageView
android:id="#+id/imgview"
android:layout_width="50dp"
android:layout_height="50dp"
android:padding="2dp" />
<TextView
android:id="#+id/txtview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="51dp"
android:layout_marginTop="1dp"
android:text="Line 1"
android:textColor="#android:color/black"
android:textSize="20sp"
android:textStyle="bold" />
<TextView
android:id="#+id/txtview2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="52dp"
android:layout_marginTop="29dp"
android:layout_marginBottom="1dp"
android:text="Line 2"
android:textSize="15sp" />
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_marginTop="3dp"
android:layout_marginEnd="3dp"
android:text="Voir plus" />
</RelativeLayout>
And I edit it with this :
ArrayList<exempleitem> ExampleList = new ArrayList<>();
ExampleList.add(new exempleitem(R.drawable.ic_android, "Line 1", "Line 2 ", button2.setId(R.id.hydro) ));
ExampleList.add(new exempleitem(R.drawable.ic_baseline_airplanemode_active_24, "Line 3", "Line 4 ",button3.setId(R.id.hel)));
ExampleList.add(new exempleitem(R.drawable.ic_baseline_beach_access_24, "Line 5", "Line 6 ",button4.setId(R.id.lit)));
My problem is that I would like to change the ID of each button, and it doesnt work.
I can't think of anything else right now, and I'm quite a begginner.
I think you don't know how a recyclerview works. It doesn't matter if a button have the same id as another as long as they live in different "cells". Thats the purpose of recyclerviews.
So you have to create a recyclerview adapter and put all the logic in there, the logic will be based on a dataset which is an array probably and your clicks will be based on the position of that array.
You can read more from Google's official guide
Hope it helps!
You shouldn't change the ids and if I am not mistaken, you are not even allowed to do that.
I read in the comments that you want to change the event of a button. Try encapsulating the actions of each button click into methods and then setOnClickListener again on the buttons to change their on click action.
Initial Code before changing action
view.findViewById(R.id.button_first).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
dosmthA();
}
});
To change action simply:
view.findViewById(R.id.button_first).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
dosmthB();
}
});
You can even create View.OnClickListener() objects and interchange them in the setOnClickListener() method like so:
View.OnClickListener action = new View.OnClickListener() {
#Override
public void onClick(View view) {
smth();
}
}
view.findViewById(R.id.button_first).setOnClickListener(action);
You can use a ViewHolder and RecyclerView Adapter to set Click listener for each view...
If you share me the adapter code snippet I can give you the code...
😁

Android button not clickable when turning visible

Hi Im posting this after not finding any possible answer here.
I have an invisible LinearLayout LL1 in a fragment that I turn visible when a recylerView data is empty, that LL1 has a button on it. The problem is simply that the button is not clickable ! I tried setting the listener in different ways but still not working. Here's the code below:
Here's the xml file:
<android.support.design.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".activity.main.MainActivity"
android:layout_marginTop="3dp">
<LinearLayout
android:id="#+id/msg_emty_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:visibility="gone"
android:orientation="vertical">
<ImageView
android:layout_width="100dp"
android:layout_height="100dp"
android:src="#drawable/ic_empty_box" />
<TextView
android:layout_marginTop="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Aucune vente n'a été trouvée !"
android:textSize="15dp"
android:layout_gravity="center_horizontal"
android:textColor="#color/greydark"/>
<Button
android:id="#+id/btnAddSale"
android:layout_marginTop="5dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Créer une vente"
android:textColor="#color/whiteTextColor"
android:background="#drawable/centre_button">
</Button>
</LinearLayout>
<android.support.v4.widget.SwipeRefreshLayout
android:id="#+id/swipe_refresh_db"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.RecyclerView
android:id="#+id/recycler_view_sale_list"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</android.support.v4.widget.SwipeRefreshLayout>
<android.support.design.widget.FloatingActionButton
android:id="#+id/fab_add_db"
android:layout_margin="20dp"
android:layout_gravity="bottom|end"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#color/colorAccesGreen"
android:src="#drawable/ic_edit"/>
</android.support.design.widget.CoordinatorLayout>
And here's the java:
private void generateSaleList(ArrayList<Sale> saleArrayList, View view) {
if(saleArrayList.isEmpty()){
getActivity().setTitle("Ventes sur portable");
msgLayout.setVisibility(View.VISIBLE);
creatSaleBtn = (Button) view.findViewById(R.id.btnAddSale);
creatSaleBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(getActivity(), "Button clicked !", Toast.LENGTH_LONG).show();
}
});
creatSaleBtn.setClickable(true);
}else{
msgLayout.setVisibility(View.GONE);
recyclerView_db = view.findViewById(R.id.recycler_view_sale_list);
adapter_db = new SaleAdapter((ArrayList<Sale>) Utils.sortArray(saleArrayList),this,1,getContext(),this);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getActivity());
recyclerView_db.setLayoutManager(layoutManager);
recyclerView_db.setAdapter(adapter_db);
adapter_db.notifyDataSetChanged();
}
}
Do you guys see what's causing this weird problem ?
You can try with requestFocusFromTouch()
Call this to try to give focus to a specific view or to one of its
descendants.
msgLayout.setVisibility(View.VISIBLE);
creatSaleBtn = (Button) view.findViewById(R.id.btnAddSale);
creatSaleBtn.requestFocusFromTouch();
Hide your RecyclerView when there's no items (set visibility to gone). The problem is that it's capturing the clicks, since it's placed on top of the button.
as u said in comments you not using creatSaleBtn.setClickable(false); anywhere, then you have to hide the recyclerView using recyclerView_db.setVisibility(View.GONE);
why? because it by default fits the full-screen due too match_parent for width and height.
please add recyclerView_db.setVisibility(View.GONE); in the saleArrayList.isEmpty() check
so your new code will be :
if(saleArrayList.isEmpty()){
recyclerView_db.setVisibility(View.GONE);
getActivity().setTitle("Ventes sur portable");
msgLayout.setVisibility(View.VISIBLE);
creatSaleBtn = (Button) view.findViewById(R.id.btnAddSale);
creatSaleBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(getActivity(), "Button clicked !", Toast.LENGTH_LONG).show();
}
});
creatSaleBtn.setClickable(true);
}
SwipeRefreshLayout's visibility should be GONE after recyclerview is empty.
of coarse recyclcerview is empty but it is still visible so your click events are caught by SwipeRefreshLayout witch is defined on top of LL1.
EDIT
instead of recyclerview, set swipe_refresh_db visibility to gone cos I doubt recyclerview is a child of swipe_refresh_dd so if you set recyclerview's visibility to gone, swipe_refresh_db is still visible and gets click events.
give it a try

I am trying display the pause icon while playing the sound and then display the play icon when the sound finish playing

here I'm trying to show the pause icon when media is playing and I want to automatically change to play icon after the media finish playing. The first time whenever I click on the play icon it would change to pause icon but it wouldn't change back to play icon after the media finish playing.
public class MainActivity extends AppCompatActivity {
ImageButton playBtn;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
playBtn = (ImageButton) findViewById(R.id.button1);
final MediaPlayer sound = MediaPlayer.create(this,R.raw.evumm);
playBtn.setBackgroundResource(R.drawable.playicon);
playBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (sound.isPlaying()) {
sound.pause();
playBtn.setBackgroundResource(R.drawable.playicon);
} else {
sound.start();
playBtn.setBackgroundResource(R.drawable.pauseicon);
}
}
});
}
}
XML code
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="develop.kokoson.playorstopapp.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Play or Pause !"
android:textSize="25dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="46dp"
android:id="#+id/textView" />
<ImageButton
android:id="#+id/button1"
android:layout_width="50dp"
android:layout_height="50dp"
android:background="#drawable/playicon"
android:layout_marginTop="81dp"
android:layout_below="#+id/textView"
android:layout_centerHorizontal="true" />
</RelativeLayout>
You could use the OnCompletionListener in MediaPlayer to do this. Just add a listener that sets the button icon back to the play icon when the sound is complete.
sound.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
playBtn.setBackgroundResource(R.drawable.playicon);
}
});

Convert Edittext to readonly and Make it editable on Button Press

i got this nw. Before clicking on the button, my EditText is just viewable:
After button press, it should convert to an editable field:
I want the EditText field line to disappear and become read only and on button click make it editable. But this just fades the line and also the text inside it loses it visual appearance.
Here's my code:
et1.setEnabled(false);
et1.setFocusable(true);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
et1.setEnabled(true);
et1.requestFocus();
}
});
Your XML should look like this,
<?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:gravity="center"
android:orientation="vertical">
<EditText
android:id="#+id/et1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName"
android:text="Name" />
<TextView
android:id="#+id/tv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
<Button
android:id="#+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
Now, add the following code to your activity class.
//init the views.
et1.setVisiblity(GONE);
tv1.setVisiblity(VISIBLE);
//if the textView is shown, the btn click will hide it and show editText
//instead, and same happens for editText
btn1.setOnClickListener(new View.OnClickListener(){
public void onClick(){
if(et1.getVisiblity() != VISIBLE) {
et1.setVisbility(VISIBLE);
tv1.setVisibility(GONE);
} else {
et1.setVisbility(GONE);
tv1.setVisibility(VISIBLE);
}
} });
P.S: This isn't executable code, although, hopefully it will get you the idea.

Categories

Resources