Android ViewFlipper How to Add Duplicate Children & Change Specific EditText in Each - android

I have set up a ViewFlipper in my Android application that will host a series of windows to display messages. Each window should correspond to a different set of messages, similar to multiple open chats. For each window I am using the same window.xml view to bring the view onto the screen, it also has the variable for the EditText need to edit.
For reference I am creating and adding children as follows:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
detector = new GestureDetector(this,this);
setContentView(R.layout.viewflip);
flipper = (ViewFlipper) findViewById(R.id.viewflip);
addChild(flipper);
addChild(flipper);
}
private void addChild(ViewFlipper flip){
int index=0;
View view = getView();
if(flip.getChildCount()==0){
flip.addView(view,index);
}
else{
flip.addView(view,flip.getChildCount());
}
}
private View getView(){
LayoutInflater inflater = this.getLayoutInflater();
View view = inflater.inflate(R.layout.window, null);
return view;
}
As you can see, I am basically duplicating the view (I'm not sure this is the proper approach to my design). So if I were to do something in the onCreate function after adding the children, such as
EditText messageHistoryText = (EditText) findViewById(R.id.messageHistory);
messageHistoryText.append("Testing :\n");
I see the text on both windows.
I thought that something like this would be better:
View v1 = flipper.getChildAt(1);
EditText messageHistoryText2 = (EditText) v1.findViewById(R.id.messageHistory);
messageHistoryText2.append("Testing2 :\n");
but when I use that one, I don't see anything at all. Perhaps there is a mistake on adding the children. Perhaps I can't use the same view, or perhaps I am selectively changing an EditText in an incorrect way.
Tips?

Try the following,
EditText messageHistoryText = flip.getChildAt(j).findViewById(R.id.messageHistory);
where j represents the child position in ViewFlipper.

Related

Dynamically add a button to a view from a listview Adapter GetView method

As a little eperiment, I'm trying to do the following.
I have an AXML describing a vertical linear layout which contains a listview (only filling 200dp of the vertical linear layout ). The AXML is inflated when the activity starts with SetContentView. Then the listview is correctly populated with values using its Adapter.
In the GetView method of the listview Adapter, I am trying to also dynamically create a button and add it to the linear layout, but for some reason the button is not added.
If I try to add the button in the constructor method of the Adapter instead, it is correctly added.
Can you tell me what could be possibly going wrong?
Let me add some code:
class TracksAdapter : BaseAdapter<string> {
Activity context;
List<Dictionary<string,string>> trackList;
// constructor
public TracksAdapter (Activity context, List<Dictionary<string,string>> trackList) {
this.context = context;
this.trackList = trackList;
// Just as a little test, if I create the button from here it will be correctly added to linear layout:
var ll = context.FindViewById<LinearLayout>(Resource.Id.linLayForResultsActivity);
Button b1 = new Button(context);
b1.Text = "Btn";
ll.AddView(b1);
}
public override View GetView(int position, View oldView, ViewGroup parent) {
// if I create the button from here it will not be added to the layout
var ll = context.FindViewById<LinearLayout>(Resource.Id.linLayForResultsActivity);
Button b1 = new Button(context);
b1.Text = "Btn";
ll.AddView(b1);
// this other code is working
View view = context.LayoutInflater.Inflate(Resource.Layout.ResultItem, null);
var artistLabel = view.FindViewById<TextView>(Resource.Id.resultArtistNameTextView);
artistLabel.Text = trackList[position]["trackArtistName"];
return view;
}
}
Update: adding some more context information because I know this can be a bit weird to understand without it:
In GetView, I don't need to return the new button I am trying to create there. GetView only need to return a listview view item, but, along its execution, GetView also has to create and add a button to the linear layout containing the listview.
The real code is much more complex than that. I have simplified it in the question. In the real code, the listview items are made of text and a button. The GetView also attaches event handlers to the buttons. Then what I need is, when a user clicks a button in any of the listview items, another button is added below the listview. So I need the code for adding another button to be in GetView, and the button needs to be added outside of the listview, ie. to the linear layout containing the listview.
Use the LayoutInflator to create a view based on your layout template, and then inject it into the view where you need it.
LayoutInflater vi = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = vi.inflate(R.layout.your_layout, null);
// fill in any details dynamically here
TextView textView = (TextView) v.findViewById(R.id.a_text_view);
textView.setText("your text");
// insert into main view
ViewGroup insertPoint = (ViewGroup) findViewById(R.id.insert_point);
insertPoint.addView(v, 0, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT));
I looked in you code, you are returning view, while you add the button to ll, you should return ll
what you return in getView() is what you see in the list item layout, since you're adding the button to ll and returning view, the button won't appear.
you can add the button to view as you implementation
Also check this:
Try using boolean addViewInLayout (View child, int index, ViewGroup.LayoutParams params)
http://developer.android.com/reference/android/view/ViewGroup.html#addViewInLayout(android.view.View, int, android.view.ViewGroup.LayoutParams)
It's working... Without making any changes now it's working as it should... ! Ugh!
I really don't know what I was doing wrong here... probably it was because of some sort of caching of older version of the installed APK.. ? I know this sort of stuff can happen, and that's why I've always been uninstalling the app before deplyoing the new version to the device... but still...!

Gallery: Effect in item selected

I need to have an scroll with items together, and the selected item should expand a part down.
I am currently using a Gallery (I tried with viewflow and viewpager, but the items have much space between them), but I need to know how can I do this effect.
I have 2 ideas, but i don't know how can I implement it.
1) The expandable part is a LinearLayout with visibility=gone, and when the item is selected, this layout should be visible. (Gallery do not have "onItemSelectedListener")
2) Treat each element as a fragment (once I use a Viewpager that use this, https://github.com/mrleolink/SimpleInfiniteCarousel)
It does not necessarily have to be a gallery, any idea is welcome
I am working on an Activity.
Depends on the behavior that you want. Some questions can more than one item be expanded at a time? Do you want the views to be paged (snap into place) or smooth scroll them?
One Suggestion I have is to make a custom view for the individual cells. Then add them programmatically to a HorizontalScrollView Object.
HorizontalScrollView hsv = new HorizontalScrollView(activity);
LinearLayout hll = new LinearLayout(activity);
hll.setOrientation(LinearLayout.HORIZONTAL);
for(int i=0;i<items.length();i++){
hsv.addView(new CustomExpandView(item));
}
The CustomExpandView would be used for your cells and could be something like this...
public class CustomExpandView extends RelativeLayout implements OnClickListener {
MyActivity mActivity = null;
ImageView ivImage, ivOverImage;
RelativeLayout rlView;
public CustomExpandView(Context context) {
super(context);
initialize();
}
public CustomExpandView(Context context, AttributeSet attrs) {
super(context, attrs);
initialize();
}
public void initialize() {
mActivity = (MyActivity) this.getContext();
LayoutInflater inflater = (LayoutInflater) mActivity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.custom_cell_expand, this, true);
//you can initialize subviews here
rlView = (RelativeLayout) getChildAt(0);
ivImage = (ImageView) rlView.getChildAt(0);
ivOverImage = (ImageView) rlView.getChildAt(1);
rlView.setOnFocusChangeListener(new OnFocusChangeListener(){
#Override
public void onFocusChange(View v, boolean hasFocus) {
LinearLayout expand = v.findViewById(R.id.view_i_want_to_expand);
if(hasFocus)
expand.setVisibility(View.VISIBLE);
else
expand.setVisibility(View.GONE);
}
});
}
You gave the answer yourself. You can use a ViewPager, with fragments, and have an animation to extend the lower part of the window. Depends on whether you want the windows to be full screen or not. A viewpager doesn't necessarily need fragments, you can use ordinary views, and an appropriate adapter. Just play with it and see which solution you like most.
Next time, just create the code and the app, and ask a much more specific question, with code to illustrate the issue you're experiencing.
You could simply define a TableView with just one TableRow (or as many as you need) and set a onClickListener for each of those Views inside the TableRow, which would make that on any click, the selected View would expand itself.
I don't know whether you'll have a static number of Views inside that row or you'll construct them dynamically, but this should work for any of them, the real "work" here about populating that row.
Once you have your row of Views, simply declare an onClickListener() on each of them. For example, this should be enough:
OnClickListener myListener = new OnClickListener() {
#Override
public void onClick(final View v) {
v.setVisibility(View.VISIBLE);
}
};
And as the onClick event for all of your items inside the TableRow:
for (View v : myTableRowViews)
v.setOnClickListener(myListener);
This has a disadvantage: You can know which View has been clicked for selection, but natively you cannot know which has been deselected, so you'll need to keep track of the last selected tab declaring a class-wide variable and setting it each time onClick() is fired, so your listener will become something like this:
// In your class declare a variable like this
View lastSelected = null;
OnClickListener myListener = new OnClickListener() {
#Override
public void onClick(final View v) {
if (lastSelected != null)
lastSelected.setVisibility(View.GONE);
v.setVisibility(View.VISIBLE);
lastSelected = v;
}
};
Additionally, you can set an animation to the effect to make it more attractive, but mainly this is the idea.
One last thing: To make it work this way you'll need to set the layout_height of both your TableRow and the View items inside, so it may expand afterwards when you set the additional part as visible. Also, to make it look good all of your Views will have to be the same height (both in the reduced and extended state).

EditText Values in Android Fragment do not refresh

I'm using the Viewpager to switch between 3 fragments, everything is working fine, except the refreshing of the second tab (or fragment). In this tab, I have a picture, some static Textviews, some dynamic TextViews and some EditText fields.
Everytime the second tab is selected, there will be called setText() on all dynamic fields. TextView components and the spinner are refreshing and updating their contents, but EditText elements do not.
I don’t understand why these fields are not updating. After tab change, I call notifiyDataSetChanged() in TabsAdapter. It calls onViewCreated() everytime I change the tab. In onViewCreated() of the second fragment I am changing the contents of the elements.
That’s the code of my fragment:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
hA = (HelloAndroidActivity)this.getSherlockActivity();
appState = ((TestApplication)this.getSherlockActivity().getApplicationContext());
final PersistenceHelper pH = new PersistenceHelper(appState);
m = pH.readMitarbeiter(toDisplay);
// Inflate the layout for this fragment
v = inflater.inflate(R.layout.detailfragment, container, false);
if(m==null) {
//If Mitarbeiter is empty
pH.closeDB();
return v;
}
//Inflating Elements
employeeName = (TextView)v.findViewById(R.id.employee_name);
cDate = (TextView)v.findViewById(R.id.department);
currentPlace = (TextView)v.findViewById(R.id.place_label);
comment_text = (EditText)v.findViewById(R.id.comment_text);
reachable = (EditText)v.findViewById(R.id.reachable_text);
state = (Spinner)v.findViewById(R.id.spinner_state);
durchwahl = (EditText)v.findViewById(R.id.durchwahl);
department = (EditText)v.findViewById(R.id.department_edit);
email = (EditText)v.findViewById(R.id.email_edit);
img = (ImageView)v.findViewById(R.id.userPic);
changeData = (Button)v.findViewById(R.id.changeButton);
//Setting Elements
employeeName.setText(m.getL_name());
currentPlace.setText(m.getStatus());
comment_text.setText(m.getBemerkung());
reachable.setText(m.getErreichbar());
email.setText(m.getS_name()+"");
durchwahl.setText(m.getDurchwahl()+"",TextView.BufferType.EDITABLE);
department.setText(m.getFunktion(),TextView.BufferType.NORMAL);
//Spinner
String[] values = { "Anwesend", "Mittagspause" , "Ausser Haus" , "Dienstreise", "Krankenstand" ,"Zeitausgleich", "Urlaub","Abwesend" };
ArrayAdapter spinnerArrayAdapter = new ArrayAdapter(this.getSherlockActivity(), android.R.layout.simple_spinner_item,values);
state.setAdapter(spinnerArrayAdapter);
state.setSelection(StateMapper.returnState(m.getStatus()));
//Image
//.......
return v;
}
As I mentioned before, my Image, TextView and Spinner Elements are refreshing their content. I also checked the content of all variables, everything seems to be fine, except these EditText elements. If I cast the EditText elements into TextView, the content is changing (in code but not in the GUI). What also makes me desperate is, that the EditText refreshes the first time I set the value.
Has anybody an idea, how I’m able to refresh the content of my EditText fields?
i am not sure but try onResume() and set your text in resume state.
or try
Intent.FLAG_ACTIVITY_CLEAR_TOP on tab change.
You could also try posting a runnable to the message queue so that the EditText's are updated after rendering (in MonoDroid/C#, see How to run a Runnable thread in Android? for java):
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle bundle)
{
EditText et = FindViewById<EditText>(...);
et.Post(() => { et.Text = "content"; });
}
Also, if you have a TextChanged event handler (say for displaying a save icon/button when the text is changed), post it in the runnable as well, and do it after the et.Text assignment. Otherwise, the TextChanged event will fire when the initial et.Text content is assigned, causing the TextChanged event to fire (ie, and the save button showing) when the USER hasn't changed anything:
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle bundle)
{
EditText et = FindViewById<EditText>(...);
et.Post(() =>
{
et.Text = "content";
et.TextChanged += TextChangedHandler;
});
}
private void TextChangedHandler(object o, EventArgs args)
{
ShowSaveButton();
}
I came across this issue in 2021. I just had to call findViewById again right before setting the text.
myEditText = (EditText)view.findViewById(R.id.myEditText);
myEditText.setText(String.valueOf(newValue));

Android: Load Layout-File dynamically

I have a question about Android.
Assume we have our main xml layout file, and defining there a place holder by using (for example) a FrameLayout. Also assume we have 2 other xml layout files displaying any content.
So what I want to do is inject dynamically and programmtically one of the two layouts into the place holder. I know there exists the concept of Activitis, Fragments, ViewFlipper etc. But I find it comfortable to do things like this:
public class MainActivity extends Activity {
private FrameLayout placeHolder;
private View view1;
private View view2;
private RelativeLayout canvasPlaceHolder;
private PuzzleCanvas canvas;
private TextView infoLabel;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// init gui
setContentView(R.layout.activity_main);
// Load layouts from xml
LayoutInflater factory = getLayoutInflater();
view1 = factory.inflate(R.layout.view1, null);
view2 = factory.inflate(R.layout.view2, null);
}
}
with for example a Button on screen that does something like this:
#Override
public void onClick(View v) {
placeHolder.removeView(view1);
placeHolder.addView(view2);
}
For example to show a loadingAnimation (view2) instead of the normal content (view1) and so I can define both views comfortable and independent in xml.
Is the use of LayoutInflater commendable? What about the performance and memory management? What do you think about this? Is that a common way in Android?
Use 'include ' tag on the xml's frame layout to include both your xml's in the main xml. All you have to do is switch their ' VISIBILITY' through java according to ur app logic.
eg: on a listener, set :
public void onClick(View v) {
innerView1.setVisibilty(View.INVISIBLE);
innerView2. setVisibilty(View.VISIBLE);
}
// Load layouts from xml
LayoutInflater factory = getLayoutInflater();
view1 = factory.inflate(R.layout.view1, null);
view2 = factory.inflate(R.layout.view2, null);
Using LayoutInflater is ok, but I suggest not directly do this action in onCreate, if your layout is very complex, it might cause ANR (draw layout over 5 secs). Since these two views only appears after user reaction, I prefer to do with sendEmptyMessage with handler.
onCreate(...){
handler.sendEmptyMessage(1);
}
private Handler handler = new Handler(){
#Override
public void handleMessage(final Message msgs) {
if(msgs.what == 1){
view1 = factory.inflate(R.layout.view1, null);
view2 = factory.inflate(R.layout.view2, null);
}
}
}

Add an array of buttons to a GridView in an Android application

I have an application that will have 5-15 buttons depending on what is available from a backend. How do I define the proper GridView layout files to include an array of buttons that will each have different text and other attributes? Each button will essentially add an item to a cart, so the onClick code will be the same except for the item it adds to the cart.
How can I define an array so I can add a variable number of buttons, but still reference each of them by a unique ID? I've seen examples of the arrays.xml, but they have created an array of strings that are pre-set. I need a way to create an object and not have the text defined in the layout or arrays xml file.
Update - Added info about adding to a GridView
I want to add this to a GridView, so calling the [addView method](http://developer.android.com/reference/android/widget/AdapterView.html#addView(android.view.View,%20int) results in an UnsupportedOperationException. I can do the following:
ImageButton b2 = new ImageButton(getApplicationContext());
b2.setBackgroundResource(R.drawable.img_3);
android.widget.LinearLayout container = (android.widget.LinearLayout) findViewById(R.id.lay);
container.addView(b2);
but that doesn't layout the buttons in a grid like I would like. Can this be done in a GridView?
In the following code, you should change the upper limits of the for to a variable.
public class MainActivity
extends Activity
implements View.OnClickListener {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TableLayout layout = new TableLayout (this);
layout.setLayoutParams( new TableLayout.LayoutParams(4,5) );
layout.setPadding(1,1,1,1);
for (int f=0; f<=13; f++) {
TableRow tr = new TableRow(this);
for (int c=0; c<=9; c++) {
Button b = new Button (this);
b.setText(""+f+c);
b.setTextSize(10.0f);
b.setTextColor(Color.rgb( 100, 200, 200));
b.setOnClickListener(this);
tr.addView(b, 30,30);
} // for
layout.addView(tr);
} // for
super.setContentView(layout);
} // ()
public void onClick(View view) {
((Button) view).setText("*");
((Button) view).setEnabled(false);
}
} // class
Here's a nice sample for you:
https://developer.android.com/guide/topics/ui/layout/gridview.html
You should just create buttons instead of imageviews in getView adapter method.
If you are using a GridView, or a ListView (etc), and are producing Views to populate them via the adapter getView(pos, convertView, viewGroup), you might encounter confusion (i did once).
If you decide to re-use the convertView parameter, you must reset everything inside of it. It is an old view being passed to you by the framework, in order to save the cost of inflating the layout. It is almost never associated with the position it was in the layout before.
class GridAdapter extends BaseAdapter // assigned to your GridView
{
public View getView(int position, View convertView, ViewGroup arg2) {
View view;
if (convertView==null)
{
view = getLayoutInflater().inflate(R.layout.gd_grid_cell, null);
}
else
{
// reusing this view saves inflate cost
// but you really have to restore everything within it to the state you want
view = convertView;
}
return view;
}
// other methods omitted (e.g. getCount, etc)
}
I think this represents one of those Android things where the concept is a little difficult to grasp at first, until you realize there's a significant optimization available within it (have to be nice to CPU on a little mobile device)

Categories

Resources