Android Relative layout spacing between elements - android

I have this Design in Relative Layout. I have to do some more things.
How can I remove that straight line between two elements ?
How can I increase space between two elements ?
How to display product side by side ?
I have tried several solutions from stackoverflow. Like I have used margin bottom but got no luck.
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="250dp"
android:background="#drawable/round_corner"
android:layout_marginRight="20dp"
>
<RelativeLayout
android:layout_width="200dp"
android:layout_height="250dp"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:paddingRight="20dp"
android:paddingLeft="20dp"
>
<de.hdodenhof.circleimageview.CircleImageView xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/product_image"
android:layout_width="match_parent"
android:layout_height="150dp"
android:layout_marginLeft="15dp"
android:layout_marginTop="20dp"
android:foregroundGravity="center"
app:civ_border_color="#d1b1b1"
/>
<View
android:layout_width="match_parent"
android:layout_height="180dp"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="1dp"
android:layout_marginTop="0dp"
android:background="#drawable/gradient" />
<TextView
android:id="#+id/product_name_english"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#id/product_image"
android:layout_alignTop="#id/product_image"
android:layout_alignRight="#id/product_image"
android:layout_alignBottom="#id/product_image"
android:layout_margin="1dp"
android:gravity="center"
android:text="Hello"
android:textColor="#FFFF"
android:fontFamily="#font/aerial"/>
<TextView
android:id="#+id/price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/product_name_english"
android:layout_centerHorizontal="true"
android:layout_marginTop="-51dp"
android:layout_marginBottom="1dp"
android:fontFamily="#font/aerial"
android:text="Hello"
android:textColor="#FFFF" />
<Button
android:layout_width="250dp"
android:layout_height="20dp"
android:layout_alignParentTop="true"
android:layout_marginTop="194dp"
android:background="#drawable/buttonshape"
android:shadowColor="#A8A8A8"
android:shadowDx="0"
android:shadowDy="0"
android:shadowRadius="5"
android:text="Add to cart"
android:textColor="#FFFFFF"
android:textSize="10sp" />
<Button
android:layout_width="250dp"
android:layout_height="20dp"
android:layout_alignParentTop="true"
android:layout_marginTop="224dp"
android:background="#drawable/buttonshape"
android:shadowColor="#A8A8A8"
android:shadowDx="0"
android:shadowDy="0"
android:shadowRadius="5"
android:text="View Product"
android:textColor="#FFFFFF"
android:textSize="10sp" />
</RelativeLayout>
</FrameLayout>
Here is the listview I am sending to my Adapter
listView = (ListView) findViewById(R.id.product_list);
mAdapter = new all_product_list_ArrayAdapter(all_product.this,data);
listView.setAdapter(mAdapter);
Here is my ArrayAdapter Class :
package com.example.yunus.ybazar_android;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.support.annotation.LayoutRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import android.support.annotation.NonNull;
import android.widget.Toast;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
public class all_product_list_ArrayAdapter extends ArrayAdapter<all_product_list_android_model> {
private final String Tag = "Filter" ;
private Context mContext;
private ArrayList<all_product_list_android_model> product_list ;
private final String urlMain = "http://118.179.70.235:28965/media/" ;
public all_product_list_ArrayAdapter(#NonNull Context context, ArrayList<all_product_list_android_model> list) {
super(context, 0, list);
mContext = context ;
product_list = list ;
}
#NonNull
#Override
public View getView(int position, #Nullable View convertView, #NonNull ViewGroup parent) {
View listItem = convertView;
if(listItem == null)
listItem = LayoutInflater.from(mContext).inflate(R.layout.all_product_list_arrayadapter_connection,parent,false);
all_product_list_android_model product_list_to_show = product_list.get(position);
ImageView image = (ImageView)listItem.findViewById(R.id.product_image);
new DownLoadImage(image).execute(urlMain.concat(product_list_to_show.product_main_image));
TextView name = (TextView) listItem.findViewById(R.id.product_name_english);
name.setText(product_list_to_show.product_name_english);
TextView price = (TextView) listItem.findViewById(R.id.price);
price.setText(("Taka ")
.concat(product_list_to_show.product_unit_price)
.concat("/")
.concat(product_list_to_show.product_unit));
final Button bt = (Button) listItem.findViewById(R.id.add_to_cart);
bt.setTag(product_list_to_show.id);
final Button bt2 = (Button) listItem.findViewById(R.id.view_product);
bt2.setTag(product_list_to_show.id);
bt.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
}
});
bt2.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(mContext, indi_product_show.class);
intent.putExtra("product_id",String.valueOf(bt2.getTag()));
mContext.startActivity(intent);
}
});
return listItem;
}
}

Set your ListView's divider to null and its Divider Height to 0 pixels:
listView = (ListView) findViewById(R.id.product_list);
listView.setDivider(null);
listView.setDividerHeight(0);
mAdapter = new all_product_list_ArrayAdapter(all_product.this,data);
listView.setAdapter(mAdapter);
more info can be found in the docs for ListView here :
ListView#setDivider(android.graphics.drawable.Drawable)
ListView#setDividerHeight(int)

use divider for ListView in this way:
<ListView
android:id="#+id/list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:divider="#null"
android:dividerHeight="0dp"/>

Related

app showing some irrelevant Text instead of a Textview

I am developing an android app, it was working fine till i add textview into main activity layout.
All images are being shown. But instead of textview an irrelevant is being shown on display screen.
Below is the code from mainactivity.java.
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.InputType;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
public class MainActivity extends AppCompatActivity {
ImageButton showBalDialog, showExpDialog, showIncomebtn, showExpbtn;
TextView bal_dialog, total_incomes;
public static final String DB_name = "mydb";
DatabaseHelper m_db;
private String amnt, src, exp, rzn;
private String incomes = "sum";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
m_db = new DatabaseHelper(this);
showBalDialog = findViewById(R.id.btnshowBalDialog);
showExpDialog = findViewById(R.id.btnshowExpDialog);
showIncomebtn = findViewById(R.id.btnshowIncomes);
showExpbtn = findViewById(R.id.btnshowExpenses);
total_incomes = findViewById(R.id.totalincomes);
incomes = String.valueOf(total_incomes);
total_incomes.setText(incomes);
}
}
Below is the code from activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
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:screenOrientation="portrait"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ImageButton
android:id="#+id/btnshowBalDialog"
android:scaleType="fitXY"
android:layout_width="150dp"
android:layout_height="150dp"
android:layout_marginStart="20dp"
android:layout_marginTop="40dp"
android:cropToPadding="false"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:background="#drawable/add_inc"> // this is the image(eye)
</ImageButton>
<ImageButton
android:id="#+id/btnshowExpDialog"
android:scaleType="fitXY"
android:layout_width="150dp"
android:layout_height="150dp"
android:layout_marginEnd="20dp"
android:layout_marginTop="40dp"
android:cropToPadding="false"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:background="#drawable/add_exp"> // this is the image(eye)
</ImageButton>
<ImageButton
android:id="#+id/btnshowIncomes"
android:scaleType="fitXY"
android:layout_width="150dp"
android:layout_height="150dp"
android:layout_marginStart="20dp"
android:layout_marginTop="40dp"
android:cropToPadding="false"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/btnshowBalDialog"
android:background="#drawable/income_his"> // this is the image(eye)
</ImageButton>
<ImageButton
android:id="#+id/btnshowExpenses"
android:scaleType="fitXY"
android:layout_width="150dp"
android:layout_height="150dp"
android:layout_marginEnd="20dp"
android:layout_marginTop="40dp"
android:cropToPadding="false"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="#id/btnshowExpDialog"
android:background="#drawable/expense_his"> // this is the image(eye)
</ImageButton>
<TextView
android:id="#+id/totalincomes"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_marginTop="20dp"
android:text="TextView"
app:layout_constraintTop_toBottomOf="#id/btnshowIncomes"
app:layout_constraintStart_toStartOf="parent">
</TextView>
</android.support.constraint.ConstraintLayout>
And below what is i am getting instead of Textview on app gui.
You have to do this:
total_incomes.setText(incomes);
your incomes value is already in String no need to use String.valueOf()
instead of
incomes = String.valueOf(total_incomes);
total_incomes.setText(incomes);
total_incomes is the Textview, you are binding the TextView with R.id.totalincome which is present in xml.
incomes = String.valueOf(total_incomes);
total_incomes.setText(incomes);
You are fetching the string value of total_incomes and store it in incomes and the same value you set on total_incomes. so in short you are setting textview id.

RecyclerView Margin

i am developing application for home security. One of the feature, that must be implemented is the ability to see connected devices (for sending notifications, blocking access, etc). So far, i've been able to create RecyclerView list of the devices, everything is perfect (for me), except that cards in this list have no spacing between them.
Screenshot of how it looks now
device_data_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:cardview="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="80dp"
android:layout_margin="8dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="80dp">
<ImageView
android:id="#+id/deviceIcon"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:scaleType="centerCrop"
android:layout_marginLeft="8dp" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="80dp"
android:layout_alignBottom="#+id/deviceIcon"
android:layout_toEndOf="#+id/deviceIcon">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Username"
android:id="#+id/deviceUsername"
android:layout_gravity="center_vertical"
android:textColor="#000000"
android:layout_marginLeft="5dp" />
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:layout_gravity="center_vertical"
android:gravity="center_vertical|center_horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="25dp"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Device Model"
android:gravity="center_vertical|right"
android:textColor="#000000"
android:id="#+id/deviceModel"
android:layout_marginLeft="5dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="25dp"
android:textColor="#000000"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Device Version"
android:gravity="center_vertical|right"
android:id="#+id/deviceVersion"
android:layout_marginLeft="5dp" />
</LinearLayout>
</LinearLayout>
devices_list.xml
<?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:padding="#dimen/activity_vertical_margin">
<view
android:id="#+id/connectedDevicesList"
class="android.support.v7.widget.RecyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerInParent="true" />
</RelativeLayout>
View Holder Code
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import com.exampleapp.R;
public class DeviceViewHolder extends RecyclerView.ViewHolder {
protected ImageView deviceIcon;
protected TextView deviceUsername;
protected TextView deviceModel;
protected TextView deviceVersion;
public DeviceViewHolder(View view) {
super(view);
this.deviceIcon = (ImageView) view.findViewById(R.id.deviceIcon);
this.deviceUsername = (TextView) view.findViewById(R.id.deviceUsername);
this.deviceModel = (TextView) view.findViewById(R.id.deviceModel);
this.deviceVersion = (TextView) view.findViewById(R.id.deviceVersion);
}
}
RecyclerView Adapter
import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import java.util.List;
import com.exampleapp.API.AuthorizedDevice;
import com.exampleapp.R;
public class ConnectedDeviceAdapter extends RecyclerView.Adapter<DeviceViewHolder> {
private List<AuthorizedDevice> devices;
private Context mContext;
public ConnectedDeviceAdapter(Context context, List<AuthorizedDevice> devices) {
this.devices = devices;
this.mContext = context;
}
#Override
public DeviceViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.connected_devices_layout, null);
DeviceViewHolder viewHolder = new DeviceViewHolder(view);
return viewHolder;
}
#Override
public void onBindViewHolder(DeviceViewHolder deviceViewHolder, int i) {
AuthorizedDevice device = devices.get(i);
if (device.getDeviceId().equals("0")) {
deviceViewHolder.deviceIcon.setBackground(ContextCompat.getDrawable(mContext, R.drawable.android_icon));
} else {
deviceViewHolder.deviceIcon.setBackground(ContextCompat.getDrawable(mContext, R.drawable.apple_icon));
}
deviceViewHolder.deviceUsername.setText(device.getUsername());
deviceViewHolder.deviceModel.setText(device.getDeviceName());
deviceViewHolder.deviceVersion.setText(device.getDeviceVersion());
}
#Override
public int getItemCount() {
return (null != devices ? devices.size() : 0);
}
}
Can anyone please help me solve this problem? Thank you very much in advance!
In your RecyclerView Adapter onCreateViewHolder:
replace:
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.connected_devices_layout, null);
with:
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.connected_devices_layout, viewGroup, false);
Or you can use RecyclerView.addItemDecoration(ItemDecoration decor).

Null pointer exception while setting onClickListener on an imageView in a fragment class [duplicate]

This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 7 years ago.
I am new to android and I am in a learning phase.I searched a lot but couldn't find an appropriate solution for this. I am getting a null pointer exception at notes.setOnClickListener though I have added findViewById statement
package com.example.hp.newcalendar;
import android.app.Activity;
import android.support.v4.app.Fragment;
import android.content.Context;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
public class PreviouslyCalledFragment extends Fragment {
ListView students;
TextView test;
ImageView notes;
SimpleDateFormat formatter = new SimpleDateFormat("dd M yyyy hh:mm a");
String dateInString="23 Dec 2015 18:00 pm";
View view;
Context mContext;
ArrayList<FieldDetails> searchResults;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
view = inflater.inflate(R.layout.activity_previously_called_fragment, container, false);
students = (ListView) view.findViewById(R.id.alreadyCalled);
notes=(ImageView)view.findViewById(R.id.notesView);
searchResults = GetSearchResults();
students.setAdapter(new CustomAdapter_Notes(mContext, searchResults));
notes.setOnClickListener(new View.OnClickListener() { //null pointer exception
#Override
public void onClick(View v) {
Toast.makeText(mContext,"hello", Toast.LENGTH_LONG).show();
}
});
return view;
}
#Override
public void onAttach(final Activity activity) {
super.onAttach(activity);
mContext = activity;
}
private ArrayList<FieldDetails> GetSearchResults() {
ArrayList<FieldDetails> results = new ArrayList<FieldDetails>();
FieldDetails fieldDetails = new FieldDetails();
fieldDetails.setName("Aman Jain");
fieldDetails.setTime("Dec 23,18:00 pm");
results.add(fieldDetails);
fieldDetails = new FieldDetails();
fieldDetails.setName("Keshav Sharma");
fieldDetails.setTime("Dec 23,19:00 pm");
results.add(fieldDetails);
return results;
}
}
xml :
<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"
tools:context=".MainActivity"
android:padding="5dp"
android:background="#ffffff">
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/alreadyCalled"
android:layout_centerHorizontal="true"
android:clickable="false"
android:layout_alignParentTop="true" />
Custom_layout xml :
<?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"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Small Text"
android:textSize="15dp"
android:id="#+id/nameOfInterviewee"
android:textColor="#008080"
android:layout_marginTop="5dp"
android:paddingBottom="5dp"
android:layout_marginLeft="2dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Small Text"
android:textSize="12dp"
android:id="#+id/dateAndTime"
android:textColor="#008080"
android:paddingBottom="10dp"
android:layout_below="#+id/nameOfInterviewee"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="-5dp"
/>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#848482"
android:id="#+id/view"
android:layout_below="#+id/dateAndTime"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<ImageView
android:layout_width="30dp"
android:layout_height="20dp"
android:id="#+id/notesView"
android:clickable="true"
android:layout_marginTop="16dp"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:src="#drawable/notes"/>
</RelativeLayout>
PLEASE HELP !!
This is most likely because R.id.notesView id is not present in your xml file R.layout.activity_previously_called_fragment. Please check the xml file or share it here.

How to reuse my xml relative layout in android

Hi I have an m file which is a relative layout and which contains 9 image buttons.
So I would like to reuse my layout multiple times just by changing the names and ID's.
I am able to do with gridview but my problem is I want use the same customs design which I have using relative layout.
So can any one help how to do it.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ImageButton
android:id="#+id/day0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignTop="#+id/day1"
android:layout_marginLeft="14dp"
android:scaleType="fitXY"
android:src="#drawable/day0" />
<ImageButton
android:id="#+id/day3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/day0"
android:layout_below="#+id/day0"
android:layout_marginTop="54dp"
android:src="#drawable/day3" />
<ImageButton
android:id="#+id/day6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/day3"
android:layout_below="#+id/day3"
android:layout_marginTop="74dp"
android:src="#drawable/day6" />
<ImageButton
android:id="#+id/day4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/day6"
android:layout_marginLeft="25dp"
android:layout_toRightOf="#+id/day3"
android:src="#drawable/day4" />
<ImageButton
android:id="#+id/day7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/day4"
android:layout_alignTop="#+id/day6"
android:src="#drawable/day7" />
<ImageButton
android:id="#+id/day1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/day4"
android:layout_alignParentTop="true"
android:layout_marginTop="39dp"
android:scaleType="fitXY"
android:src="#drawable/day1" />
<ImageButton
android:id="#+id/day8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/day5"
android:layout_alignTop="#+id/day7"
android:src="#drawable/day8" />
<ImageButton
android:id="#+id/day2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/day1"
android:layout_marginLeft="28dp"
android:layout_toRightOf="#+id/day1"
android:scaleType="fitXY"
android:src="#drawable/day2" />
<ImageButton
android:id="#+id/day5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/day6"
android:layout_alignLeft="#+id/day2"
android:src="#drawable/day5" />
<TextView
android:id="#+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/textView4"
android:layout_alignBottom="#+id/textView4"
android:layout_alignLeft="#+id/day7"
android:text="Emotions"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/textView8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/textView7"
android:layout_alignLeft="#+id/Ans6b"
android:text="Leadership"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/Ans6b"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/question6a"
android:layout_alignBottom="#+id/question6a"
android:layout_alignRight="#+id/day1"
android:text="gruppstart"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/day3"
android:layout_toLeftOf="#+id/day4"
android:text="Adaption"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/textView7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/day6"
android:layout_toLeftOf="#+id/day4"
android:text="Behavior"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/textView9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView3"
android:layout_below="#+id/day8"
android:text="Satisfaction"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/textView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="#+id/day5"
android:layout_below="#+id/day5"
android:text="Insight"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/day2"
android:layout_below="#+id/day2"
android:text="relationer"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/question6a"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView7"
android:layout_below="#+id/day0"
android:layout_marginLeft="15dp"
android:text="Goal"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/username"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toLeftOf="#+id/textView3"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium" />
</RelativeLayout>
This is my xml file I would like to call this xml file for different activities but just by changing the names ID and some display image buttons.
How to do that can any one please suggest me
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import com.xylem.flygt.R;
import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.app.ProgressDialog;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.TableLayout;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ViewFlipper;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
public class MenuActivity extends Activity
{
static GridView gridView;
Bitmap bmp = null;
public ArrayList<Bitmap> bitmapArray = new ArrayList<Bitmap>();
Context context;
ImageButton day0, day1, day2, day3, day4, day5, day6, day7, day8;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.grid_layout);
day0 = (ImageButton) findViewById(R.id.day0);
day1 = (ImageButton) findViewById(R.id.day1);
day2 = (ImageButton) findViewById(R.id.day2);
day3 = (ImageButton) findViewById(R.id.day3);
day4 = (ImageButton) findViewById(R.id.day4);
day5 = (ImageButton) findViewById(R.id.day5);
day6 = (ImageButton) findViewById(R.id.day6);
day7 = (ImageButton) findViewById(R.id.day7);
day8 = (ImageButton) findViewById(R.id.day8);
gridView = (GridView) findViewById(R.id.grid_view);
gridView.setBackgroundColor(Color.parseColor("#E3E4FA"));
gridView.setOnItemClickListener(new OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> parent, View v, int position, long id)
{
System.out.println("Clicked id " + position);
if(position == 0)
{
System.out.println(" Day 0 ");
}
if(position == 1) {
Intent i = new Intent(getApplicationContext(), nextActivity.class);
i.putExtra("dayno", "1");
System.out.println(" Day 1 ");
startActivity(i);
}
if(position == 2)
{
Intent i = new Intent(getApplicationContext(), nextActivity.class);
i.putExtra("dayno", "2");
System.out.println(" Day 2 ");
startActivity(i);
}
if(position == 3)
{ Intent i = new Intent(getApplicationContext(), nextActivity.class);
i.putExtra("dayno", "3");
System.out.println(" Day 3 ");
startActivity(i);
}
if(position == 4)
{
Intent i = new Intent(getApplicationContext(), nextActivity.class);
i.putExtra("dayno", "4");
System.out.println(" Day 4 ");
startActivity(i);
}
if(position == 5)
{
Intent i = new Intent(getApplicationContext(), nextActivity.class);
i.putExtra("dayno", "5");
System.out.println(" Day 5 ");
startActivity(i);
}
if(position == 6)
{
Intent i = new Intent(getApplicationContext(), nextActivity.class);
i.putExtra("dayno", "6");
System.out.println(" Day 6");
startActivity(i);
}
if(position == 7)
{
Intent i = new Intent(getApplicationContext(), nextActivity.class);
i.putExtra("dayno", "7");
System.out.println(" Day 7 ");
startActivity(i);
}
if(position == 8)
{
Intent i = new Intent(getApplicationContext(), nextActivity.class);
i.putExtra("dayno", "8");
System.out.println(" Day 8 ");
startActivity(i);
}
}
});
new Checkday().execute();
}
#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;
}
class Checkday extends AsyncTask<String, String, String>
{
protected void onPreExecute()
{
super.onPreExecute();
}
#Override
protected String doInBackground(String... arg0)
{
return null;
}
protected void onPostExecute(String file_url)
{
gridView.setAdapter(new CalenderAdaptor(MenuActivity.this));
}
}
}
I have written my activity java it itself creates the image buttons in a gridview. But I use like this I want to use in such way that I call my customs layout in different activities because the layout is same only the IDs and some images will change.
If I understand it right - you have a custom design layout with buttons and you want to use this same layout in other screen as well.
You don't need to rename anything, just put the layout in a separate xml, let's say CommonButtonLayout.xml. This should contain like this:
<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android" >
.... your layout .....
</merge>
Now you want to use this in other layout files. You need to include this xml directly there like the following.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
...
...
>
<include
android:layout_centerVertical="true"
layout="#layout/CommonButtonLayout" />
</RelativeLayout>

Fragment and Toggle Button

I am very new to Android. I am trying to add a simple toggle button to a class which extends Fragment class.
I tried everything but I could not get it working. I managed to add a normal button but that is not useful for me.
All I want is add a toggle button.
Please help me.
Thanks all
<?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="#cccccc" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="18dp"
android:text="Light Tab"
android:textColor="#333333"
android:textSize="20sp" />
<Button
android:id="#+id/fragment_button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView1"
android:layout_alignRight="#+id/textView1"
android:layout_below="#+id/textView1"
android:layout_marginTop="61dp"
android:text="#string/btn_fragment" />
</RelativeLayout>
package com.baris.smartgame;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.app.Activity;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class Light extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.light, container, false);
((Button) view.findViewById(R.id.fragment_button))
.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Activity activity = getActivity();
if (activity != null) {
Toast.makeText(activity,
R.string.toast_you_just_clicked_a_fragment,
Toast.LENGTH_SHORT).show();
}
}
});
return view;
}
}
Use This as an Example :
<LinearLayout
android:id="#+id/txtDayTypes"
android:layout_below="#+id/main_header"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<ToggleButton
android:id="#+id/tglDay1"
android:layout_width="160dp"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="Saturday"
android:textOn="Saturday"
android:textOff="Saturday"
android:checked="true"/>
</LinearLayout>
and you can Access it by :
if(currentButton != R.id.tglDay1) ((ToggleButton)findViewById(R.id.tglDay1)).setChecked(false);

Categories

Resources