I have a problem with adding multiple popup menus to different buttons. However, I configured to add one popup menu to one setting button as you can see in the screenshot below but when I apply the same code to other setting buttons app crashes.
please tell me how to add multiple pop up menu for all different settings button.
https://i.stack.imgur.com/u5wjf.jpg
here's my java code
package com.techjapreet.shivshankarkiringtone;
import android.media.MediaPlayer;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.PopupMenu;
import android.widget.Toast;
public class ringtone_tab extends AppCompatActivity {
Button ringtonepopup;
Button ringtonepopup2;
Button ringtonepopup3;
Button ringtonepopup4;
Button ringtonepopup5;
Button ringtonepopup6;
Button clk1;
Button clk2;
Button clk3;
Button clk4;
Button clk5;
Button clk6;
MediaPlayer mediaPlayer;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_ringtone_tab);
final Button ringtonepopup = (Button) findViewById(R.id.btn_setting1);
ringtonepopup.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
PopupMenu popupMenu = new PopupMenu(ringtone_tab.this, ringtonepopup);
popupMenu.getMenuInflater().inflate(R.menu.popup_menu, popupMenu.getMenu());
popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
Toast.makeText(ringtone_tab.this, "" + item.getTitle(), Toast.LENGTH_SHORT).show();
return true;
}
});
popupMenu.show();
}
});
clk6 = (Button) findViewById(R.id.btn_play6);
clk5 = (Button) findViewById(R.id.btn_play5);
clk4 = (Button) findViewById(R.id.btn_play4);
clk3 = (Button) findViewById(R.id.btn_play3);
clk2 = (Button) findViewById(R.id.btn_play2);
clk1 = (Button) findViewById(R.id.btn_play1);
mediaPlayer = new MediaPlayer();
}
public void setBtn_play6(View v)
{
stopPlayer();
mediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.shiv_vandana);
mediaPlayer.start();
}
public void setBtn_play5(View v)
{
stopPlayer();
mediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.shiv_tandav_mantra);
mediaPlayer.start();
}
public void setBtn_play4(View v)
{
stopPlayer();
mediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.shiv_shiv_om);
mediaPlayer.start();
}
public void setBtn_play3(View v)
{
stopPlayer();
mediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.shiv_shiv);
mediaPlayer.start();
}
public void setBtn_play2(View v)
{
stopPlayer();
mediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.shiv_aaradhna);
mediaPlayer.start();
}
public void setBtn_play1(View v)
{
stopPlayer();
mediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.shiv_shankar);
mediaPlayer.start();
}
private void stopPlayer(){
if(mediaPlayer != null && mediaPlayer.isPlaying())
{mediaPlayer.stop();
mediaPlayer.reset();
mediaPlayer.release();
}
}
}
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"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RelativeLayout
android:id="#+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="80dp"
android:orientation="horizontal"
android:padding="5dp">
<Button
android:id="#+id/btn_play1"
android:layout_width="45dp"
android:layout_height="45dp"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_marginStart="5dp"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:layout_marginEnd="5dp"
android:layout_marginRight="5dp"
android:layout_marginBottom="5dp"
android:background="#drawable/ic_play"
android:focusable="false"
android:focusableInTouchMode="false"
android:onClick="setBtn_play1" />
<Button
android:id="#+id/btn_setting1"
android:onClick="setsetting1"
android:layout_width="45dp"
android:layout_height="45dp"
android:layout_alignParentTop="true"
android:layout_marginStart="5dp"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:layout_marginEnd="5dp"
android:layout_marginRight="5dp"
android:layout_marginBottom="5dp"
android:layout_toStartOf="#+id/btn_play1"
android:layout_toLeftOf="#+id/btn_play1"
android:background="#drawable/ic_setting"
android:focusable="false"
android:focusableInTouchMode="false"
android:src="#drawable/ic_setting" />
<ImageView
android:id="#+id/img_grid_item1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/txt_grid_item1"
android:layout_alignBottom="#+id/txt_grid_item1"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:contentDescription="#string/todo"
android:gravity="center_vertical"
android:src="#drawable/ic_logo"
android:visibility="visible" />
<TextView
android:id="#+id/txt_grid_item1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginStart="2dp"
android:layout_marginLeft="2dp"
android:layout_marginTop="4dp"
android:layout_marginEnd="4dp"
android:layout_marginRight="4dp"
android:layout_toStartOf="#+id/btn_setting1"
android:layout_toLeftOf="#+id/btn_setting1"
android:layout_toEndOf="#+id/img_grid_item1"
android:layout_toRightOf="#+id/img_grid_item1"
android:gravity="center_vertical"
android:text="#string/textview"
android:textColor="#color/colorPrimaryDark"
android:textSize="30sp"
android:textStyle="bold" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/RelativeLayout2"
android:layout_width="match_parent"
android:layout_height="80dp"
android:orientation="horizontal"
android:padding="5dp">
<Button
android:id="#+id/btn_play2"
android:layout_width="45dp"
android:layout_height="45dp"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_marginStart="5dp"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:layout_marginEnd="5dp"
android:layout_marginRight="5dp"
android:layout_marginBottom="5dp"
android:background="#drawable/ic_play"
android:focusable="false"
android:focusableInTouchMode="false"
android:onClick="setBtn_play2"/>
<Button
android:id="#+id/btn_setting2"
android:onClick="setsetting2"
android:layout_width="45dp"
android:layout_height="45dp"
android:layout_alignParentTop="true"
android:layout_marginStart="5dp"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:layout_marginEnd="5dp"
android:layout_marginRight="5dp"
android:layout_marginBottom="5dp"
android:layout_toStartOf="#+id/btn_play2"
android:layout_toLeftOf="#+id/btn_play2"
android:background="#drawable/ic_setting"
android:focusable="false"
android:focusableInTouchMode="false"
android:src="#drawable/ic_setting" />
<ImageView
android:id="#+id/img_grid_item2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/txt_grid_item2"
android:layout_alignBottom="#+id/txt_grid_item2"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:contentDescription="#string/todo1"
android:gravity="center_vertical"
android:src="#drawable/ic_logo"
android:visibility="visible" />
<TextView
android:id="#+id/txt_grid_item2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toStartOf="#+id/btn_setting2"
android:layout_toLeftOf="#+id/btn_setting2"
android:layout_toEndOf="#+id/img_grid_item2"
android:layout_toRightOf="#+id/img_grid_item2"
android:gravity="center_vertical"
android:text="#string/textview1"
android:textColor="#color/colorPrimaryDark"
android:textSize="30sp"
android:textStyle="bold" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/RelativeLayout3"
android:layout_width="match_parent"
android:layout_height="80dp"
android:orientation="horizontal"
android:padding="5dp">
<Button
android:id="#+id/btn_play3"
android:layout_width="45dp"
android:layout_height="45dp"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_marginStart="5dp"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:layout_marginEnd="5dp"
android:layout_marginRight="5dp"
android:layout_marginBottom="5dp"
android:background="#drawable/ic_play"
android:focusable="false"
android:focusableInTouchMode="false"
android:onClick="setBtn_play3"/>
<Button
android:id="#+id/btn_setting3"
android:onClick="setsetting3"
android:layout_width="45dp"
android:layout_height="45dp"
android:layout_alignParentTop="true"
android:layout_marginStart="5dp"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:layout_marginEnd="5dp"
android:layout_marginRight="5dp"
android:layout_marginBottom="5dp"
android:layout_toStartOf="#+id/btn_play3"
android:layout_toLeftOf="#+id/btn_play3"
android:background="#drawable/ic_setting"
android:focusable="false"
android:focusableInTouchMode="false"
android:src="#drawable/ic_setting" />
<ImageView
android:id="#+id/img_grid_item3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/txt_grid_item3"
android:layout_alignBottom="#+id/txt_grid_item3"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:contentDescription="#string/todo2"
android:gravity="center_vertical"
android:src="#drawable/ic_logo"
android:visibility="visible" />
<TextView
android:id="#+id/txt_grid_item3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toStartOf="#+id/btn_setting3"
android:layout_toLeftOf="#+id/btn_setting3"
android:layout_toEndOf="#+id/img_grid_item3"
android:layout_toRightOf="#+id/img_grid_item3"
android:gravity="center_vertical"
android:text="#string/textview2"
android:textColor="#color/colorPrimaryDark"
android:textSize="30sp"
android:textStyle="bold" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/RelativeLayout4"
android:layout_width="match_parent"
android:layout_height="80dp"
android:orientation="horizontal"
android:padding="5dp">
<Button
android:id="#+id/btn_play4"
android:layout_width="45dp"
android:layout_height="45dp"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_marginStart="5dp"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:layout_marginEnd="5dp"
android:layout_marginRight="5dp"
android:layout_marginBottom="5dp"
android:background="#drawable/ic_play"
android:focusable="false"
android:focusableInTouchMode="false"
android:onClick="setBtn_play4"/>
<Button
android:id="#+id/btn_setting4"
android:onClick="setsetting4"
android:layout_width="45dp"
android:layout_height="45dp"
android:layout_alignParentTop="true"
android:layout_margin="5dp"
android:layout_toStartOf="#+id/btn_play4"
android:layout_toLeftOf="#+id/btn_play4"
android:background="#drawable/ic_setting"
android:focusable="false"
android:focusableInTouchMode="false"
android:src="#drawable/ic_setting" />
<ImageView
android:id="#+id/img_grid_item4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/txt_grid_item4"
android:layout_alignBottom="#+id/txt_grid_item4"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:contentDescription="#string/todo3"
android:gravity="center_vertical"
android:src="#drawable/ic_logo"
android:visibility="visible" />
<TextView
android:id="#+id/txt_grid_item4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toStartOf="#+id/btn_setting4"
android:layout_toLeftOf="#+id/btn_setting4"
android:layout_toEndOf="#+id/img_grid_item4"
android:layout_toRightOf="#+id/img_grid_item4"
android:gravity="center_vertical"
android:text="#string/textview3"
android:textColor="#color/colorPrimaryDark"
android:textSize="30sp"
android:textStyle="bold" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/RelativeLayout5"
android:layout_width="match_parent"
android:layout_height="80dp"
android:orientation="horizontal"
android:padding="5dp">
<Button
android:id="#+id/btn_play5"
android:layout_width="45dp"
android:layout_height="45dp"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_marginStart="5dp"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:layout_marginEnd="5dp"
android:layout_marginRight="5dp"
android:layout_marginBottom="5dp"
android:background="#drawable/ic_play"
android:focusable="false"
android:focusableInTouchMode="false"
android:onClick="setBtn_play5"/>
<Button
android:id="#+id/btn_setting5"
android:onClick="setsetting5"
android:layout_width="45dp"
android:layout_height="45dp"
android:layout_alignParentTop="true"
android:layout_margin="5dp"
android:layout_toStartOf="#+id/btn_play5"
android:layout_toLeftOf="#+id/btn_play5"
android:background="#drawable/ic_setting"
android:focusable="false"
android:focusableInTouchMode="false"
android:src="#drawable/ic_setting" />
<ImageView
android:id="#+id/img_grid_item5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/txt_grid_item5"
android:layout_alignBottom="#+id/txt_grid_item5"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:contentDescription="#string/todo4"
android:gravity="center_vertical"
android:src="#drawable/ic_logo"
android:visibility="visible" />
<TextView
android:id="#+id/txt_grid_item5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toStartOf="#+id/btn_setting5"
android:layout_toLeftOf="#+id/btn_setting5"
android:layout_toEndOf="#+id/img_grid_item5"
android:layout_toRightOf="#+id/img_grid_item5"
android:gravity="center_vertical"
android:text="#string/textview4"
android:textColor="#color/colorPrimaryDark"
android:textSize="30sp"
android:textStyle="bold" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/RelativeLayout6"
android:layout_width="match_parent"
android:layout_height="80dp"
android:orientation="horizontal"
android:padding="5dp">
<Button
android:id="#+id/btn_play6"
android:layout_width="45dp"
android:layout_height="45dp"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_marginStart="5dp"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:layout_marginEnd="5dp"
android:layout_marginRight="5dp"
android:layout_marginBottom="5dp"
android:background="#drawable/ic_play"
android:focusable="false"
android:focusableInTouchMode="false"
android:onClick="setBtn_play6"/>
<Button
android:id="#+id/btn_setting6"
android:onClick="setsetting6"
android:layout_width="45dp"
android:layout_height="45dp"
android:layout_alignParentTop="true"
android:layout_margin="5dp"
android:layout_toStartOf="#+id/btn_play6"
android:layout_toLeftOf="#+id/btn_play6"
android:background="#drawable/ic_setting"
android:focusable="false"
android:focusableInTouchMode="false"
android:src="#drawable/ic_setting" />
<ImageView
android:id="#+id/img_grid_item6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/txt_grid_item6"
android:layout_alignBottom="#+id/txt_grid_item6"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:contentDescription="#string/todo5"
android:gravity="center_vertical"
android:src="#drawable/ic_logo"
android:visibility="visible" />
<TextView
android:id="#+id/txt_grid_item6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toStartOf="#+id/btn_setting6"
android:layout_toLeftOf="#+id/btn_setting6"
android:layout_toEndOf="#+id/img_grid_item6"
android:layout_toRightOf="#+id/img_grid_item6"
android:gravity="center_vertical"
android:text="#string/textview5"
android:textColor="#color/colorPrimaryDark"
android:textSize="30sp"
android:textStyle="bold" />
</RelativeLayout>
</LinearLayout>
Define one listener:
View.OnClickListener listener = new View.OnClickListener() {
#Override
public void onClick(View v) {
// place your code here
switch (v.getId()) {
case R.id.btn_play1:
PopupMenu popupMenu = new PopupMenu(ringtone_tab.this, v);
popupMenu.getMenuInflater().inflate(R.menu.popup_menu, popupMenu.getMenu());
popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
Toast.makeText(ringtone_tab.this, "" + item.getTitle(), Toast.LENGTH_SHORT).show();
return true;
}
});
popupMenu.show();
break;
case R.id.btn_play2:
// place your code here
break;
}
}
};
and apply this listener to all the buttons:
clk1.setOnClickListener(listener);
clk2.setOnClickListener(listener);
.................................
Related
I have to program a TicTacToe app:
This is the code of the activity_main.xml:
(It's a simple 3x3 field of TextViews on a black background)
<?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="#000000"
android:screenOrientation="portrait"
tools:context=".MainActivity">
<androidx.gridlayout.widget.GridLayout
android:layout_width="294dp"
android:layout_height="286dp"
android:background="#000000"
app:layout_constraintBottom_toTopOf="#+id/button"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView">
<TextView
android:id="#+id/tictactoe_center_left"
android:layout_width="97dp"
android:layout_height="wrap_content"
android:clickable="true"
android:focusable="true"
android:editable="false"
android:ems="10"
android:enabled="false"
android:onClick="click_center_left"
android:text="#string/game_playersymbol_x"
android:textAlignment="center"
android:textColor="#FFFFFF"
android:textSize="70sp"
android:textStyle="bold"
app:layout_column="0"
app:layout_row="1" />
<TextView
android:id="#+id/tictactoe_center_right"
android:layout_width="97dp"
android:layout_height="wrap_content"
android:clickable="true"
android:focusable="true"
android:editable="false"
android:ems="10"
android:enabled="false"
android:onClick="click_center_right"
android:text="#string/game_playersymbol_x"
android:textAlignment="center"
android:textColor="#FFFFFF"
android:textSize="70sp"
android:textStyle="bold"
app:layout_column="2"
app:layout_row="1" />
<TextView
android:id="#+id/tictactoe_center_center"
android:layout_width="97dp"
android:layout_height="wrap_content"
android:clickable="true"
android:focusable="true"
android:editable="false"
android:ems="10"
android:enabled="false"
android:onClick="click_center_center"
android:text="#string/game_playersymbol_x"
android:textAlignment="center"
android:textColor="#FFFFFF"
android:textSize="70sp"
android:textStyle="bold"
app:layout_column="1"
app:layout_row="1" />
<TextView
android:id="#+id/tictactoe_top_left"
android:layout_width="97dp"
android:layout_height="wrap_content"
android:clickable="true"
android:ems="10"
android:enabled="false"
android:focusable="true"
android:onClick="click_top_left"
android:textAlignment="center"
android:textColor="#FFFFFF"
android:textSize="70sp"
android:textStyle="bold"
app:layout_column="0"
app:layout_row="0"
tools:text="#string/game_playersymbol_x" />
<TextView
android:id="#+id/tictactoe_top_center"
android:layout_width="97dp"
android:layout_height="wrap_content"
android:clickable="true"
android:focusable="true"
android:editable="false"
android:ems="10"
android:enabled="false"
android:onClick="click_top_center"
android:text="#string/game_playersymbol_x"
android:textAlignment="center"
android:textColor="#FFFFFF"
android:textSize="70sp"
android:textStyle="bold" />
<TextView
android:id="#+id/tictactoe_top_right"
android:layout_width="97dp"
android:layout_height="wrap_content"
android:clickable="true"
android:focusable="true"
android:editable="false"
android:ems="10"
android:enabled="false"
android:onClick="click_top_right"
android:text="#string/game_playersymbol_x"
android:textAlignment="center"
android:textColor="#FFFFFF"
android:textSize="70sp"
android:textStyle="bold" />
<TextView
android:id="#+id/tictactoe_bottom_left"
android:layout_width="97dp"
android:layout_height="wrap_content"
android:clickable="true"
android:focusable="true"
android:editable="false"
android:ems="10"
android:enabled="false"
android:onClick="click_bottom_left"
android:text="#string/game_playersymbol_x"
android:textAlignment="center"
android:textColor="#FFFFFF"
android:textSize="70sp"
android:textStyle="bold"
app:layout_column="0"
app:layout_row="2" />
<TextView
android:id="#+id/tictactoe_bottom_right"
android:layout_width="97dp"
android:layout_height="wrap_content"
android:clickable="true"
android:focusable="true"
android:editable="false"
android:ems="10"
android:enabled="false"
android:onClick="click_bottom_right"
android:text="#string/game_playersymbol_x"
android:textAlignment="center"
android:textColor="#FFFFFF"
android:textSize="70sp"
android:textStyle="bold"
app:layout_column="2"
app:layout_row="2" />
<TextView
android:id="#+id/tictactoe_bottom_center"
android:layout_width="97dp"
android:layout_height="wrap_content"
android:clickable="true"
android:focusable="true"
android:editable="false"
android:ems="10"
android:enabled="false"
android:onClick="click_bottom_center"
android:text="#string/game_playersymbol_x"
android:textAlignment="center"
android:textColor="#FFFFFF"
android:textSize="70sp"
android:textStyle="bold"
app:layout_column="1"
app:layout_row="2" />
</androidx.gridlayout.widget.GridLayout>
<Button
android:id="#+id/button"
android:layout_width="213dp"
android:layout_height="61dp"
android:layout_marginBottom="56dp"
android:background="#drawable/roundbutton"
android:text="#string/menu_play_singleplayer"
android:textColor="#FFFFFF"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/app_name"
android:textColor="#979797"
android:textSize="50sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.497"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.066" />
</androidx.constraintlayout.widget.ConstraintLayout>
This is the code of the MainActivity.java:
package com.example.tictactoe;
import androidx.appcompat.app.AppCompatActivity;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private int counter = 1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getSupportActionBar().hide();
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
public void click_top_left(View v) { click_action((TextView)findViewById(R.id.tictactoe_top_left)); }
public void click_top_center(View v) { click_action((TextView)findViewById(R.id.tictactoe_top_center)); }
public void click_top_right(View v) { click_action((TextView)findViewById(R.id.tictactoe_top_right)); }
public void click_center_left(View v) { click_action((TextView)findViewById(R.id.tictactoe_center_left)); }
public void click_center_center(View v) { click_action((TextView)findViewById(R.id.tictactoe_center_center)); }
public void click_center_right(View v) { click_action((TextView)findViewById(R.id.tictactoe_center_right)); }
public void click_bottom_left(View v) { click_action((TextView)findViewById(R.id.tictactoe_bottom_left)); }
public void click_bottom_center(View v) { click_action((TextView)findViewById(R.id.tictactoe_bottom_center)); }
public void click_bottom_right(View v) { click_action((TextView)findViewById(R.id.tictactoe_bottom_right)); }
public void click_action(TextView tv) {
if ((counter%2)==0)
tv.setText("O");
else
tv.setText("X");
counter++;
}
}
I've set the android:clickable to true.
I've set a onclick listener.
I simply cannot find my mistake...
I would appreciate any kind of help!
You have set to all the TextViews:
android:enabled="false"
Why?
Change it to:
android:enabled="true"
first give an id to your textview then Define a Sample like Textview textview find it in java and use setOnClickListener() in onCreate method just like below.It will definitely works.
textveiw.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) { }});
In TextView1 text animation runs from right to left, and in TextView2 constantly changing text. The problem is that when I perform TextView2.setText ( "...") in the text animation TextView1 restarted. Is it possible to prevent the restart of the animation?
XML code TextView1:
<TextView
android:id="#+id/artistAlbum"
android:layout_width="wrap_content"
android:layout_height="24dp"
android:ellipsize="marquee"
android:focusable="true"
android:focusableInTouchMode="true"
android:marqueeRepeatLimit="marquee_forever"
android:paddingTop="2dp"
android:scrollHorizontally="true"
android:singleLine="true"
android:text="text text text text text text text"
android:textSize="14sp" />
Java code initialization TextView1:
final TextView textView1 = (TextView) rootView.findViewById(R.id.textView1);
textView1.setSelected(true);
XML code TextView2:
<TextView
android:id="#+id/seekSongDuration"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true"
android:clickable="true"
android:paddingBottom="12dp"
android:paddingEnd="2dp"
android:paddingStart="10dp"
android:paddingTop="12dp"
android:text="0%"
android:textSize="20sp" />
XML code:
<TextView
android:id="#+id/fullSongDuration"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:background="#color/background"
android:clickable="true"
android:paddingBottom="12dp"
android:paddingEnd="10dp"
android:paddingStart="2dp"
android:paddingTop="12dp"
android:text="3:15"
android:textSize="20sp" />
<org.adw.library.widgets.discreteseekbar.DiscreteSeekBar
android:id="#+id/seekDuration"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignTop="#id/seekSongDuration"
android:layout_toEndOf="#id/seekSongDuration"
android:layout_toStartOf="#id/fullSongDuration"
android:paddingBottom="10dp"
android:paddingTop="10dp"
app:dsb_trackHeight="3dp" />
<com.andexert.library.RippleView
android:id="#id/butPlayPause"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#id/seekSongDuration"
android:layout_centerHorizontal="true"
android:background="#color/background"
app:rv_alpha="100"
app:rv_centered="true"
app:rv_color="#android:color/white"
app:rv_framerate="15"
app:rv_rippleDuration="300">
<ImageView
android:id="#+id/imgPlayPause"
android:layout_width="60dp"
android:layout_height="60dp"
android:src="#drawable/ic_play_circle_outline_black_48dp" />
</com.andexert.library.RippleView>
<com.andexert.library.RippleView
android:id="#+id/butShufle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#id/seekSongDuration"
android:layout_alignTop="#id/butPlayPause"
android:background="#color/background"
app:rv_alpha="150"
app:rv_color="#android:color/white"
app:rv_framerate="15"
app:rv_rippleDuration="300">
<ImageView
android:id="#+id/imgShufle"
android:layout_width="50dp"
android:layout_height="60dp"
android:layout_centerInParent="true"
android:paddingLeft="8dp"
android:paddingRight="8dp"
android:src="#drawable/ic_shuffle_black_36dp" />
</com.andexert.library.RippleView>
<com.andexert.library.RippleView
android:id="#+id/butPreviosSong"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#id/seekSongDuration"
android:layout_alignTop="#id/butPlayPause"
android:layout_toEndOf="#id/butShufle"
android:layout_toStartOf="#id/butPlayPause"
android:background="#color/background"
app:rv_alpha="100"
app:rv_color="#android:color/white"
app:rv_framerate="20"
app:rv_rippleDuration="300"
app:rv_type="rectangle">
<ImageView
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_centerInParent="true"
android:src="#drawable/ic_fast_rewind_black_48dp" />
</com.andexert.library.RippleView>
<com.andexert.library.RippleView
android:id="#+id/butLooping"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#id/seekSongDuration"
android:layout_alignParentEnd="true"
android:layout_alignTop="#id/butPlayPause"
android:background="#color/background"
app:rv_alpha="150"
app:rv_color="#android:color/white"
app:rv_framerate="20"
app:rv_rippleDuration="300">
<ImageView
android:id="#+id/imgLoopReaped"
android:layout_width="50dp"
android:layout_height="60dp"
android:layout_centerInParent="true"
android:paddingLeft="7dp"
android:paddingRight="7dp"
android:src="#drawable/ic_repeat_black_36dp" />
</com.andexert.library.RippleView>
<com.andexert.library.RippleView
android:id="#+id/butNextSong"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#id/seekSongDuration"
android:layout_alignTop="#+id/butPlayPause"
android:layout_toEndOf="#id/butPlayPause"
android:layout_toStartOf="#id/butLooping"
android:background="#color/background"
app:rv_alpha="100"
app:rv_color="#android:color/white"
app:rv_framerate="20"
app:rv_rippleDuration="300"
app:rv_type="rectangle">
<ImageView
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_centerInParent="true"
android:src="#drawable/ic_fast_forward_black_48dp" />
</com.andexert.library.RippleView>
<RelativeLayout
android:id="#id/currentSongLayout"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_above="#+id/butPlayPause"
android:background="#color/backgroundCurrentTrack"
android:clickable="true"
android:focusable="true"
android:focusableInTouchMode="true">
<com.andexert.library.RippleView
android:id="#+id/butSettingCurrentSong"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentTop="true"
app:rv_alpha="100"
app:rv_centered="true"
app:rv_color="#android:color/black"
app:rv_framerate="15"
app:rv_rippleDuration="300">
<ImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:padding="8dp"
android:src="#drawable/ic_more_circle_vert_black_36dp" />
</com.andexert.library.RippleView>
<ImageView
android:id="#+id/iconCurrentPlaylist"
android:layout_width="50dp"
android:layout_height="50dp"
android:padding="5dp"
android:src="#drawable/ic_playlist_play_black_36dp" />
<TextView
android:id="#+id/txtNumberSongs"
android:layout_width="wrap_content"
android:layout_height="23dp"
android:layout_marginBottom="3dp"
android:layout_marginTop="3dp"
android:layout_toEndOf="#id/iconCurrentPlaylist"
android:background="#drawable/shape_ellipse"
android:paddingEnd="6dp"
android:paddingStart="6dp"
android:paddingTop="3dp"
android:text="1/1"
android:textAlignment="center"
android:textColor="#color/grayLite"
android:textSize="12sp" />
<TextView
android:id="#+id/songTitle"
android:layout_width="wrap_content"
android:layout_height="26dp"
android:layout_toEndOf="#id/txtNumberSongs"
android:layout_toStartOf="#id/butSettingCurrentSong"
android:ellipsize="marquee"
android:focusable="true"
android:focusableInTouchMode="true"
android:marqueeRepeatLimit="marquee_forever"
android:paddingLeft="5dp"
android:paddingTop="2dp"
android:scrollHorizontally="true"
android:singleLine="true"
android:text="Title"
android:textSize="18sp"
android:textStyle="bold" />
<TextView
android:id="#+id/artistAlbum"
android:layout_width="wrap_content"
android:layout_height="24dp"
android:layout_below="#id/songTitle"
android:layout_toEndOf="#id/iconCurrentPlaylist"
android:layout_toStartOf="#id/butSettingCurrentSong"
android:ellipsize="marquee"
android:focusable="true"
android:focusableInTouchMode="true"
android:marqueeRepeatLimit="marquee_forever"
android:paddingTop="2dp"
android:scrollHorizontally="true"
android:singleLine="true"
android:text="Artist - Album"
android:textSize="14sp" />
</RelativeLayout>
Java code:
private TextView songArtistAlbum;
private TextView txtSeek;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
...
songArtistAlbum = (TextView) rootView.findViewById(R.id.artistAlbum);
songArtistAlbum.setSelected(true);
txtSeek = (TextView) rootView.findViewById(R.id.seekSongDuration);
...
}
private DiscreteSeekBar.OnProgressChangeListener onDurationProgressChangeListener = new DiscreteSeekBar.OnProgressChangeListener() {
#Override
public void onProgressChanged(DiscreteSeekBar seekBar, int value, boolean fromUser) {
if (!fromUser) {
txtSeek.setText(Total.msecTo_MM_SS(seekBar.getProgress()));
}
}
#Override
public void onStartTrackingTouch(DiscreteSeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(DiscreteSeekBar seekBar) {
txtSeek.setText(Total.msecTo_MM_SS(seekBar.getProgress()));
}
};
Thanks in advance.
Try to wrap your TextView1 with LinearLayout.
Check Activity class TextView object and its assign id.
My EditText is at the bottom of screen, so when I'm tapping on it to edit it, the keyboard is hiding it. I want that when I tap on EditText, the keyboard should remain below it & should not hide it.
Here's the link to the screenshot showing EditText before tapping on it: http://imgur.com/cgme1HT
Here's the link to the screenshot after tapping on EditText (the EditText field is below the keyboard now): http://imgur.com/nloFrkn
Here's my SettingUpUserProfile.java file's code:
public class SettingUpUserProfile extends AppCompatActivity {
public static final int TAKE_PHOTO_REQUEST = 0;
public static final int PICK_PHOTO_REQUEST = 1;
private static final int RESULT_LOAD_IMG = 2;
String imgDecodableString;
protected ImageView userProfilePicture;
#Override
protected void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_setting_up_user_profile);
userProfilePicture = (ImageView) findViewById(R.id.userProfilePicture);
userProfilePicture.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
AlertDialog.Builder builder = new AlertDialog.Builder(SettingUpUserProfile.this);
builder.setTitle(null);
builder.setItems(R.array.pickImage_options, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int position) {
switch (position) {
case 0:
Intent intentCaptureFromCamera = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intentCaptureFromCamera, TAKE_PHOTO_REQUEST);
break;
case 1:
Intent chooseFromGalley = new Intent(Intent.ACTION_GET_CONTENT);
chooseFromGalley.setType("image/*");
startActivityForResult(chooseFromGalley, PICK_PHOTO_REQUEST);
break;
}
}
});
AlertDialog alertDialog = builder.create();
alertDialog.show();
}
});
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
try {
// When an Image is picked
Uri uri = data.getData();
try {
Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), uri);
// Log.d(TAG, String.valueOf(bitmap));
ImageView imageView = (ImageView) findViewById(R.id.userProfilePicture);
imageView.setImageBitmap(bitmap);
} catch (IOException e) {
e.printStackTrace();
}
} catch (Exception e) {
Toast.makeText(this, "Something went wrong", Toast.LENGTH_LONG)
.show();
}
}
}
Here's my activity_setting_up_user_profile.xml file's code:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/light_purple"
tools:context="com.abc.xyz.SettingUpUserProfile">
<TextView
android:id="#+id/settingUpUserProfileText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="40dp"
android:text="#string/settingUpUserProfileText1"
android:textColor="#color/white"
android:textStyle="bold"
android:textSize="30sp"
android:gravity="center_horizontal|center_vertical"/>
<ImageView
android:id="#+id/userProfilePicture"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_below="#+id/settingUpUserProfileText1"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_marginTop="100dp"
android:clickable="true"
android:src="#drawable/ic_face_white_48dp" />
<TextView
android:id="#+id/settingUpUserProfileText2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/userProfilePicture"
android:layout_marginTop="5dp"
android:text="#string/settingUpUserProfileText2"
android:textColor="#color/white"
android:textSize="15sp"
android:gravity="center_horizontal|center_vertical"/>
<EditText
android:id="#+id/userName"
android:background="#drawable/phone_number_edit_text_design"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/settingUpUserProfileText2"
android:layout_marginTop="80dp"
android:layout_marginLeft="10dp"
android:layout_marginStart="10dp"
android:layout_marginRight="10dp"
android:layout_marginEnd="10dp"
android:gravity="center_horizontal|center_vertical"
android:hint="#string/hint_userName"
android:textColor="#color/white"
android:textColorHint="#E0E0E0"
android:textCursorDrawable="#null"
android:inputType="textPersonName"/>
<Button
android:id="#+id/buttonAllSet"
android:background="#color/white"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/userName"
android:layout_marginTop="20dp"
android:text="#string/button_allSet"
android:textStyle="bold"
android:textColor="#color/light_purple"
android:layout_marginEnd="120dp"
android:layout_marginStart="120dp"
android:gravity="center_horizontal|center_vertical"/>
</RelativeLayout>
I really have no clue about what to do here!
Please let me know.
I'm new to StackOverflow, so please cooperate.
Thanks in advance.
I had the same issue where the softkeyboard was on top of the EditText views which were placed on the bottom of the screen. I was able to find a solution by adding a single line to my AndroidManifest.xml file's relevant activity.
Put layout inside a ScrollView.
android:windowSoftInputMode="adjustResize|stateHidden"
This is how the whole activity tag looks like:
<activity
android:name="com.my.MainActivity"
android:screenOrientation="portrait"
android:label="#string/title_activity_main"
android:windowSoftInputMode="adjustResize|stateHidden" >
</activity>
Here the most important value is the adjustResize. This will shift the whole UI up to give room for the softkeyboard.
Simply use a ScrollView as the parent view in your xml file.
Options-1
Try using android:windowSoftInputMode="adjustPan" in activity in AndroidManifest.xml
Options-2
Use ScrollView as parent in xml. Replace your xml with this:
<ScrollView 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="com.abc.xyz.SettingUpUserProfile" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/light_purple">
<TextView
android:id="#+id/settingUpUserProfileText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="40dp"
android:text="#string/settingUpUserProfileText1"
android:textColor="#color/white"
android:textStyle="bold"
android:textSize="30sp"
android:gravity="center_horizontal|center_vertical"/>
<ImageView
android:id="#+id/userProfilePicture"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_below="#+id/settingUpUserProfileText1"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_marginTop="100dp"
android:clickable="true"
android:src="#drawable/ic_face_white_48dp" />
<TextView
android:id="#+id/settingUpUserProfileText2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/userProfilePicture"
android:layout_marginTop="5dp"
android:text="#string/settingUpUserProfileText2"
android:textColor="#color/white"
android:textSize="15sp"
android:gravity="center_horizontal|center_vertical"/>
<EditText
android:id="#+id/userName"
android:background="#drawable/phone_number_edit_text_design"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/settingUpUserProfileText2"
android:layout_marginTop="80dp"
android:layout_marginLeft="10dp"
android:layout_marginStart="10dp"
android:layout_marginRight="10dp"
android:layout_marginEnd="10dp"
android:gravity="center_horizontal|center_vertical"
android:hint="#string/hint_userName"
android:textColor="#color/white"
android:textColorHint="#E0E0E0"
android:textCursorDrawable="#null"
android:inputType="textPersonName"/>
<Button
android:id="#+id/buttonAllSet"
android:background="#color/white"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/userName"
android:layout_marginTop="20dp"
android:text="#string/button_allSet"
android:textStyle="bold"
android:textColor="#color/light_purple"
android:layout_marginEnd="120dp"
android:layout_marginStart="120dp"
android:gravity="center_horizontal|center_vertical"/>
</RelativeLayout>
</ScrollView>
Thank you all for your answers.
I solved the problem by changing my activity_setting_up_user_profile.xml file's code to this:
<?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="match_parent"
android:orientation="vertical"
android:background="#color/light_purple"
tools:context="com.abc.xyz.SettingUpUserProfile" >
<ScrollView
android:id="#+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="#+id/settingUpUserProfileText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="40dp"
android:gravity="center_horizontal|center_vertical"
android:text="#string/settingUpUserProfileText1"
android:textColor="#color/white"
android:textSize="30sp"
android:textStyle="bold" />
<ImageView
android:id="#+id/userProfilePicture"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_below="#+id/settingUpUserProfileText1"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_marginTop="100dp"
android:clickable="true"
android:src="#drawable/ic_face_white_48dp" />
<TextView
android:id="#+id/settingUpUserProfileText2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/userProfilePicture"
android:layout_marginTop="5dp"
android:gravity="center_horizontal|center_vertical"
android:text="#string/settingUpUserProfileText2"
android:textColor="#color/white"
android:textSize="15sp" />
<EditText
android:id="#+id/userName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/settingUpUserProfileText2"
android:layout_marginEnd="10dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginStart="10dp"
android:layout_marginTop="80dp"
android:background="#drawable/phone_number_edit_text_design"
android:gravity="center_horizontal|center_vertical"
android:hint="#string/hint_userName"
android:inputType="textPersonName"
android:textColor="#color/white"
android:textColorHint="#E0E0E0"
android:textCursorDrawable="#null" />
<Button
android:id="#+id/buttonAllSet"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/userName"
android:layout_marginEnd="120dp"
android:layout_marginStart="120dp"
android:layout_marginTop="20dp"
android:background="#color/white"
android:gravity="center_horizontal|center_vertical"
android:text="#string/button_allSet"
android:textColor="#color/light_purple"
android:textStyle="bold" />
</RelativeLayout>
</ScrollView>
</LinearLayout>
This was COOL!
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/scrollView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
tools:context="com.abc.xyz.SettingUpUserProfile" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/light_purple" >
<TextView
android:id="#+id/settingUpUserProfileText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="40dp"
android:gravity="center_horizontal|center_vertical"
android:text="#string/settingUpUserProfileText1"
android:textColor="#color/white"
android:textSize="30sp"
android:textStyle="bold" />
<ImageView
android:id="#+id/userProfilePicture"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_below="#+id/settingUpUserProfileText1"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_marginTop="100dp"
android:clickable="true"
android:src="#drawable/ic_face_white_48dp" />
<TextView
android:id="#+id/settingUpUserProfileText2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/userProfilePicture"
android:layout_marginTop="5dp"
android:gravity="center_horizontal|center_vertical"
android:text="#string/settingUpUserProfileText2"
android:textColor="#color/white"
android:textSize="15sp" />
<EditText
android:id="#+id/userName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/settingUpUserProfileText2"
android:layout_marginEnd="10dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginStart="10dp"
android:layout_marginTop="80dp"
android:background="#drawable/phone_number_edit_text_design"
android:gravity="center_horizontal|center_vertical"
android:hint="#string/hint_userName"
android:inputType="textPersonName"
android:textColor="#color/white"
android:textColorHint="#E0E0E0"
android:textCursorDrawable="#null" />
<Button
android:id="#+id/buttonAllSet"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/userName"
android:layout_marginEnd="120dp"
android:layout_marginStart="120dp"
android:layout_marginTop="20dp"
android:background="#color/white"
android:gravity="center_horizontal|center_vertical"
android:text="#string/button_allSet"
android:textColor="#color/light_purple"
android:textStyle="bold" />
</RelativeLayout>
</LinearLayout>
</ScrollView>
I think because my ListView is set to layout_height="match_parent", the Buttons are not called onClick method.
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
if(convertView == null) {
Log.d(TAG_DEBUG,"0");
} else{
Button button1 = (Button) convertView.findViewById(R.id.button1);
button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.d(TAG_DEBUG, "TEST");
}
});
}
return super.getView(position, convertView, parent);
}
My layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:id="#+id/num_ord"
android:background="#drawable/fon_ord"
android:layout_gravity="center_horizontal"
android:layout_marginTop="10dp"
android:textColor="#FFFFFF"
android:focusable="false" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:id="#+id/title_ord"
android:textColor="#000000"
android:typeface="serif"
android:textStyle="normal|italic"
android:focusable="false" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Sum:"
android:id="#+id/textView2"
android:textColor="#FFFFFF"
android:background="#drawable/fon_ord"
android:focusable="false" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:id="#+id/sum_ord"
android:layout_gravity="left|center_horizontal"
android:layout_marginTop="-24dp"
android:textColor="#000000"
android:layout_marginLeft="64dp"
android:focusable="false" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Col:"
android:id="#+id/textView3"
android:textColor="#FFFFFF"
android:background="#drawable/fon_ord"
android:focusable="false" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:id="#+id/count_ord"
android:layout_gravity="left|center_horizontal"
android:layout_marginTop="-24dp"
android:textColor="#000000"
android:layout_marginLeft="106dp"
android:focusable="false" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="DD:"
android:id="#+id/textView4"
android:textColor="#ffffff"
android:background="#drawable/fon_ord"
android:focusable="false" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:id="#+id/date_ord"
android:layout_gravity="left|center_horizontal"
android:layout_marginTop="-24dp"
android:textColor="#000000"
android:layout_marginLeft="50dp"
android:focusable="false" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="127dp"
android:layout_height="36dp"
android:text="Del"
android:id="#+id/button1"
android:layout_marginTop="20dp"
android:focusable="false" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="127dp"
android:layout_height="36dp"
android:text="Ed"
android:id="#+id/button2"
android:layout_gravity="right"
android:layout_marginTop="-37dp"
android:layout_marginRight="4dp"
android:focusable="false" />
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/list_orders"
android:focusable="true" />
</LinearLayout>
When I press the buttons, nothing happens, why?
I nedd to click on items in list activity to do something, but its not working
i search for this problem and saw some answers about this such as :
android:focusable="false"
android:clickable="false"
android:focusableInTouchMode="false"
or
android:descendantFocusability="afterDescendants"
android:descendantFocusability="beforeDescendants"
android:descendantFocusability="blocksDescendants"
but those are not worked
this is my listActivity :
public class TrainListActivity extends SherlockListActivity
{
public static String varStart = "com.example.traininfo.startcity";
public static String varDestination = "com.example.traininfo.destinationcity";
private String start;
private String destination;
ArrayList<TrainType> trains;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_trainlist);
Log.i("SearchTrain", "try to get extras...");
start = getIntent().getExtras().getString(varStart);
destination = getIntent().getExtras().getString(varDestination);
TrainController tc = new TrainController(this);
trains = new ArrayList<TrainType>();
trains = tc.getTrainList(start, destination).getTrain();
Log.i("SearchTrain", "got the train list...");
TrainListAdapter adapter = new TrainListAdapter(this, trains);
Log.i("SearchTrain", "adapter initialized successfully!!");
setListAdapter(adapter);
}
#Override
protected void onListItemClick(ListView l, View v, int position, long id)
{
// I want to do something here :(
super.onListItemClick(l, v, position, id);
Log.i("trainList", "on click");
}
}
row xml file :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#27ae60"
android:paddingBottom="5dp"
android:focusable="true"
android:focusableInTouchMode="true"
android:clickable="true" >
<TextView
android:id="#+id/tvTrainName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:gravity="center"
android:text="#string/resultTrainName"
android:textColor="#android:color/white"
android:textSize="20sp"
android:focusable="false"
android:clickable="false"
android:focusableInTouchMode="false"
android:textStyle="bold" />
<TextView
android:id="#+id/tvStartLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="#id/tvTrainName"
android:gravity="right"
android:text="#string/resultTimeOut"
android:focusable="false"
android:clickable="false"
android:focusableInTouchMode="false"
android:textColor="#android:color/white"
android:textSize="15sp" />
<TextView
android:id="#+id/tvTimeOut"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/tvTrainName"
android:layout_marginRight="30sp"
android:layout_toLeftOf="#id/tvStartLabel"
android:text="#string/resultTimeOutEx"
android:focusable="false"
android:clickable="false"
android:focusableInTouchMode="false"
android:textColor="#android:color/white"
android:textSize="15sp" />
<TextView
android:id="#+id/tvFinishLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="#id/tvStartLabel"
android:gravity="right"
android:focusable="false"
android:clickable="false"
android:focusableInTouchMode="false"
android:text="#string/resultTimeIn"
android:textColor="#android:color/white"
android:textSize="15sp" />
<TextView
android:id="#+id/tvTimeIn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="#+id/tvTimeOut"
android:layout_below="#id/tvTimeOut"
android:text="#string/resultTimeInEx"
android:focusable="false"
android:clickable="false"
android:focusableInTouchMode="false"
android:textColor="#android:color/white"
android:textSize="15sp" />
<TextView
android:id="#+id/tvPrices"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="#id/tvTimeIn"
android:gravity="right"
android:focusable="false"
android:focusableInTouchMode="false"
android:clickable="false"
android:textColor="#android:color/white"
android:textSize="15sp" />
<ImageButton
android:id="#+id/btnMoreInfo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="23dp"
android:focusable="false"
android:clickable="false"
android:focusableInTouchMode="false"
android:background="#android:color/transparent"
android:contentDescription="#string/resultMoreInfo"
android:src="#drawable/action_info" />
and ListActivity xml File :
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:drawSelectorOnTop="false"
android:clickable="true"
android:descendantFocusability="afterDescendants" />
</LinearLayout>
please help me :(
Use normal activity and put a ListView in xml. Use inflator to use your custom row design. Just put the focusable=false for your imagebutton. It will allow you to click on the row and you can apply a separate click listener for your imagebutton to handle its click separately.
I faced the same problem not too long ago. It seems that the ImageButton takes focus regardless what you do. What I did was to replace the ImageButton with and ImageView and set the listener to it.
This is how your xml files should look like.
row xml file :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#27ae60"
android:paddingBottom="5dp" >
<TextView
android:id="#+id/tvTrainName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:gravity="center"
android:text="#string/resultTrainName"
android:textColor="#android:color/white"
android:textSize="20sp"
android:textStyle="bold" />
<TextView
android:id="#+id/tvStartLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="#id/tvTrainName"
android:gravity="right"
android:text="#string/resultTimeOut"
android:textColor="#android:color/white"
android:textSize="15sp" />
<TextView
android:id="#+id/tvTimeOut"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/tvTrainName"
android:layout_marginRight="30sp"
android:layout_toLeftOf="#id/tvStartLabel"
android:text="#string/resultTimeOutEx"
android:textColor="#android:color/white"
android:textSize="15sp" />
<TextView
android:id="#+id/tvFinishLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="#id/tvStartLabel"
android:gravity="right"
android:text="#string/resultTimeIn"
android:textColor="#android:color/white"
android:textSize="15sp" />
<TextView
android:id="#+id/tvTimeIn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="#+id/tvTimeOut"
android:layout_below="#id/tvTimeOut"
android:text="#string/resultTimeInEx"
android:textColor="#android:color/white"
android:textSize="15sp" />
<TextView
android:id="#+id/tvPrices"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="#id/tvTimeIn"
android:gravity="right"
android:textColor="#android:color/white"
android:textSize="15sp" />
<ImageView
android:id="#+id/btnMoreInfo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="23dp"
android:background="#android:color/transparent"
android:contentDescription="#string/resultMoreInfo"
android:src="#drawable/action_info" />
Activity xml.
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:drawSelectorOnTop="false"/>
</LinearLayout>