My problem is, i want to implement a activity with a list and image and in the bottom of image there is four text .When I am going to open my app it has to show one selected text with its related image and list as default.when clicking on rest of the text it has to show their related list and images in the same activity respectively.Since i am new to android please help me out.
Here is xml code
<RelativeLayout
android:id="#+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="2.5"
>
<ImageView
android:id="#+id/mainimage"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/pestf"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="20dp"
android:orientation="horizontal"
android:layout_gravity="bottom"
android:layout_alignParentBottom="true">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Pest Control"
android:id="#+id/pestf"
android:onClick="click1"
android:clickable="true"
android:textStyle="normal"
android:textSize="15dp"
android:textColor="#cf0c07"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/packf"
android:layout_marginLeft="5dp"
android:id="#+id/packf"
android:onClick="click1"
android:clickable="true"
android:textStyle="normal"
android:textSize="15dp"
android:textColor="#cf0c07"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Cleaning"
android:layout_marginLeft="5dp"
android:id="#+id/cleanf"
android:onClick="click1"
android:clickable="true"
android:textStyle="normal"
android:textSize="15dp"
android:textColor="#cf0c07"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="Landscaping"
android:id="#+id/landf"
android:onClick="click1"
android:clickable="true"
android:layout_marginLeft="5dp"
android:textStyle="normal"
android:textSize="15dp"
android:textColor="#cf0c07"/>
</LinearLayout>
</RelativeLayout>
<ListView
android:id="#+id/lst"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/ll_pest"
android:layout_weight="2.2"
android:background="#FFFFFF"
>
</ListView>
<LinearLayout
android:id="#+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/lst"
android:layout_weight="3.5"
android:gravity="center"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginBottom="5dp"
android:layout_marginTop="3dp"
android:layout_alignParentBottom="true"
android:background="#drawable/border_layout">
<TextView
android:id="#+id/txt_pest_Email"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:background="#FFFFFF"
android:text="#string/btn7"
android:drawablePadding="5dp"
android:drawableTop="#drawable/icon7"
android:textStyle="bold"
android:textColor="#000000"
/>
<View
android:layout_width="1dp"
android:layout_height="fill_parent"
android:background="#82020202" />
<TextView
android:id="#+id/txt_pest_phone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:background="#ffffff"
android:text="#string/btn8"
android:drawablePadding="5dp"
android:drawableTop="#drawable/icon8"
android:textStyle="bold"
android:textColor="#000000"
/>
</LinearLayout>
Here is Activity code
public class demo2 extends AppCompatActivity {
TextView tv1,tv2,tv3,tv4,txtEmail,txtPhone;
ListView lv;
ImageView img;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_demo2);
String[] adobe_products = getResources().getStringArray(R.array.adobe_products);
lv = (ListView) findViewById(R.id.lst);
img = (ImageView) findViewById(R.id.mainimage);
txtEmail=(TextView)findViewById(R.id.txt_pest_Email);
txtPhone=(TextView)findViewById(R.id.txt_pest_phone);
tv1 = (TextView) findViewById(R.id.text1);
tv2 = (TextView) findViewById(R.id.text2);
tv3 = (TextView) findViewById(R.id.text3);
tv4 = (TextView) findViewById(R.id.text4);
lv.setAdapter(new ArrayAdapter<String>(this, R.layout.list_item, adobe_products));
tv1.setTextColor(getResources().getColor(R.color.blue));
txtPhone.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String mobileNo = "08042589999";
String uri = "tel:" + mobileNo.trim();
Intent intent = new Intent(Intent.ACTION_CALL);
intent.setData(Uri.parse(uri));
startActivity(intent);
}
});
txtEmail.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(demo2.this, Booknow.class);
// sending data to new activity
i.putExtra("flag",true);
i.putExtra("cat",1);
startActivity(i);
}
});
// Inflate the layout for this fragment
}
public void click2(View v){
img.setBackgroundResource(R.drawable.packers);
String[] countries = getResources().getStringArray(R.array.country);
lv.setAdapter(new ArrayAdapter<String>(this, R.layout.list_item1, countries));
tv2.setTextColor(getResources().getColor(R.color.blue));
tv1.setTextColor(getResources().getColor(R.color.red));
tv3.setTextColor(getResources().getColor(R.color.red));
tv4.setTextColor(getResources().getColor(R.color.red));
}
public void click1(View v){
img.setBackgroundResource(R.drawable.pestf);
String[] adobe = getResources().getStringArray(R.array.adobe_products);
lv.setAdapter(new ArrayAdapter<String>(this, R.layout.list_item1, adobe));
tv1.setTextColor(getResources().getColor(R.color.blue));
tv2.setTextColor(getResources().getColor(R.color.red));
tv3.setTextColor(getResources().getColor(R.color.red));
tv4.setTextColor(getResources().getColor(R.color.red));
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// Launching new Activity on selecting single List Item
Intent i = new Intent(demo2.this, BedbugControl.class);
// sending data to new activity
i.putExtra("position", position);
startActivity(i);
}
});
}
public void click3(View v){
img.setBackgroundResource(R.drawable.cleaningf);
String[] sweets = getResources().getStringArray(R.array.sweets);
lv.setAdapter(new ArrayAdapter<String>(this, R.layout.list_item1, sweets));
tv3.setTextColor(getResources().getColor(R.color.blue));
tv1.setTextColor(getResources().getColor(R.color.red));
tv2.setTextColor(getResources().getColor(R.color.red));
tv4.setTextColor(getResources().getColor(R.color.red));
}
public void click4(View v) {
img.setBackgroundResource(R.drawable.landf);
String[] companies = getResources().getStringArray(R.array.company);
lv.setAdapter(new ArrayAdapter<String>(this, R.layout.list_item1, companies));
tv4.setTextColor(getResources().getColor(R.color.blue));
tv1.setTextColor(getResources().getColor(R.color.red));
tv2.setTextColor(getResources().getColor(R.color.red));
tv3.setTextColor(getResources().getColor(R.color.red));
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_demo2, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Related
I'm trying to make a navigation drawer that opens a new activity when the user intent an item.
I don't know how to do this?
There is 5 items in my drawer and I want each of them to open a different activity using intent not fragment .
when i click to the item anythink do , i need to go in other activity using intent
My code is the following
public class menu extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
TextView tt1 , txtnom , txtadress;
Typeface ty;
ViewPager viewp;
TabLayout tab;
ImageView img;
private DrawerLayout mdrawer;
private ActionBarDrawerToggle mtoggle;
private int[] tabIcons = {
R.drawable.ic_tablayout_tache,
R.drawable.ic_ic_tablayout_rendez,
R.drawable.ic_tablayout_eve,
R.drawable.ic_projettab
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
Toolbar tol = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(tol);
getSupportActionBar().setTitle("");
tt1 = (TextView) findViewById(R.id.textbar);
ty = Typeface.createFromAsset(getAssets(), "fonts/BALOOBHAI-REGULAR.TTF");
tt1.setTypeface(ty);
mdrawer = (DrawerLayout) findViewById(R.id.drawerlayout);
mtoggle = new ActionBarDrawerToggle(this, mdrawer, R.string.ouvrir, R.string.fermer);
mdrawer.addDrawerListener(mtoggle);
mtoggle.syncState();
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
viewp = (ViewPager) findViewById(R.id.viewpage);
tab = (TabLayout) findViewById(R.id.tablayout);
setupViewPager(viewp);
tab.setupWithViewPager(viewp);
setupTabIcons();
NavigationView navigationView = (NavigationView)findViewById(R.id.navview);
View header = LayoutInflater.from(this).inflate(R.layout.header_menu , null);
navigationView.addHeaderView(header);
navigationView.setNavigationItemSelectedListener(this);
img = (ImageView)header.findViewById(R.id.pic);
txtnom = (TextView)header.findViewById(R.id.textnom);
txtadress = (TextView)header.findViewById(R.id.textadr);
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
if (user != null){
Picasso.with(getApplicationContext()).load(user.getPhotoUrl()).transform(new CircleTransform()).into(img);
String name = user.getDisplayName();
String email = user.getEmail();
txtnom.setText(name);
txtadress.setText(email);
} else {
gopagelogin();
}
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (mtoggle.onOptionsItemSelected(item)) {
return true;
}
int id = item.getItemId();
if ( id == R.id.dec) {
alertmsg();
}
return super.onOptionsItemSelected(item);
}
public void runThis(View v) {
Intent ii = new Intent(menu.this, partietache.class);
startActivity(ii);
}
public void Rendezvous(View v) {
Intent intent = new Intent(menu.this, partierendezvous.class);
startActivity(intent);
}
public void Evenement(View v) {
Intent intent = new Intent(menu.this, partieevenement.class);
startActivity(intent);
}
public void Projet(View v) {
Intent intent = new Intent(menu.this, partieprojet.class);
startActivity(intent);
}
private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFragment(new Tachefragment(), "Tâches");
adapter.addFragment(new RendezvousFragment(), "Rendez-vous");
adapter.addFragment(new Evenementfragment(), "Événements");
adapter.addFragment(new ProjetFragment(), "Projets");
viewPager.setAdapter(adapter);
}
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
int id = item.getItemId();
if (id == R.id.para) {
Intent i = new Intent(menu.this, settingmenu.class);
startActivity(i);
} else if (id == R.id.prof) {
Toast.makeText(this, "clicked", Toast.LENGTH_SHORT).show();
}
mdrawer.closeDrawer(GravityCompat.START);
return true;
}
class ViewPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
#Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
#Override
public int getCount() {
return mFragmentList.size();
}
public void addFragment(Fragment fragment, String taches) {
mFragmentList.add(fragment);
mFragmentTitleList.add(taches);
}
#Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
private void setupTabIcons() {
tab.getTabAt(0).setIcon(tabIcons[0]);
tab.getTabAt(1).setIcon(tabIcons[1]);
tab.getTabAt(2).setIcon(tabIcons[2]);
tab.getTabAt(3).setIcon(tabIcons[3]);
}
private void gopagelogin() {
Intent ii = new Intent(this , login.class);
ii.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(ii);
}
private void alertmsg() {
AlertDialog.Builder alert = new AlertDialog.Builder(menu.this);
alert.setTitle("Déconnexion ?");
alert.setMessage("voulez-vous déconnecter?");
alert.setPositiveButton("Oui", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
FirebaseAuth.getInstance().signOut();
gopagelogin();
}
});
alert.setNegativeButton("Non", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.cancel();
}
});
alert.show();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.deconnexion , menu);
return true;
}}
My xml code :
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/drawerlayout"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:fitsSystemWindows="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include
android:id="#+id/toolbar"
layout="#layout/mytoolbar" />
<android.support.design.widget.TabLayout
android:id="#+id/tablayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="#style/AppTabLayout"
app:tabTextAppearance="#style/AppTabTextAppearance"
app:tabMode="fixed" />
<android.support.v4.view.ViewPager
android:id="#+id/viewpage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/tablayout" />
</LinearLayout>
<android.support.design.widget.NavigationView
android:id="#+id/navview"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_gravity="start"
android:background="#ededed"
app:itemIconTint="#000000"
app:itemTextColor="#000000"
app:menu="#menu/menudeb">
</android.support.design.widget.NavigationView>
<RelativeLayout xmlns:fab="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignEnd="#+id/material_design_android_floating_action_menu"
android:layout_alignParentTop="true"
android:layout_alignRight="#+id/material_design_android_floating_action_menu">
<TextView
android:id="#+id/textbar"
android:layout_width="125dp"
android:layout_height="30dp"
android:layout_gravity="center"
android:layout_marginBottom="100dp"
android:layout_marginLeft="125dp"
android:layout_marginTop="11dp"
android:gravity="center"
android:text="WACKTY"
android:textColor="#01b698"
android:textSize="24dp" />
<RelativeLayout
xmlns:fab="http://schemas.android.com/apk/res-auto"
android:id="#+id/relativeLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="#+id/textmenu">
<com.github.clans.fab.FloatingActionMenu
android:id="#+id/menu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_marginBottom="20dp"
android:layout_marginEnd="12dp"
android:layout_marginRight="12dp"
fab:menu_animationDelayPerItem="55"
fab:menu_backgroundColor="#android:color/transparent"
fab:menu_buttonSpacing="0dp"
fab:menu_colorNormal="#ffffff"
fab:menu_colorPressed="#ffffff"
fab:menu_colorRipple="#99d4d4d4"
fab:menu_fab_size="normal"
fab:menu_icon="#drawable/ic_action_plus"
fab:menu_labels_colorNormal="#c4c0c0"
fab:menu_labels_colorPressed="#444"
fab:menu_labels_colorRipple="#66efecec"
fab:menu_labels_cornerRadius="0dp"
fab:menu_labels_ellipsize="none"
fab:menu_labels_hideAnimation="#anim/fab_slide_out_to_right"
fab:menu_labels_margin="0dp"
fab:menu_labels_maxLines="-1"
fab:menu_labels_padding="1dp"
fab:menu_labels_position="left"
fab:menu_labels_showAnimation="#anim/fab_slide_in_from_right"
fab:menu_labels_showShadow="false"
fab:menu_labels_singleLine="false"
fab:menu_labels_textColor="#000000"
fab:menu_labels_textSize="14sp"
fab:menu_openDirection="up">
<com.github.clans.fab.FloatingActionButton
android:id="#+id/projet"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_projeticon"
fab:fab_colorNormal="#ffffff"
fab:fab_colorPressed="#444"
fab:fab_label="Projet"
android:onClick="Projet"
fab:fab_size="mini" />
<com.github.clans.fab.FloatingActionButton
android:id="#+id/événement"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_eventicon"
fab:fab_colorNormal="#ffffff"
fab:fab_colorPressed="#444"
fab:fab_label="Événement"
android:onClick="Evenement"
fab:fab_size="mini" />
<com.github.clans.fab.FloatingActionButton
android:id="#+id/rendezvous"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="Rendezvous"
android:src="#drawable/ic_action_rendez"
fab:fab_colorNormal="#ffffff"
fab:fab_colorPressed="#444"
fab:fab_label="Rendez-Vous"
fab:fab_size="mini" />
<com.github.clans.fab.FloatingActionButton
android:id="#+id/tache"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="runThis"
android:src="#drawable/ic_action_task"
fab:fab_colorNormal="#ffffff"
fab:fab_colorPressed="#444"
fab:fab_label="Tâches"
fab:fab_size="mini" />
</com.github.clans.fab.FloatingActionMenu>
</RelativeLayout>
</RelativeLayout>
Menu xml :
<menu xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android">
<group android:checkableBehavior="single">
<item
android:id="#+id/prof"
android:icon="#drawable/ic_user"
android:title="Profile"
/>
<item
android:id="#+id/proj"
android:icon="#drawable/ic_project"
android:title="Projets"/>
<item
android:id="#+id/para"
android:icon="#drawable/ic_action_set"
android:title="Parametres" />
</group>
<item android:title="Compte">
<menu>
<item
android:id="#+id/dec"
android:icon="#drawable/ic_action_set"
android:title="Déconnexion"/>
</menu>
</item>
</menu>
In your overrided onNavigationItemSelected(MenuItem item) method you have to getItemId and on the selected Item you have to open activity through intent.
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
int id = item.getItemId();
if (id == R.id.ItemId) {
Intent i = new Intent(YourActivityName.this, OtherActivity.class);
startActivity(i);
}
mdrawer.closeDrawer(GravityCompat.START);
return true;
}
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/drawerlayout"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:fitsSystemWindows="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include
android:id="#+id/toolbar"
layout="#layout/mytoolbar" />
<android.support.design.widget.TabLayout
android:id="#+id/tablayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="#style/AppTabLayout"
app:tabTextAppearance="#style/AppTabTextAppearance"
app:tabMode="fixed" />
<android.support.v4.view.ViewPager
android:id="#+id/viewpage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/tablayout" />
</LinearLayout>
<RelativeLayout xmlns:fab="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignEnd="#+id/material_design_android_floating_action_menu"
android:layout_alignParentTop="true"
android:layout_alignRight="#+id/material_design_android_floating_action_menu">
<TextView
android:id="#+id/textbar"
android:layout_width="125dp"
android:layout_height="30dp"
android:layout_gravity="center"
android:layout_marginBottom="100dp"
android:layout_marginLeft="125dp"
android:layout_marginTop="11dp"
android:gravity="center"
android:text="WACKTY"
android:textColor="#01b698"
android:textSize="24dp" />
<RelativeLayout
xmlns:fab="http://schemas.android.com/apk/res-auto"
android:id="#+id/relativeLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="#+id/textmenu">
<com.github.clans.fab.FloatingActionMenu
android:id="#+id/menu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_marginBottom="20dp"
android:layout_marginEnd="12dp"
android:layout_marginRight="12dp"
fab:menu_animationDelayPerItem="55"
fab:menu_backgroundColor="#android:color/transparent"
fab:menu_buttonSpacing="0dp"
fab:menu_colorNormal="#ffffff"
fab:menu_colorPressed="#ffffff"
fab:menu_colorRipple="#99d4d4d4"
fab:menu_fab_size="normal"
fab:menu_icon="#drawable/ic_action_plus"
fab:menu_labels_colorNormal="#c4c0c0"
fab:menu_labels_colorPressed="#444"
fab:menu_labels_colorRipple="#66efecec"
fab:menu_labels_cornerRadius="0dp"
fab:menu_labels_ellipsize="none"
fab:menu_labels_hideAnimation="#anim/fab_slide_out_to_right"
fab:menu_labels_margin="0dp"
fab:menu_labels_maxLines="-1"
fab:menu_labels_padding="1dp"
fab:menu_labels_position="left"
fab:menu_labels_showAnimation="#anim/fab_slide_in_from_right"
fab:menu_labels_showShadow="false"
fab:menu_labels_singleLine="false"
fab:menu_labels_textColor="#000000"
fab:menu_labels_textSize="14sp"
fab:menu_openDirection="up">
<com.github.clans.fab.FloatingActionButton
android:id="#+id/projet"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_projeticon"
fab:fab_colorNormal="#ffffff"
fab:fab_colorPressed="#444"
fab:fab_label="Projet"
android:onClick="Projet"
fab:fab_size="mini" />
<com.github.clans.fab.FloatingActionButton
android:id="#+id/événement"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_eventicon"
fab:fab_colorNormal="#ffffff"
fab:fab_colorPressed="#444"
fab:fab_label="Événement"
android:onClick="Evenement"
fab:fab_size="mini" />
<com.github.clans.fab.FloatingActionButton
android:id="#+id/rendezvous"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="Rendezvous"
android:src="#drawable/ic_action_rendez"
fab:fab_colorNormal="#ffffff"
fab:fab_colorPressed="#444"
fab:fab_label="Rendez-Vous"
fab:fab_size="mini" />
<com.github.clans.fab.FloatingActionButton
android:id="#+id/tache"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="runThis"
android:src="#drawable/ic_action_task"
fab:fab_colorNormal="#ffffff"
fab:fab_colorPressed="#444"
fab:fab_label="Tâches"
fab:fab_size="mini" />
</com.github.clans.fab.FloatingActionMenu>
</RelativeLayout>
</RelativeLayout>
<android.support.design.widget.NavigationView
android:id="#+id/navview"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_gravity="start"
android:background="#ededed"
app:itemIconTint="#000000"
app:itemTextColor="#000000"
app:menu="#menu/menudeb">
</android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>
I try to show a Listview to show my data
This is main_fragment.java
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_main, container, false);
final List<import_fragment.Contact> Contacts = new ArrayList<import_fragment.Contact>();
ListView contactListView;
final EditText nametxt, emailTxt, phoneTxt, addressTxt;
nametxt = (EditText) view.findViewById(R.id.txtName);
emailTxt = (EditText) view.findViewById(R.id.txtEmail);
phoneTxt = (EditText) view.findViewById(R.id.txtPhone);
addressTxt = (EditText) view.findViewById(R.id.txtAddress);
contactListView = (ListView) view.findViewById(R.id.listView);
dbHandler = new DatabaseHandler(getActivity().getApplicationContext());
final Button addBtn = (Button) view.findViewById(R.id.btnadd);
addBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Uri imageUri = Uri.parse("android.resource://org.intracode.contactmanager/drawable/no_user_logo.png");
import_fragment.Contact contact = new import_fragment.Contact(dbHandler.getContactsCount(), String.valueOf(nametxt.getText()), String.valueOf(phoneTxt.getText()), String.valueOf(emailTxt.getText()), String.valueOf(addressTxt.getText()), imageUri);
if (!contactExists(contact)) {
dbHandler.createContact(contact);
Contacts.add(contact);
if (contactAdapter != null) contactAdapter.notifyDataSetChanged();
Toast.makeText(getActivity().getApplicationContext(), String.valueOf(nametxt.getText()) + " has been added to your Contacts!", Toast.LENGTH_SHORT).show();
return;
}
Toast.makeText(getActivity().getApplicationContext(), String.valueOf(nametxt.getText()) + " already exists. Please use a different name.", Toast.LENGTH_SHORT).show();
}
});
final Button addContact = (Button) view.findViewById(R.id.btnadd);
nametxt.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
addContact.setEnabled(!nametxt.getText().toString().trim().equals(""));
}
#Override
public void afterTextChanged(Editable s) {
}
});
return view;
}
ArrayAdapter<import_fragment.Contact> contactAdapter;
ListView contactListView;
DatabaseHandler dbHandler;
int longClickedItemIndex;
public void onActivityResult(int reqCode, int resCode, Intent data) {
Uri imageUri = Uri.parse("android.resource://org.intracode.contactmanager/drawable/no_user_logo.png");
ImageView contactImageImgView;
contactImageImgView = (ImageView) getActivity().findViewById(R.id.ivContactImage);
if (resCode == Activity.RESULT_OK) {
if (reqCode == 1) {
imageUri = data.getData();
contactImageImgView.setImageURI(data.getData());
}
}
}
public void onCreateContextMenu(ContextMenu menu, View view, ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, view, menuInfo);
{
ImageView contactImageImgView;
contactImageImgView = (ImageView) view.findViewById(R.id.ivContactImage);
contactImageImgView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Contact Image"), 1);
}
});
if (dbHandler.getContactsCount() != 0)
Contacts.addAll(dbHandler.getAllContacts());
populateList();
}
menu.setHeaderIcon(R.drawable.pencil_icon);
menu.setHeaderTitle("Contact Options");
menu.add(Menu.NONE, EDIT, menu.NONE, "Edit Contact");
menu.add(Menu.NONE, DELETE, menu.NONE, "Delete Contact");
}
public boolean onContextItemSelected(MenuItem item) {
switch (item.getItemId()) {
case EDIT:
// TODO: Implement editing a contact
break;
case DELETE:
dbHandler.deleteContact(Contacts.get(longClickedItemIndex));
Contacts.remove(longClickedItemIndex);
contactAdapter.notifyDataSetChanged();
break;
}
return super.onContextItemSelected(item);
}
private boolean contactExists(import_fragment.Contact contact) {
String name = contact.getName();
int contactCount = Contacts.size();
for (int i = 0; i < contactCount; i++) {
if (name.compareToIgnoreCase(Contacts.get(i).getName()) == 0)
return true;
}
return false;
}
private void populateList() {
contactAdapter = new ContactListAdapter(getActivity());
contactListView.setAdapter(contactAdapter);
}
final List<import_fragment.Contact> Contacts = new ArrayList<import_fragment.Contact>();
private class ContactListAdapter extends ArrayAdapter<import_fragment.Contact> {
public ContactListAdapter(Context cntx) {
super (cntx, R.layout.fragment_import, Contacts);
}
#Override
public View getView(int position, View view, ViewGroup parent) {
if (view == null)
view = getActivity().getLayoutInflater().inflate(R.layout.fragment_import, parent, false);
import_fragment.Contact currentContact = Contacts.get(position);
TextView name = (TextView) view.findViewById(R.id.contactName);
name.setText(currentContact.getName());
TextView phone = (TextView) view.findViewById(R.id.phoneNumber);
phone.setText(currentContact.getPhone());
TextView email = (TextView) view.findViewById(R.id.emailAddress);
email.setText(currentContact.getEmail());
TextView address = (TextView) view.findViewById(R.id.cAddress);
address.setText(currentContact.getAddress());
return view;
}
}
This is import_fragment.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<ImageView
android:layout_width="75dp"
android:layout_height="75dp"
android:id="#+id/ivContactImage" />
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="91dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Contact Name"
android:id="#+id/contactName"
android:layout_gravity="left|center_vertical"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Phone"
android:id="#+id/phoneNumber"
android:layout_gravity="left|center_vertical"
android:layout_marginTop="5dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Email"
android:id="#+id/emailAddress"
android:layout_gravity="left|center_vertical"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Address"
android:id="#+id/cAddress"
android:layout_gravity="left|center_vertical"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="My Contacts"
android:id="#+id/textView"
android:layout_gravity="center"
android:layout_marginTop="10dp"/>
<ListView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/listView"
android:layout_gravity="center"/>
</LinearLayout>
My listview in import_fragment.xml, when i open import_fragment.xml in app its appear only Contact Name and Phone and Email and Address which is added in import_fragment.xml and not the new contact information which in database added by user.
You have to setAdapter in onCreateView. Currently you are populating the list in onCreateContextMenu. Moreover, after setting the adapter, you have to call notifyDataSetChanged() as well
Try this way, i have tried this in my machine and it show me listview properly , i have done with weightSum so it will be applicable for all devices , just change your xml file with below one
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="4">
<ImageView
android:id="#+id/ivContactImage"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" />
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="3"
android:orientation="vertical">
<TextView
android:id="#+id/contactName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left|center_vertical"
android:text="Contact Name"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/phoneNumber"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left|center_vertical"
android:layout_marginTop="5dp"
android:text="Phone" />
<TextView
android:id="#+id/emailAddress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left|center_vertical"
android:text="Email" />
<TextView
android:id="#+id/cAddress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left|center_vertical"
android:text="Address" />
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left|center_vertical"
android:text="My Contacts"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
</LinearLayout>
<ListView
android:id="#+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center" />
Change both of your LinearLayout's height to
android:layout_height="fill_parent"
Your listView is actually "there", it's just that the container (layout) of the listView isn't big enough to fit it.
EDIT:
You are defining two different list of Contacts there! Both activity and adapter class are referring to two different Contacts. Remove the one inside the onCreateView function.
final List<import_fragment.Contact> Contacts = new ArrayList<import_fragment.Contact>();
I have a ViewFlipper populated with multiple ImageView. but now, i wanted to populate the view flipper with images from mysql database. But i don't know how to implement it.
here's my ViewFlipper.xml
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Login">
<ViewFlipper
android:id="#+id/view_flipper"
android:layout_width="fill_parent"
android:flipInterval="5000"
android:inAnimation="#android:anim/slide_in_left"
android:outAnimation="#android:anim/slide_out_right"
android:layout_height="fill_parent"
android:layout_centerInParent="true">
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:adjustViewBounds="true"
android:scaleType="centerCrop"
android:src="#drawable/login_pic1"/>
<ImageView android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:adjustViewBounds="true"
android:scaleType="centerCrop"
android:src="#drawable/login_pic2"/>
<ImageView android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:adjustViewBounds="true"
android:scaleType="centerCrop"
android:src="#drawable/login_pic3"/>
<ImageView android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:adjustViewBounds="true"
android:scaleType="centerCrop"
android:src="#drawable/login_pic4"/>
<ImageView android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:adjustViewBounds="true"
android:scaleType="centerCrop"
android:src="#drawable/login_pic5"/>
<ImageView android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:adjustViewBounds="true"
android:scaleType="centerCrop"
android:src="#drawable/pic6"/>
<ImageView android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:adjustViewBounds="true"
android:scaleType="centerCrop"
android:src="#drawable/pic7"/>
</ViewFlipper>
</RelativeLayout>
and here is my MainActivity.java
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
Button btn_login = (Button)findViewById(R.id.login);
Button btn_signup = (Button)findViewById(R.id.create);
final EditText txt_username = (EditText)findViewById(R.id.username);
final EditText txt_password = (EditText)findViewById(R.id.password);
getSupportActionBar().hide();
ViewFlipper flipper = (ViewFlipper)findViewById(R.id.view_flipper);
flipper.startFlipping();
Bitmap bitmap = BitmapFactory.decodeResource(this.getResources(), R.drawable.logo);
Bitmap circularBitmap = ImageConverter.getRoundedCornerBitmap(bitmap, 80);
ImageView circularImageView = (ImageView)findViewById(R.id.logo);
circularImageView.setImageBitmap(circularBitmap);
View.OnClickListener login_click = new View.OnClickListener(){
//login form validation
public void onClick(View view) {
String user = txt_username.getText().toString();
String pass = txt_password.getText().toString();
if(user.length()==0)
{
txt_username.requestFocus();
txt_username.setError("Username cant be empty");
}
else if(pass.length()==0){
txt_password.requestFocus();
txt_password.setError("Password cant be empty");
}
else
{
Toast.makeText(Login.this,"Login Successful", Toast.LENGTH_LONG).show();
startActivity(new Intent(Login.this,Homepage.class));
}
}
};
btn_login.setOnClickListener(login_click);
View.OnClickListener signup_click = new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(Login.this,Registration.class));
}
};
btn_signup.setOnClickListener(signup_click);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_login, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
and here my JSON.
Hope you can help me :)
i have some problem here when i want to use my own customize Toolbar instead the default system Toolbar, now i want to use that toolbar on my MainActivity.
i have make my own xml resource file for the toolbar, and i have included it on my MainActivity layout file, but when i run the app i got some error said
android.view.InflateException: Binary XML file line #2: Error inflating class android.support.v7.widget.toolbar
I don't know what happen, i have try to change the setting on my xml file but still the error appear.
Please master, help me.
Thanks Before.
NB. This my code for app_bar.xml (toolbar resource file)
<android.support.v7.widget.toolbar xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/primaryColor">
and this one for my MainActivity.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#fafafa"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity">
<include
android:id="#+id/app_bar"
layout="#layout/app_bar" />
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="#string/Title"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#f325272f"
android:textStyle="bold" />
<Button
android:id="#+id/btnNewTrans"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView"
android:layout_centerHorizontal="true"
android:layout_marginTop="73dp"
android:background="#drawable/custom_button"
android:text="#string/NewTransaction" />
<Button
android:id="#+id/btnViewCashflow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/textView3"
android:layout_alignBottom="#+id/textView3"
android:layout_alignLeft="#+id/textView"
android:layout_alignStart="#+id/textView"
android:background="#drawable/custom_button"
android:text="#string/ViewCashflow" />
<Button
android:id="#+id/btnAddCateg"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/textView2"
android:layout_marginBottom="73dp"
android:layout_toLeftOf="#+id/btnRate"
android:layout_toStartOf="#+id/btnRate"
android:background="#drawable/custom_button"
android:text="#string/AddCategory" />
<Button
android:id="#+id/btnRate"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/btnAddCateg"
android:layout_alignLeft="#+id/spnCategSelect"
android:layout_alignStart="#+id/spnCategSelect"
android:text="#string/RateUs" />
<Button
android:id="#+id/btnSetting"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/btnRate"
android:layout_toEndOf="#+id/btnRate"
android:layout_toRightOf="#+id/btnRate"
android:text="#string/Setting" />
<Button
android:id="#+id/btnAbout"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/btnSetting"
android:layout_toEndOf="#+id/btnSetting"
android:layout_toRightOf="#+id/btnSetting"
android:text="#string/About" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="#string/Trademark"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#f325272f"
android:textStyle="bold" />
<Spinner
android:id="#+id/spnCategSelect"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/btnAddCateg"
android:layout_alignEnd="#+id/textView"
android:layout_alignLeft="#+id/btnViewCashflow"
android:layout_alignRight="#+id/textView"
android:layout_alignStart="#+id/btnViewCashflow"
android:spinnerMode="dropdown" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/btnNewTrans"
android:layout_centerHorizontal="true"
android:layout_marginTop="38dp"
android:text="#string/SelectCategory"
android:textAppearance="?android:attr/textAppearanceSmall" />
and this one for my MainActivity.java
public class MainActivity extends AppCompatActivity implements OnItemSelectedListener {
private static Button BtnINewTrans;
private static Button BtnIViewCash;
private static Button BtnIAddCateg;
Spinner my_Spinner;
DatabaseHelper dbHelper = new DatabaseHelper(this);
public static String catSelected = null;
private Toolbar toolbar;
//ArrayAdapter<String> adapterCategory;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar=(Toolbar)findViewById(R.id.app_bar);
setSupportActionBar(toolbar);
my_Spinner = (Spinner)findViewById(R.id.spnCategSelect);
my_Spinner.setOnItemSelectedListener(this);
select_spinner_Category();
onButtonClickButtonListener();
}
/*ArrayList<String> my_array = new ArrayList<String>();
my_array = getTableValues();*/
/*ArrayAdapter my_Adapter = new ArrayAdapter(this, R.layout.spinner_row, my_array);
My_spinner.setAdapter(my_Adapter);*/
public void select_spinner_Category () {
my_Spinner = (Spinner)findViewById(R.id.spnCategSelect);
DatabaseHelper dbH = new DatabaseHelper(getApplicationContext());
List<String> listCategory = dbH.getAllCategory();
ArrayAdapter<String> adapterCategory = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, listCategory);
adapterCategory
.setDropDownViewResource(android.R.layout.simple_spinner_item);
my_Spinner.setAdapter(adapterCategory);
}
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position,
long id){
String label = parent.getItemAtPosition(position).toString();
Toast.makeText(parent.getContext(), "You selected "+label,
Toast.LENGTH_LONG).show();
catSelected = label;
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
public String getCatSelected(){
return catSelected;
}
/*ArrayList<String> arrayCategory;
arrayCategory = dbHelper.getAllCategory();
selectCategory = (Spinner) findViewById(R.id.spnCategSelect);
ArrayAdapter adapterCategory = new ArrayAdapter(this, android.R.layout.simple_spinner_item, arrayCategory);
// adapterCategory = new ArrayList<String>(this, android.R.layout.simple_spinner_item, R.id.spnCategSelect, AllCategoryList);
adapterCategory.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
selectCategory.setAdapter(adapterCategory);
selectCategory.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(getBaseContext(), parent.getItemAtPosition(position) + " selected", Toast.LENGTH_LONG).show();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}*/
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
public void onButtonClickButtonListener(){
BtnINewTrans = (Button)findViewById(R.id.btnNewTrans);
BtnINewTrans.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intentNewTrans = new Intent ("com.example.ever_ncn.cashflow.NewTransaction");
startActivity(intentNewTrans);
}
}
);
BtnIViewCash = (Button)findViewById(R.id.btnViewCashflow);
BtnIViewCash.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intentViewCash = new Intent ("com.example.ever_ncn.cashflow.ViewCashflow");
startActivity(intentViewCash);
}
}
);
BtnIAddCateg = (Button)findViewById(R.id.btnAddCateg);
BtnIAddCateg.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intentAddCateg = new Intent ("com.example.ever_ncn.cashflow.CategorySetting");
startActivity(intentAddCateg);
}
}
);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Classes are case sensitive: you have android.support.v7.widget.toolbar with a lowercase 't' when it should be android.support.v7.widget.Toolbar
I am trying to add a custom header that isnt clickable but will have a checkbox that will "check all" checkboxes under it.
This is my List Fragment
public class AssesmentListFragment extends ListFragment {
private static String BUNDLE_KEY_APPLICATION = "LIST_ITEM";
FastAssesmentListAdapter adapter;
View listHeader;
public AssesmentListFragment() {}
public AssesmentListFragment(Data[] data) {
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Data[] assestments = {new Data("Assesment ID","Name", "Date"), new Data("123456", "Assestment 2", "9/12/12"),
new Data("345672", "Assesment 3", "9/13/12"), new Data("566893", "Assesment 4", "9/14/12")};
//This is the part that makes the app crash
View header = getActivity().getLayoutInflater().inflate(R.layout.list_adapter_assesments, null);
ListView listView = getListView();
listView.addHeaderView(header);
adapter = new FastAssesmentListAdapter(getActivity(), assestments);
setListAdapter(adapter);
updateList(assestments);
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
private void updateList(Data[] assestments) {
// NOTE: addAll is not being used to support pre-honeycomb devices
synchronized(adapter) {
adapter.clear();
adapter.addAll(assestments);
adapter.notifyDataSetChanged();
}
}
#Override
public void onListItemClick(ListView parentView, View selectedItemView, int position, long id) {
String model = (String) parentView.getItemAtPosition(position);
((FacilityActivity) getActivity()).onItemSelected(model);
}
#Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
//outState.putInt("curChoice", mCurCheckPosition);
}
}
This is the layout I am trying to use for header
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingLeft="25dp"
android:paddingRight="10dp"
android:orientation="horizontal">
<TextView android:id="#+id/adapter_header_textview_column1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="4"
android:textColor="#color/defaultTextColor"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="28sp"
android:text="Assesment ID" />
<TextView android:id="#+id/adapter_header_textview_column2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="4"
android:textColor="#color/defaultTextColor"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="28sp"
android:text="Name" />
<TextView android:id="#+id/adapter_header_textview_column3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="3"
android:textColor="#color/defaultTextColor"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="28sp"
android:text="Date"/>
<CheckBox
android:id="#+id/header_check_box"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:color="#color/defaultTextColor"
android:layout_weight=".5"
android:gravity="center" />
</LinearLayout>
<View
android:layout_width="fill_parent"
android:layout_height="5dp"
android:background="#color/BPGreenColor" />
<ListView
android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
Then this is the array adapter I am using:
public class FastAssesmentListAdapter extends ArrayAdapter<Data> {
private static int LAYOUT_ID = R.layout.list_adapter_with_checkbox_three_column;
private final Data[] assesments;
private final Context context;
LinearLayout listHeader;
static class ViewHolder {
protected TextView column1;
protected TextView column2;
protected TextView column3;
protected CheckBox checkbox;
}
public FastAssesmentListAdapter(Context context, Data[] assesments) {
super(context, LAYOUT_ID, assesments);
this.context = context;
this.assesments = assesments;
}
//ListFragment and array adapter will automatically call this over and over to auto populate the list
#Override
public View getView(int position, View convertView, ViewGroup parent) {
final Data item = getItem(position);
// Formulate row view (create if it does not exist yet)
View view = convertView;
if(view == null) {
LayoutInflater inflater = ((Activity) getContext()).getLayoutInflater();
view = inflater.inflate(LAYOUT_ID, null);
final ViewHolder viewHolder = new ViewHolder();
viewHolder.column1 = (TextView) view.findViewById(R.id.adapter_textview_column1);
viewHolder.column2 = (TextView) view.findViewById(R.id.adapter_textview_column2);
viewHolder.column3 = (TextView) view.findViewById(R.id.adapter_textview_column3);
viewHolder.checkbox = (CheckBox) view.findViewById(R.id.check_box);
view.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getContext(), "Clicked",
Toast.LENGTH_SHORT).show();
Intent intent = new Intent(getContext(), FacilityActivity.class);
getContext().startActivity(intent);
}
});
if(viewHolder.checkbox != null) {
viewHolder.checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
if(isChecked) {
item.setSelected(isChecked);
Toast.makeText(getContext(), "Checked",
Toast.LENGTH_SHORT).show();
}
}
});
}
view.setTag(viewHolder);
viewHolder.checkbox.setTag(position);
}
ViewHolder viewHolder = (ViewHolder) view.getTag();
viewHolder.checkbox.setTag(position);
viewHolder.column1.setText(item.getColumn1());
viewHolder.column2.setText(item.getColumn2());
viewHolder.column3.setText(item.getColumn3());
viewHolder.checkbox.setChecked(item.isSelected());
return view;
}
}
on a side note, the onlistitemclicked in the fragment doesnt work, i have to set a listener in the adapter and then it works. any ideas on that? but mainly I need to figure out how to use a custom header and custom rows in the list view. Here is the layout for the rows
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingLeft="25dp"
android:paddingRight="10dp"
android:orientation="horizontal">
<TextView android:id="#+id/adapter_textview_column1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="4"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="25sp" />
<TextView android:id="#+id/adapter_textview_column2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="4"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="25sp" />
<TextView android:id="#+id/adapter_textview_column3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="3"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="25sp" />
<CheckBox
android:id="#+id/check_box"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight=".5"
android:gravity="center" />
</LinearLayout>