I want to open new activity on on-click for gridview - java

I am new to android. I have created material design Grid View. Now what I want to do, when I click on each grid view it should take to me each new activity. When i click on ALPHABETS it will go to ListAlphabet.java. Thank you for help.
My activity_main.xml is :
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/android_coordinator_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:id="#+id/appbar_layout"
android:layout_height="#dimen/app_bar_height"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.design.widget.CollapsingToolbarLayout
android:id="#+id/collapsing_toolbar_android_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:contentScrim="?attr/colorPrimary"
app:expandedTitleMarginStart="#dimen/expanded_toolbar_title_margin_start"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<ImageView
android:id="#+id/image_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerInside"
android:src="#drawable/code"
app:layout_collapseMode="parallax"
app:layout_collapseParallaxMultiplier="0.7" />
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_collapseMode="pin"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"
app:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar" />
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:id="#+id/nestedscrollview"
android:layout_height="match_parent"
android:fillViewport="true"
app:layout_behavior="#string/appbar_scrolling_view_behavior">
<LinearLayout
android:id="#+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<GridView
android:id="#+id/grid"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:columnWidth="100dp"
android:gravity="center"
android:listSelector="#00000000"
android:numColumns="auto_fit"
android:stretchMode="columnWidth" />
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
</android.support.design.widget.CoordinatorLayout>
gridview_custom_layout.xml is :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/android_gridview_custom_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
android:padding="10dp">
<com.andexert.library.RippleView
android:id="#+id/more"
rv_centered="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
app:rv_color="#fff"
app:rv_rippleDuration="200">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/linearLayout"
android:orientation="vertical">
<ImageView
android:id="#+id/gridview_image"
android:layout_width="80dp"
android:layout_height="80dp"
android:src="#mipmap/ic_launcher" />
<TextView
android:id="#+id/gridview_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/grid_image"
android:layout_marginTop="10dp"
android:gravity="center"
android:text="Grid View Item"
android:textColor="#444"
android:textSize="12sp"
android:textStyle="bold" />
</LinearLayout>
</com.andexert.library.RippleView>
</LinearLayout>
MainActivity.java is :
package com.affinityapp.sj.alphabet;
import android.content.Context;
import android.os.Bundle;
import android.support.design.widget.CollapsingToolbarLayout;
import android.support.design.widget.CoordinatorLayout;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.widget.GridView;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
Toolbar toolbar;
CollapsingToolbarLayout collapsingToolbarLayoutAndroid;
CoordinatorLayout rootLayoutAndroid;
GridView gridView;
Context context;
ArrayList arrayList;
public static String[] gridViewStrings = {
"ALPHABETS",
"NUMBERS",
"MONTH",
"DAYS",
"ANIMALS",
"CALL US",
};
public static int[] gridViewImages = {
R.drawable.icon_alphabet,
R.drawable.icon_number,
R.drawable.icon_calendar,
R.drawable.icon_days,
R.drawable.icon_animal,
R.drawable.icon_call
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
gridView = (GridView) findViewById(R.id.grid);
gridView.setAdapter(new CustomAndroidGridViewAdapter(this, gridViewStrings, gridViewImages));
initInstances();
}
private void initInstances() {
rootLayoutAndroid = (CoordinatorLayout) findViewById(R.id.android_coordinator_layout);
collapsingToolbarLayoutAndroid = (CollapsingToolbarLayout) findViewById(R.id.collapsing_toolbar_android_layout);
collapsingToolbarLayoutAndroid.setTitle("e-Learning");
}
}
CustomAndroidGridViewAdapter.java is :
package com.affinityapp.sj.alphabet;
/**
* Created by SJ on 23-01-2017.
*/
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
/**
* Created by HP on 5/11/2016.
*/
public class CustomAndroidGridViewAdapter extends BaseAdapter {
private Context mContext;
private final String[] string;
private final int[] Imageid;
public CustomAndroidGridViewAdapter(Context c,String[] string,int[] Imageid ) {
mContext = c;
this.Imageid = Imageid;
this.string = string;
}
#Override
public int getCount() {
return string.length;
}
#Override
public Object getItem(int p) {
return null;
}
#Override
public long getItemId(int p) {
return 0;
}
#Override
public View getView(int p, View convertView, ViewGroup parent) {
View grid;
LayoutInflater inflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
grid = new View(mContext);
grid = inflater.inflate(R.layout.gridview_custom_layout, null);
TextView textView = (TextView) grid.findViewById(R.id.gridview_text);
ImageView imageView = (ImageView)grid.findViewById(R.id.gridview_image);
textView.setText(string[p]);
imageView.setImageResource(Imageid[p]);
} else {
grid = (View) convertView;
}
return grid;
}
}

So, you can use this way to click grid view:
public class MainActivity extends AppCompatActivity {
Toolbar toolbar;
CollapsingToolbarLayout collapsingToolbarLayoutAndroid;
CoordinatorLayout rootLayoutAndroid;
GridView gridView;
Context context;
ArrayList arrayList;
public static String[] gridViewStrings = {
"ALPHABETS",
"NUMBERS",
"MONTH",
"DAYS",
"ANIMALS",
"CALL US",
};
public static int[] gridViewImages = {
R.drawable.icon_alphabet,
R.drawable.icon_number,
R.drawable.icon_calendar,
R.drawable.icon_days,
R.drawable.icon_animal,
R.drawable.icon_call
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
gridView = (GridView) findViewById(R.id.grid);
gridView.setAdapter(new CustomAndroidGridViewAdapter(this, gridViewStrings, gridViewImages));
initInstances();
gridView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
// Do whatever you want, like start new Activity or display new view or display dialog box or display just message
Toast.makeText(MainActivity.this, "" + position, Toast.LENGTH_SHORT).show();
}
});
}
private void initInstances() {
rootLayoutAndroid = (CoordinatorLayout) findViewById(R.id.android_coordinator_layout);
collapsingToolbarLayoutAndroid = (CollapsingToolbarLayout) findViewById(R.id.collapsing_toolbar_android_layout);
collapsingToolbarLayoutAndroid.setTitle("e-Learning");
}
}
Hope, this will help you.

Implement onItemClickListener method in you activity:
gridview.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
final Intent intent;
switch(position)
{
case 0:
intent = new Intent(context, FirstActivity.class);
break;
case 1:
intent = new Intent(context, SecondActivity.class);
break;
...
default:
intent = new Intent(context, DefaultActivity.class);
break;
}
startActivity(intent);
}
});

Possibly duplicate question...
Research thoroughly before posting a question please.
Answer can be found in this thread: How can I give an imageview click effect like a button on Android?
Edit:
In your current activity, you need to create a variable ImageButton.
ImageButton myButton = (ImageButton) findViewById(R.id.the_button_you_created_on_the_layout.xml);
Then you need to set a click listener to this button
myButton.setOnClickListener(new View.OnClickListner(){
// When the button is pressed/clicked, it will run the code below
#Override
public void onClick()
// Intent is what you use to start another activity
Intent myIntent = new Intent(this, YourActivity.class);
startActivity(intent);
}
});

Related

Intro slide helper by viewPager

i wanted to use intro slider to my App
i learned it by this video https://www.youtube.com/watch?v=byLKoPgB7yA&t=22s
I do like video tutorial but I have a problem with dots
when I slide to any page the dots(. ) will be copy and increase
where is my problem and what should I do ?
package time.one.just.show.introslyder;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Html;
import android.widget.LinearLayout;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private ViewPager mSlideViewPager;
private LinearLayout mDotsLayout;
private SlyderAdapter slyderAdapter;
//dots of any Slide pages
private TextView[] mDots;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mSlideViewPager = findViewById(R.id.viewPager);
mDotsLayout = findViewById(R.id.dots);
slyderAdapter = new SlyderAdapter(this);
mSlideViewPager.setAdapter(slyderAdapter);
addDotsIndiccator(0);
mSlideViewPager.addOnPageChangeListener(viewListener);
}
private void addDotsIndiccator(int position) {
mDots = new TextView[3];
for (int i = 0; i < mDots.length; i++) {
mDots[i] = new TextView(this);
mDots[i].setText(Html.fromHtml("&#8226"));
mDots[i].setTextSize(35);
mDots[i].setTextColor(getResources().getColor(R.color.colorWhiteNearToGray));
mDotsLayout.addView(mDots[i]);
}
if (mDots.length > 0) {
mDots[position].setTextColor(getResources().getColor(R.color.colorWite));
}
}
ViewPager.OnPageChangeListener viewListener = new ViewPager.OnPageChangeListener() {
#Override
public void onPageScrolled(int i, float v, int i1) {
}
#Override
public void onPageSelected(int i) {
addDotsIndiccator(i);
}
#Override
public void onPageScrollStateChanged(int i) {
}
};}
And this is my SideAdapter class
package time.one.just.show.introslyder;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v4.view.PagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
public class SlyderAdapter extends PagerAdapter {
Context context;
LayoutInflater layoutInflater;
public SlyderAdapter(Context context) {
this.context = context;
}
public int[] slide_imagesArray = {
R.drawable.eat,
R.drawable.men,
R.drawable.sleep};
public String[] slide_headerArray = {
"EAT", "men", "code"};
public String[] slide_descriptionArray = {
"this is 1st", "this is 2nd", "this is 3rd"
};
#Override
public int getCount() {
return slide_headerArray.length;
}
#Override
public boolean isViewFromObject(#NonNull View view, #NonNull Object o) {
return view == o;
}
#NonNull
#Override
public Object instantiateItem(#NonNull ViewGroup container, int position) {
layoutInflater = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
View view = layoutInflater.inflate(R.layout.slide_layoout, container, false);
ImageView slideImageView = (ImageView) view.findViewById(R.id.slideImage);
TextView slideheader = (TextView) view.findViewById(R.id.slideheader);
TextView slidedescription = (TextView) view.findViewById(R.id.slideDescription);
slideImageView.setImageResource(slide_imagesArray[position]);
slideheader.setText(slide_headerArray[position]);
slidedescription.setText(slide_descriptionArray[position]);
container.addView(view);
return view;
}
#Override
public void destroyItem(#NonNull ViewGroup container, int position, #NonNull Object object) {
container.removeView((RelativeLayout) object);
}
}
and this is my SliderLayout
<?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"
android:orientation="vertical"
android:layout_width="match_parent" android:layout_height="match_parent">
<ImageView
android:id="#+id/slideImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="89dp"
app:srcCompat="#drawable/eat" />
<TextView
android:id="#+id/slideheader"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentBottom="true"
android:layout_marginStart="122dp"
android:layout_marginBottom="287dp"
android:text="بدون نیاز به اینترنت"
android:textSize="20sp"
android:textStyle="bold" />
<TextView
android:id="#+id/slideDescription"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentBottom="true"
android:layout_marginStart="0dp"
android:layout_marginBottom="171dp"
android:text="آرشیو کامل این خواننده محبوب همیشه در جیب شم ، ، هر کدام ز آهنگ ها را خواستید می توانید دانلود کنید و هر زمان دلتون خوست به آن ها گوش بدهید حتی بدون نیز به اینترنت" />
</RelativeLayout>
and activtymain 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:background="#drawable/main_background"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<android.support.v4.view.ViewPager
android:id="#+id/viewPager"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v4.view.ViewPager>
<LinearLayout
android:id="#+id/dots"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:orientation="horizontal"></LinearLayout>
</RelativeLayout>
I found a solution
just by add this inside my App
mDotsLayout.removeAllViews();
thank you all

Listview OnItemClick Event

I have following code that should:
listView = (ListView) findViewById(R.id.listview_github_entries);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
}
});
This is what I load into the ListView:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/list_item"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="10dp"
android:orientation="horizontal"
android:clickable="true">
<ImageView
android:layout_width="100dp"
android:layout_height="100dp"
android:src="#mipmap/github_icon"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/github_name"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/github_url"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
Here is the Adapter :
public class GithubEntryAdapter extends ArrayAdapter<GithubEntry>{
public GithubEntryAdapter(Activity context, ArrayList<GithubEntry> githubEntries){
super(context, 0, githubEntries);
}
public View getView(int position, View convertView, ViewGroup parent){
View listItemView = convertView;
if (listItemView == null){
listItemView = LayoutInflater.from(getContext()).inflate(
R.layout.list_item, parent, false);
}
GithubEntry currentGithubEntry = getItem(position);
TextView github_url = (TextView) listItemView.findViewById(R.id.github_url);
github_url.setText(currentGithubEntry.getGithub_url());
TextView github_name = (TextView) listItemView.findViewById(R.id.github_name);
github_name.setText(currentGithubEntry.getGithub_name());
return listItemView;
}
}
This is not working for me. Im no quit sure where I should place this code. Can I place this in the onCreate? If not where should I move it? I completly new in Android and I have also not much experience in Java.
Here is what i did for you..
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
private ListView listView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView) findViewById(R.id.listview_github_entries);
listView.setAdapter(new GithubEntryAdapter(MainActivity.this, getList()));
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
TextView github_url_tv = view.findViewById(R.id.github_url);
String url_text= github_url_tv.getText().toString();
Toast.makeText(MainActivity.this, url_text", Toast.LENGTH_LONG).show();
}
});
}
private ArrayList<GithubEntry> getList() {
ArrayList<GithubEntry> githubEntries = new ArrayList<>();
GithubEntry githubEntry = new GithubEntry();
githubEntry.setGithub_name("Name");
githubEntry.setGithub_url("url");
GithubEntry githubEntry1 = new GithubEntry();
githubEntry1.setGithub_name("Name");
githubEntry1.setGithub_url("url");
GithubEntry githubEntry2 = new GithubEntry();
githubEntry2.setGithub_name("Name");
githubEntry2.setGithub_url("url");
githubEntries.add(githubEntry);
githubEntries.add(githubEntry1);
githubEntries.add(githubEntry2);
return githubEntries;
}
}
Here is adapter
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import java.util.ArrayList;
public class GithubEntryAdapter extends ArrayAdapter<GithubEntry> {
public GithubEntryAdapter(Activity context, ArrayList<GithubEntry>
githubEntries){
super(context, 0, githubEntries);
}
public View getView(int position, View convertView, ViewGroup parent){
View listItemView = convertView;
if (listItemView == null){
listItemView = LayoutInflater.from(getContext()).inflate(
R.layout.list_item, parent, false);
}
GithubEntry currentGithubEntry = getItem(position);
TextView github_url = (TextView) listItemView.findViewById(R.id.github_url);
github_url.setText(currentGithubEntry.getGithub_url());
TextView github_name = (TextView) listItemView.findViewById(R.id.github_name);
github_name.setText(currentGithubEntry.getGithub_name());
return listItemView;
}
}
here is POJO(plan java object) class
class GithubEntry {
private String Github_url;
private String Github_name;
public String getGithub_url() {
return Github_url;
}
public void setGithub_url(String github_url) {
Github_url = github_url;
}
public String getGithub_name() {
return Github_name;
}
public void setGithub_name(String github_name) {
Github_name = github_name;
}
}
and here is list_item
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/list_item"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="10dp"
android:orientation="horizontal"
android:clickable="false">
<ImageView
android:layout_width="100dp"
android:layout_height="100dp"
android:src="#mipmap/ic_launcher_round"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/github_name"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/github_url"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
and here is activity layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.kaimeramedia.githubentry.MainActivity">
<ListView
android:id="#+id/listview_github_entries"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
If mean that the OnItemClickListeneter not working, then you need to implement a custom adapter by extending ArrayAdater to serve you custom row, And in the custom adapter you can use a callback interface or implement a listener on the view it self See the example.
I think you want to display the list of values here. You should write it inside onCreate because onCreate is a method where you method starts and run. So, Put it inside onCreate. For more correct answer please explain everything.

Implementing Swipe functionality on portion of screen while using bottom navigation?

I am developing my first Android application as part of a college project. I am relatively new to java. My application consists of a bottom navigation, which contains 4 separate pages. Within these pages I have a head section, containing a title, contact button and logout button.
Underneath the head section I wish to have a midsection which will in itself contain 3 swipe panels containing information. Essentially there should be 3 panels for each page, with there being 4 pages (So 4 sets of 3 swipe panels).
This is where I'm having difficulty.. I'm unsure of how to nest the fragments so that only that midsection will be swipe-able. Here is a screenshot of what I have so far:
Screenshot of app
Here is my Code for my MainActivity.java class:
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.view.MenuItem;
public class MainActivity extends AppCompatActivity {
private static final String SELECTED_ITEM = "arg_selected_item";
private BottomNavigationView mBottomNav;
private int mSelectedItem;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mBottomNav = (BottomNavigationView) findViewById(R.id.navigation);
mBottomNav.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
selectFragment(item);
return true;
}
});
MenuItem selectedItem;
if (savedInstanceState != null) {
mSelectedItem = savedInstanceState.getInt(SELECTED_ITEM, 0);
selectedItem = mBottomNav.getMenu().findItem(mSelectedItem);
} else {
selectedItem = mBottomNav.getMenu().getItem(0);
}
selectFragment(selectedItem);
}
#Override
protected void onSaveInstanceState(Bundle outState) {
outState.putInt(SELECTED_ITEM, mSelectedItem);
super.onSaveInstanceState(outState);
}
#Override
public void onBackPressed() {
MenuItem homeItem = mBottomNav.getMenu().getItem(0);
if (mSelectedItem != homeItem.getItemId()) {
// select home item
selectFragment(homeItem);
} else {
super.onBackPressed();
}
}
private void selectFragment(MenuItem item) {
Fragment frag = null;
// init corresponding fragment
switch (item.getItemId()) {
case R.id.menu_home:
frag = MenuFragment.newInstance(getString(R.string.text_home),
getString(R.string.test_home));
break;
case R.id.menu_notifications:
//Call to a function that does the stuff here
frag = MenuFragment.newInstance(getString(R.string.text_notifications),
getString(R.string.test_notifications));
break;
case R.id.menu_search:
frag = MenuFragment.newInstance(getString(R.string.text_search),
getString(R.string.test_search));
break;
case R.id.menu_followed:
frag = MenuFragment.newInstance(getString(R.string.text_follow),
getString(R.string.test_follow));
}
// update selected item
mSelectedItem = item.getItemId();
// uncheck the other items.
for (int i = 0; i< mBottomNav.getMenu().size(); i++) {
MenuItem menuItem = mBottomNav.getMenu().getItem(i);
menuItem.setChecked(menuItem.getItemId() == item.getItemId());
}
if (frag != null) {
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.add(R.id.container, frag, frag.getTag());
ft.commit();
}
}
}
Here is the acitivity_main.xml file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:design="http://schemas.android.com/apk/res-auto"
android:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.socialivemusic.socialivetestproject.MainActivity">
<FrameLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#ff292929">
</FrameLayout>
<android.support.design.widget.BottomNavigationView
android:id="#+id/navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="start"
android:layout_weight="0.06"
android:background="#ff000000"
android:textAlignment="center"
design:menu="#menu/bottom_nav_items" />
</LinearLayout>
And here is my MenuFragment.java class:
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import org.w3c.dom.Text;
public class MenuFragment extends Fragment {
private static final String ARG_TEXT = "arg_text";
private static final String ARG_TESTSTRING = "arg_testString";
private String mText;
private String mTest;
private View mContent;
private TextView mTextView;
private TextView mTestView;
private Button contactButton;
private Button logoutButton;
public static Fragment newInstance(String text, String testString) {
Fragment frag = new MenuFragment();
Bundle args = new Bundle();
args.putString(ARG_TEXT, text);
args.putString(ARG_TESTSTRING, testString);
frag.setArguments(args);
return frag;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_menu, container, false);
}
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
// retrieve text and color from bundle or savedInstanceState
if (savedInstanceState == null) {
Bundle args = getArguments();
mText = args.getString(ARG_TEXT);
mTest = args.getString(ARG_TESTSTRING);
} else {
mText = savedInstanceState.getString(ARG_TEXT);
mTest = savedInstanceState.getString(ARG_TESTSTRING);
}
// initialize views
mContent = view.findViewById(R.id.fragment_content);
mTextView = (TextView) view.findViewById(R.id.text);
mTestView = (TextView) view.findViewById(R.id.test);
contactButton = (Button) view.findViewById(R.id.contactButton);
logoutButton = (Button) view.findViewById(R.id.logoutbutton);
// set text and background color
mTextView.setText(mText);
mTestView.setText(mTest);
}
#Override
public void onSaveInstanceState(Bundle outState) {
outState.putString(ARG_TEXT, mText);
outState.putString(ARG_TESTSTRING, mTest);
super.onSaveInstanceState(outState);
}
}
Here is the fragments_menu.xml file:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/fragment_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.socialivemusic.socialivetestproject.MenuFragment">
<LinearLayout
android:layout_width="match_parent"
android:layout_marginLeft="17dp"
android:layout_marginRight="17dp"
android:layout_height="150dp"
android:layout_marginTop="15dp"
android:background="#ff434343"
android:weightSum="1">
<Button
android:text="CONTACT"
android:textColor="#ffffff"
android:layout_width="420dp"
android:layout_marginLeft="10dp"
android:layout_marginStart="10dp"
android:layout_height="90dp"
android:layout_gravity="center"
android:id="#+id/contactButton"
android:layout_weight="0.33"
android:background="#drawable/round_button"
android:textSize="20sp"/>
<TextView
android:id="#+id/text"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:layout_gravity="center"
android:textColor="#969696"
android:textSize="40sp"
android:textStyle="bold"
android:layout_weight="0.355"
/>
<Button
android:text="LOGOUT"
android:textColor="#ffffff"
android:layout_width="420dp"
android:layout_height="90dp"
android:layout_gravity="center"
android:id="#+id/logoutbutton"
android:layout_weight="0.33"
android:background="#drawable/logout_button"
android:textSize="20sp"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_marginLeft="17dp"
android:layout_marginRight="17dp"
android:layout_height="match_parent"
android:layout_marginBottom="15dp"
android:layout_marginTop="185dp"
android:background="#ff434343">
<TextView
android:id="#+id/test"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
</RelativeLayout>
The second LinearLayout in the above file is the one which should be the swipe-panel. The only other files on the project are xml files dealing with layout of the bottom navigation bar itself, so not relevant to the trouble I'm having.
Does anyone have any recommendations on how to approach this? A helping hand on how to start this part of the project would be amazing! Thanks in advance!

Unable to click on custom listview, blocksDescendants not working

I'm fairly new to Android programming and tried everything I could I found on SO, but it still doesn't work.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/ScrollView01"
android:layout_width="fill_parent"
android:fillViewport="true"
android:layout_height="fill_parent">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_main"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context="com.northcityproductions.androidiosapptransfertest1.MainActivity">
<ListView
android:id="#+id/listView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="18dp"
android:headerDividersEnabled="false"
android:footerDividersEnabled="false" />
<ImageView
android:layout_width="48dp"
android:layout_height="48dp"
android:id="#+id/imageView1"
android:layout_gravity="center_vertical"
android:clickable="false"
android:padding="5dp" />
<TextView
android:id="#+id/textView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dip"
android:paddingStart="50dip"
android:textSize="20dip"
android:layout_gravity="center_vertical"
android:textStyle="bold"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:clickable="false"
android:layout_alignParentStart="true">
</TextView>
</RelativeLayout>
AndroidListAdapter.java
package com.northcityproductions.androidiosapptransfertest1;
import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
public class AndroidListAdapter extends ArrayAdapter {
List<String> androidListViewStrings = new ArrayList<String>();
List<Drawable> imagesId = new ArrayList<Drawable>();
Context context;
public AndroidListAdapter(Activity context, List<Drawable> imagesId, List<String> textListView) {
super(context, R.layout.activity_main, textListView);
this.androidListViewStrings = textListView;
this.imagesId = imagesId;
this.context = context;
}
#Override
public View getView(int i, View view, ViewGroup viewGroup) {
LayoutInflater layoutInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View viewRow = layoutInflater.inflate(R.layout.activity_main, null,
true);
TextView mtextView = (TextView) viewRow.findViewById(R.id.textView1);
ImageView mimageView = (ImageView) viewRow.findViewById(R.id.imageView1);
mtextView.setText(androidListViewStrings.get(i));
mimageView.setImageDrawable(imagesId.get(i));
return viewRow;
}
}
Finally, MainActivity.java
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//get list of apps
int flags = PackageManager.GET_META_DATA |
PackageManager.GET_SHARED_LIBRARY_FILES |
PackageManager.GET_UNINSTALLED_PACKAGES;
PackageManager pm = getPackageManager();
List<ApplicationInfo> applications = pm.getInstalledApplications(flags);
List<String> applicationsInstalled = new ArrayList<String>();
List<Drawable> applicationIcons = new ArrayList<Drawable>();
//Create map and sort alphabetically
Map applicationsList = new HashMap();
for (ApplicationInfo appInfo : applications) {
if ((appInfo.flags & ApplicationInfo.FLAG_SYSTEM) == 1) {
// System application
} else {
// Installed by user
String appName = (String) pm.getApplicationLabel((appInfo));
appName = appName.substring(0, 1).toUpperCase() + appName.substring(1);
applicationsList.put(appName, pm.getApplicationIcon(appInfo));
}
}
Map<String, Drawable> treeApps = new TreeMap<String, Drawable>(applicationsList);
for (Map.Entry<String, Drawable> appMap : treeApps.entrySet()) {
applicationsInstalled.add(appMap.getKey());
applicationIcons.add(appMap.getValue());
}
//Arrange them in listview
final AndroidListAdapter androidListAdapter = new AndroidListAdapter(this, applicationIcons, applicationsInstalled);
ListView lv1 = (ListView) findViewById(R.id.listView1);
lv1.setAdapter(androidListAdapter);
lv1.setOnItemClickListener(new OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long rowId) {
AlertDialog.Builder adb = new AlertDialog.Builder(MainActivity.this);
adb.setTitle("List");
adb.setMessage(" selected Item is="+parent.getItemAtPosition(position));
adb.setPositiveButton("Ok", null);
adb.show();
}
});
}
}
I have tried adding the blocksDescendants code in my activity_main.xml under Listview, and Relativelayout, but that doesn't seem to fix it. I also tried setting the focusable property to false for the imageview and textview but that didn't fix it either.

Adding a remove button in my item list

I am fairly new to Java and Android development. I am currently creating a shopping app and was looking at adding a "Remove" button to my Shopping Cart list.
Here is my current code for my list activity for my shopping cart, I am not really sure where to begin and some guidance would be greatly appreciated.
Thanks
package .shopper;
import java.util.List;
import android.os.Bundle;
import android.app.Activity;
import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.support.v4.app.NavUtils;
public class CartListActivity extends ListActivity {
public class ProductListAdapter extends BaseAdapter {
private final Context context;
private List<Product> itemList;
public List<Product> getItemList() {
return itemList;
}
public void setItemList(List<Product> itemList) {
this.itemList = itemList;
}
public Context getContext() {
return context;
}
public ProductListAdapter(Context c) {
context = c;
}
#Override
public int getCount() {
if(itemList == null) return 0;
else return itemList.size();
}
#Override
public Object getItem(int position) {
if (itemList == null) return null;
else return itemList.get(position);
}
#Override
public long getItemId(int position) {
if (itemList == null) return 0;
else return itemList.get(position).hashCode();
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View cell = convertView;
if (cell == null) {
// get layout from mobile xml
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
cell = inflater.inflate(R.layout.adapter_product_list, parent, false);
}
Product p = itemList.get(position);
//set value into textview according to position
TextView textView = (TextView) cell.findViewById(R.id.product_title);
textView.setText(p.getProductName());
// add £ symbol
textView = (TextView) cell.findViewById(R.id.product_info);
textView.setText("Price: " + "£"+ p.getPrice());
//set value into imageview according to position
ImageView imgView = (ImageView) cell.findViewById(R.id.product_image);
// clear the image
imgView.setImageDrawable(null);
//and load from the network
p.loadImage(imgView, 54, 54);
return cell;
}
}
public static final Integer[] productIcons = {
0, // index 0 is empty
R.drawable.books,
R.drawable.films,
R.drawable.music,
R.drawable.games,
};
private int categoryId;
private ProductListAdapter adapter;
private ListViewLoader loader;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// get the category from the intent
Intent intent = getIntent();
categoryId = intent.getIntExtra(MainActivity.SELECTED_CATEGORY, 0);
adapter = new ProductListAdapter(this);
setListAdapter(adapter);
// Show the Up button in the action bar.
setupActionBar();
loader = new ListViewLoader(adapter, categoryId);
loader.execute(String.format(MainActivity.WEBSERVER_GETLIST, categoryId));
}
/**
* Set up the {#link android.app.ActionBar}.
*/
private void setupActionBar() {
getActionBar().setDisplayHomeAsUpEnabled(true);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.product_list, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.show_cart:
//create the intent for the cart activity
Intent intent = new Intent(getApplicationContext(), CartActivity.class);
startActivity(intent);
return true;
}
return super.onOptionsItemSelected(item);
}
#Override
public void onListItemClick(ListView l, View v, int position, long id) {
//create an intent
Intent intent = new Intent(this, ProductActivity.class);
Product p = (Product)adapter.getItem(position);
//specify the extra parameters we want to pass
intent.putExtra(MainActivity.SELECTED_CATEGORY, p.getCategoryId());
intent.putExtra(MainActivity.SELECTED_PRODUCTID, p.getProductId());
intent.putExtra(MainActivity.SELECTED_PRODUCTNAME, p.getProductName());
intent.putExtra(MainActivity.SELECTED_PRODUCTPRICE, p.getPrice());
intent.putExtra(MainActivity.SELECTED_SUITABLEFORKIDS, p.getSuitableForKids());
startActivity(intent);
}
}
EDIT:
XML for adapter_product_list
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="8dp">
<ImageView
android:id="#+id/product_image"
android:layout_width="54dp"
android:layout_height="54dp"
android:padding="5dp"
android:layout_alignParentLeft="true" />
<TextView
android:id="#+id/product_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/product_image"
android:layout_alignTop="#+id/product_image"
android:textColor="#446688"
android:textSize="20sp" />
<TextView
android:id="#+id/product_info"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/product_image"
android:layout_below="#+id/product_title"
android:textColor="#777777"
android:textSize="16sp"
android:textStyle="italic" />
</RelativeLayout>
Change your xml like following
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="8dp" >
<ImageView
android:id="#+id/product_image"
android:layout_width="54dp"
android:layout_height="54dp"
android:layout_alignParentLeft="true"
android:padding="5dp"
android:src="#drawable/ic_launcher" />
<TextView
android:id="#+id/product_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/product_image"
android:layout_toRightOf="#+id/product_image"
android:text="adjkajdjk"
android:textColor="#446688"
android:textSize="20sp" />
<TextView
android:id="#+id/product_info"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/product_title"
android:layout_toRightOf="#+id/product_image"
android:text="adjkajdjk"
android:textColor="#777777"
android:textSize="16sp"
android:textStyle="italic" />
<Button
android:id="#+id/delete_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="Delete" />
</RelativeLayout>
And in your adapter inside getView() function add this
Button deleteBtn = (Button) findViewById(R.id.delete_btn);
deleteBtn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v)
{
itemList.remove(position);
notifyDataSetChanged();
}
});
1) hope p.loadImage(imgView, 54, 54); is running in a asyncTask
2) if you want to click on a button in a row of a ListView than you shoudl add the button to your row layout and implent onClickListenr of that button in your getView method of adapter.in onClick method yous hould have:
if(position<itemList.size()){
//TO REMOVE TEM FROM ARRAY LIST
itemList.remove(position);
//TO Update the ListView
notifyDataSetChanged();
}
you can implement a multy line delete,by adding a checkbox in your listRow and adding the positon of the item to be deleted to an arrayList of Integers and after click-ing the delete button you should delete all the items from the imteList and after that call notifyDataSetChanged()
EDIT
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="8dp">
<ImageView
android:id="#+id/product_image"
android:layout_width="54dp"
android:layout_height="54dp"
android:padding="5dp"
android:layout_alignParentLeft="true" />
<TextView
android:id="#+id/product_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/product_image"
android:layout_alignTop="#+id/product_image"
android:textColor="#446688"
android:textSize="20sp" />
<TextView
android:id="#+id/product_info"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/product_image"
android:layout_below="#+id/product_title"
android:textColor="#777777"
android:textSize="16sp"
android:textStyle="italic" />
<Button
android:id="#+id/deleteBtn"
android:layout_width="wrap_content
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/product_info"
</RelativeLayout>
you should add this button to your layout, and in getView method of adapter add
Button delete=v.findViewById(R.id.deleteBtn);
delete.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
//THE PART I WROTE ABOVE
}
})

Categories

Resources