I have one relativeLayout and three view's inside RelativeLayout. There views are place on each other.
<RelativeLayout
android:id="#+id/includelayoutgroup"
android:layout_width="match_parent"
android:layout_height="match_parent">
<View xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/transparentColorView"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:clickable="true"
android:visibility="visible" />
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/keyboard_main_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:visibility="visible">
<include
layout="#layout/transfer_keyboard_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/finalRequestView"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:clickable="true"
android:visibility="visible">
<include
layout="#layout/fragment_transfer_new_version_container_child"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
</RelativeLayout>
Is it a possible to reverse position my view's? I mean,in my code last Layout is finalRequestView and in button click i would to my last view in first position,and other view's place on my view,
How i can solve this problem?
I try to reverse layout by following demo & it works as you want, please take a look
ReverseLayout.java
public class ReverseLayout extends AppCompatActivity {
LinearLayout llRoot;
Button btReverseLayout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.reverse_layout);
llRoot = (LinearLayout) findViewById(R.id.llRoot);
btReverseLayout = (Button) findViewById(R.id.btReverseLayout);
btReverseLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
ArrayList<View> alViews = new ArrayList<View>();
for (int i = llRoot.getChildCount() - 1; i >= 0; i--) {
View view = llRoot.getChildAt(i);
llRoot.removeViewAt(i);
alViews.add(view);
}
for (int j = 0; j < alViews.size(); j++) {
llRoot.addView(alViews.get(j));
}
}
});
}
}
reverse_layout.xml layout file
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_6"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.tosc185.testproject.Activity6">
<LinearLayout
android:id="#+id/llRoot"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#FF0000"
android:gravity="center"
android:orientation="vertical"
android:padding="15dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1"
android:textSize="16dp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#00FF00"
android:gravity="center"
android:orientation="vertical"
android:padding="15dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2"
android:textSize="16dp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#0000FF"
android:gravity="center"
android:orientation="vertical"
android:padding="15dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="3"
android:textSize="16dp" />
</LinearLayout>
</LinearLayout>
<Button
android:id="#+id/btReverseLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="Reverse Layout" />
</RelativeLayout>
Related
I have a ListView, where i changed appearence of row, but listview have size of one row, instead of fullscreen.
and my scrollview is working but listview is not working.
activity_graph_view.xml
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_graph_table_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="00dp"
android:background="#color/background"
android:orientation="vertical">
<android.support.design.widget.AppBarLayout
android:id="#+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:id="#+id/container_toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<include
android:id="#+id/toolbar"
layout="#layout/toolbar" />
</LinearLayout>
</android.support.design.widget.AppBarLayout>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/appbar"
android:fillViewport="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="5dp"
android:orientation="vertical">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="5dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp">
<LinearLayout
android:id="#+id/layoutTableOne"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="5dp"
android:visibility="visible">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp"
android:text="Table View"
android:textSize="#dimen/subheading"
android:textColor="#color/subheading"
android:textAllCaps="false" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ListView
android:id="#+id/lv_spirometer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:divider="#color/mdtp_button_selected"
android:dividerHeight="1dp" />
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
android:visibility="visible"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="5dp"
android:weightSum="1">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="2">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1.9"
android:padding="10dp"
android:text="Graph View"
android:textAllCaps="false"
android:textColor="#color/subheading"
android:textSize="#dimen/subheading" />
</LinearLayout>
<com.jjoe64.graphview.GraphView
android:id="#+id/grapfinal"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_weight="1" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="5dp"
android:weightSum="1">
<LinearLayout
android:id="#+id/grphtextMain"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<LinearLayout
android:id="#+id/grphtextColor"
android:layout_width="10dp"
android:layout_height="10dp"
android:layout_marginTop="3dp"
android:orientation="vertical" />
<TextView
android:id="#+id/grphtext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:text="Hello"
android:textColor="#color/subheading"
android:textSize="14dp" />
</LinearLayout>
<LinearLayout
android:id="#+id/grphtextMain2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<LinearLayout
android:id="#+id/grphtextColor2"
android:layout_width="10dp"
android:layout_height="10dp"
android:layout_marginTop="3dp"
android:background="#color/red_btn_bg_color"
android:orientation="vertical" />
<TextView
android:id="#+id/grphtext2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:text="Hello"
android:textColor="#color/subheading"
android:textSize="14dp" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
android:visibility="visible"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="5dp"
android:weightSum="1">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="2">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1.9"
android:padding="10dp"
android:text="Graph View"
android:textAllCaps="false"
android:textColor="#color/subheading"
android:textSize="#dimen/subheading" />
</LinearLayout>
<com.jjoe64.graphview.GraphView
android:id="#+id/grapfinal1"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_weight="1" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="5dp"
android:weightSum="1">
<LinearLayout
android:id="#+id/grphtextMain1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<LinearLayout
android:id="#+id/grphtextColor1"
android:layout_width="10dp"
android:layout_height="10dp"
android:layout_marginTop="3dp"
android:background="#color/red_btn_bg_color"
android:orientation="vertical" />
<TextView
android:id="#+id/grphtext1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:text="Hello"
android:textColor="#color/subheading"
android:textSize="14dp" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
</ScrollView>
</RelativeLayout>
spirometer_item.xml
<?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.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
app:cardBackgroundColor="#fff4f4f3"
app:cardElevation="10dp"
app:cardPreventCornerOverlap="true"
card_view:cardCornerRadius="8dp">
<LinearLayout
android:padding="5dp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<TextView
android:id="#+id/tv_date"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center"
android:text="" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_marginLeft="10dp"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="#+id/tv_fvc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="FVC 3.15 L" />
<TextView
android:id="#+id/tv_fev1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="FEV1 2.44 L" />
<TextView
android:id="#+id/tv_pef"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="PEF 3.74 L/s" />
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
CustomAdapter1.java
public class CustomAdapter1 extends BaseAdapter {
ArrayList<GetUserSpirometer> arrayList;
public CustomAdapter1(ArrayList<GetUserSpirometer> arrayList){
this.arrayList=arrayList;
Log.e("arraylist length",""+arrayList.size());
}
#Override
public int getCount() {
return arrayList.size();
}
#Override
public Object getItem(int position) {
return position;
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater= getLayoutInflater();
ViewHolder1 holder=new ViewHolder1();
convertView = inflater.inflate(R.layout.spirometer_item, parent, false);
holder.tv_date=(TextView)convertView.findViewById(R.id.tv_date);
holder.tv_fvc=(TextView)convertView.findViewById(R.id.tv_fvc);
holder.tv_fev1=(TextView)convertView.findViewById(R.id.tv_fev1);
holder.tv_pef=(TextView)convertView.findViewById(R.id.tv_pef);
String[] arr = getDate(Long.parseLong(arrayList.get(position).get_date()), "MMM dd, yyy/hh:mm a").split("/");
holder.tv_date.setText(arr[0] + "\n" + arr[1]);
holder.tv_fvc.setText(arrayList.get(position).get_userfvc());
holder.tv_fev1.setText(arrayList.get(position).get_userfev1());
holder.tv_pef.setText(arrayList.get(position).get_userpef());
convertView.setTag(holder);
return convertView;
}
}
Items are not scrolling.
how to solve this. please help.
thanks in advance.
Then you need to change itemView's xml file ,
android:layout_height="match_parent"
instead of like ,
android:layout_height="wrap_content"
Simple change in spirometer_item.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
....Your design code
</LinearLayout>
I was having problem with the same from such a long time. Then I found a solution that worked for me.
Add a ListViewHelper java class. Here below is code for ListViewHelper.java
package com.molescope;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListAdapter;
import android.widget.ListView;
public class ListViewHelper {
public static void getListViewSize(ListView listView){
ListAdapter adapter = listView.getAdapter();
if(adapter!=null){
int totalHeight = 0;
//setting list adapter in loop tp get final size
for (int i=0; i<adapter.getCount(); i++){
View listItem = adapter.getView(i, null, listView);
listItem.measure(0,0);
totalHeight += listItem.getMeasuredHeight();
}
//setting listview items in adapter
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalHeight + (listView.getDividerHeight() *
(adapter.getCount()-1));
listView.setLayoutParams(params);
}else{
return;
}
}
}
And after adding this java file, in your code wherever you are setting adapter to listview, right after that line add the code below:
ListView myList=(ListView) findViewById(R.id.listView);
myList.setAdapter(new ArrayAdapter<String>.
(this,android.R.layout.simple_list_item_1, listview_array));
ListViewHelper.getListViewSize(myList);
I have two ViewPager on the same view with weight= 70/30 . The problem is when the keyboard pops, my both view keep the same weight and do not display my EditText.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:fab="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:focusableInTouchMode="true"
tools:context="com.suez.memboard.fragments.formWaterQuality">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="100"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_weight="30">
<com.suez.memboard.controls.TextView
android:id="#+id/path"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:textAppearance="#style/FreeTextBold" />
<LinearLayout
android:id="#+id/date_picker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp">
<com.suez.memboard.controls.TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="forms.label.intervention_date"
android:textAppearance="#style/FreeText"
android:textSize="15sp" />
<com.suez.memboard.controls.TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" : "
android:textAppearance="#style/FreeText"
android:textSize="15sp" />
<com.suez.memboard.controls.DateTimeText
android:id="#+id/date"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.7"
android:drawableRight="#drawable/ic_date_range_black_24dp"
android:focusable="false"
android:hint="forms.label.intervention_date"
fab:type="date" />
</LinearLayout>
<Spinner
android:id="#+id/spin_location"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.design.widget.TabLayout
android:id="#+id/tabA"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#color/suez_blue_1"
app:tabGravity="fill" />
<ImageButton
android:id="#+id/buttonAdd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#color/suez_blue_1"
android:src="#drawable/ic_add_box_white_48dp" />
</LinearLayout>
<android.support.v4.view.ViewPager
android:id="#+id/viewA"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_weight="70">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.design.widget.TabLayout
android:id="#+id/tabB"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/suez_blue_1"
app:tabGravity="fill" />
</LinearLayout>
<android.support.v4.view.ViewPager
android:id="#+id/viewB"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
<FAB ....>
I think my last solution is to make disappear my picker at the top when my keyboard pops. How can I do this?
Hope this help you :
public class YourActivity extends BaseActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.your_activity);
attachKeyboardListeners();
}
#Override
protected void onShowKeyboard(int keyboardHeight) {
// when keyboard is shown
yourWidget.setVisibility(View.GONE);
}
#Override
protected void onHideKeyboard() {
// when keyboard is hidden
yourWidget.setVisibility(View.VISIBLE);
}
}
Try to use ScrollView in parent view, for example-
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:fab="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:focusableInTouchMode="true">
<ScrollView
android:id="#+id/scroll_view_parent"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:fillViewport="true"
android:scrollbars="none">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
..................................
type your code here
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/colorWhite"
android:orientation="vertical">
..................................
type your code here
</LinearLayout>
</LinearLayout>
</ScrollView>
</RelativeLayout>
You can set listeners when keyboard is opened and closed.
// ContentView is the root view of the layout of this activity/fragment
contentView.getViewTreeObserver().addOnGlobalLayoutListener(
new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
Rect r = new Rect();
contentView.getWindowVisibleDisplayFrame(r);
int screenHeight = contentView.getRootView().getHeight();
// r.bottom is the position above soft keypad or device button.
// if keypad is shown, the r.bottom is smaller than that before.
int keypadHeight = screenHeight - r.bottom;
Log.d(TAG, "keypadHeight = " + keypadHeight);
if (keypadHeight > screenHeight * 0.15) { // 0.15 ratio is perhaps enough to determine keypad height.
// keyboard is opened
} else {
// keyboard is closed
}
}
});
<LinearLayout
android:id="#+id/layout_question_detail"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:visibility="gone">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="55dp"
>
<Button
android:id="#+id/btn_question_backButton"
android:layout_width="50dp"
android:layout_height="50dp"
android:background="#drawable/backicon_round"/>
<TextView
android:id="#+id/text_question_detail_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:textColor="#color/color_green"
android:text="Error"
android:textSize="19dp"
/>
</RelativeLayout>
<ImageView
android:id="#+id/image_question_detail_image"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="3"
android:scaleType="centerCrop"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:orientation="vertical"
android:layout_weight="2">
<TextView
android:id="#+id/text_question_detail_userComments"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20dp"
android:layout_marginTop="10dp"
android:layout_weight="1"
android:layout_marginBottom="25dp"
/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="3">
<ListView
android:id="#+id/list_question_detail_expComments"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:divider="#android:color/transparent"
android:dividerHeight="10dp"></ListView>
</RelativeLayout>
</LinearLayout>
</LinearLayout>
Now, I set layout for title on the top. Then, a Imageview, a textview and a listview follow below this layout, and only 1 textview is inside listview.
listview can vary in size.
The problem is, if the size of the listview is very big, I can only scroll the screen assigned to listview.
But, I want scroll the entire screen.
To solve this problem, I added scrollview outside of the first linearlayout.
However, It didn't work.(the imageview disappeared)
What can I do?
you can make your listview addheaderview,and put your imageview and textview into headerview
yourlayout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/layout_question_detail"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="55dp">
<Button
android:id="#+id/btn_question_backButton"
android:layout_width="50dp"
android:layout_height="50dp" />
<TextView
android:id="#+id/text_question_detail_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="Error"
android:textColor="#000000"
android:textSize="19dp"
/>
</RelativeLayout>
<ListView
android:id="#+id/list_question_detail_expComments"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:divider="#android:color/transparent"
android:dividerHeight="10dp"></ListView>
lv_header.xml
<?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"
>
<ImageView
android:id="#+id/image_question_detail_image"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="3"
android:scaleType="centerCrop" />
<TextView
android:id="#+id/text_question_detail_userComments"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="25dp"
android:layout_marginTop="10dp"
android:layout_weight="1"
android:textSize="20dp" />
Activity.class
public class MainActivity extends AppCompatActivity {
private ListView list_question_detail_expComments;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
list_question_detail_expComments = (ListView) findViewById(R.id.list_question_detail_expComments);
View lvHeaderView = View.inflate(this,R.layout.lv_header,null);
list_question_detail_expComments.addHeaderView(lvHeaderView);
list_question_detail_expComments.setAdapter(new LvAdapter());
}
private class LvAdapter extends BaseAdapter{
#Override
public int getCount() {
return 20;
}
#Override
public Object getItem(int i) {
return null;
}
#Override
public long getItemId(int i) {
return 0;
}
#Override
public View getView(int i, View view, ViewGroup viewGroup) {
TextView tv = new TextView(MainActivity.this);
tv.setText("test"+i);
return tv;
}
}
}
I am trying to change programmatically the background color of ListView item when the user tap on it. I am trying to implement a solution using setOnItemClickListener, but I have trouble making the listener work. Here is my code:
ArrayList<Integer> menuIcons = app.Theme.Icons.getMenuPanelIconList();
ArrayList<String> menuText = app.Languages.Active.getMenuPanelItemNames();
listview_menu = (ListView) findViewById(R.id.listview_menu);
listview_menu.setBackgroundColor(Color.parseColor("#" + app.Theme.ThemeBackColor));
MenuPanelListViewAdapter menuPanelListViewAdapter = new MenuPanelListViewAdapter(ActivityMain.this, menuIcons, menuText);
listview_menu.setAdapter(menuPanelListViewAdapter);
listview_menu.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adpterView, View view, int position,
long id) {
for (int i = 0; i < listview_menu.getChildCount(); i++) {
if(position == i ){
listview_menu.getChildAt(i).setBackgroundColor(Color.BLUE);
}else{
listview_menu.getChildAt(i).setBackgroundColor(Color.TRANSPARENT);
}
}
}
});
Here is my mainactivity.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:fresco="http://schemas.android.com/apk/res-auto"
android:id="#+id/relativelayout_base"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:id="#+id/relativelayout_toolbar"
android:layout_width="match_parent"
android:layout_height="?android:actionBarSize">
<LinearLayout
android:id="#+id/linearlayout_menu"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:clickable="true"
android:gravity="center"
android:paddingLeft="10dp"
android:paddingRight="10dp">
<ImageView
android:id="#+id/imageview_menu"
android:layout_width="20dp"
android:layout_height="20dp"
android:src="#drawable/menushow_g" />
</LinearLayout>
<com.hotelstayapp.android.custom.CustomTextView
android:id="#+id/customtextview_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toRightOf="#+id/linearlayout_menu"
android:textColor="#FFFFFF"
android:textSize="27sp"
android:ellipsize="end"
android:singleLine="true"
android:paddingBottom="1dp"/>
</RelativeLayout>
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.hotelstayapp.android.UI.ActivityMain"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/relativelayout_toolbar">
<FrameLayout
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/relativelayout_menupanel"
android:layout_width="280dp"
android:layout_height="match_parent"
android:layout_gravity="left">
<RelativeLayout
android:id="#+id/relativelayout_menuheader"
android:layout_width="match_parent"
android:layout_height="130dp"
android:clickable="true">
<com.facebook.drawee.view.SimpleDraweeView
fresco:actualImageScaleType="centerCrop"
android:id="#+id/imageview_menubgimage"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:adjustViewBounds="true"
android:scaleType="centerCrop"
/>
<com.facebook.drawee.view.SimpleDraweeView
android:layout_width="240dp"
android:layout_height="70dp"
fresco:actualImageScaleType="fitCenter"
android:id="#+id/imageview_menulogo"
android:layout_centerInParent="true"
/>
</RelativeLayout>
<ListView
android:id="#+id/listview_menu"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#id/relativelayout_menuheader"
android:layout_gravity="right"
android:divider="#android:color/transparent"
android:dividerHeight="0dp"
android:paddingTop="10dp"
android:smoothScrollbar="true"
android:focusable="false"
android:focusableInTouchMode="false">
</ListView>
</RelativeLayout>
</android.support.v4.widget.DrawerLayout>
</RelativeLayout>
Here is my ListView items 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="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="1"
android:paddingBottom="14dp"
android:paddingTop="14dp"
android:paddingLeft="0dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:layout_weight="0.8"
android:gravity="center">
<ImageView
android:layout_width="23dp"
android:layout_height="23dp"
android:id="#+id/imageViewIcon"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:src="#drawable/menuhome_g"
android:layout_centerHorizontal="true" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:layout_weight="0.2"
android:paddingLeft="15dp"
android:gravity="center_vertical">
<com.hotelstayapp.android.custom.CustomTextView
android:id="#+id/textViewMenuItemName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center|left"
android:text="Home"
android:textColor="#000000"
android:textSize="15sp"
android:textStyle="bold"/>
</LinearLayout>
</LinearLayout>
<View
android:id="#+id/viewMenuDivider"
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#eeeeee"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"></View>
</LinearLayout>
First change background of all item to transperent then set clicked view's background to your desired color like below.
MenuPanelListViewAdapter menuPanelListViewAdapter = new MenuPanelListViewAdapter(ActivityMain.this, menuIcons, menuText);
listview_menu.setAdapter(menuPanelListViewAdapter);
listview_menu.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adpterView, View view, int position,
long id) {
for (int i = 0; i < listview_menu.getChildCount(); i++) {
listview_menu.getChildAt(i).setBackgroundColor(Color.TRANSPARENT);
}
view.setBackgroundColor(Color.BLUE);
menuPanelListViewAdapter.notifyDataSetChanged(); // this is importent
}
});
I am having a problems with adding scroll view in my dynamically created buttons.here is my xml code.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<LinearLayout
android:id="#+id/linear1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="27dp"
android:background="#drawable/custom_border"
android:orientation="vertical" >
<EditText
android:id="#+id/editText1"
android:layout_width="243dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignTop="#+id/linearLayout1"
android:layout_marginTop="28dp"
android:ems="10"
android:hint="#string/hinttextitem" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/editText1"
android:layout_alignParentRight="true"
android:layout_marginRight="26dp"
android:background="#drawable/button_shape"
android:onClick="newonClick"
android:text="click" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/linear1"
android:layout_centerHorizontal="true"
android:layout_marginTop="80dp"
android:id="#+id/dynamic"
android:background="#drawable/dynamic_border"
android:orientation="vertical" >
</LinearLayout>
</RelativeLayout>
here is my java code
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void newonClick(View V){
LinearLayout layout=(LinearLayout)findViewById(R.id.linear1);
LinearLayout dynamiclayout=(LinearLayout)findViewById(R.id.dynamic);
dynamiclayout.removeAllViews();
EditText edittext = (EditText) findViewById(R.id.editText1);
int x=Integer.valueOf(edittext.getText().toString());
for(int i=1;i<=x;i++)
{
Button addButton =new Button(this);
addButton.setText("Button"+i);
addButton.setId(i);
dynamiclayout.addView(addButton);
addButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Toast.makeText(getApplicationContext(),
"button is clicked"+v.getId(), 8000).show();
}
});
}
if am adding scroll view in dynamic layout, it will disappear.so how can i add vertical scroll view only for dynamically created buttons.can anyone solve my problem?
try this: place scroll view for LinearLayout only.
<ScrollView
android:layout_width="wrap_content"
android:layout_below="#+id/linear1"
android:layout_height="wrap_content" >
<LinearLayout
android:id="#+id/dynamic"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerHorizontal="true"
android:layout_marginTop="80dp"
android:background="#drawable/dynamic_border"
android:orientation="vertical" >
</LinearLayout>
</ScrollView>
Try to use,
In XML,
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/linear1"
android:id="#+id/scrollView1">
<LinearLayout
android:id="#+id/dynamic"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerHorizontal="true"
android:layout_marginTop="80dp"
android:orientation="vertical" >
</LinearLayout>
</ScrollView>
In Java:
this.scrollView = (ScrollView) findViewById(R.id.scrollView1);
this.linearLayout = (LinearLayout) findViewById(R.id.dynamic);
this.linearLayout.setOrientation(LinearLayout.VERTICAL);
Button[] btn = new Button[10];
for (int i = 0; i < 10; i++) {
btn[i] = new Button(this);
btn[i].setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
btn[i].setText("This is the button" + i);
this.linearLayout.addView(btn[i]);
}
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/linear1" >
<LinearLayout
android:id="#+id/dynamic"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerHorizontal="true"
android:layout_marginTop="80dp"
android:orientation="vertical" >
</LinearLayout>
</ScrollView>
please replace this code: