I am having a little trouble with this relative layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/background" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="#string/one_string"
android:onClick="goToScene2" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:text="#string/two_string"
android:onClick="goToScene1" />
<Button
android:id="#+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="#string/three_string" />
<ImageView
android:id="#+id/custView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:url="#drawable/panel_sets" />
</RelativeLayout>
wich renders the activity like this (scene2 in transition):
But i want to move ImageView like this image in order to later move it by aplying a transition in other layout.xml (this will be scene1). How can i do it?
Thank you very much
You Can Use Custom SlidingDrawer and canvas object to draw whatever you want
1.Create Custom class by implementing View and try this:
Paint paint = new Paint(); paint.setStyle(Paint.Style.FILL);paint.setColor(Color.BLACK); canvas.drawPaint(paint);paint.setColor(Color.WHITE); canvas.drawCircle(100,100,30,paint);
In XML Layout try to set yout custom class and slidingdrawer
<SlidingDrawer
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/Mydrawer" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:handle="#+id/handle"
android:content="#+id/content"><com.exmaple.MyCustomView
android:id="#+id/customview" android:layout_width="fill_parent"
android:layout_height="fill_parent"></com.exmaple.MyCustomView><ImageView
android:id="#id/handle" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:src="#drawable/def_tray" /></SlidingDrawer>
And finally in you Activity defined what happen when drawer close and open
MyslidingDrawer.setOnDrawerCloseListener(new OnDrawerCloseListener() { #Overridepublic void onDrawerClosed(){}});MyCustomView customView = (MyCustomView) findViewById(R.id.mycontent);customView.setOnTouchListener(new OnTouchListener() {#Override
public boolean onTouch(View v, MotionEvent event) {return true;}});
Related
I'm learning android and And I'm facing some troubles. I kindly request you to solve these problems.
I have 2 linear layouts inside a linear layout, both are placed horizontally. The left side section contains some buttons and if we click the button the respective layout must come on the right-side section. As of now, if I click the button the specific layout is appearing but not inside the second linear layout. And also, I want to set a default layout to appear on the right side layout. For ex, here I've added the "breakfastdishes.xml", which I want as a default right-side layout, and when I click on the buttons from the left-side layout, according to id the right-side layout must change. Can you please help me to achieve it?
Here is my code:
MenuSection.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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MenuSection"
android:background="#drawable/gradient"
android:padding="10dp">
<ScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<Button
android:id="#+id/breakfast"
android:layout_width="220dp"
android:layout_height="65dp"
android:text="#string/breakfast"
android:textSize="20dp"
android:background="#drawable/menu_category"
android:fontFamily="sans-serif"
android:textStyle="bold"
app:backgroundTint="#AC8E0D"/>
<Button
android:id="#+id/lunch"
android:layout_width="220dp"
android:layout_height="65dp"
android:layout_marginTop="60dp"
android:text="#string/lunch"
android:textSize="20dp"
android:background="#drawable/menu_category"
android:fontFamily="sans-serif"
android:textStyle="bold"
app:backgroundTint="#0A5FAA"/>
<Button
android:id="#+id/eveningSnacks"
android:layout_width="220dp"
android:layout_height="65dp"
android:layout_marginTop="60dp"
android:background="#drawable/menu_category"
android:fontFamily="sans-serif"
android:text="#string/snacks"
android:textSize="20dp"
android:textStyle="bold"
app:backgroundTint="#DF5124" />
<Button
android:id="#+id/dinner"
android:layout_width="220dp"
android:layout_height="65dp"
android:layout_marginTop="60dp"
android:text="#string/dinner"
android:textSize="20dp"
android:background="#drawable/menu_category"
android:fontFamily="sans-serif"
android:textStyle="bold"
app:backgroundTint="#14A61A"/>
</LinearLayout>
</ScrollView>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="10dp"
android:id="#+id/itemsDisplay">
<LinearLayout
android:id="#+id/menuDispArea"
android:layout_width="508dp"
android:layout_height="412dp">
<!-- <include-->
<!-- layout="#layout/breakfastdishes"-->
<!-- android:layout_width="508dp"-->
<!-- android:layout_height="412dp" />-->
</LinearLayout>
</ScrollView>
</LinearLayout>
BreakfastDishes.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"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/gradient"
android:paddingStart="12dp"
android:paddingLeft="10dp"
android:paddingTop="20dp"
android:paddingEnd="12dp"
android:paddingRight="10dp"
android:paddingBottom="20dp">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left"
android:orientation="vertical">
<ImageView
android:id="#+id/burger"
android:layout_width="155dp"
android:layout_height="102dp"
android:layout_x="10dp"
android:layout_y="10dp"
app:srcCompat="#mipmap/burger_breakfast" />
<TextView
android:id="#+id/burgerTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/burger"
android:layout_alignTop="#+id/burger"
android:layout_alignRight="#+id/burger"
android:layout_alignBottom="#+id/burger"
android:layout_gravity="center_horizontal"
android:layout_margin="1dp"
android:gravity="center"
android:text="#string/burger"
android:textColor="#FFF"
android:textSize="16sp"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="50dp"
android:orientation="vertical">
<ImageView
android:id="#+id/eggOmlet"
android:layout_width="160dp"
android:layout_height="104dp"
android:layout_x="233dp"
android:layout_y="7dp"
app:srcCompat="#mipmap/egg_omlet" />
<TextView
android:id="#+id/eggOmletTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/eggOmlet"
android:layout_alignTop="#+id/eggOmlet"
android:layout_alignRight="#+id/eggOmlet"
android:layout_alignBottom="#+id/eggOmlet"
android:layout_gravity="center_horizontal"
android:layout_margin="1dp"
android:gravity="center"
android:text="#string/eggOmlet"
android:textColor="#FFF"
android:textSize="16sp"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
MenuSection.java
package com.example.restaurant;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MenuSection extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_menu_section);
Button breakfast = (Button) findViewById(R.id.breakfast);
Button lunch = (Button) findViewById(R.id.lunch);
breakfast.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(MenuSection.this, BreakfastDishes.class);
startActivity(intent);
}
});
lunch.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(MenuSection.this, LunchDishes.class);
startActivity(intent);
}
});
}
#Override
public void onBackPressed(){
//super.onBackPressed();
}
}
Akshay, here are a few things you have to keep in mind ;
In the button click listener you are launching new activity instead of updating
the existing layout. When you use Intent it will launch new activity and
close the current activity. So in your case do not use startActivity
Use findViewById for the menuDispArea layout and add update
the content when user clicks on the button.
never use dp for text size, use sp
you can divide the layout using LinearLayout weights to split your layouts on
all the devices instead of hardcoding the height and width
https://developer.android.com/guide/topics/ui/layout/linear#Weight
edit: Added sample
LayoutInflater inflater = LayoutInflater.from(MenuSection.this);
LinearLayout parentLayout = findViewById(R.id.menuDispArea);
View menuLayout= inflater.inflate(R.layout.BreakfastDishes, parentLayout, false);
parentLayout.addView(menuLayout);
You can use this to inflate any XML in your layout dynamically. Make sure you are doing this inside button onClick for your use case.
I want to make layout for Image Slider, so have to create the view pager as well as have to make a overlay at the bottom of view pager.
How can I create that layout for Image Slider.
As i mentioned above image like that want to make layout in android
Thanks in Advance
I used a component like this before.
Basicly those codes should be enough for you. It doesn't have to be complicated realy.
These are your layout codes. I'm sure you will manage to use them in java codes according to your needs.
Here is the link for horizontalListView:
https://github.com/sephiroth74/HorizontalVariableListView
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v4.view.ViewPager
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffAAAAAA" />
<!--You may use here-->
<!--HorizontalListView or-->
<!--RecyclerView with horizontal layoutManager-->
<!--I would use HorizontalListView it's easier-->
<!--You may find it by searching in google-->
<HorizontalListView
android:layout_width="match_parent"
android:layout_gravity="bottom"
android:layout_height="150dp"
android:background="#66FFffFF" />
</FrameLayout>
Happy coding.
this is xml code :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/white"
android:orientation="vertical" >
<ImageView
android:id="#+id/selected"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_above="#+id/gallery_relative_layout"
android:layout_marginLeft="30dip"
android:layout_marginRight="30dip"
android:layout_marginTop="30dip"
/>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#000000"
android:layout_marginTop="300dp"
android:layout_above="#+id/gallery_relative_layout"
/>
<RelativeLayout
android:id="#+id/gallery_relative_layout"
android:layout_width="fill_parent"
android:layout_height="200dip"
android:layout_alignParentBottom="true"
android:orientation="horizontal"
android:paddingTop="20dp">
<HorizontalScrollView
android:id="#+id/hor_scroll_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<LinearLayout
android:id="#+id/gallery"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<ImageView
android:id="#+id/image1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/im1"
android:onClick="biggerView"/>
<ImageView
android:id="#+id/image2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/im2"
android:onClick="biggerView"/>
<ImageView
android:id="#+id/image3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/im3"
android:onClick="biggerView"/>
<ImageView
android:id="#+id/image4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/im4"
android:onClick="biggerView"/>
<ImageView
android:id="#+id/image5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/im5"
android:onClick="biggerView"/>
<ImageView
android:id="#+id/image6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/im6"
android:onClick="biggerView"/>
<ImageView
android:id="#+id/image7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/im7"
android:onClick="biggerView"/>
</LinearLayout>
</HorizontalScrollView>
</RelativeLayout>
</RelativeLayout>
this is MainActivity.java code :
public class MainActivity extends Activity {
ImageView im;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void biggerView(View v)
{
im=(ImageView)findViewById(R.id.selected);
switch (v.getId())
{
case R.id.image1: im.setImageResource(R.drawable.im1);
break;
case R.id.image2: im.setImageResource(R.drawable.im2);
break;
case R.id.image3: im.setImageResource(R.drawable.im3);
break;
case R.id.image4: im.setImageResource(R.drawable.im4);
break;
case R.id.image5: im.setImageResource(R.drawable.im5);
break;
case R.id.image6: im.setImageResource(R.drawable.im6);
break;
case R.id.image7: im.setImageResource(R.drawable.im7);
break;
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
here is output :
When you click any image it display as follow.
NOTE : Here I have used my Image used your Image and see the result.
You can use Recycler View at Bottom. and add a LinearLayoutManager with Horizontal scrolling. add a a single item image view in your adapter. It will definitely help you :)
Here is my full layout file:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:orientation="vertical" >
<RelativeLayout
android:id="#+id/relativeLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:orientation="horizontal" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:onClick="showButtons"
android:text="#string/buttons_label" />
</RelativeLayout>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/relativeLayout1"
android:layout_centerHorizontal="true"
android:onClick="showSpinners"
android:text="#string/spinners_label" />
</RelativeLayout>
And here is where I call showSpinners:
private void showSpinners(View clickedButton){
goToActivity(SpinnerActivity.class);
}
Eclipse underlines in yellow showSpinners by saying it's never used locally, my guess is it is not detecting my onClick attribute in the layout file.
I'm wondering why it just wont work and what I should do to make it work...
Change the your method to :
public void showSpinners(View clickedButton) {
// do the work
}
The method needs to be visible outside the class (source).
I want to create a following kind of layout in android , rite now i have created with fixed height of listview which is causing a problem with different dimension of screens of mobiles.
Initially when this screen appears a button will be at the bottom side only with empty listview and as an when the items will get added in a list view , it grows but button will remain steady at the bottom.
so far i have written following code
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<ImageView
android:id="#+id/cText"
android:src="#drawable/c_text"
android:visibility="visible"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="50dip"
/>
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:drawSelectorOnTop="false"
/>
<ImageButton
android:id="#+id/cBtn"
android:layout_width="150dip"
android:layout_height="52dip"
android:src="#drawable/preview"
android:layout_alignParentTop="#+id/list"
android:layout_gravity="center_horizontal"/>
</RelativeLayout>
Try this
< ?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="fill_parent"
>
<ImageButton
android:id="#+id/cBtn"
android:layout_centerHorizontal="true"
android:layout_width="150dip"
android:layout_height="52dip"
android:src="#drawable/preview"
android:layout_alignParentBottom="true"
/>
<ListView
android:id="#+id//listview01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_above="#+id/cBtn"
android:layout_marginBottom="10dip"
/>
< /RelativeLayout>
You can add a Button as a footer to List View. The sample code sinppet is shown below
Step1: Create a Button
<Button android:id="#+id/footer" android:gravity="center"
android:layout_gravity="center"
android:text="My Footer"
android:layout_width="wrap_content"
android:layout_height="0dp" android:layout_weight="1"/>
Step2: Make it as Footer to the List View
ListView myList;
View footerView;
ViewGroup footerViewOriginal = (ViewGroup) findViewById(R.id.footer);
footerView = inflater2.inflate(R.layout.footer,footerViewOriginal);
footerView.findViewById(R.id.footer);
myList.addFooterView(footerView, null, true);
Step3: Create on ClickListener and write the action what you want to perform
footerView.findViewById(R.id.footer).setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
return false;
}
Here is my problem:
When I click on the map view , the "setBuiltInZoomControls" method displays the zoom controls and then hides it. I want to copy that functionality so that when I click on the map view I want to display the map, satellite and traffic buttons, but my code doesn't work.
Can you help me on this? I have included my code below.
This is my xml code:
parent"
android:layout_height="fill_parent"
android:id="#+id/mapLayout"
>
<com.google.android.maps.MapView
android:id="#+id/mapview1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:enabled="true"
android:clickable="true"
android:apiKey="0K5oQk-IVWGzfMbBkOhx5EDXDXhlBO1PJI8mLag" />
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="48px"
android:background="#550000ff"
android:padding="2px"
android:id="#+id/mapBtnsControlls"
android:visibility="invisible">
<Button
android:layout_width="wrap_content"
android:id="#+id/btnMap"
android:layout_height="wrap_content"
android:text="Map"
android:layout_marginLeft="53px"
android:layout_marginRight="10px">
</Button>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/btnSatellite"
android:text="Satellite"
android:layout_toRightOf="#+id/btnMap"
android:layout_marginRight="10px">
</Button>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/btnTraffic"
android:layout_toRightOf="#+id/btnSatellite"
android:text="Traffic">
</Button>
</RelativeLayout>
</RelativeLayout>
This is my java code:
MapView googleMap= (MapView) findViewById(R.id.mapview1);
RelativeLayout mapBtnsLayout = (RelativeLayout) findViewById(R.id.mapBtnsControlls);
googleMap.setClickable(true);
googleMap.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
mapBtnsLayout.setVisibility(View.VISIBLE);
}
});
Instead of using setClickable() and setOnClickListener() on the MapView, try adding an Overlay subclass and overriding onTouchEvent() to show your controls.