How Whatsapp attachment's window can override keyboard? - android

I'm working on a similiar problem developing my chat and I was checking on how whatsapp solves it to get an idea to work on it.
On mobile and inside a conversation, when your keyboard is up on the screen and you click on attachments, the attachments window overrides the keboard like the image below. It also look likes the activity under it freezes ( you can look at the slash "|" on the EditText, it seems that is freeze ).
I was wondering how WP could override the keyboard and why it looks like the activity under it is freezing, thanks in advance!

/Use the below code Snipet without using any library/
action_attach.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
openDialog();
}
});
private void openDialog() {
LayoutInflater layoutInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// inflate the custom popup layout
final View inflatedView;
inflatedView = layoutInflater.inflate(R.layout.custom_dialog_options_menu, null, false);
LinearLayout layoutGallery;
layoutGallery = (LinearLayout) inflatedView.findViewById(R.id.layoutGallery);
layoutGallery.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
FloatingView.dismissWindow();
}
});
FloatingView.onShowPopup(this, inflatedView);
}
/**Sample Layout for Pop Up window ie: custom_dialog_options_menu
and change layout according to your choice***/
<?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="wrap_content"
android:orientation="vertical"
android:paddingTop="82dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#FFF"
android:orientation="horizontal">
<LinearLayout
android:id="#+id/layoutGallery"
android:layout_width="0dp"
android:layout_marginTop="18dp"
android:layout_height="wrap_content"
android:layout_marginLeft="40dp"
android:layout_weight="1"
android:orientation="vertical">
<ImageView
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_gravity="center"
android:src="#drawable/gallery_attach_icon_selector"/>
<TextView
android:id="#+id/tvGallery"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="Gallery"
android:layout_marginTop="10dp"
android:textSize="16sp"
android:textColor="#4d4747"/>
</LinearLayout>
<LinearLayout
android:id="#+id/layoutPhoto"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_marginTop="18dp"
android:orientation="vertical">
<ImageView
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_gravity="center"
android:src="#drawable/camera_attach_icon_selector"/>
<TextView
android:id="#+id/tvPhoto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="Camera"
android:layout_marginTop="10dp"
android:textSize="16sp"
android:textColor="#4d4747"/>
</LinearLayout>
<LinearLayout
android:id="#+id/layoutVideo"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginRight="40dp"
android:layout_weight="1"
android:layout_marginTop="18dp"
android:orientation="vertical">
<ImageView
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_gravity="center"
android:src="#drawable/video_attach_icon_selector"/>
<TextView
android:id="#+id/tvVideo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="Video"
android:layout_marginTop="10dp"
android:textSize="16sp"
android:textColor="#4d4747"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
/**FloatingView Class**/
public class FloatingView
{
/* Floating view is used to display a custom view for attachments in the chat screen */
import android.app.Activity;
import android.graphics.Point;
import android.view.Display;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.PopupWindow;
private static PopupWindow popWindow;
private FloatingView() {
}
public static void onShowPopup(Activity activity, View inflatedView) {
// get device size
Display display = activity.getWindowManager().getDefaultDisplay();
final Point size = new Point();
display.getSize(size);
// fill the data to the list items
// set height depends on the device size
popWindow = new PopupWindow(inflatedView, size.x, ViewGroup.LayoutParams.WRAP_CONTENT,
true);
// set a background drawable with rounders corners
popWindow.setBackgroundDrawable(activity.getResources().getDrawable(
R.drawable.comment_popup_bg));
// make it focusable to show the keyboard to enter in `EditText`
popWindow.setFocusable(true);
// make it outside touchable to dismiss the popup window
popWindow.setOutsideTouchable(true);
popWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
// show the popup at bottom of the screen and set some margin at
// bottom ie,
popWindow.showAtLocation(activity.getCurrentFocus(), Gravity.BOTTOM, 0,
0);
}
public static void dismissWindow() {
popWindow.dismiss();
}

Related

Why are adjacent Buttons moving when I change the size of the text in a Button

Here is the layout when each Button has the same size text:
And here is the layout when I increase the size of the bottom right Button's text:
What's going on here? Why is the "0" Button moving lower than the two Buttons adjacent to it?
Here is the layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="#android:color/white"
android:orientation="vertical"
android:padding="6dp">
<TextView
android:id="#+id/input_textview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:textColorHint="#color/hint_color"
android:singleLine="true"
android:textSize="40sp"/>
<GridLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:columnCount="3">
<Button
android:id="#+id/the_1_button"
android:background="#android:color/white"
android:text="1"/>
<Button
android:id="#+id/the_2_button"
android:background="#android:color/white"
android:text="2"/>
<Button
android:id="#+id/the_3_button"
android:background="#android:color/white"
android:text="3"/>
<Button
android:id="#+id/the_4_button"
android:background="#android:color/white"
android:text="4"/>
<Button
android:id="#+id/the_5_button"
android:background="#android:color/white"
android:text="5"/>
<Button
android:id="#+id/the_6_button"
android:background="#android:color/white"
android:text="6"/>
<Button
android:id="#+id/the_7_button"
android:background="#android:color/white"
android:text="7"/>
<Button
android:id="#+id/the_8_button"
android:background="#android:color/white"
android:text="8"/>
<Button
android:id="#+id/the_9_button"
android:background="#android:color/white"
android:text="9"/>
<ImageButton
android:id="#+id/the_backspace_button"
style="#style/Widget.AppCompat.Button"
android:background="#android:color/white"
android:src="#drawable/ic_backspace"/>
<Button
android:id="#+id/the_0_button"
android:background="#android:color/white"
android:text="0"/>
<Button
android:id="#+id/the_done_button"
android:layout_width="88dp"
android:layout_height="48dp"
android:minWidth="0dp"
android:minHeight="0dp"
android:background="#android:color/white"
android:text="done"/>
</GridLayout>
</LinearLayout>
Update:
I made the text of the "DONE" Button super large -- 100sp -- and set maxWidth and maxHeight for it equal to the height and width of the other Buttons. Here is the result:
*the blue is the GridLayout's background, the red the "0" Button's, and the yellow the "DONE" Button's
Why would changing the size of the text of the "DONE" Button affect anything other than that specific Button if the size of the Button never changes?
I think there is a problem with the done button. You set the background of it. Then you also set its text as "done" .
Just set it text empty and the problem may resolve ;)
What about using the grid:layout_rowWeight or grid:layout_rowHeight attributes to be the same for every button?
Look at this:
https://stackoverflow.com/a/32291319/2205825
Try this .....
Use GridView instead of many Button that not give you any Layout problem (Adjustment of Views).
Have look on the below example that solve your problem about Adjustment of Buttons
use this layout instead of your layout .....
<?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="vertical"
android:padding="6dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<TextView
android:id="#+id/input_textview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:singleLine="true"
android:textSize="40sp" />
</LinearLayout>
<GridView
android:id="#+id/gridview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:columnWidth="100dp"
android:gravity="center"
android:numColumns="3" />
</LinearLayout>
add these 2 line in your OnCreate method .....
GridView gridview = (GridView) findViewById(R.id.gridview);
gridview.setAdapter(new CustomAdapter(this));
add this adapter in your project .....
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageButton;
/**
* Created by sushildlh on 10/14/16.
*/
public class CustomAdapter extends BaseAdapter {
private Context mContext;
// Keep all Images in array
public String[] text = {
"1", "2", "3", "4", "5", "6", "7", "8", "9", "", "0", "DONE"
};
// Constructor
public CustomAdapter(Context c) {
mContext = c;
}
public int getCount() {
return 12;
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
// create a new textView for each item referenced by the Adapter
public View getView(int position, View convertView, ViewGroup parent) {
Holder holder = null;
if (holder == null) {
LayoutInflater inflater = ((Activity) mContext).getLayoutInflater();
convertView = inflater.inflate(R.layout.item, parent, false);
holder = new Holder();
holder.button = (Button) convertView.findViewById(R.id.button);
holder.image = (ImageButton) convertView.findViewById(R.id.image);
convertView.setTag(holder);
} else {
holder = (Holder) convertView.getTag();
}
if (position != 9) {
holder.image.setVisibility(View.GONE);
holder.button.setText(text[position]);
} else {
holder.image.setImageResource(R.drawable.ic_backspace_black_24dp); //change ic_backspace_black_24dp into to your image ......
holder.button.setVisibility(View.GONE);
}
return convertView;
}
public static class Holder {
Button button;
ImageButton image;
}
}
and this is item.xml layout into your layout folder ....
<?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="vertical">
<Button
android:id="#+id/button"
android:layout_width="100dp"
android:layout_height="60dp"
android:gravity="center"
android:background="#null"
android:textSize="25dp"/>
<ImageButton
android:id="#+id/image"
android:layout_width="100dp"
android:layout_height="60dp"
android:focusable="false"
android:background="#null"
android:gravity="center" />
</LinearLayout>
above code give you the output like this ....
Note:- Always use GridView OR ListView for same pattern of Views .....
You can use the android:layout_weight property.
It appears that the GridLayout is reserving enough height for the actual text size and some default padding despite the layout_height for the Done button.
Build it with 'Linear Layouts' instead.
Was facing the same problem, so just i did was to change the height and width of the button to it parent RowTable's match parent & wrap content and it solved my problem

Remove and add layout dynamically by click button in Android

I'm working on a big project and I can not solve the problem of a dynamically adding of components.I want to add layout into other layout by click on a button ADD. After this I want to remove it by click a button REMOVE.
Specially for stackoverflow I build a small example of what I want to do.
To ADD it's not a problem but remove it's a problem.When I click a "remove" button this remove not what I need (I want remove parent of "remove" button).
After this I want to ask something more important.I will need save all this data to the DB.So I don't know how to get data from each Text Fields and put it into list (or something else) because all this Text Fields have same ID.
So I see two way of solution:
1)Change there ID dynamically
2)Something else))
Thank you very much!!!
This is
sub_fields.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:nestedScrollingEnabled="false"
android:id="#+id/detailsLayout"
android:focusableInTouchMode="true">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:ems="10"
android:id="#+id/txtName"
android:hint="name" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="phone"
android:ems="10"
android:id="#+id/txtPhone"
android:layout_gravity="center_horizontal"
android:hint="phone" />
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ADD"
android:id="#+id/btnAddd"
android:onClick="onClickAddd" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="REMOVE"
android:id="#+id/btnRemove"
android:onClick="onClickAddd" />
</LinearLayout>
</LinearLayout>
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:nestedScrollingEnabled="false"
android:id="#+id/generalLayout">
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="56dp"
android:gravity="center"
android:background="#7d65258a">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="FILL FIELDS"
android:id="#+id/textView" />
</LinearLayout>
<ScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/subLayoutFields">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:ems="10"
android:id="#+id/txtName"
android:hint="name" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="phone"
android:ems="10"
android:id="#+id/txtPhone"
android:layout_gravity="center_horizontal"
android:hint="phone" />
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ADD"
android:id="#+id/btnAdd"
android:onClick="onClickAdd" />
</LinearLayout>
</LinearLayout>
MainActivity.java
package andrey.adddinamicallycontrolsapp;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void onClickAdd(View view) {
LayoutInflater ltInflater = getLayoutInflater();
final LinearLayout subLayoutFields = (LinearLayout) findViewById(R.id.subLayoutFields);
final View view1 = ltInflater.inflate(R.layout.sub_fields, subLayoutFields, true);
Button buttonRemove = (Button)view1.findViewById(R.id.btnRemove);
buttonRemove.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
subLayoutFields.removeView((LinearLayout)(v.getParent().getParent()));
}});
}
Before adding some view by addView(childView) you should save reference to that. In that case removeView(childView) will work. Getting that view by traveling in view tree by getParent() is not a good idea.
By the way, where is your addView call?
maybe try subLayoutFields.removeView(view1) - view1 is final
if you want to somehow differ your widgets (for storing in db, iterating or whatever you want) you may set for them dynamic ids using View.generateViewId() (API17) or using custom method from most voted answer here
Thank you guys!!!
The answer is :
public void onClickAddPanel(View view) {
LayoutInflater ltInflater = getLayoutInflater();
LinearLayout subLayoutFieldsForBtnAdd = (LinearLayout) findViewById(R.id.productDetails);
View view1 = ltInflater.inflate(R.layout.product_details, subLayoutFieldsForBtnAdd, true);
}
public void onClickRemovePanel(View v) {
View v1 = (View) v.getParent();
LinearLayout subLayoutFieldsForBtnRemove = (LinearLayout) findViewById(R.id.productDetails);
subLayoutFieldsForBtnRemove.removeView(v1);
}

Button visibility problems in Android

I'm creating a toolbar that toggles the visibility of buttons when you click a button from a toolbar. So, if the user clicks on the "Draw" button, invisible buttons "Pencil" and Pen" will become visible above the "Draw" button. If you click the "Draw" button again, the buttons "Pencil" and "Pen" will become invisible again.
Within my xml file I have set the visibilty of some buttons to be "invisible" so when I launch the activity they won't be seen. This part is straight forward.
.xml file of btnDrawLine - (Update # 12:21)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="fill_parent" >
<com.odhranlynch.testSection.UserInterface
android:id="#+id/UserInterface"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentTop="true" />
<Button
android:id="#+id/btnDraw"
android:layout_width="80dip"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:text="Draw" />
<Button
android:id="#+id/btnDrawLine"
android:layout_width="80dip"
android:layout_height="wrap_content"
android:layout_above="#+id/btnDraw"
android:layout_alignParentLeft="true"
android:visibility="visible"
android:text="Line" />
<Button
android:id="#+id/btnDrawCurve"
android:layout_width="80dip"
android:layout_height="wrap_content"
android:layout_above="#+id/btnDrawLine"
android:layout_alignParentLeft="true"
android:visibility="visible"
android:text="Curve" />
<Button
android:id="#+id/btnCutout"
android:layout_width="80dip"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_toRightOf="#+id/btnDraw"
android:text="Cutout" />
<Button
android:id="#+id/btnCutInner"
android:layout_width="80dip"
android:layout_height="wrap_content"
android:layout_above="#+id/btnDraw"
android:layout_toRightOf="#+id/btnDraw"
android:visibility="visible"
android:text="Inner" />
<Button
android:id="#+id/btnCutOutter"
android:layout_width="80dip"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/btnDrawCurve"
android:layout_alignBottom="#+id/btnDrawCurve"
android:layout_toLeftOf="#+id/btnCancel"
android:visibility="visible"
android:text="Outter" />
<Button
android:id="#+id/btnCancel"
android:layout_width="80dip"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_toLeftOf="#+id/btnFinish"
android:text="Cancel" />
<Button
android:id="#+id/btnFinish"
android:layout_width="80dip"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:text="Finish" />
</RelativeLayout>
Next, when the user clicks a button that is visible, I'd like the invisible buttons to appear.
Here's the thing, they won't reappear!lol I'm confused by it.
I would be very grateful if someone would be kind enough to shed so light onto this for me :)
testActivity.java
package com.odhranlynch.testSection;
import com.odhranlynch.testSection.R;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
public class testActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.new_product);
// Find buttons and give them a name.
final View btnDraw = findViewById(R.id.btnDraw);
final View btnCutOut = findViewById(R.id.btnCutout);
final View btnDrawLine = findViewById(R.id.btnDrawLine);
final View btnDrawCurve = findViewById(R.id.btnDrawCurve);
final View btnCutInner = findViewById(R.id.btnCutInner);
final View btnCutOutter = findViewById(R.id.btnCutOutter);
//Draw Button clicked (UI Toolbar).
btnDraw.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
//Treat button as a toggle button
//So if a sub-button (e.g. Draw Line) is visible, then we know the button has
//been toggled to visible so lets now make it invisible.
if (btnDrawLine.getVisibility()== View.VISIBLE) {
//Its visible.
btnDrawLine.setVisibility(View.INVISIBLE);
btnDrawCurve.setVisibility(View.INVISIBLE);
Log.d("TAG", "INVISIBLE");
} else {
// Either gone or invisible
btnDrawLine.setVisibility(View.VISIBLE);
btnDrawCurve.setVisibility(View.VISIBLE);
Log.d("TAG", "VISIBLE");
}
}
});
}
}
As a further point to mention, if I set the visibilty of the buttons to be visible within the .xml file I can toggle the visibilty perfectly fine during runtime!
Again, I would be grateful of some help :)
Try replacing View.INVISIBLE to View.GONE.
Your code is working fine..
The XML file
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/hello" />
<Button
android:id="#+id/btnDrawLine"
android:layout_width="80dip"
android:layout_height="wrap_content"
android:layout_above="#+id/btnDraw"
android:layout_alignParentLeft="true"
android:visibility="invisible"
android:text="Line" />
<Button
android:id="#+id/draw"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="draw" />
</LinearLayout>
The activity
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
public class DrawCanvasActivity extends Activity {
private static final String Number1 = "9686801147";
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final View btnDrawLine = findViewById(R.id.btnDrawLine);
Button btnDraw = (Button) findViewById(R.id.draw);
btnDraw.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
if (btnDrawLine.getVisibility()== View.VISIBLE) {
btnDrawLine.setVisibility(View.INVISIBLE);
Log.d("TAG", "INVISIBLE");
} else {
btnDrawLine.setVisibility(View.VISIBLE);
Log.d("TAG", "VISIBLE");
}
}
});
}
}
The button line appears when the draw button is clicked.Guess there may be problem in the view formatting in your code.
If you want ot use gone/visible, just add a LinearLayout around your buttons, which you want to hide. LinearLayout will have a layout_width=wrap_content; and you referer position of other element to this Layout.
After you free to change visibility to gone/visible of your buttons.

Android sliding menu to change content on page can this be done?

The main page has a horizontalScrollView across the top and a number of TextView text fields within it. I would like to be able to scroll and click on any on the TextView text and it change the RelativeLayout to match the selection made in the HorizontalScrollView.
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<HorizontalScrollView android:id="#+id/horizontalScrollView1" android:layout_height="wrap_content" android:scrollbars="none" android:layout_width="wrap_content">
<LinearLayout android:layout_width="match_parent" android:orientation="horizontal" android:layout_height="match_parent" android:id="#+id/linearLayout1">
<TextView android:layout_height="wrap_content" android:layout_weight="1" android:layout_width="70dp" android:id="#+id/EditTOne" android:text="tOne"></TextView>
<TextView android:layout_height="wrap_content" android:layout_weight="1" android:layout_width="70dp" android:id="#+id/EditTTwo" android:text="tTwo"></TextView>
<TextView android:layout_height="wrap_content" android:layout_weight="1" android:layout_width="70dp" android:id="#+id/EditTThree" android:text="tThree"></TextView>
<TextView android:layout_height="wrap_content" android:layout_weight="1" android:layout_width="70dp" android:id="#+id/EditTFour" android:text="tFour"></TextView>
<TextView android:layout_height="wrap_content" android:layout_weight="1" android:layout_width="70dp" android:id="#+id/EditTFive" android:text="tFive"></TextView>
<TextView android:layout_height="wrap_content" android:layout_weight="1" android:layout_width="70dp" android:id="#+id/EditTSix" android:text="tSix"></TextView>
<TextView android:layout_height="wrap_content" android:layout_weight="1" android:layout_width="70dp" android:id="#+id/EditTSeven" android:text="tSeven"></TextView>
</LinearLayout>
</HorizontalScrollView>
<RelativeLayout android:layout_height="wrap_content" android:layout_width="match_parent" android:id="#+id/relativeLayout1"></RelativeLayout>
</LinearLayout>
I have a second layout page called tone which would be linked to the menu option tOne in the HorizontalScrollView.
tone.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView android:id="#+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="This is tOne"></TextView>
</LinearLayout>
The main activity will flag if one of the first 4 options are selected by writing to the LogCat. Is it possible to display the tone.xml within the relativelayout if the user clicks the tOne option from the HorizontalScrollView? Then if the user selects tTwo display ttwo.xml in the RelativeLayoutView etc?
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.RelativeLayout;
import android.widget.TextView;
public class SlideMenu extends Activity implements OnClickListener {
/** Called when the activity is first created. */
TextView tOne, tTwo, tThree, tFour, tFive, tSix, tSeven, tEight, tNine, tTen;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
RelativeLayout vSpace = (RelativeLayout)findViewById(R.id.relativeLayout1);
tOne = (TextView)findViewById(R.id.EditTOne);
tTwo = (TextView)findViewById(R.id.EditTTwo);
tThree = (TextView)findViewById(R.id.EditTThree);
tFour = (TextView)findViewById(R.id.EditTFour);
tFive = (TextView)findViewById(R.id.EditTFive);
tSix = (TextView)findViewById(R.id.EditTSix);
tSeven = (TextView)findViewById(R.id.EditTSeven);
tOne.setOnClickListener(this);
tTwo.setOnClickListener(this);
tThree.setOnClickListener(this);
tFour.setOnClickListener(this);
tFive.setOnClickListener(this);
tSix.setOnClickListener(this);
tSeven.setOnClickListener(this);
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId()) {
//--------------
case R.id.EditTOne:
System.out.println("Text One pressed");
break;
case R.id.EditTTwo:
System.out.println("Text Two pressed");
break;
case R.id.EditTThree:
System.out.println("Text Three pressed");
break;
case R.id.EditTFour:
System.out.println("Text Four pressed");
break;
}
}
}
I believe it is fairly simple. I added
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.tone, vSpace);
after you print "Text One Pressed". Be sure to make vSpace a field first.
To do this with all of them, call
vSpace.removeAllViews()
before you inflate each view.

How to add a view to custom listview in android?

I have made custom listview in android activity which has progressbar. On top of that listview there is another progress bar which shows progress as day passes in the month. Now i want to set a bar according to the progress of the month progress bar in custom listview which has four progress bars. Please see the attached image.
The progress of the month progress bar and the bar which we put in listview progress bar should be at same position means aligned and this bar which we put should move when the top progress increases as day passes.
In Layout file I have used LinearLayout where I want to put the bar.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:layout_marginTop="5dip">
<RelativeLayout android:id="#+id/Top"
android:layout_centerHorizontal="true" android:layout_width="250dip"
android:layout_height="wrap_content">
<TextView android:id="#+id/LeftText" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="O"
android:textColor="#FFFFFF" android:textStyle="normal"
android:layout_alignParentLeft="true" />
<RelativeLayout android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_alignParentRight="true">
<TextView android:id="#+id/TextOne" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="O"
android:textColor="#FFFFFF" android:textStyle="normal"
android:layout_marginRight="5dip" />
<TextView android:id="#+id/TextTwo" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="#string/outof"
android:textColor="#FFFFFF" android:textStyle="normal"
android:layout_toRightOf="#+id/TextOne" android:layout_marginRight="5dip" />
<TextView android:id="#+id/RightText" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="O"
android:textColor="#FFFFFF" android:textStyle="normal"
android:layout_toRightOf="#+id/TextTwo" />
</RelativeLayout>
</RelativeLayout>
<RelativeLayout android:id="#+id/Bottom"
android:layout_below="#+id/Top" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_centerHorizontal="true">
<ProgressBar android:id="#+id/CustomPro"
android:layout_width="250dip" android:layout_height="wrap_content"
android:progress="0" android:max="100" android:secondaryProgress="0"
style="?android:attr/progressBarStyleHorizontal"
android:progressDrawable="#drawable/accountdataprogress"
android:layout_centerHorizontal="true" />
<TextView android:id="#+id/MiddleText" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="0%"
android:textColor="#000000" android:textStyle="bold"
android:layout_centerHorizontal="true" />
</RelativeLayout>
<RelativeLayout android:id="#+id/Middle"
android:layout_centerHorizontal="true" android:layout_width="250dip"
android:layout_height="wrap_content" android:layout_below="#+id/Top">
<LinearLayout android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="#+id/PuttingBar"
android:layout_alignParentLeft="true">
</LinearLayout>
</RelativeLayout>
<View android:layout_width="fill_parent" android:layout_height="10dip"
android:background="#00000000" android:layout_below="#+id/Bottom" />
</RelativeLayout>
In Java I have done something like this.
import java.util.ArrayList;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ProgressBar;
import android.widget.TextView;
public class ProgressListAdapter extends BaseAdapter
{
private Context mContext;
private ArrayList<ProgressList> mPList;
private TextView mLeft, mMiddle, mRight, mTextOne;
private ProgressBar mCustomProgressBar;
private LinearLayout mLinearLayout;
public ProgressListAdapter(Context nContext, ArrayList<ProgressList> nPList)
{
this.mContext = nContext;
this.mPList = nPList;
}
#Override
public int getCount()
{
return mPList.size();
}
#Override
public Object getItem(int nPosition)
{
return mPList.get(nPosition);
}
#Override
public long getItemId(int nPosition)
{
return nPosition;
}
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
ProgressList mEntry = mPList.get(position);
if (convertView == null)
{
LayoutInflater mInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = mInflater.inflate(R.layout.customprogress, null);
}
mLeft = (TextView) convertView.findViewById(R.id.LeftText);
mLeft.setText(mEntry.getValueText());
mMiddle = (TextView) convertView.findViewById(R.id.MiddleText);
mCustomProgressBar = (ProgressBar) convertView.findViewById(R.id.CustomPro);
int mBarPosition = Usage.mElapsed;
mLinearLayout = (LinearLayout) convertView.findViewById(R.id.PuttingBar);
View mView = new View(mContext);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(2, 25);
layoutParams.setMargins(mBarPosition, 0, 0, 0);
mView.setMinimumHeight(25);
mView.setMinimumWidth(2);
mView.setBackgroundColor(Color.rgb(12, 56, 99));
mLinearLayout.addView(mView, layoutParams);
mView.invalidate();
if(mEntry.getValueNumber() > mBarPosition)
{
mCustomProgressBar.setProgress(mBarPosition);
mCustomProgressBar.setSecondaryProgress(mEntry.getValueNumber());
}
else if(mEntry.getValueNumber() <= mBarPosition)
{
mCustomProgressBar.setProgress((mEntry.getValueNumber()));
mCustomProgressBar.setSecondaryProgress(0);
}
mMiddle.setText(Integer.toString(mEntry.getValueNumber()) + "%");
mTextOne = (TextView) convertView.findViewById(R.id.TextOne);
mTextOne.setText(mEntry.getValue());
mRight = (TextView) convertView.findViewById(R.id.RightText);
mRight.setText(mEntry.getValueOne());
return convertView;
}
}
But When I run It shows extra bar in progress bar like this
Anybody has done kind of work please give me your hand. Waiting for fruitful reply.
Thanks.
You should add call to removeAllViews in your getView funcion:
mLinearLayout = (LinearLayout) convertView.findViewById(R.id.PuttingBar);
mLinearLayout.removeAllViews();
View mView = new View(mContext);
That's because LinearLayout accumulates views, and there might be multiple calls to your addView function. So you need to remove already added view before adding another PuttingBar with new margin.

Categories

Resources