I want to add collapsing toolbar in my app. I want to add a graph layout also image view and text views inside the collapsing toolbar layout. Like this image.
This graph is in collapsing toolbar layout. When we do scroll up graph goes up and remains as a toolbar.
Now I am trying to add views under collapsing toolbar layout. But I am not able to move them and align them. How to do?
Layout:
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/parentPanel"
xmlns:fab="http://schemas.android.com/apk/res-auto">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="250dp"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
android:id="#+id/appbar">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/ic_reorder_black_48dp"
android:id="#+id/navigationMenu" />
</android.support.v7.widget.Toolbar>
<android.support.design.widget.CollapsingToolbarLayout
android:id="#+id/collapsing_toolbar"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:contentScrim="?attr/colorPrimary"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/ic_check_box_white_24dp"/>
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffe5e5e5"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
android:id="#+id/scrollView">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingTop="10dp">
<include layout="#layout/card_layout" />
<include layout="#layout/card_layout" />
<include layout="#layout/card_layout" />
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
<android.support.design.widget.FloatingActionButton
android:id="#+id/fab"
app:layout_anchor="#id/appbar"
app:layout_anchorGravity="bottom|right|end"
style="#style/FabStyle"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_anchor = "#id/parentPanel"
app:layout_anchorGravity = "bottom|right|end"
android:padding="20dp"
android:weightSum="1">
<com.github.clans.fab.FloatingActionMenu
android:id="#+id/menu1"
android:layout_width="match_parent"
android:layout_height="219dp"
android:paddingRight="10dp"
android:paddingBottom="10dp"
android:paddingLeft="10dp"
fab:menu_labels_ellipsize="end"
fab:menu_labels_singleLine="true"
fab:menu_backgroundColor="#ccffffff"
fab:menu_fab_label="Menu label"
android:layout_gravity="bottom">
<com.github.clans.fab.FloatingActionButton
android:id="#+id/fab1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_add_white_24dp"
fab:fab_size="mini"
fab:fab_label="addList" />
<com.github.clans.fab.FloatingActionButton
android:id="#+id/fab2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_list_white_24dp"
fab:fab_size="mini"
fab:fab_label="list1" />
<com.github.clans.fab.FloatingActionButton
android:id="#+id/fab3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_list_white_24dp"
fab:fab_size="mini"
fab:fab_label="list2" />
<com.github.clans.fab.FloatingActionButton
android:id="#+id/fab4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_list_white_24dp"
fab:fab_size="mini"
fab:fab_label="list3" />
</com.github.clans.fab.FloatingActionMenu>
</LinearLayout>
Activity:
public class MainActivity extends AppCompatActivity {
private CollapsingToolbarLayout collapsingToolbarLayout = null;
private FloatingActionButton fab1;
private FloatingActionButton fab2;
private FloatingActionButton fab3;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
final FloatingActionMenu menu1 = (FloatingActionMenu) findViewById(R.id.menu1);
collapsingToolbarLayout = (CollapsingToolbarLayout) findViewById(R.id.collapsing_toolbar);
collapsingToolbarLayout.setTitle("List Title");
ImageView navigationMenu = (ImageView)findViewById(R.id.navigationMenu);
fab1 = (FloatingActionButton) findViewById(R.id.fab1);
fab2 = (FloatingActionButton) findViewById(R.id.fab2);
fab3 = (FloatingActionButton) findViewById(R.id.fab3);
fab1.setOnClickListener(clickListener);
fab2.setOnClickListener(clickListener);
fab3.setOnClickListener(clickListener);
navigationMenu.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this,NavigationMenuActivity.class));
overridePendingTransition(R.anim.left, R.anim.enter);
}
});
menu1.setOnMenuButtonClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (menu1.isOpened()) {
Toast.makeText(MainActivity.this, menu1.getMenuButtonLabelText(), Toast.LENGTH_SHORT).show();
}
menu1.toggle(true);
}
});
menu1.setClosedOnTouchOutside(true);
dynamicToolbarColor();
toolbarTextAppernce();
}
private void dynamicToolbarColor() {
Bitmap bitmap = BitmapFactory.decodeResource(getResources(),
R.drawable.b);
Palette.from(bitmap).generate(new Palette.PaletteAsyncListener() {
#Override
public void onGenerated(Palette palette) {
collapsingToolbarLayout.setContentScrimColor(palette.getMutedColor(getResources().getColor(R.color.colorPrimary)));
collapsingToolbarLayout.setStatusBarScrimColor(palette.getMutedColor(getResources().getColor(R.color.colorPrimaryDark)));
}
});
}
private void toolbarTextAppernce() {
collapsingToolbarLayout.setCollapsedTitleTextAppearance(R.style.collapsedappbar);
collapsingToolbarLayout.setExpandedTitleTextAppearance(R.style.expandedappbar);
}
private View.OnClickListener clickListener = new View.OnClickListener() {
#Override
public void onClick(View v) {
String text = "";
switch (v.getId()) {
case R.id.fab1:
text = fab1.getLabelText();
break;
case R.id.fab2:
text = fab2.getLabelText();
fab2.setVisibility(View.GONE);
break;
case R.id.fab3:
text = fab3.getLabelText();
fab2.setVisibility(View.VISIBLE);
break;
}
}
};
}
Now I have a single image view inside collapsing toolbar layout and that I can't move or align. How to align view and add views under collapsing toolbar?
Thank you..
CollapsingToolbarLayout is a subclass of FrameLayout.
For maximum flexibility, put all your widgets inside a LinearLayout or RelativeLayout and make it the only child of the CollapsingToolbarLayout. Also for best results with vertical sizing, set the height to a specific dimension.
<android.support.design.widget.CollapsingToolbarLayout
android:id="#+id/collapsing_toolbar"
android:layout_width="match_parent"
android:layout_height="#dimen/graph_toolbar_height"
app:contentScrim="?attr/colorPrimary"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_collapseMode="parallax">
<!-- graph UI here -->
</RelativeLayout>
</android.support.design.widget.CollapsingToolbarLayout>
Create a layout inside collapsingtoolbarlayout and add your views in there.
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/AppTheme.AppBarOverlay">
<android.support.design.widget.CollapsingToolbarLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
app:contentScrim="#color/colorPrimary"
app:expandedTitleGravity="bottom|center_horizontal"
app:expandedTitleMarginBottom="64dp"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:layout_width="match_parent"
android:layout_height="230dp"
android:scaleType="centerCrop"
android:src="#drawable/ic_about"
app:layout_collapseMode="parallax" />
<TextView
android:layout_width="match_parent"
android:layout_height="48dp"
android:background="#color/colorAccent"
android:text="#string/about_title"
android:textSize="#dimen/text_size_large" />
</LinearLayout>
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?actionBarSize"
android:layout_marginTop="#dimen/status_bar_height"
app:layout_collapseMode="pin"
app:popupTheme="#style/AppTheme.PopupOverlay" />
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
Related
I'm trying to set a TextView that's in a fragment but the text won't update in the view. The text is 1 of 2 pages in a ViewPager. The ViewPager is inside another fragment that has a button which when clicked uses an interface to the MainActivity that calls the changeText() that's inside the fragment with my text.
Fragment Inside ViewPager:
public class PlaceholderFragment extends Fragment {
public TextView textFV;
public PlaceholderFragment() {}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.tab_pull, container, false);
textFV = rootView.findViewById(R.id.textFV);
return rootView;
}
#Override
public void onViewCreated(#NonNull View view, Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
public void changeText() {
textFV.setText("Test"); - Won't update view.
System.out.println(textFV.getText()); - Shows correct text.
}
}
Relevant part of Fragment that contains ViewPager:
OnHeadlineSelectedListener callback;
public void setOnHeadlineSelectedListener(OnHeadlineSelectedListener callback) {
this.callback = callback;
}
public interface OnHeadlineSelectedListener {
void onArticleSelected();
}
//...
#Override
public void onClick (View v) {
callback.onArticleSelected();
}
Relevant MainActivity Part:
public void onArticleSelected() {
PlaceholderFragment articleFrag = (PlaceholderFragment) getSupportFragmentManager().findFragmentById(R.id.viewpager);
if (articleFrag != null) {
articleFrag.changeText();
}
}
Here is the layout that contains the TextView:
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="android.support.design.widget.AppBarLayout$ScrollingViewBehavior">
<LinearLayout
android:id="#+id/tab_1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="10dp">
<LinearLayout
android:id="#+id/containerPull"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:visibility="gone">
<TextView
android:id="#+id/textsLabel"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="#dimen/label_left_margin"
android:layout_marginTop="5dp"
android:text="#string/label_common"
android:textColor="#color/colorLabelText"
android:textSize="18sp" />
<TextView
android:id="#+id/textFV"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Hello, World!"
android:textColor="#android:color/black"
android:textSize="18sp" />
</LinearLayout>
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
</android.support.constraint.ConstraintLayout>
Here's where the button and ViewPager is. The button is the ImageButton:
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:id="#+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/white">
<android.support.v7.widget.Toolbar
android:id="#+id/pullToolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:title="Information"
app:titleTextAppearance="#style/TextAppearance.Widget.AppCompat.Toolbar.Title.Montserrat">
<ImageButton
android:id="#+id/toolbarButtonPull"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end"
android:layout_marginEnd="1dp"
android:background="#drawable/button_toolbar_default"
android:contentDescription="Pull"
android:padding="15dp"
android:src="#drawable/ic_arrow_downward_black_24dp" />
</android.support.v7.widget.Toolbar>
<android.support.design.widget.TabLayout
android:id="#+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabMode="fixed"
app:tabTextAppearance="#style/TextAppearance.Design.Tab.NoCaps">
<android.support.design.widget.TabItem
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Tab1" />
<android.support.design.widget.TabItem
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Tab2" />
</android.support.design.widget.TabLayout>
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="#+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="android.support.design.widget.AppBarLayout$ScrollingViewBehavior" />
</android.support.design.widget.CoordinatorLayout>
Looks like your textFV is inside layout that has visibility gone. Can it be the problem?
<LinearLayout
android:id="#+id/containerPull"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
**android:visibility="gone">**
<TextView
android:id="#+id/textsLabel"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="#dimen/label_left_margin"
android:layout_marginTop="5dp"
android:text="#string/label_common"
android:textColor="#color/colorLabelText"
android:textSize="18sp" />
<TextView
android:id="#+id/textFV"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Hello, World!"
android:textColor="#android:color/black"
android:textSize="18sp" />
I designed two buttons b1,b2 inside "KeyboardService.java" which should update the value of PACK_LIB inside "Stickers.java".
Three files attached:
main_board_layout.xml
Stickers.java
KeyboardService.java
I assigned both buttons with IDs of Button1 = b1 and Button2 = b2.
The built is successful but when I click on the buttons both do not work.
I think it is maybe a problem with this line ->
final Button button1 = (Button) mainBoard.findViewById(R.id.b1);
Because the Buttons are inside a FrameLayout. But when I use ID of that it won't work either.
Any ideas?
-------main_board_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/main_board"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="#color/black"
android:orientation="vertical">
<android.inputmethodservice.KeyboardView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/keyboard_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/black" />
<!--top bar-->
<View
android:layout_width="match_parent"
android:layout_height="0dp"
android:background="#ffffff"/>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ffffff"
android:padding="2dp"
android:id="#+id/buttons"
>
<Button
android:id="#+id/b1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/button1" />
<Button
android:id="#+id/b2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:text="#string/button2"
/>
<TextView
android:id="#+id/packNameLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="start"
android:text="#string/app_name"
android:textColor="#000000"
android:textSize="1sp"
android:visibility="gone"
/>
<ImageView
android:id="#+id/btShareLinkGP"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_gravity="center"
android:src="#mipmap/bt_share"
tools:srcCompat="#mipmap/bt_share"
/>
</FrameLayout>
<View
android:layout_width="match_parent"
android:layout_height="3dp"
android:background="#000000"/>
<ScrollView
android:id="#+id/gif_view"
android:layout_width="match_parent"
android:layout_height="190dp"
android:background="#color/black"
android:paddingLeft="0dp"
android:paddingRight="15dp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/black"
android:gravity="left|center"
android:orientation="horizontal"
android:padding="0dp">
<android.support.v7.widget.RecyclerView
android:id="#+id/pack_recycler_view"
android:layout_width="0dp"
android:layout_height="0dp"
android:background="#color/black"
android:orientation="horizontal"
app:layoutManager="android.support.v7.widget.LinearLayoutManager" />
</LinearLayout>
</LinearLayout>
-------Stickers.java
public static String PACK_LIB ="";
public void setDefaultStickerPack() {
checkVersion(true);
InputStream in = null;
String packList[]=new String[0];
final String PACK_APP="pack_app";
final String PACK_ICON="pack_on.png";
String curAssets="";
-------KeyboardService.java
#Override
public View onCreateInputView() {
mainBoard = (LinearLayout) getLayoutInflater().inflate(R.layout.main_board_layout, null);
packNameLabel = (TextView) mainBoard.findViewById(R.id.packNameLabel);
scrollView = (ScrollView) mainBoard.findViewById(R.id.gif_view);
stickerView = (RecyclerView) getLayoutInflater().inflate(R.layout.recycler_view, null);
stickerView.addItemDecoration(new MarginDecoration(this));
stickerView.setHasFixedSize(true);
stickerView.setLayoutManager(new GridLayoutManager(this, 4));
scrollView.addView(stickerView);
ImageView btShareLinkGP = (ImageView) mainBoard.findViewById(R.id.btShareLinkGP);
btShareLinkGP.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
shareLinkToGP();
}
});
// packs bar
packView = (RecyclerView) mainBoard.findViewById(R.id.pack_recycler_view);
// BUTTONS ACTIONS
final Button button1 = (Button) mainBoard.findViewById(R.id.b1);
button1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Stickers.PACK_LIB = "allstickers";
}
});
final Button button2 = (Button) mainBoard.findViewById(R.id.b2);
button2.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Stickers.PACK_LIB = "teststickers";
}
});
showStickers();
return mainBoard;
}
As you can see, the 3 dots of the menu or the search button are in the top third of the tool bar. How can I center it?
If its relevant - some of my XML's:
tool_bar.xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/ColorPrimary"
android:elevation="4dp" >
<ImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_gravity="start"
android:src="#drawable/app_icon" />
</android.support.v7.widget.Toolbar>
activity_main.xml:
<include
android:id="#+id/tool_bar"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="2"
layout="#layout/tool_bar" >
</include>
<other views>
searchable.xml:
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:hint="#string/hint_search"
android:icon="#drawable/magnifying_glass"
android:label="#string/app_name"
android:searchSuggestAuthority="com.app.recommendations.content_provider"
android:searchSuggestIntentAction="android.intent.action.VIEW"
android:searchSuggestIntentData=""
android:voiceSearchMode="showVoiceSearchButton|launchRecognizer" />
main_menu.xml:
...
<item
android:id="#+id/search"
android:icon="#drawable/magnifying_glass"
android:title="#string/hint_search"
app:actionViewClass="android.support.v7.widget.SearchView"
app:showAsAction="collapseActionView|ifRoom"/>
...
Change the code of your too_bar.xml
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="#style/AppTheme.PopupOverlay">
<ImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_gravity="start"
android:src="#drawable/app_icon" />
</android.support.v7.widget.Toolbar>
</android.support.design.widget.AppBarLayout>
You can create a custom ToolBar with ActionMenuView.
In your toolbar.xml
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="#style/AppTheme.PopupOverlay" >
<android.support.v7.widget.ActionMenuView
android:id="#+id/action_menu"
android:gravity="center"
android:layout_width="wrap_content"
android:layout_height="?attr/actionBarSize"/>
</android.support.v7.widget.Toolbar>
In your MainActivity,
public class MainActivity extends AppCompatActivity {
ActionMenuView actionMenuView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
actionMenuView = (ActionMenuView) toolbar.findViewById(R.id.action_menu);
actionMenuView.setOnMenuItemClickListener(new ActionMenuView.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem menuItem) {
return onOptionsItemSelected(menuItem);
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, actionMenuView.getMenu());
return true;
}
}
Set ActionViewMenu's gravity to 'center'. I hope it will help you.
Instead of using android:layout_weight="2" and android:layout_height="0dp", I just setted the height to wrap_content and that made the toolbar lean mean and centered!
Hello.. I have a layout in which I have ImageView and EditText view. I want to add a different layout on click of ImageView of arrow.
It should look like this after click event of ImageView. How can I get this??
Pleas help...
For perfect answer please add your xml file and java file source code.
Example
MainActivity.java
public class MainActivity extends Activity {
LinearLayout linear1,linear2;
ImageView imgArrow;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
linear1 = (LinearLayout) findViewById(R.id.linear1);
linear2 = (LinearLayout) findViewById(R.id.linear2);
imgArrow = (ImageView) findViewById(R.id.imgArrow);
linear1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(linear2.getVisibility() == View.GONE){
linear2.setVisibility(View.VISIBLE);
imgArrow.setImageResource(R.drawable.up);
}else{
linear2.setVisibility(View.GONE);
imgArrow.setImageResource(R.drawable.down);
}
}
});
}
}
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:id="#+id/linear1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<ImageView
android:id="#+id/btnClick"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.3"
android:src="#drawable/ic_launcher" />
<TextView
android:id="#+id/tv1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="0.5"
android:text="Additional Contact" />
<ImageView
android:id="#+id/imgArrow"
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_weight="0.2"
android:src="#drawable/down" />
</LinearLayout>
<LinearLayout
android:id="#+id/linear2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:visibility="gone"
android:orientation="vertical" >
<TextView
android:id="#+id/txt1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="User Name here" />
<TextView
android:id="#+id/txt2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="Phone number here" />
<Button
android:id="#+id/btnone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="Click Here" />
</LinearLayout>
</LinearLayout>
Here down and up is image file for arrow.
so put that both arrow file in drawable folder.
You could just set an OnClickListener to the ImageView, which contains the arrow. Then assign a method, which handles the click and produces your different layout
I am working on ViewPager. I wrote code which can create ViewPager and also I have some java class, but I want my ViewPager must be below the layout.
This is a my xml code
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#fa6a6a"
>
<RelativeLayout
android:id="#+id/relativeLayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#171717" >
<ImageButton
android:id="#+id/menu_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="12dp"
android:background="#drawable/ic_launcher" />
<ImageView
android:id="#+id/image"
android:layout_width="wrap_content"
android:layout_height="7.0mm"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:background="#drawable/ic_launcher" />
<ToggleButton
android:id="#+id/button_search"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="12dp"
android:background="#drawable/ic_launcher"
android:padding="2dip"
android:textOff=""
android:textOn="" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_below="#+id/relativeLayout1" >
<android.support.v4.view.ViewPager
android:id="#+id/vp_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/relativeLayout1" >
</android.support.v4.view.ViewPager>
</RelativeLayout>
This is a my java code:
public class MainActivity extends FragmentActivity implements
ActionBar.TabListener {
private ViewPager viewPager;
private TabsPagerAdapter mAdapter;
private ActionBar actionBar;
// Tab titles
private String[] tabs = { "test1", "test2", "test3", "test4",
"test5" };
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewPager = (ViewPager) findViewById(R.id.vp_main);
actionBar = getActionBar();
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setDisplayShowHomeEnabled(false);
mAdapter = new TabsPagerAdapter(getSupportFragmentManager());
viewPager.setAdapter(mAdapter);
actionBar.setHomeButtonEnabled(false);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// Adding Tabs
for (String tab_name : tabs) {
actionBar.addTab(actionBar.newTab().setText(tab_name)
.setTabListener(this));
}
/**
* on swiping the viewpager make respective tab selected
* */
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageSelected(int position) {
actionBar.setSelectedNavigationItem(position);
}
#Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
#Override
public void onPageScrollStateChanged(int arg0) {
}
});
}
#Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
}
#Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
// on tab selected
// show respected fragment view
viewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
}
}
result it like this
I want my viewpager(actionBar)should be rendered below the layout
Check It Out,
It will help u..
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#fa6a6a" >
<RelativeLayout
android:id="#+id/relativeLayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#171717" >
<ImageButton
android:id="#+id/menu_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="12dp"
android:background="#drawable/ic_launcher" />
<ImageView
android:id="#+id/image"
android:layout_width="wrap_content"
android:layout_height="7.0mm"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:background="#drawable/ic_launcher" />
<ToggleButton
android:id="#+id/button_search"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="12dp"
android:background="#drawable/ic_launcher"
android:padding="2dip"
android:textOff=""
android:textOn="" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_below="#+id/relativeLayout1" >
<android.support.v4.view.ViewPager
android:id="#+id/vp_main"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</android.support.v4.view.ViewPager>
</RelativeLayout>
</RelativeLayout>
Try this :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#fa6a6a" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true">
<android.support.v4.view.ViewPager
android:id="#+id/vp_main"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v4.view.ViewPager>
</RelativeLayout>
<RelativeLayout
android:id="#+id/relativeLayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#171717" >
<ImageButton
android:id="#+id/menu_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="12dp"
android:background="#drawable/ic_launcher" />
<ImageView
android:id="#+id/image"
android:layout_width="wrap_content"
android:layout_height="7.0mm"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:background="#drawable/ic_launcher" />
<ToggleButton
android:id="#+id/button_search"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="12dp"
android:background="#drawable/ic_launcher"
android:padding="2dip"
android:textOff=""
android:textOn="" />
</RelativeLayout>
</RelativeLayout>
Also dont forget to clean your project . Hope it helps