Screen loads from middle in Android - android

I am creating an app similar to flipkart. I have viewpager with images and I have horizontal scroll gridview to load products like new products, favourite products, best products so on.
Everything is loading fine accept that strange issue when I open the app the app automatically shows from the middle.
How do I force it to show the screen from top rather than middle.
For Slideshow I am using viewpager and for horizontal scrollview I am using TwoWayGridView API.
Has anybody come across this issue? How do I force the app to run from the top?
Please check the screenshot below:
The above screen shows the screen loads in the middle and not from top.
PS: This is from flipkart app. Showing how it would show on my app? Sorry not allowed to share the app's design part.
#SuppressWarnings({"unchecked" })
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
if (rootView != null)
{
ViewGroup parent = (ViewGroup) rootView.getParent();
if (parent != null)
parent.removeView(rootView);
}
try
{
rootView = inflater.inflate(R.layout.main, container, false);
}
catch (InflateException e)
{
}
TextView[] bestproduct = new TextView[]
{
(TextView) rootView.findViewById(R.id.bestproduct1),
(TextView) rootView.findViewById(R.id.bestproduct2),
};
RelativeLayout [] relativeLayout = new RelativeLayout[]
{
(RelativeLayout) rootView.findViewById(R.id.relativeLayout1),
(RelativeLayout) rootView.findViewById(R.id.relativeLayout2),
};
bannerItems = new ArrayList<String>();
newproductList = new ArrayList<ProductFrontScreenDetails>();
dbmaster = new dbMaster(getActivity());
customerID = dbmaster.getCustID();
final ScrollView main = (ScrollView) rootView.findViewById(R.id.scrollView1);
main.scrollTo(0,0);
progressContainer = rootView.findViewById(R.id.progressContainer);
progressContainer.setVisibility(View.INVISIBLE);
viewPager = (ViewPager)rootView.findViewById(R.id.myviewpager);
myPagerAdapter = new MyPagerAdapter();
viewPager.setAdapter(myPagerAdapter);
mIndicator = (CirclePageIndicator)rootView.findViewById(R.id.indicator);
mIndicator.setViewPager(viewPager);
TAG_SUCCESS = getResources().getString(R.string.tagsuccess);
TAG_DETAIL = getResources().getString(R.string.tagdetails);
TAG_IMAGE = getResources().getString(R.string.productimage);
TAG_NAME = getResources().getString(R.string.productname);
TAG_BANNER_IMAGE = getResources().getString(R.string.bannerimage);
TAG_PRICE = getResources().getString(R.string.productprice);
TAG_DISCOUNT = getResources().getString(R.string.productdiscount);
FetchBannerImagesData();
gridViewnew = (TwoWayGridView) rootView.findViewById(R.id.newproductgridview);
gridViewnew.setOnItemClickListener(new OnItemClickListener()
{
#Override
public void onItemClick(TwoWayAdapterView<?> parent, View view, int position, long id)
{
}
});
gridView1 = (TwoWayGridView) rootView.findViewById(R.id.gridView1);
gridView1.setOnItemClickListener(new OnItemClickListener()
{
#Override
public void onItemClick(TwoWayAdapterView<?> parent, View view, int position, long id)
{
}
});
gridView2 = (TwoWayGridView) rootView.findViewById(R.id.gridView2);
gridView2.setOnItemClickListener(new OnItemClickListener()
{
#Override
public void onItemClick(TwoWayAdapterView<?> parent, View view, int position, long id)
{
}
});
return rootView;
}

Related

Dialog with Gridview sometimes doesn't load

I'm making an app that makes use of a GridView to display subcategories.
I have the categories displayed on screen and when clicked a dialog appears with a GridView displaying subcategories and a button to view everything in that category.
Here's the problem:
Sometimes the GridView doesn't load on the first click, I have to close it and then click on the category again for it to open. Categories are basically images with a textview on top. Pictures are loaded from the Internet.
Code for Dialog:
public class dialogFragment extends android.support.v4.app.DialogFragment {
private Button mViewAll;
private GridView mSubCatsView;
public ArrayList<category> mCats = new ArrayList<>();
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.dialog_fragment, container);
mCats.addAll(((shopActivity)getActivity()).mDialogCats);
mViewAll = v.findViewById(R.id.viewAllBTN);
mSubCatsView = v.findViewById(R.id.subCatsView);
final gridAdapter adapada = new gridAdapter(getActivity(), mCats);
mSubCatsView.setAdapter(adapada);
mSubCatsView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
ArrayList<category> subSubCats = new ArrayList<>();
for (int y = 0; y < ((shopActivity)getActivity()).mCategoriesAll.size(); y++){
if(mCats.get(i).getId() == ((shopActivity)getActivity()).mCategoriesAll.get(y).getParent()){
subSubCats.add(((shopActivity)getActivity()).mCategoriesAll.get(y));
}
}
if(subSubCats.size() > 0){
mCats.clear();
mCats.addAll(subSubCats);
adapada.notifyDataSetChanged();
}
}
});
mViewAll.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
}
});
return v;
}
}
If anyone else has this problem, I fixed it by using a RecyclerView + GridLayoutManager to mimic a GridView.

SearchView inside a fragment containing ListView?

I am using fragments in my app. Every fragment has a listview. The ListViews are custom listviews containing a single image view.
<ImageView
android:id="#+id/imageView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
app:srcCompat="#android:drawable/ic_notification_overlay" />
The listview items are images. I want to implement searchview. I want to search by name of each listview item eg "jingle bells etc" and also by number i.e "1,2. . . and so on.
My java file containing listview titles is.
public class carolFrag extends Fragment {
//code
public carolFrag() {
// Required empty public constructor
}
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
}
private static final String TAG = "carolFrag";
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup main_content, #Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_carol, main_content, false);
final int[] menuImage = {R.drawable.carol_1_title, R.drawable.carol_2_title,R.drawable.carol_3_title,R.drawable.carol_4_title,R.drawable.carol_5_title,R.drawable.carol_6_title,R.drawable.carol_7_title,R.drawable.carol_8_title,R.drawable.carol_9_title,R.drawable.carol_10_title,R.drawable.carol_11_title};
final ListView listView = (ListView) view.findViewById(R.id.CarolList);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
Intent intent1 = new Intent (getActivity(), PackageC.class)
intent1.putExtra("position", position);
startActivity(intent1);
}
});
AdapterCarols adapter = new AdapterCarols(getContext(), menuImage);
listView.setAdapter(adapter);
// Inflate the layout for this fragment
return view;
}
}
I am unable to make any logic as the array has drawables in it.
Because the array is full of drawables, and it's not a database thing or anithing every solution you i'll get will be a work around. The simples, i believe, is to have another array maybe an - String[] - with the names of the drawables in the same order they are in the original - final int[] menuImage - you can make the search in the array of String and save the position of the results. Then use those positions to create a new array ( int[] ) putting in this new array the drawables that matches those positions. Finally change the adapter of the list view, passing the array with the results and notifydatachange(); in the listview for it to update visually.
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup
main_content, #Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_carol, main_content, false);
final int[] menuImage = {R.drawable.carol_1_title, R.drawable.carol_2_title,R.drawable.carol_3_title,R.drawable.carol_4_title,R.drawable.carol_5_title,R.drawable.carol_6_title,R.drawable.carol_7_title,R.drawable.carol_8_title,R.drawable.carol_9_title,R.drawable.carol_10_title,R.drawable.carol_11_title};
final ListView listView = (ListView) view.findViewById(R.id.CarolList);
SearchView sv = view.findviewById(R.id.searchview);
sv.setOnQueryTextListener(new SearchView.OnQueryTextListener()
{
public boolean onQueryTextChange(String par1)
{
return false;
}
public boolean onQueryTextSubmit(String par1)
{
int [] searchresults = PerformSearch(par1);
AdapterCarols newadapter = new
AdapterCarols(getContext(), searchresults);
listView.setAdapter(adapter);
listview.invalidate(); // don't remember listview refresh method. I work with Recyclerview, they have a notifyDatasetChange(); method.
}
});
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
Intent intent1 = new Intent (getActivity(), PackageC.class)
intent1.putExtra("position", position);
startActivity(intent1);
}
});
AdapterCarols adapter = new AdapterCarols(getContext(), menuImage);
listView.setAdapter(adapter);
// Inflate the layout for this fragment
return view;
}
private int[] PerformSearch(par1)
{
String [] strcarols = {carol_1_title,carol_2_title,carol_3_title,carol_4_title,carol_5_title,carol_6_title,carol_7_title,carol_8_title,carol_9_title,carol_10_title,carol_11_title};
int[] menuImage = {R.drawable.carol_1_title, R.drawable.carol_2_title,R.drawable.carol_3_title,R.drawable.carol_4_title,R.drawable.carol_5_title,R.drawable.carol_6_title,R.drawable.carol_7_title,R.drawable.carol_8_title,R.drawable.carol_9_title,R.drawable.carol_10_title,R.drawable.carol_11_title};
int[] results = new int[];
int carolsresultcount = 0, index =0;
/// simplest way
for(String carol:strcarols)
{
if(carol.contains(par1))
{
results [carolsresultcount++] = menuImage [index];
}
index++;
}
return results;
}
I hope this can solve your problem.

Refresh fragment listview on Activity back button press

I have a fragment which contains a listview of some items. On click of an item in the list view an Activity is started to view the details about the list view. There is a back button on the Activity which brings us back to the fragment page. Once the back button is pressed and the activity page closes I want the data to be refreshed in the fragment list view. I have tried onResume() method but it does not refresh the data.
Can someone please help me how to achieve this am pretty new at this.
public class FavCommittee extends Fragment {
public ArrayList<Committee> favCommitteeData = new ArrayList<>();
#Nullable
#Override
public void onResume(){
super.onResume();
final SharedPreferences pref = getContext().getSharedPreferences("MyFav", 0);
final SharedPreferences.Editor editor = pref.edit();
View rootView =getView();
Map<String,?> entries = pref.getAll();
final Set<String> keys = entries.keySet();
int count=0;
for (String key:keys) {
String val = pref.getString(key, null);
String[] store = val.split(":");
if (store[0].equals("committee"))
count=count+1;
}
for (int i=0;i<keys.size();i++) {
String val = pref.getString(Integer.toString(i), null);
if (val != null) {
String[] store = val.split(":");
if (store[0].equals("committee")) {
count = count - 1;
new GetAllCommittees(getContext(), rootView).execute(store[1], Integer.toString(count));
}
}
} final ListView yourListView = (ListView) rootView.findViewById(R.id.house_listview);
yourListView.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
Committee obj = (Committee)yourListView.getItemAtPosition(position);
Intent intent = new Intent(FavCommittee.this.getActivity(), committee_info.class);
intent.putExtra("Committee",obj.getCommittee_id());
startActivity(intent);
}
});
}
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.house, container, false);
return rootView;
}
When you are transitioning between Fragments, call addToBackStack() as part of your FragmentTransaction:
FragmentTransaction tx = fragmentManager.beginTransation();
tx.replace( R.id.fragment, new MyFragment() ).addToBackStack( "tag").commit();
If you require more detailed control (i.e. when some Fragments are visible, you want to suppress the back key) you can set an OnKeyListener on the parent view of your fragment:
//You need to add the following line for this solution to work,
fragment.getView().setFocusableInTouchMode(true);
fragment.getView().requestFocus();
fragment.getView().setOnKeyListener( new OnKeyListener()
{
#Override
public boolean onKey( View v, int keyCode, KeyEvent event )
{
if( keyCode == KeyEvent.KEYCODE_BACK )
{
return true;
}
return false;
}
} );
getSupportFragmentManager().beginTransaction().add(R.id.fragContainer, new FavCommittee (), FavCommittee .class.getName()).commit();
Try this code.
Put the code where u add your Fragment in Activity

ListView shows not correct data when scrolling after update date base

I put a list of exercises in the lesson. The user can modify this exercise to show or not. When he clicks on the exercise, changing the status (show or not show) exercise.
All changes made to the database works fine. But when the user made ​​the change, when you scroll through the picture "check" is displayed not correctly in emerging lines.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
....
db = new MyDatabase(this);
getOnListExes();
db.close();
}
....
public void getOnListExes() {
onListExesChek = db.getListExes(prog_man, prog_woman, orderBy);
sAdapter = new SimpleCursorAdapter(this,
R.layout.exeslist, onListExesChek,
new String[] {"exes_bodypart", "exes_name", "exes_name"},
new int[] {R.id.exesPartlist_chek, R.id.exesNamelist_chek,
R.id.chek_img}) {
public View getView(int position, View convertView, ViewGroup parent) {
View row = super.getView(position, convertView, parent);
TextView exesPartlist_chek =
(TextView) row.findViewById(R.id.exesPartlist_chek);
TextView exesNamelist_chek =
(TextView) row.findViewById(R.id.exesNamelist_chek);
ImageView imageGender =
(ImageView) row.findViewById(R.id.chek_img);
return row;
}
};
sAdapter.setViewBinder(new MyViewBinder());
listExesChek.setAdapter(sAdapter);
}
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
positionChek = position;
selectExe_id = Long.toString(id);
db = new MyDatabase(this);
onListExesChek = db.getListExes(prog_man, prog_woman, orderBy);
onListExesChek.moveToPosition(positionChek);
if (
onListExesChek.getInt(onListExesChek.getColumnIndex("prog_man_chek"))
>= Integer.valueOf(prog_man).intValue() &
onListExesChek.getInt(onListExesChek.getColumnIndex("prog_woman_chek"))
>= Integer.valueOf(prog_woman).intValue()) {
if(prog_man.equals("1")) {
prog_man_chek = new ContentValues();
prog_man_chek.put("prog_man_chek", "0");
int upProg_man_chek = db.setExe(prog_man_chek,
selectExe_id);
}
if(prog_woman.equals("1")) {
prog_woman_chek = new ContentValues();
prog_woman_chek.put("prog_woman_chek", "0");
int upProg_woman_chek = db.setExe(prog_woman_chek,
selectExe_id);
} else {
if(prog_man.equals("1")) {
prog_man_chek = new ContentValues();
prog_man_chek.put("prog_man_chek", "1");
int upProg_man_chek = db.setExe(prog_man_chek,
selectExe_id);
}
if(prog_woman.equals("1")) {
prog_woman_chek = new ContentValues();
prog_woman_chek.put("prog_woman_chek", "1");
int upProg_woman_chek = db.setExe(prog_woman_chek,
selectExe_id);
}
}
db.close();
updateView(position);
}
void updateView(int index){
db = new MyDatabase(this);
onListExesChek = db.getListExes(prog_man, prog_woman, orderBy);
onListExesChek.moveToPosition(positionChek);
View v = listExesChek.getChildAt(index -
listExesChek.getFirstVisiblePosition());
if (
onListExesChek.getInt(onListExesChek.getColumnIndex("prog_man_chek"))
>= Integer.valueOf(prog_man).intValue() &
onListExesChek.getInt(onListExesChek.getColumnIndex("prog_woman_chek"))
>= Integer.valueOf(prog_woman).intValue()
) {
ImageView imageGender = (ImageView) v.findViewById(R.id.chek_img);
imageGender.setImageResource(R.drawable.check);
} else {
ImageView imageGender = (ImageView) v.findViewById(R.id.chek_img);
imageGender.setImageResource(R.drawable.notchek);
}
db.close();
}
class MyViewBinder implements SimpleCursorAdapter.ViewBinder {
public boolean setViewValue (View view, Cursor cursor, int columnIndex) {
switch (view.getId()) {
case R.id.chek_img:
if (onListExesChek.getInt(onListExesChek.
getColumnIndex("prog_man_chek"))
>= Integer.valueOf(prog_man).intValue() &
onListExesChek.getInt(onListExesChek.getColumnIndex("prog_woman_chek"))
>= Integer.valueOf(prog_woman).intValue()
) {
((ImageView) view).setImageResource(R.drawable.check);
} else {
(
(ImageView) view).setImageResource(R.drawable.notchek);
}
return true;
}
return false;
}
}
It's a side-effect of ListView's view recycling. You're not setting a default resource in the getView method so the ImageView will "keep" whatever you set it to in the updateView method. This effect is really noticeable in longer lists where you'll see a repeating incorrect state.
You should be able to fix this by setting the state of the ImageView within your getView method.
ListViews recycle views, which means at first a base set of list entries is inflated from XML.
for more check
android listview displays false data after scrolling (custom adapter)
ListView is not showing correct values after scrolling
both works for me

Using Intent with actions

Right. I have an action that needs to call another activity. As I understand this, I need to use Intents to do so if I want to parse values to this activity.
However, my code fails and im a little lost as to why.
My main activity:
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);
final ArrayList<menuItem> AMI = new ArrayList<menuItem>();
/*Menu item: String name, String menu ID*/
/*ToDo: Logic to fecth new menu structure from server*/
menuItem MI1 = new menuItem("menu item 1","1");
menuItem MI2 = new menuItem("menu item 2","2");
AMI.add(MI1);
AMI.add(MI2);
GridView gridview = (GridView) findViewById(R.id.GridView01);
gridview.setAdapter(new menuAdapter(this, AMI));
gridview.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> parent, View v, int position, long id)
{
//Toast.makeText(Runner.this, AMI.get(position).getMenuID(), Toast.LENGTH_SHORT).show();
Intent myIntent = new Intent(v.getContext(), showMenu.class);
myIntent.putExtra("parentID", AMI.get(position).getMenuID());
startActivityForResult(myIntent, 0);
}
});
The "Toast" works just fine, however when I call the showMenu class it crashes.
The showMenu class looks as follows:
public class showMenu extends Activity{
public String menuParent = "";
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.submenu);
Bundle extras = getIntent().getExtras();
if(extras !=null)
{
menuParent = extras.getString("parentID");
}
/*ToDo: Logic to fecth new menu structure from server*/
final ArrayList<menuItem> AMI = new ArrayList<menuItem>();
menuItem MI1 = new menuItem("submenu 1","1");
menuItem MI2 = new menuItem("submenu 2","2");
AMI.add(MI1);
AMI.add(MI2);
GridView gridview = (GridView) findViewById(R.id.GridView01);
gridview.setAdapter(new subMenuAdapter(this, AMI));
}
public class subMenuAdapter extends BaseAdapter {
private ArrayList<menuItem> MIL = new ArrayList<menuItem>();
public static final int ACTIVITY_CREATE = 10;
public subMenuAdapter(Context c, ArrayList<menuItem> AMI) {
MIL = AMI;
}
public int getCount() {
return MIL.size();
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
public View getView(int position, View convertView, ViewGroup parent) {
View v;
if (convertView == null) {
LayoutInflater li = getLayoutInflater();
v = (LinearLayout) li.inflate(R.layout.grid_item, null);
TextView tv = (TextView)v.findViewById(R.id.grid_text);
tv.setText(MIL.get(position).getMenuname());
} else {
v = convertView;
}
return v;
}
}
}
Any idea why it crashes?
I think you have to register your Intent in your AndroidManifest.xml
<activity
android:name="Package.Name.showMenu"
android:theme="#android:style/Theme.Light"></activity>
As both activities are from same application, there are some other ways as well to pass data between instead of intents. check the link below:
http://developer.android.com/resources/faq/framework.html#3
If you want to use only intents, can you please specify the error you are facing, so that someone can reply you correctly.
if you want to parse values to another activity,you must use startActivityForResult.This is called sub_activity.

Categories

Resources