all my views and non focusable and non clickable except the switch which is clickable but making it non clickable still doesn't make the list view fire
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/timed_events_list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:clickable="false"
android:descendantFocusability="blocksDescendants"
android:focusable="false"
android:focusableInTouchMode="false"
>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<TextView
android:id="#+id/event_name"
android:layout_width="wrap_content"
android:layout_height="25dp"
android:textSize="20sp"
android:textStyle="bold"
android:singleLine="true"
android:focusable="false"
android:focusableInTouchMode="false"
android:clickable="false"
/>
<TextView
android:id="#+id/event_time"
android:layout_width="wrap_content"
android:layout_height="26dp"
android:singleLine="true"
android:focusable="false"
android:focusableInTouchMode="false"
android:clickable="false"
/>
</LinearLayout>
<Switch
android:id="#+id/state"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:onClick="isActivated"
android:focusable="false"
android:focusableInTouchMode="false"
/>
</RelativeLayout>
below is the OnItemSelectedListener which is in the onCreate method
listView.setOnItemSelectedListener( new OnItemSelectedListener(){
#Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
Toast.makeText(ActivityContext, "its working", Toast.LENGTH_LONG).show();
Intent intent ;
if(listAdapter.getItemViewType(position) == 1){
intent = new Intent(ActivityContext,Volume.class);
Volume.currentPref((SoundDetails) timers.get(position),position);
} else{
intent = new Intent(ActivityContext,Message.class);
Message.currentMessage((MessageListDetails) timers.get(position),position);
}
startActivity(intent);
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}});
For ListView you need to set onItemClickListener instead of onItemSelectedListener
chanage like this..
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// perform your operation
}
});
Related
I have a nav fragment section called GroupbookFragment which uses a TabLayout with 3 Tabs.
(GroupbookKrippeFragment, GroupbookHafenFragment & GroupbookKindergartenFragment)
Each Tab has a CardView with a RecyclerView in it. The goal is to have a global "Add Child" Button on the GroupbookFragment level, which forward you to another activity with a form where you can create a new child. After filling data & clicking a save button, you'll come back to the GroupbookFragmentview and the new created data from the form will show up in the correct CardView within one of the three corresponding fragments.
So far I successfully implemented all functionalities when the methods and "Add child" button are already IN the corresponding tab child fragment (e.g. GroupbookKrippeFragment). The problem here is, whenever I change the tab from the tabLayout, the "Add Child" button is moving away aswell. So after moving all methods & the button into the parent fragment GroupbookFragment I can navigate through all pages and fill the form & save, but the recyclerView won't update and show the new created data in the child's recyclerView anymore?!
Thanks in advance for any help! Here is the code view:
GroupbookFragment.java
public class GroupbookFragment extends Fragment {
// initializing elements
RecyclerView recyclerView;
ChildListAdapter childListAdapter;
List<Child> children = new ArrayList<>();
RoomDb database;
FloatingActionButton addChildBtn;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_groupbook, container, false);
// inflate the layout for GB Krippe fragment
View viewKrippe = inflater.inflate(R.layout.fragment_groupbook_krippe, container, false);
// link recycler view to correct element
recyclerView = viewKrippe.findViewById(R.id.recycler_view_krippe);
// Tab layout initialization
TabLayout tabLayout = view.findViewById(R.id.tabLayout);
ViewPager2 viewPager = view.findViewById(R.id.viewPager_groupBook);
GroupbookAdapter adapterGroupBook = new GroupbookAdapter(getChildFragmentManager(), getLifecycle());
viewPager.setAdapter(adapterGroupBook);
// set 3 Tab titles
tabLayout.addTab(tabLayout.newTab().setText("Krippe"));
tabLayout.addTab(tabLayout.newTab().setText("Hafen"));
tabLayout.addTab(tabLayout.newTab().setText("Kindergarten"));
tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
#Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(TabLayout.Tab tab) {
}
#Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
viewPager.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
#Override
public void onPageSelected(int position) {
tabLayout.selectTab(tabLayout.getTabAt(position));
}
});
return view;
}
#Override
public void onViewCreated(#NonNull View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
// add Child code
database = RoomDb.getInstance(getContext());
children = database.mainDAO().getAll();
updateRecycler(children);
// link var to app element & set on click route
addChildBtn = view.findViewById(R.id.child_add_btn);
addChildBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(GroupbookFragment.this.getContext(), AddChildActivity.class);
startActivityForResult(intent, 101);
}
});
}
#Override
public void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 101) {
if (resultCode == Activity.RESULT_OK) {
Child new_child = (Child) data.getSerializableExtra("child");
database.mainDAO().insert(new_child);
children.clear();
children.addAll(database.mainDAO().getAll());
childListAdapter.notifyDataSetChanged();
}
}
}
void updateRecycler(List<Child> children) {
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(GroupbookFragment.this.getContext()));
childListAdapter = new ChildListAdapter(GroupbookFragment.this.getContext(), children, childClickListener);
recyclerView.setAdapter(childListAdapter);
}
private final ChildClickListener childClickListener = new ChildClickListener() {
#Override
public void onClick(Child child) {
}
#Override
public void onLongClick(Child child, CardView cardView) {
}
};
}
GroupbookFragment.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"
tools:context=".Fragments.HomeFragment">
<com.google.android.material.tabs.TabLayout
android:id="#+id/tabLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:tabIndicatorAnimationMode="elastic" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toBottomOf="#id/tabLayout">
<androidx.viewpager2.widget.ViewPager2
android:id="#+id/viewPager_groupBook"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true" />
<!-- Add floating button to add child-->
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="#+id/child_add_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_margin="#dimen/margin_cardView"
android:backgroundTint="#color/green"
android:src="#drawable/ic_add_child"
app:tint="#color/white" />
</RelativeLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
GroupbookKrippeFragment.xml
<?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=".Fragments.Groupbook.GroupbookKrippeFragment">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<androidx.cardview.widget.CardView
android:id="#+id/groupBook_krippe_1"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_margin="#dimen/margin_cardView"
app:cardCornerRadius="#dimen/cornerRadius_Card"
app:cardElevation="#dimen/elevation_default"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintBottom_toTopOf="#id/groupBook_krippe_2"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_weight="1">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/creme_500"
android:layout_weight="6"
android:padding="#dimen/padding_default">
<androidx.constraintlayout.widget.Guideline
android:id="#+id/guideline_group_1_start"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.1" />
<ImageView
android:id="#+id/ic_groupBook_krippe_1"
android:layout_width="#dimen/iconSize_groupBook_header"
android:layout_height="match_parent"
android:scaleType="fitCenter"
android:src="#drawable/ic_groupbook_krippe_1_logo"
app:tint="#color/white"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toStartOf="#id/guideline_group_1_start"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
/>
<TextView
android:id="#+id/title_groupBook_krippe_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="#dimen/margin_default"
android:text="Gruppe 1"
android:textAllCaps="true"
android:textSize="#dimen/textSize_groupBook_title"
android:textColor="#color/white"
android:fontFamily="#font/candy_beans"
app:layout_constrainedWidth="true"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toEndOf="#id/guideline_group_1_start"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toTopOf="#id/desc_groupBook_krippe_1"/>
<TextView
android:id="#+id/desc_groupBook_krippe_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="#dimen/margin_default"
android:text="Gesamt: 12 | Anwesend: 10"
android:textSize="#dimen/textSize_groupBook_subTitle"
android:textColor="#color/white"
android:fontFamily="#font/candy_beans"
app:layout_constrainedWidth="true"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toEndOf="#id/guideline_group_1_start"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="#id/title_groupBook_krippe_1"
app:layout_constraintBottom_toBottomOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:padding="#dimen/padding_default"
android:orientation="vertical">
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TableRow>
<TextView
android:text="ID"
android:textStyle="bold"
android:gravity="center"
android:layout_weight="0.5"/>
<TextView
android:text="Vorname"
android:textStyle="bold"
android:gravity="center"
android:layout_weight="0.5"/>
<TextView
android:text="Nachname"
android:textStyle="bold"
android:gravity="center"
android:layout_weight="0.5"/>
<TextView
android:text="Geburtstag"
android:textStyle="bold"
android:gravity="center"
android:layout_weight="0.5"/>
<TextView
android:text="Geburtsort"
android:textStyle="bold"
android:gravity="center"
android:layout_weight="0.5"/>
<TextView
android:text="Konfession"
android:textStyle="bold"
android:gravity="center"
android:layout_weight="0.5"/>
<TextView
android:text="Nationalität"
android:textStyle="bold"
android:gravity="center"
android:layout_weight="0.5"/>
</TableRow>
</TableLayout>
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recycler_view_krippe"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</androidx.recyclerview.widget.RecyclerView>
</LinearLayout>
</LinearLayout>
</androidx.cardview.widget.CardView>
</androidx.constraintlayout.widget.ConstraintLayout>
</RelativeLayout>
"I was trying to create Toast message when user clicks on Description TextView and Like ImageButton. But the list_item is not responding to touch Events "
"I went through many other people answering about changing focus.But none of them are working"
EventsListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
final EventsObject mEventsObject = mEventsAdapter.getItem(position);
final String mEventUrl = mEventsObject.geteLink();
Log.e(TAG, "Inside ListVIew");
final boolean status = mEventsObject.hasLiked();
//LikeButton likeButton = view.findViewById(R.id.heart_button);
TextView description = view.findViewById(R.id.eventDesc);
//final TextView likesCountTextView = view.findViewById(R.id.likesCount);
Toast.makeText(MainActivity.this, "Liked", Toast.LENGTH_SHORT).show();
description.setFocusable(false);
description.setFocusableInTouchMode(false);
description.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (!TextUtils.isEmpty(mEventUrl)) {
Intent openLinkInBrowser = new Intent(Intent.ACTION_VIEW);
openLinkInBrowser.setData(Uri.parse(mEventUrl));
startActivity(openLinkInBrowser);
} else {
Toast.makeText(MainActivity.this, "Links are not provided", Toast.LENGTH_SHORT).show();
}
}
});
Button loveBtn = view.findViewById(R.id.loveButton);
loveBtn.setFocusable(false);
loveBtn.setFocusableInTouchMode(false);
loveBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (status) {
Toast.makeText(MainActivity.this, "Liked", Toast.LENGTH_SHORT).show();
mEventsObject.setHeartLiked(true);
} else {
Toast.makeText(MainActivity.this, "Disliked", Toast.LENGTH_SHORT).show();
mEventsObject.setHeartLiked(false);
}
}
});
}
XML for list_item is
?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:layout_margin="8dp"
android:orientation="vertical"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:orientation="horizontal">
<TextView
android:id="#+id/organiser"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:paddingLeft="16dp"
android:textAllCaps="true"
android:textColor="#ffffff"
android:textSize="16sp"
android:textStyle="bold"
tools:text="Organiser" />
<TextView
android:id="#+id/dateOfEvent"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="right"
android:textColor="#ffffff"
android:textStyle="bold"
tools:text="12/03/20" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:orientation="horizontal">
<ImageView
android:id="#+id/organiserImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/events_circle"
android:padding="16dp"
android:src="#mipmap/ic_launcher"
android:textColor="#ffffff" />
<TextView
android:id="#+id/eventDesc"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="4dp"
android:gravity="fill"
android:textColor="#ffffff"
android:textSize="16sp"
tools:text="#string/test_event_desc" />
</LinearLayout>
<ImageButton
android:id="#+id/loveButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginBottom="8dp"
android:background="#drawable/events_love"
android:scaleType="center"
android:src="#drawable/love" />
<!--
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginTop="0dp">
<com.like.LikeButton
app:icon_type="heart"
app:icon_size="18dp"
android:id="#+id/heart_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
app:circle_start_color="#ff2134"
app:circle_end_color="#000000"
/>
-->
<!--<TextView
android:id="#+id/likesCount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
tools:text="10"
android:textColor="#ffffff"/>
</LinearLayout>-->
</LinearLayout>
"I would like to have list_item's responding to click events and showing toast message..
Please Help..
THanks in Advance !!"
It may be because you are trying to perform click inside item click listener of ListView.
You can fix it by creating a custom adapter for listview. Customer ListView Adapter
After creating this custom adapter you can get the reference of description and loveBtn and perform click operation on this.
Your adapter's getView() code will be like this-
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView==null) {
LayoutInflater layoutInflater = LayoutInflater.from(context);
convertView=layoutInflater.inflate(R.layout.list_row, null);
TextView description=convertView.findViewById(R.id.eventDesc);
Button loveBtn=convertView.findViewById(R.id.loveButton);
}
description.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (!TextUtils.isEmpty(mEventUrl)) {
Intent openLinkInBrowser = new Intent(Intent.ACTION_VIEW);
openLinkInBrowser.setData(Uri.parse(mEventUrl));
startActivity(openLinkInBrowser);
} else {
Toast.makeText(MainActivity.this, "Links are not provided", Toast.LENGTH_SHORT).show();
}
}
});
loveBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (status) {
Toast.makeText(MainActivity.this, "Liked", Toast.LENGTH_SHORT).show();
mEventsObject.setHeartLiked(true);
} else {
Toast.makeText(MainActivity.this, "Disliked", Toast.LENGTH_SHORT).show();
mEventsObject.setHeartLiked(false);
}
}
});
return convertView;
}
I have a listview in a constraintlayout.
when I try to do a long click on one of the elements of the list, to inflate the context menu, there are 2 problems:
1: It triggers the long click only sometimes, really random
2: After the long click was triggered the "normal" click is also triggered even though the onContextItemSelected returns true (to indicate the event was handled)
for some list elements, I want to have both the onClickListener and the long click for the contextmenu, on others only the contextmenu. (the listview is registered for contextmenu).
Heres the XML of the MainActivity
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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"
tools:context="it.netknights.piauthenticator.MainActivity"
tools:layout_editor_absoluteY="81dp"
tools:layout_editor_absoluteX="0dp">
<android.support.design.widget.FloatingActionButton
android:id="#+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
app:backgroundTint="#color/PIBLUE"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:srcCompat="#drawable/ic_add_white_24dp" />
<TextView
android:id="#+id/countdownfield"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginLeft="8dp"
android:layout_marginStart="8dp"
android:backgroundTint="#color/PIBLUE"
android:text="TextView"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent" />
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="#color/PIBLUE"
android:elevation="4dp"
android:minHeight="?attr/actionBarSize"
android:theme="?attr/actionBarTheme"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:popupTheme="#color/PIBLUE"
tools:layout_editor_absoluteY="-57dp" />
<ListView
android:id="#+id/listview"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toTopOf="#+id/countdownfield"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="#+id/toolbar"
app:layout_constraintVertical_bias="1.0" />
</android.support.constraint.ConstraintLayout>
and the XML of the listentry
<android.support.constraint.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:longClickable="true"
tools:layout_editor_absoluteY="81dp"
tools:layout_editor_absoluteX="0dp">
<TextView
android:id="#+id/textViewToken"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="124891"
android:textSize="28sp"
android:textStyle="bold"
tools:layout_editor_absoluteY="0dp"
android:layout_marginLeft="0dp"
app:layout_constraintLeft_toLeftOf="parent" />
<!-- android:clickable="false"
android:focusable="false"
android:focusableInTouchMode="false"-->
<TextView
android:id="#+id/textViewLabel"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="privacyidea something"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_marginTop="0dp"
app:layout_constraintTop_toBottomOf="#+id/textViewToken"
android:layout_marginLeft="0dp"
app:layout_constraintLeft_toLeftOf="parent" />
<ProgressBar
android:id="#+id/progressBar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="0dp"
android:layout_height="20dp"
android:layout_marginLeft="0dp"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginTop="0dp"
app:layout_constraintTop_toBottomOf="#+id/textViewLabel"
android:layout_marginRight="0dp"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintHorizontal_bias="0.517"
/>
</android.support.constraint.ConstraintLayout>
the part of the MainActivity.java
final ListView listview = (ListView) findViewById(R.id.listview);
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(view.getContext(), "SHORT itemclick", Toast.LENGTH_SHORT).show();
}
});
registerForContextMenu(listview);
and the getview method from my custom adapter:
#Override
public View getView(final int position, View v, ViewGroup parent) {
if (v == null) {
final LayoutInflater inflater = LayoutInflater.from(parent.getContext());
v = inflater.inflate(R.layout.tokenentry, parent, false);
}
v.setLongClickable(true);
v.setTag(position);
final ProgressBar progressBar = (ProgressBar) v.findViewById(R.id.progressBar);
final Token token = getItem(position);
final TextView tmp2 = (TextView) v.findViewById(R.id.textViewToken);
final TextView tmp1 = (TextView) v.findViewById(R.id.textViewLabel);
if (token.getType().equals(HOTP)) {
progressBar.setVisibility(GONE);
} else {
progressBar.setVisibility(VISIBLE);
//v.setClickable(false);
}
progressBar.setTag(position);
progressBar.setMax(token.getPeriod());
progressBar.getProgressDrawable().setColorFilter(
Color.rgb(0x83, 0xc9, 0x27), android.graphics.PorterDuff.Mode.SRC_IN);
token.setPb(progressBar);
tmp1.setText(token.getLabel());
tmp2.setText(token.getCurrentOTP());
EDIT: thank you for taking your time to post answers.
Ive solved the/my problem: the rootelement of the listentries was a contraintlayout and that doesn't seem to work properly with the scenarios i want, so i changed it so a relativelayout and it now it works perfectly!
hi Cxc the problem might be due to listView registered for both onClick and onLongClick . So instead of setting the onClickListener for the entire list view try setting the onClick listener to only the item view for which you need both onClickListener and long click.
You can set onClickListener to particular views inside getView() method based on their position or view type.
for example,
#Override
public View getView(final int position, View v, ViewGroup parent) {
if (v == null) {
final LayoutInflater inflater = LayoutInflater.from(parent.getContext());
v = inflater.inflate(R.layout.tokenentry, parent, false);
}
v.setLongClickable(true);
v.setTag(position);
if(position % 2 == 0)
{
v.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(v.getContext(), "SHORT itemclick",
Toast.LENGTH_SHORT).show();
}
});
hope this helps
I think you can use both onClick and onLongClick and override the onLongClick method making it returns true to avoid conflict of triggering onClick event
ex:
GridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
<do somthing here>
}
});
GridView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> adapterView, final View view, final int i, long l) {
<do something here>
return true;
}
});
I'm pretty new to Android Studio and I'm having difficulty starting a new activity. I've triple checked my code and I can't figure out what my problem is. I've also google searched for a couple of hours but nobody seems to have had the same problem as me. Logcat isn't reporting a problem, but when I run the app, and click the button, the app goes to a blank screen. Please help!
public class OriginalFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_original, container, false);
return rootView;
}
}
public class MainFragment extends Fragment {
private AlertDialog mDialog;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
//Sets up the about button
View aboutButton = rootView.findViewById(R.id.about_button);
aboutButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle(R.string.about_title);
builder.setMessage(R.string.about_text);
builder.setCancelable(false);
builder.setPositiveButton(R.string.ok_label,
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
}
});
mDialog=builder.show();
}
});
View originalButton = rootView.findViewById(R.id.original_button);
originalButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(getActivity(), OriginalActivity.class);
getActivity().startActivity(intent);
}
});
View pictureButton = rootView.findViewById(R.id.picture_button);
originalButton.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view){
Intent intent = new Intent(getActivity(), PictureActivity.class);
getActivity().startActivity(intent);
}
});
return rootView;
}
#Override
public void onPause(){
super.onPause();
if (mDialog != null)
mDialog.dismiss();
}
}
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
<FrameLayout
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:clipChildren= "false"
tools:context= "org.example.abstract_art.MainActivity">
<fragment android:id= "#+id/main_fragment"
class= "org.example.abstract_art.MainFragment"
android:layout_width= "wrap_content"
android:layout_height= "wrap_content"
android:layout_gravity= "center"
tools:layout= "#layout/fragment_main" />
</FrameLayout>
<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="#drawable/select_shapes_background"
tools:context="org.example.abstract_art.OriginalActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Select Shapes"
android:id="#+id/textView"
android:textSize="#dimen/menu_text_size"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<fragment
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:name="org.example.abstract_art.OriginalFragment"
android:id="#+id/fragment"
android:layout_below="#+id/textView"
android:layout_centerHorizontal="true"
tools:layout="#layout/fragment_original" />
</RelativeLayout>
<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:background="#drawable/menu_background"
android:elevation="#dimen/elevation_high"
android:orientation="vertical"
android:padding="#dimen/menu_padding"
tools:context=".org.example.abstract_art.MainFragment">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="#dimen/menu_space"
android:text="#string/long_app_name"
android:textAppearance="?android:textAppearanceLarge"
android:textSize="#dimen/menu_text_size"/>
<Button
android:id="#+id/original_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="#dimen/menu_button_margin"
android:padding="#dimen/menu_button_padding"
android:text="#string/original_label"/>
<Button
android:id="#+id/picture_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="#dimen/menu_button_margin"
android:padding="#dimen/menu_button_padding"
android:text="#string/picture_label"/>
<Button
android:id="#+id/about_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="#dimen/menu_button_margin"
android:padding="#dimen/menu_button_padding"
android:text="#string/about_label"/>
</LinearLayout>
<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="#drawable/menu_background"
android:orientation="vertical"
android:padding="#dimen/menu_padding"
tools:context="org.example.abstract_art.OriginalFragment">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Circles"
android:id="#+id/circlesText"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:textSize="#dimen/menu_text_size"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Rectangles"
android:id="#+id/rectanglesText"
android:layout_below="#+id/circlesText"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:textSize="#dimen/menu_text_size"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Squares"
android:id="#+id/squaresText"
android:layout_below="#+id/rectanglesText"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:textSize="#dimen/menu_text_size"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Triangles"
android:id="#+id/trianglesText"
android:layout_below="#+id/squaresText"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:textSize="#dimen/menu_text_size"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="4 Point Stars"
android:id="#+id/fourStarText"
android:layout_below="#+id/trianglesText"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:textSize="#dimen/menu_text_size"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="6 Point Stars"
android:id="#+id/sixStarText"
android:layout_below="#+id/fourStarText"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:textSize="#dimen/menu_text_size"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Create Art!"
android:id="#+id/createOriginalButton"
android:layout_below="#+id/sixStarText"
android:layout_centerHorizontal="true" />
</RelativeLayout>
Please Make sure Below Things
You have Define Main Activity, OriginalActivity,PictureActivity in your Manifest file
Please Make sure You also set Layout in all Activity or Fragment and Bind Components Of All Layout in that class
try below code
Button originalBtn; //Define class Level
originalBtn=(Button)rootView.findViewById(R.id.original_button);
Now you can set click Listener and code in it
Second main things that you define two times originalButton Click Listener so i think thats why you get problem
I think second Listener should be pictureButton Click Listener
I have a grid view with images in it. Once one is clicked it should go to PlayVideoActivity but onItemClick never get called.
I followed in the debugger and tried clicking the image but it never fired onItemClick.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_videos);
gridView = (GridView) findViewById(R.id.gridView);
customGridAdapter = new GridViewAdapter(this, R.layout.row_grid,
getData());
gridView.setAdapter(customGridAdapter);
gridView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> a, View v, int i, long l) {
Toast.makeText(videoChannelActivity.this, i + "#Selected",
Toast.LENGTH_SHORT).show();
Intent myIntent = new Intent(videoChannelActivity.this, PlayVideoActivity.class);
startActivity(myIntent);
}
});
}
row_grid.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginTop="5dp"
android:background="#drawable/grid_color_selector"
android:clickable="true"
android:focusable="true"
android:gravity="center"
android:orientation="vertical"
android:padding="5dp" >
<ImageView
android:id="#+id/image"
android:layout_width="280dp"
android:layout_height="158dp"
android:contentDescription="#string/desc"
android:scaleType="centerInside" />
<TextView
android:id="#+id/text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:gravity="center"
android:textSize="12sp" >
</TextView>
Add android:descendantFocusability="blocksDescendants" attribute in parent layout of row_grid.xml
You should remove
android:clickable="true"
android:focusable="true"