Android GUI refresh (no threads used) - android

I have an activity with an AutoCompleteTextView (text).
When i select an item the code below is executed:
text.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Bundle args= new Bundle();
for (Student s: studentsBook.getStudentsList()){
if (s.getName().equals(((TextView)view).getText().toString())){
args.putSerializable("Student",s);
break;
}
}
theLayout.setVisibility(View.GONE); //removing elements (button, textviews...)
addButton.setVisibility(View.GONE); //removing elements
//simply adding a fragment through supportfragmentmanager and fragment transactions
//Fragment receives arguments (args) which contain a string to be showed.
//A tag: "DataFragment" is provided in order to get the fragment back in other parts of code.
//getMainView returns the container in which the fragment has to be created/showed.
dataFragment=(StudentDataFragment)addFragment(StudentDataFragment.class,R.layout.student_data_fragment,getMainView().getId(),args,"DataFragment");
}
});
The fragment has only this method:
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
myLayout=(ViewGroup)inflater.inflate(R.layout.student_data_fragment,container,false);
student=(Student)getArguments().getSerializable("Student");
tv=((TextView)myLayout.findViewById(R.id.textView17));
tv.setText(student.toString());
return myLayout;
}
I can't see the string, it seems that the gui gets not updated, but if an orientation change happens the string appears..
I also managed the back button to remove the fragment if present and set visible the elements "gone". The code runs successfully but no gui refresh appears to be run.
No threads are involved in this situation, so i think we are in the UI-thread right?

Solved, just messing up the content view of the activity in other parts of code

Related

Physical Android Back Button does not respond when coming from a selected list item

My app has quite a few separate activity/fragment pairs, and relies on the Android universal back button for much of its navigation. This button works fine, EXCEPT when I'm trying to return from a DetailView activity back to a list of search results.
Here's what the search results code looks like:
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle saveInstanceState) {
View v = inflater.inflate(R.layout.results_fragment, container, false);
ListView lv;
lv = (ListView)v.findViewById(R.id.listViewResults);
lv.setAdapter(SearchResultsAdapter);
lv.setEmptyView(v.findViewById(R.id.emptyElement));
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapter, View view, int position, long id) {
ItemType selectedItem;
selectedItem = (ItemType)adapter.getItemAtPosition(position);
Intent i = new Intent(getActivity(), DetailViewActivity.class);
i.putExtra(DetailViewFragment.RESULT_ID, resultIdNumber);
startActivity(i);
}
});
// ... some other stuff
return v;
}
The DetailView is simply a collection of images and text.
The search returns expected results, and selecting the item shows the correct DetailViewFragment.
It seems like a very typical architecture, so I'm not sure why navigation back to the results page should be so problematic. I tried setting breakpoints to determine if the results activity ever restarted, but apparently it did not.
If you want to make something when the back button is pressed, you have to override it:
#Override
public void onBackPressed()
{
// code here
finish(); // to end activity:
}

Fragment with button clicks

Since, i am new to android, i am trying to learn fragments and how they work.I tried to make a length converter app which basically converts meter to centimeters.Simple, right?
Now I have two portions of the activity,one being the two edittexts which are the part of the activity layout, while the other one being the fragment.
This fragment basically contains keypad, in short, Numbers and operators displayed on it. Like a normal calci would have.
Now i read about the fragment life cycle and how it is supposed to work.
So The first thing that i did was to put everything in onCreateView method.
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, Bundle savedInstanceState) {
getRootView=inflater.inflate(R.layout.calci_keyboard,container,false);
GridLayout gridLayout=(GridLayout) getRootView.findViewById(R.id.calciKeyboardGrid);
for(int i=0;i<gridLayout.getChildCount();i++){
b=(Button)gridLayout.getChildAt(i);
b.setBackground(getResources().getDrawable(R.drawable.button_dark_gradient));
b.setOnClickListener(this);
}
return getRootView;
}
The thing is that, click events work but edittext settext doesn't seem to work. Edittexts are behaving weirdly.
Now, to remove that i thought i am accessing the Activity UI's , so i should do this inside onActivityCreated function ,So, i tried this too.
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, Bundle savedInstanceState) {
getRootView=inflater.inflate(R.layout.calci_keyboard,container,false);
return getRootView;
}
#Override
public void onActivityCreated(#Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
#SuppressLint("InflateParams") View getView=(GridLayout) LayoutInflater.from(getActivity()).inflate(R.layout.calci_keyboard,null,false);
GridLayout gridLayout=(GridLayout) getView.findViewById(R.id.calciKeyboardGrid); // i Logged this obj and it was there
for(int i=0;i<gridLayout.getChildCount();i++){
b=(Button)gridLayout.getChildAt(i);
b.setBackground(getResources().getDrawable(R.drawable.button_dark_gradient));
b.setOnClickListener(this);
}
}
When i do things this way i don't seem to get my clicks working?
How am i supposed to do this problem? Can't find any solution.
Go easy on me,Thanks :)
Below one shows my onClick event:.
public void onClick(View view) {
View focussedChild=getActivity().getCurrentFocus();
switch (view.getId()){
case R.id.calciKeyboardNine:{
if (focussedChild instanceof EditText) {
firstPart=new StringBuilder("");
secondPart=new StringBuilder("");
EditText editText=(EditText)focussedChild;
if(focussedChild.getId()==R.id.lengthConverterFirst){
if(!TextUtils.isEmpty(firstPart.toString()))
firstPart.replace(0,firstPart.length()-1,editText.getText().toString()+"9");
firstPart.append("9");
editText.setText(firstPart.toString());
editText.setSelection(editText.length());
int a=Integer.parseInt(firstPart.toString());
a=a*100;
editText=getActivity().findViewById(R.id.lengthConverterSecond);//second edit text
editText.setText(Integer.toString(a));
editText.setSelection(editText.length());
}else if(focussedChild.getId()==R.id.lengthConverterSecond){
if(!TextUtils.isEmpty(secondPart.toString()))
secondPart.replace(0,secondPart.length()-1,editText.getText().toString()+"9");
secondPart.append("9");
editText.setText(secondPart.toString());
editText.setSelection(editText.length());
double a=Integer.parseInt(firstPart.toString());
a=a/100;
editText=getActivity().findViewById(R.id.lengthConverterFirst);//first edit text
editText.setText(Double.toString(a));
editText.setSelection(editText.length());
}
}
}
}
}
Now, to remove that i thought i am accessing the Activity UI's , so i should do this inside onActivityCreated function ,So, i tried this too - it didn't work because onActivityCreated() is method of fragment not activity.
try this - just make your edittexts static in activity and then you can access them in fragment by the activity's name like MainActivity.editText(). hope this helps

How to show image from inside the nested fragments?

Although there are many questions about nested fragments but still it got me stumped in this case.I am making an android app which has an Activity having a frame layout.I am loading a fragment(say Fragment B) into the frame layout.Fragment B has a ViewPager that contains two fragments.First fragment of viewpager(say Fragment VP1) has a gridview that loads images from network.Now on griditem image click I want to show the image in full size in a new fragment which has NetowrkImageView. How do i do this? I tried need to call getChildFragmentManager(),but didn't work. If you guys need code I'll show that. Thanks in advance.
onCreateView() of VP1
public View onCreateView(LayoutInflater inflater, #Nullable final ViewGroup container, #Nullable Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.fragment_images_and_videos,null);
gridItemList=new ArrayList<>();
gridview= (GridView) view.findViewById(R.id.gridview);
adapter=new ImagesVideosGridviewAdapter(getActivity(),gridItemList);
gridview.setAdapter(adapter);
gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// GridItemModel gridItem= (GridItemModel) parent.getItemAtPosition(position);
Bundle args=new Bundle();
args.putString("url",gridItemList.get(position).getUrl());
FragmentTransaction transaction=getChildFragmentManager().beginTransaction();
transaction.replace(R.id.frame_container_event_specific,new ShowFullImageFragment()).commit();//frame_container_event_specific is present in the main activity.
}
});
new LoadMedia().execute("");
return view;
}
Logcat error
java.lang.IllegalArgumentException: No view found for id 0x7f0d008a (com.test.rajat.a10times:id/frame_container_event_specific) for fragment ShowFullImageFragment{ccc6a0 #0 id=0x7f0d008a}
Because your VP1 layout doesn't contain R.id.frame_container_event_specific, the fragment manager is unable to find the view. It's in the activity layout, so you better let the fragment manager from the activity to replace the fragment.
But think again about using the fragment approach. I think it's much simpler and easier to make the full image fragment as a standalone Activity. It displays full image anyway. Otherwise, even though you are able to replace the fragment correctly, you have to do a lot to manage the fragment back stack, like replacing the previous fragment when user hits back button.

Way to include two fragment in one fragment in android

Currently there are two fragment : one for the area for adding image view, text view.
The other is a list fragment
I would like to include both in one fragment , that means the area fragment is at the top of the list fragment , however, they are two class so how to include them , or I need to re-arrange the code to one class?
Also, how do I change the list fragment to fragment ?(Since setadapter and onclick event are not available in fragment class).Thanks.
code example : the List fragment part
public class SlidingMenuListFragment extends ListFragment {
protected List<SlidingMenuListItem> slidingMenuList;
private SlidingMenuBuilderBase slidingMenuBuilderBase;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// We set here a custom layout which uses holo light theme colors.
return inflater.inflate(R.layout.sliding_menu_holo_light_list, null);
}
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
// We get a list from our specially created list data class.
slidingMenuList = SlidingMenuList.getSlidingMenu(getActivity());
if (slidingMenuList == null)
return;
// We pass our taken list to the adapter.
SlidingMenuListAdapter adapter = new SlidingMenuListAdapter(
getActivity(), R.layout.sliding_menu_holo_light_list_row, slidingMenuList);
setListAdapter(adapter);
}
// We could define item click actions here, but instead we want our builder
// to be responsible for that.
#Override
public void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
l.setSelection(position);
SlidingMenuListItem item = slidingMenuList.get(position);
slidingMenuBuilderBase.onListItemClick(item);
}
// We can not provide a builder as an argument inside a fragment
// constructor, so that is why we have separate method for that.
public void setMenuBuilder(SlidingMenuBuilderBase slidingMenuBuilderBase) {
this.slidingMenuBuilderBase = slidingMenuBuilderBase;
}
Since Android 4.2, you can use Nested Fragments.
And to older versions, you should use the Support Library

Convert my application to Fragments: How to handle activities?

I'm planning to convert an existing android application to fragments layout.
The idea is to have the classical two panel layout (item list on left, and details on right).
Actually the application is composed by 4 activites:
A ChoiceListActivity with all the available options
3 different activities, one for each operation available on the tool.
Now i started to work on the conversion and i created a FragmentActivity classs, that is the main class:
public class MainFragment extends FragmentActivity {
private static final String TAG = "MainFragment";
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_main);
if(findViewById(R.id.fragment_container)!=null){
Log.i(TAG, "No Tablet");
Intent i = new Intent(MainFragment.this, main.ChoiceActivity.class);
startActivity(i);
} else {
Log.i(TAG, "Tablet");
}
}
}
And i created a ChoiceListFragment:
`
public class ChoiceListFragment extends ListFragment {
#Override
public void onListItemClick(ListView l, View v, int position, long id) {
Toast.makeText(getActivity(), getListView().getItemAtPosition(position).toString(), Toast.LENGTH_LONG).show();
super.onListItemClick(l, v, position, id);
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
String[] options = getResources().getStringArray(R.array.listitems);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(inflater.getContext(), R.layout.list_item, options);
setListAdapter(adapter);
return super.onCreateView(inflater, container, savedInstanceState);
}
}
That fragment will be the left side of the panel.
My problem is for the right side. The idea is that for every element of the list the corresponding activity (or fragment?) will be shown.
So what is the correct way?
Is a good idea to start an activity in the right fragment when the user select an item?
Or i must switch between fragments programmatically? And how to do that (i found many tutorials, but they use always the same activity for the right panel changing some data inside it)?
I have created the following class for the right fragment (but i'm not sure that i'm doing it correctly):
public class RightFragment extends Fragment {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.main, container, false);
}
}
I noticed that i can eventually change the layout using the LayoutInflater object during onCreate method, but this simply siwtch the layout on the screen, The objects declared in the layout aren't initialized (nor eventListener added, etc). So how to do that?
Maybe i should Create an Intent and use startActivity to launch the existing activities, or this is a bad idea into a fragment?
Actually the xml layout is:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<fragment
android:id="#+id/choicelist_fragment"
android:name="main.fragments.ChoiceListFragment"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" />
<fragment
android:id="#+id/right_fragment"
android:name="main.fragments.RightFragment"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="2" />
</LinearLayout>
Ok i found myself the solution, it was not clear on a first moment, but reading some documentation, looking at many tutorials maybe i understand how it works.
First of all i removed the second fragment *right_fragment* from the layout (check the question), i replaced it with an empty FrameLayout called *activity_container* that will be the container of my fragments.
The idea behind is simply use the FragmentManager to replace the fragment inside the container.
So i updated the onListItemClick method into the ChoiceListFragment, and depending on what is the list item tapped, it creates a new Fragment and replace it into the *activity_container*. The updated method is similar to the following:
public void onListItemClick(ListView l, View v, int position, long id) {
String itemName = getListView().getItemAtPosition(position).toString();
switch(position){
case OPTION_ONE: getFragmentManager().beginTransaction().replace(R.id.activity_container, new OptionOneFragment()).commit();
break;
case RESISTOR_VALUE:
getFragmentManager().beginTransaction().replace(R.id.activity_container, new OptionTwoFragment()).commit();
break;
default:
Toast.makeText(getActivity(), getListView().getItemAtPosition(position).toString(), Toast.LENGTH_LONG).show();
break;
}
super.onListItemClick(l, v, position, id);
}
In that way every component of the application has its own fragment, handled by a different class.
You're on the right track. On small screens, clicking a list item starts a DetailActivity, which is a simple wrapper around a DetailFragment. On a larger screen, clicking the list item would replace the right hand side with a new instance of DetailFragment.
If you are using eclipse and ADT, I would suggest taking a look at the MasterDetailFlow template, which can be accessed by creating a new Android project or a new Android Activity.

Categories

Resources