Hello everyone and sorry if this question is answered.
I'm doing a geography game and I want to know how to change my ImageView depending on the button I press.
For example, I have an Europe map with an arrow that points to Spain. I want that when I press the Spain button, the map changes into a map with an arrow that points to Portugal.
I've tried this code but I can't make it work:
public void cargar_por(View v){
ImageView iv = (ImageView)findViewById(R.id.imageView);
if(flag==false){
Drawable por=setFeatureDrawable(iv,por);
iv.setImageDrawable(R.drawable.por);
flag = true;
}
else{
flag=false;
}
}
How could I do it?
Thanks in advance.
EDIT: I'm going to add the full code.
The xml file looks like this:
<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="match_parent" android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context=".MainActivity">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:weightSum="1">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_weight="0.91"
android:weightSum="1"
android:focusableInTouchMode="true"
android:focusable="true">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal">
<Chronometer
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/chronometer"
android:layout_gravity="right"
android:enabled="true" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Nivel 1"
android:id="#+id/textView5"
android:layout_gravity="center_horizontal"
android:background="#ffffffff" />
</LinearLayout>
<TableLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/map"
android:src="#drawable/spa"
android:visibility="visible"
android:layout_weight="0.93" />
</TableLayout>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="131dp"
android:layout_gravity="center_horizontal"
android:background="#3f073dff">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/layout1">
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="60dp"
android:weightSum="1">
<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:weightSum="1">
<Button
android:layout_width="181dp"
android:layout_height="match_parent"
android:text="ESPAÑA"
android:id="#+id/button5"
android:onClick="cargar_por" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="PORTUGAL"
android:id="#+id/button6" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal"
android:weightSum="1">
<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="0.40"
android:weightSum="1">
<Button
android:layout_width="match_parent"
android:layout_height="58dp"
android:text="FRANCIA"
android:id="#+id/button7"
android:layout_weight="0.86" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="0.40">
<Button
android:layout_width="match_parent"
android:layout_height="59dp"
android:text="ITALIA"
android:id="#+id/button8" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>
And the Java code like this:
package marti.victor.geografia;
import android.graphics.drawable.Drawable;
import android.os.SystemClock;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.*;
import android.widget.Button;
import android.widget.Chronometer;
import android.widget.ImageButton;
import android.widget.ImageView;
public class Juego extends ActionBarActivity {
// boolean flag = false;
ImageView image = (ImageView) findViewById(R.id.map);
Button but;
int a = 0;
Chronometer myChronometer;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.juego);
myChronometer = (Chronometer)findViewById(R.id.chronometer);
myChronometer.start();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main_activity2, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/*public void cargar_por(View v){
ImageView iv = (ImageView)findViewById(R.id.imageView);
but=(Button)findViewById(R.id.button5);
but.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
iv.setImageDrawable(R.drawable.por);
}
});
}*/
}
try this
ImageView iv = (ImageView)findViewById(R.id.imageView);
spainbutton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
iv.setImageDrawable(R.drawable.por);
}
});
in your case I think the flag is not working properly.
Related
I have an app with MainActivity that contains a FrameLayout which would be replaced by a fragment based on which item is clicked in the bottom layout. I have a fragment named "bookings" that must appear in the body area when "Bookings" option is clicked. The "bookings" fragment contains a spinner and a button that are placed at the center of screen inside of RelativeLayout. When the fragment's layout is placed directly inside of body of MainActivity, the spinner and button is centered, just the way I want it to be.
<RelativeLayout xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/bottompanel"
android:orientation="vertical">
<!-- The FrameLayout goes here -->
<RelativeLayout xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<Spinner
android:id="#+id/city_spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_above="#+id/center_space"
android:layout_marginBottom="10dp"
android:layout_marginTop="10dp"
android:dropDownVerticalOffset="20dp"
android:dropDownWidth="match_parent"
android:popupBackground="#d5ddea"
android:spinnerMode="dropdown" />
<Space
android:id="#+id/center_space"
android:layout_height="0dp"
android:layout_width="match_parent"
android:layout_centerInParent="true"/>
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Done"
android:layout_centerHorizontal="true"
android:layout_below="#+id/center_space"
android:onClick="doneButton" />
</RelativeLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="75dp"
android:orientation="horizontal"
android:layout_alignParentBottom="true"
android:weightSum="3"
android:layout_weight="0"
android:id="#+id/bottompanel">
<LinearLayout
android:id="#+id/profile_button"
android:layout_width="0dp"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_weight="1"
android:clickable="true">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_profile"
android:layout_gravity="center"
android:layout_weight="1"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Profile"
android:layout_gravity="center"
android:layout_weight="1"/>
</LinearLayout>
<LinearLayout
android:id="#+id/bookings_button"
android:clickable="true"
android:layout_width="0dp"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_weight="1">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_bookings"
android:layout_gravity="center"
android:layout_weight="1"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Bookings"
android:layout_gravity="center"
android:layout_weight="1"/>
</LinearLayout>
<LinearLayout
android:id="#+id/games_button"
android:clickable="true"
android:layout_width="0dp"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_weight="1">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_tournaments"
android:layout_gravity="center"
android:layout_weight="1"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Games"
android:layout_gravity="center"
android:layout_weight="1"/>
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_weight="1">
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_weight="1">
</LinearLayout>
</LinearLayout>
</RelativeLayout>
But when I replace the FrameLayout with the fragment, the spinner disappears and the button moves at the top.
Code in activity_main.xml:
<RelativeLayout xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/bottompanel"
android:orientation="vertical">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/fragment_frame"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="75dp"
android:orientation="horizontal"
android:layout_alignParentBottom="true"
android:weightSum="3"
android:layout_weight="0"
android:id="#+id/bottompanel">
<LinearLayout
android:id="#+id/profile_button"
android:layout_width="0dp"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_weight="1"
android:clickable="true">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_profile"
android:layout_gravity="center"
android:layout_weight="1"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Profile"
android:layout_gravity="center"
android:layout_weight="1"/>
</LinearLayout>
<LinearLayout
android:id="#+id/bookings_button"
android:clickable="true"
android:layout_width="0dp"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_weight="1">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_bookings"
android:layout_gravity="center"
android:layout_weight="1"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Bookings"
android:layout_gravity="center"
android:layout_weight="1"/>
</LinearLayout>
<LinearLayout
android:id="#+id/games_button"
android:clickable="true"
android:layout_width="0dp"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_weight="1">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_tournaments"
android:layout_gravity="center"
android:layout_weight="1"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Games"
android:layout_gravity="center"
android:layout_weight="1"/>
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_weight="1">
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_weight="1">
</LinearLayout>
</LinearLayout>
</RelativeLayout>
Code in fragment_bookings.xml:
<RelativeLayout xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<Spinner
android:id="#+id/city_spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_above="#+id/center_space"
android:layout_marginBottom="10dp"
android:layout_marginTop="10dp"
android:dropDownVerticalOffset="20dp"
android:dropDownWidth="match_parent"
android:popupBackground="#d5ddea"
android:spinnerMode="dropdown" />
<Space
android:id="#+id/center_space"
android:layout_height="0dp"
android:layout_width="match_parent"
android:layout_centerInParent="true"/>
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Done"
android:layout_centerHorizontal="true"
android:layout_below="#+id/center_space"
android:onClick="doneButton" />
</RelativeLayout>
Code of onCreateView of bookings.java:
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_bookings, container, false);
Spinner loc = (Spinner)v.findViewById(R.id.city_spinner);
String[] cities = new String[]{
"Select a City",
"ABC",
"DEF",
"GHI",
"JKL",
"MNO"
};
// Initializing an ArrayAdapter
ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(
this.getActivity(),android.R.layout.simple_spinner_item,cities
);
spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_item);
loc.setAdapter(spinnerArrayAdapter);
loc.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> adapterView, View view, int pos, long id) {
L1 = id;
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
return v;
}
Code in MainActivity.java:
package com.example.user.temporary;
import android.app.Activity;
import android.net.Uri;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
public class MainActivity extends AppCompatActivity implements
profile.OnFragmentInteractionListener,
bookings.OnFragmentInteractionListener,
games.OnFragmentInteractionListener {
private Activity myActivity;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.myActivity = this;
LinearLayout profilebtn = (LinearLayout)this.findViewById(R.id.profile_button);
LinearLayout bookingsbtn = (LinearLayout)this.findViewById(R.id.bookings_button);
LinearLayout gamesbtn = (LinearLayout)this.findViewById(R.id.games_button);
profilebtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
profile profileFragment = new profile();
FragmentTransaction trans = getSupportFragmentManager().beginTransaction();
trans.replace(R.id.fragment_frame, profileFragment);
trans.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
trans.commit();
}
});
bookingsbtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
bookings bookings_frame = new bookings();
FragmentTransaction trans = getSupportFragmentManager().beginTransaction();
trans.replace(R.id.fragment_frame, bookings_frame);
trans.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
trans.commit();
}
});
gamesbtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
games games_frame = new games();
FragmentTransaction trans = getSupportFragmentManager().beginTransaction();
trans.replace(R.id.fragment_frame, games_frame);
trans.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
trans.commit();
}
});
}
#Override
public void onFragmentInteraction(Uri uri){
}
}
Change height of FrameLayout to match_parent from wrap_content
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/bottompanel"
android:orientation="vertical">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/fragment_frame"/>
</LinearLayout>
You have match_parent in the height for fragment layout. Try changing it to wrap_content and see if it works
I'm trying to add a subview below another view after an animation. anyone have any idea what i'm doing wrong?
this.animate().translationY(newY - 170).setDuration(500).withEndAction(new Runnable() {
#Override
public void run() {
clearAnimation();
RouteBreakDownLayout routeBreakDownLayout = new RouteBreakDownLayout(mContext);
RelativeLayout.LayoutParams p = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
p.addRule(RelativeLayout.BELOW, R.id.selected_route_layout);
routeBreakDownLayout.setLayoutParams(p);
addView(routeBreakDownLayout);
}
}).start();
Here is the XML for the layout. I have a scrollview embeded which will hold the content that needs to be animated in.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/selected_route_layout2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="bottom"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RelativeLayout
android:id="#+id/selected_route_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_gravity="bottom"
android:layout_alignParentBottom="true"
android:background="#FFF"
android:gravity="bottom"
android:padding="10dip">
<TextView
android:id="#+id/time_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginRight="10dp"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#1D1C18" />
<TextView
android:id="#+id/eta_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/time_label"
android:layout_toEndOf="#+id/time_label"
android:layout_toRightOf="#+id/time_label"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#7EC82F" />
<LinearLayout
android:id="#+id/route_breakdown"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="#+id/time_label"
android:layout_centerVertical="true"
android:layout_marginBottom="10dip"
android:layout_marginTop="10dip"
android:gravity="center_vertical"
android:orientation="horizontal" />
<TextView
android:id="#+id/via_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="#+id/route_breakdown"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#BDBDBD" />
<ImageButton
android:id="#+id/arrow_up"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:background="#null"
android:padding="15dip"
android:src="#drawable/transit_symbol_up_arrow" />
</RelativeLayout>
<ScrollView
android:layout_below="#+id/selected_route_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:showDividers="middle"
android:divider="?android:dividerHorizontal"
android:animateLayoutChanges="true"
android:paddingLeft="16dp"
android:paddingRight="16dp" />
</ScrollView>
</RelativeLayout>
What goes wrong for you in your code-snippet?
Here's working example I came up with:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/containerRelativeLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<View
android:id="#+id/headerView"
android:background="#00FF00"
android:layout_width="match_parent"
android:layout_height="25dp"/>
<View
android:id="#+id/detailsView"
android:layout_alignParentBottom="true"
android:layout_width="match_parent"
android:layout_height="40dp"
android:clickable="true"
android:background="#FF0000"/>
</RelativeLayout>
And MainActivity is
package klogi.com.animationstestapp;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.RelativeLayout;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final View detailsView = findViewById(R.id.detailsView);
final View headerView = findViewById(R.id.headerView);
final RelativeLayout container = (RelativeLayout)findViewById(R.id.containerRelativeLayout);
container.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
detailsView.animate().translationY(-detailsView.getY()).setDuration(500).withEndAction(new Runnable() {
#Override
public void run() {
Button additionalButton = new Button(MainActivity.this);
additionalButton.setText("Additional button");
RelativeLayout.LayoutParams additionalButtonParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
additionalButtonParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
additionalButton.setLayoutParams(additionalButtonParams);
container.addView(additionalButton);
}
}).start();
headerView.animate().translationY(detailsView.getHeight()).setDuration(500).start();
}
});
}
}
It works just as expected.
Have you tried to add your control(RouteBreakDownLayout) without animation? Does it work? It can be something like control is actually adding, but it's out of the viewport, etc. - so far, I can just guessing.
In the following code Sliding drawer menu bar is coming but when i click on the menu items nothing happens.I am looking forward to open the sliding drawer on button click of the buttonmenu.I dont know how to open a sliding drawer on button click.When i click on the drawer it disappers to the left hand side.I want to write the code like this if i click on menu item (home) it should open Mainactivity.class.I have not written the code for that.
package com.bar.start;
import com.bar.barapp.R;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;
public class MainActivity extends Activity {
Button loc,buttonmenu;
String[] menu;
DrawerLayout dLayout;
ListView dList;
ArrayAdapter<String> adapter;
#Override
protected void onCreate(Bundle savedInstanceState)
{
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
//this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.homescreen);
// getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.titlebar);
menu = new String[]{"Home","Android","Windows","Linux","Raspberry Pi","WordPress","Videos","Contact Us"};
dLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
dList = (ListView) findViewById(R.id.left_drawer);
adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,menu);
dList.setAdapter(adapter);
dList.setSelector(android.R.color.holo_blue_dark);
dList.setOnItemClickListener(new OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> arg0, View v, int position, long id) {
dLayout.closeDrawers();
Bundle args = new Bundle();
args.putString("Menu", menu[position]);
Fragment detail = new Menu_view();
detail.setArguments(args);
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction().replace(R.id.content_frame, detail).commit();
}
});
loc=(Button)findViewById(R.id.location);
loc.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent in =new Intent(MainActivity.this,LocationActivity.class);
startActivity(in);
}
});
buttonmenu=(Button)findViewById(R.id.menubutton);
buttonmenu.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Toast.makeText(getApplicationContext(), "hello", Toast.LENGTH_LONG).show();
}
});
}
}
homescreen.xml
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<ListView android:id="#+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:dividerHeight="0dp"
android:background="#800000"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#drawable/startupscreen">
<ScrollView
android:id="#+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:id="#+id/menulayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="1"
android:paddingTop="30dp"
android:orientation="horizontal">
<Button
android:id="#+id/menubutton"
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_weight="0.3"
android:text="#string/menu"
/>
<TextView
android:layout_weight="0.2"
android:layout_width="0dp"
android:layout_height="wrap_content"/>
<TextView
android:layout_weight="0.2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="#string/search"
android:textSize="14sp"
android:textColor="#color/black"/>
<EditText
android:layout_weight="0.3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:paddingRight="10dp"
android:focusable="false"
android:focusableInTouchMode="true"/>
</LinearLayout>
<ExpandableListView
android:layout_below="#+id/menulayout"
android:id="#+id/searchres"
android:layout_width="fill_parent"
android:layout_height="350dp"
android:clipChildren="true"
android:groupIndicator="#null"
android:background="#color/gray"
/>
<LinearLayout
android:id="#+id/buttons"
android:layout_width="match_parent"
android:layout_height="50dp"
android:weightSum="1.5"
android:orientation="horizontal"
android:layout_below="#+id/searchres">
<Button
android:id="#+id/location"
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_weight="0.5"
android:text="#string/location"
android:background="#drawable/btn_image"
/>
<Button
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_weight="0.5"
android:visibility="invisible"
/>
<Button
android:id="#+id/barype"
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_weight="0.5"
android:text="#string/bartype"
android:background="#drawable/btn_image"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center_vertical|center_horizontal"
android:layout_below="#+id/buttons">
<Button
android:id="#+id/joggle"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:background="#drawable/btn_image"
android:text="#string/joggle"
/>
</LinearLayout>
</LinearLayout>
</ScrollView>
</RelativeLayout>
</android.support.v4.widget.DrawerLayout>
Menuview.java
public class Menu_view extends Fragment{
TextView text;
#Override
public View onCreateView(LayoutInflater inflater,ViewGroup container, Bundle args) {
View view = inflater.inflate(R.layout.menu_detail, container, false);
String menu = getArguments().getString("Menu");
text= (TextView) view.findViewById(R.id.detail);
text.setText(menu);
return view;
}
}
menu_detail.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:gravity="center"
android:background="#5ba4e5"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="40px"
android:textColor="#000000"
android:layout_gravity="center"
android:id="#+id/detail"/>
</LinearLayout>
I am basically trying to create a recycler view that has a linear layout manager with a horizontal scrolling. When I was using a linear layout manager with vertical scrolling, the design of the recycler view rows was properly aligned as per provided in the layout file but the design elements gets placed in completely weird places when I am using horizontal scrolling
Here is my code for the recycler view row :
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:orientation="vertical"
card_view:cardCornerRadius="2dp"
card_view:cardUseCompatPadding="true">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="250dp">
<ImageView
android:id="#+id/shop_product_image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop" />
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="match_parent">
<ImageView
android:id="#+id/favorite"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginRight="12dp"
android:padding="10dp"
android:src="#drawable/ic_favorite_selected" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:background="#color/black80"
android:padding="8dp"
android:text="1200 INR"
android:textColor="#color/white"
android:textSize="18sp" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="#color/black80">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/shop_product_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="8dp"
android:paddingTop="4dp"
android:paddingBottom="2dp"
android:textColor="#color/white"
android:textSize="16sp" />
<TextView
android:id="#+id/shop_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="8dp"
android:paddingBottom="4dp"
android:text="The Indian Shop"
android:textColor="#color/white"
android:textSize="12sp" />
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="150 likes"
android:textColor="#color/white"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:paddingRight="8dp"/>
</RelativeLayout>
</RelativeLayout>
The layout file for the activity layout is :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
xmlns:fab="http://schemas.android.com/apk/res-auto"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.paaltao.activity.MyShopActivity">
<include
android:id="#+id/app_bar"
layout="#layout/app_bar" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/product_view"
android:layout_below="#+id/app_bar"
>
<RelativeLayout
android:id="#+id/shop_cover_area"
android:layout_width="match_parent"
android:layout_height="150dp"
android:visibility="visible">
<ImageView
android:id="#+id/shop_cover_image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:contentDescription="#string/shop_cover_image"
android:scaleType="centerCrop"
android:src="#drawable/apple_small" />
</RelativeLayout>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="6dp"
android:orientation="vertical"
android:visibility="gone"
card_view:cardCornerRadius="5dp"
card_view:cardUseCompatPadding="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/shop_start_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="16dp"
android:text="#string/product_first_launch_text"
android:textColor="#color/black" />
</LinearLayout>
</android.support.v7.widget.CardView>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/shop_cover_area"
>
<android.support.v7.widget.RecyclerView
android:id="#+id/shop_products_recycler_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:padding="8dp"
/>
</RelativeLayout>
</RelativeLayout>
<com.paaltao.classes.AddFloatingActionButton
android:id="#+id/multiple_actions1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_gravity="end"
fab:fab_addButtonColorPressed="#color/white_pressed"
fab:fab_addButtonPlusIconColor="#color/white"
fab:fab_labelStyle="#style/menu_labels_style"
app:fab_addButtonColorNormal="#color/lightGreen"/>
</RelativeLayout>
The activity code is as follows :
package com.paaltao.activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import com.paaltao.Adapters.ShopProductAdapter;
import com.paaltao.R;
import com.paaltao.classes.AddFloatingActionButton;
import com.paaltao.classes.Product;
import java.util.ArrayList;
import java.util.List;
public class MyShopActivity extends ActionBarActivity {
private RecyclerView mRecyclerView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my_shop);
mRecyclerView = (RecyclerView) findViewById(R.id.shop_products_recycler_view);
mRecyclerView.setHasFixedSize(true);
mRecyclerView.setLayoutManager(new LinearLayoutManager(getApplicationContext(),LinearLayoutManager.HORIZONTAL,false));
mRecyclerView.setAdapter(new ShopProductAdapter(getApplicationContext(), getData()));
Toolbar toolbar = (Toolbar) this.findViewById(R.id.app_bar);
toolbar.setTitleTextColor(Color.WHITE);
this.setSupportActionBar(toolbar);
this.setTitle("Shop");
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
AddFloatingActionButton addProductButton = (AddFloatingActionButton)findViewById(R.id.multiple_actions1);
addProductButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.d("Tag","Clicked");
Intent intent = new Intent(MyShopActivity.this, AddProductActivity.class);
startActivity(intent);
}
});
}
public static List<Product> getData(){
ArrayList data = new ArrayList();
int[] icons = {R.drawable.apple_small,R.drawable.ic_launcher,R.drawable.bag_icon,R.drawable.notify_icon,R.drawable.apple_small,
R.drawable.bag_icon,R.drawable.apple_small,R.drawable.notify_icon,R.drawable.apple_small,R.drawable.ic_launcher};
String[] product_name = {"Handmade","Photography","Electronics","Electronics","Electronics","Electronics","Electronics",
"Electronics","Electronics","Electronics"};
for(int i=0; i<icons.length && i< product_name.length;i++){
Product current = new Product();
current.setProduct_name(product_name[i]);
current.setProduct_id(icons[i]);
data.add(current);
}
return data;
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_my_shop, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
When I use vertical orientation in recycled view the design works fine. The problem only occurs when I set the orientation to LinearLayoutManager.HORIZONTAL.
Please help
It was very foolish of me to ask such a question :P
In the layout containing the design of recycler view row, The width and height cannot be set as "match parent". Instead the thing works simply for "wrap content". To enable horizontal scrolling the row must be given fixed width and height.
I have the following activity. It is an simplification of a draggable map. The problem is that mytext2 is not clickable, even if it is visible. Can anybody tell me how to make it clickable? And for some reason the inner-RelativeLayout gets not bigger than screen size, even when i set this high dp.
<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="match_parent"
android:background="#0000ff"
android:clipChildren="false"
tools:context="${relativePackage}.${activityClass}" >
<RelativeLayout
android:layout_width="100000dp"
android:layout_height="100000dp"
android:background="#00ff00"
android:translationX="-100dp" >
<TextView
android:id="#+id/mytext1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#ff0000"
android:text="#string/hello_world"
android:translationX="400dp"
android:translationY="100dp" />
<TextView
android:id="#+id/mytext2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#ff0000"
android:text="#string/hello_world"
android:translationX="200dp"
android:translationY="100dp" />
</RelativeLayout>
</RelativeLayout>
class
package com.example.relativatest;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.mytext1).setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
System.out.println("HELLO!1");
}
});
findViewById(R.id.mytext2).setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
System.out.println("HELLO!2");
}
});
}
}
Use this 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="match_parent"
android:background="#0000ff"
android:clipChildren="false"
tools:context="${relativePackage}.${activityClass}" >
<RelativeLayout
android:layout_width="100000dp"
android:layout_height="100000dp"
android:background="#00ff00"
android:translationX="-100dp" >
<TextView
android:id="#+id/mytext1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#ff0000"
android:text="#string/hello_world"
android:focusable="true"
android:padding="20dp"
android:translationX="400dp"
android:clickable="true"
android:translationY="100dp" />
<TextView
android:id="#+id/mytext2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#ff0000"
android:text="#string/hello_world"
android:translationX="200dp"
android:clickable="true"
android:padding="20dp"
android:focusable="true"
android:translationY="100dp" />
</RelativeLayout>
</RelativeLayout>