How to give equal space programmatically(Java) in the following code? - java

I'm just an beginner.
I'm unable to provide equal space between Item Name and quantity. I want Item Name to occupy space available.
I had tired using setGravity and setColumnStretchable but I'm unable to provide equal space.
<?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"
android:orientation="vertical"
tools:context=".MainActivity">
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1">
<TableLayout
android:id="#+id/Table_Text_View"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1">
<TableRow>
<TextView
android:id="#+id/Item_Name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="3"
android:text="Item Name" />
<TextView
android:id="#+id/quantity"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Quantity" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Item Purchase" />
</TableRow>
</TableLayout>
</ScrollView>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="#+id/Edit_Text_View1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="Name of Item"
android:imeOptions="actionNext"
android:inputType="textMultiLine" />
<EditText
android:id="#+id/Edit_Text_View2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/Edit_Text_View1"
android:hint="Quantity"
android:imeOptions="actionDone"
android:inputType="number" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_toRightOf="#id/Edit_Text_View2"
android:onClick="doneClick"
android:text="Done" />
</RelativeLayout>
public void doneClick(View view) {
EditText editTextView1 = (EditText) findViewById(R.id.Edit_Text_View1);
String itemTextView1 = editTextView1.getText().toString();
EditText editTextView2 = (EditText) findViewById(R.id.Edit_Text_View2);
String itemTextView2 = editTextView2.getText().toString();
TableLayout table = (TableLayout) findViewById(R.id.Table_Text_View);
TableRow row = new TableRow(this);
TableRow.LayoutParams lay = new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT);
row.setLayoutParams(lay);
TextView itemName = new TextView(this);
TextView quantity = new TextView(this);
itemName.setText(itemTextView1);
quantity.setText(itemTextView2);
row.addView(itemName);
row.addView(quantity);
table.addView(row);
}
I expect Item Name to occupy available space, show that it matches with upper row and row that is being created.

Related

linear layout java and xml

I'm just trying to add spinners dynamicallyand I found my way here Add Spinner dynamically in a loop
that works good but my issue is the view takes place instead of my original layout (a header, a footer and an image view).
I would like to place it in a specific layout in the xml file, between the header and the footer for example.
Is there a way to do this?
my code
protected void onCreate(Bundle savedInstanceState) {
Intent i = getIntent();
conteneur = "container No : " + i.getStringExtra("container");
conteneur_s = i.getStringExtra("container");
fichano = i.getStringExtra("fichano");
pos = i.getIntExtra("pos", 0);
latitude = i.getDoubleExtra("geolat", 0);
longitude = i.getDoubleExtra("geolon", 0);
volume = i.getStringExtra("volume");
saver = i.getStringExtra("saver");
poids = i.getStringExtra("poids");
tel = i.getStringExtra("telpass");
driver = i.getStringExtra("driverpass");
tour = i.getStringExtra("tour");
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_parc);
container = (TextView) findViewById(R.id.nom);
container.setText(conteneur);
RelativeLayout lr = (RelativeLayout) findViewById(R.id.full_layout);
GradientDrawable gd = new GradientDrawable(
GradientDrawable.Orientation.TOP_BOTTOM,
new int[] {0xFFFFFFFF,0xFFB0B0B0});
gd.setCornerRadius(0f);
lr.setBackground(gd);
showImg = (ImageView) findViewById(R.id.imageView1);
String pathparc = Environment.getExternalStorageDirectory() + "/SmartCollecte/PARC/PARAM";
File f = new File(pathparc);
final File file[] = f.listFiles();
LinearLayout layout = new LinearLayout(MAIN_parcActivity.this);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(140, 398);
layoutParams.setMargins(24, 50, 24, 0);
layout.setOrientation(LinearLayout.VERTICAL);
for (int j = 0; j < file.length; j++) {
String[] namewext;
namewext = FilenameUtils.removeExtension(file[j].getName()).split("_");
Toast.makeText(getApplicationContext(),namewext[1],Toast.LENGTH_SHORT).show();
Spinner spinner = new Spinner(getApplicationContext());
String[]choix = new String[]{"choisissez"+j, "Propreté", "Détérioration", "Accès", "Maintenance", "autre"};
List<String> str = new ArrayList<>();
str.add(namewext[1]);
Collections.addAll(str,choix);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(MAIN_parcActivity.this,
android.R.layout.simple_spinner_item, str);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
layout.addView(spinner);
setContentView(layout,layoutParams);
}
}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/full_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#909090"
>
<LinearLayout
android:id="#+id/header"
android:layout_width="fill_parent"
android:layout_height="60sp"
android:orientation="horizontal"
android:background="#E25D63"
android:layout_alignParentTop="true"
android:weightSum="3">
<ImageView
android:layout_width="60sp"
android:layout_height="60sp"
android:gravity="left"
android:src="#drawable/backinblack"
android:background="#CFCFCF"
android:layout_weight="0.20"
android:onClick="fermer"
/>
<TextView
android:id="#+id/nom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#220c0c"
android:text="Tournée"
android:textStyle="bold"
android:paddingLeft="30dp"
android:paddingRight="30dp"
android:paddingTop="4dp"
android:paddingBottom="4dp"
android:layout_weight="0.80"
android:background="#E25D63"
android:layout_gravity="center_vertical"
>
</TextView>
<ImageView
android:layout_width="60dp"
android:layout_height="60dp"
android:src="#drawable/anom_ctn"
android:layout_weight="0.20"
android:background="#CFCFCF"
android:layout_gravity="center_vertical"
/>
</LinearLayout>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/scroll"
android:layout_below="#+id/header"
android:layout_above="#+id/footer"
android:fillViewport="true">
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:orientation="vertical">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id = "#+id/spin"
android:orientation="vertical"
android:padding="10dip"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dip"
android:gravity="center_horizontal"
android:text="Etat de parc"
android:textSize="25sp"
android:layout_marginBottom="5dp"/>
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id = "#+id/spin2"
android:orientation="vertical"
android:padding="10dip"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/spin">
<Spinner
android:id="#+id/gametime"
android:layout_marginTop="10dip"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:prompt="#string/anomalie"
android:visibility="visible"/>
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/boutons"
android:orientation="vertical"
android:padding="10dip"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/spin2">
</LinearLayout>
<ImageView
android:id="#+id/imageView1"
android:layout_below="#id/boutons"
android:layout_margin="40sp"
android:adjustViewBounds="true"
android:layout_centerHorizontal="true"
android:layout_height="fill_parent"
android:layout_width="wrap_content"></ImageView>
</LinearLayout>
</ScrollView>
<LinearLayout
android:id="#+id/footer"
android:layout_width="fill_parent"
android:layout_height="60sp"
android:orientation="horizontal"
android:background="#cfcfcf"
android:weightSum="3"
android:layout_alignParentBottom="true"
>
<ImageView
android:layout_width="60dp"
android:layout_height="60dp"
android:src="#drawable/photo"
android:layout_weight="0.80"
android:paddingRight="30sp"
android:onClick="photo"
android:clickable="true"
/>
<View android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="1.40"/>
<ImageView
android:layout_width="60dp"
android:layout_height="60dp"
android:src="#drawable/validation_test"
android:layout_weight="0.80"
android:paddingTop="10sp"
android:paddingLeft="25sp"
android:onClick="enregistrer"
android:clickable="true"
/>
</LinearLayout>
I'm not terribly sure here, I wonder if it is replacing the view because you aren't setting a unique ID for each view? That or failing to call layout.invalidate() after finishing a run through the loop.

Increasing scrollview by adding buttons in a relativelayout (or linearlayout ) from bottom to top

Could really use some help with this one.
I'm trying to start with a single button at the bottom of my screen. When my counter++ button is clicked, a second button will be dynamically created above the button at the bottom of the screen. I was able to get this far, the problem is that the height of my scrollview doesn't increase when my buttons reach the top of the screen. Please see my example below.
Example
I'm assuming the scrollview only increases when items are added below the current view although if that's the case, is there no way I can create several buttons starting at the bottom going upwards?
Here my code.
XML:
<RelativeLayout 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="wrap_content"
android:layout_height="wrap_content"
tools:context="com.example.bbetzner.ttt.Map">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
>
<RelativeLayout
android:id="#+id/layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<Button
android:id="#+id/count"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="count++"
/>
<Button
android:id="#+id/floor0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="0"
/>
</RelativeLayout>
</ScrollView>
</RelativeLayout>
Java:
public void addfloor(){
Button myButton = new Button(this);
myButton.setText(""+floor);
margincount += 100;
RelativeLayout ll = findViewById(R.id.layout);
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
lp.addRule(RelativeLayout.ABOVE, R.id.floor0);
lp.addRule(RelativeLayout.CENTER_HORIZONTAL);
lp.bottomMargin = margincount;
ll.addView(myButton, lp);
}
I've also tried doing this in a linear layout to see if the scrollview would increase but I couldn't figure out how to stack buttons from bottom to top in a linear layout. :S
Thanks in advance, yall are awesome!
#Texas use below code using that you are achieving what you want:
<RelativeLayout 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="wrap_content"
android:layout_height="wrap_content">
<LinearLayout
android:id="#+id/layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<Button
android:id="#+id/count"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="count++" />
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
android:scrollbars="vertical">
<LinearLayout
android:id="#+id/lladdViews"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:gravity="bottom"
android:orientation="vertical">
<Button
android:id="#+id/floor0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:text="0" />
</LinearLayout>
</ScrollView>
</LinearLayout>
and here is Java File:
public class SampleActivity extends Activity {
LinearLayout lladdViews;
Button count;
int floor = 0;
private int margincount = 0;
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sample_activity);
lladdViews = (LinearLayout) findViewById(R.id.lladdViews);
count = (Button) findViewById(R.id.count);
count.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
addfloor();
}
});
}
public void addfloor() {
Button myButton = new Button(this);
myButton.setText("" + floor);
margincount += 10;
RelativeLayout ll = new RelativeLayout(this);
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
lp.addRule(RelativeLayout.ABOVE, R.id.floor0);
lp.addRule(RelativeLayout.CENTER_HORIZONTAL);
lp.bottomMargin = margincount;
ll.addView(myButton, lp);
lladdViews.addView(ll);
}
}
Here is your designing part happy coding.....
<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"
android:orientation="vertical"
>
<LinearLayout
android:id="#+id/layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
>
<Button
android:id="#+id/count"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="count++"
/>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
>
<LinearLayout
android:id="#+id/layout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
>
<Button
android:id="#+id/floor0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="0"
/>
<Button
android:id="#+id/floor1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="7"
/>
<Button
android:id="#+id/floor2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="6"
/>
<Button
android:id="#+id/floor3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="5"
/>
<Button
android:id="#+id/floor4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="4"
/>
<Button
android:id="#+id/floor5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="0"
/><Button
android:id="#+id/floor6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="3"
/><Button
android:id="#+id/floor7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="2"
/>
<Button
android:id="#+id/floor8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="1"
/>
<Button
android:id="#+id/floor9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="0"
/>
</LinearLayout>
</ScrollView>
</LinearLayout>

Rating bar shows more stars when I rotate my application

I have this weird issue with my rating bar stars.
This is my app in vertical position
This is my app in horizontal position
As you can see the stars completely change
This is my xml file:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:background="#drawable/main"
tools:context="com.example.draganam.blankapp.Nivo1Activity">
<TableLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:shrinkColumns="1,0" android:stretchColumns="0,1">
<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="50dp">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/slika0"
android:src="#drawable/becutan1"
android:layout_column="0"
android:layout_marginRight="40px" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/slika1"
android:src="#drawable/blizoo1"
android:layout_column="1" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">
<RatingBar
android:id="#+id/rb0"
style="?android:attr/ratingBarStyleIndicator"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:numStars="5"
android:stepSize="1.0"
android:isIndicator="true"
android:scaleX="0.7"
android:scaleY="0.7"/>
<RatingBar
android:id="#+id/rb1"
style="?android:attr/ratingBarStyleIndicator"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:numStars="5"
android:stepSize="1.0"
android:isIndicator="true"
android:scaleX="0.7"
android:scaleY="0.7"/>
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="50dp">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/slika2"
android:src="#drawable/brilijant1"
android:layout_column="0"
android:layout_marginRight="40px" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/slika3"
android:src="#drawable/vitaminka1"
android:layout_column="1" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">
<RatingBar
android:id="#+id/rb2"
style="?android:attr/ratingBarStyleIndicator"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:numStars="5"
android:stepSize="1.0"
android:isIndicator="true"
android:scaleX="0.7"
android:scaleY="0.7"/>
<RatingBar
android:id="#+id/rb3"
style="?android:attr/ratingBarStyleIndicator"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:numStars="5"
android:stepSize="1.0"
android:isIndicator="true"
android:scaleX="0.7"
android:scaleY="0.7"/>
</TableRow>
</TableLayout>
</RelativeLayout>
This is my java code:
Context ctx2 = this;
String[][] arrayWithInformations = new String[5][];
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_nivo1);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
DatabaseOperations databaseOperations = new DatabaseOperations(ctx2);
Cursor cursorLogo = databaseOperations.getInformationsForLogos(databaseOperations);
// looping through all rows and adding to list
Integer i = 0;
if (cursorLogo.moveToFirst()) {
do {
String[] helperArray = {cursorLogo.getString(0),cursorLogo.getString(1),cursorLogo.getString(2),cursorLogo.getString(3),
cursorLogo.getString(4),cursorLogo.getString(5)};
arrayWithInformations[i] = helperArray;
i++;
} while (cursorLogo.moveToNext());
}
int firstRating = Integer.parseInt(arrayWithInformations[0][4]);
int secondRating = Integer.parseInt(arrayWithInformations[1][4]);
int thirdRating = Integer.parseInt(arrayWithInformations[2][4]);
int fourthRating = Integer.parseInt(arrayWithInformations[3][4]);
RatingBar rb0 = (RatingBar) findViewById(R.id.rb0);
rb0.setRating(firstRating);
RatingBar rb1 = (RatingBar) findViewById(R.id.rb1);
rb1.setRating(secondRating);
RatingBar rb2 = (RatingBar) findViewById(R.id.rb2);
rb2.setRating(thirdRating);
RatingBar rb3 = (RatingBar) findViewById(R.id.rb3);
rb3.setRating(fourthRating);
I get the rating values from database file and everything works fine , the stars are ok but when I rotate the phone they are all wrong. I know that when u rotate your phone the current activity gets loaded again, but it's the same code and it loads wrong. Maybe it's multiplying the stars.. I may sound funny but I have no idea what is going wrong. Someone please give me a lead..
I found out the problem !
android:shrinkColumns="1,0" android:stretchColumns="0,1"
Setting this to the TableLayout caused this strange issue.
Thanks everyone for your interest in helping me ..

RadioButton switching between layouts

So I would like to have a calculator in my app. User will first click on RadioButton which would be for Imperial and Metric units, and depending on which one does user click, one of the layouts would appear.
As you know imperial units has feets and inches, for which I need two EditTexts, but metric unit needs only one.
So that's what I want, switching between layouts in same activity to avoid using fragments.
Any ideas?
EDIT
xml file
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<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"
android:paddingBottom="10dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingTop="10dp"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="hr.app.liftme.liftmehr.KalkulatoriBFMan"
tools:showIn="#layout/activity_kalkulatori_bfman"
android:orientation="vertical">
<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:foreground="?android:attr/selectableItemBackground"
android:clickable="false"
android:id="#+id/Osnove5"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
card_view:cardCornerRadius="2dp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_weight="1"
android:elevation="50dp"
card_view:cardBackgroundColor="#ffffff"
android:layout_marginBottom="10dp">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/linearLayout28"
android:layout_marginBottom="20dp"
android:layout_marginTop="55dp"
android:padding="7dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="#string/bf_uvodno"
android:id="#+id/textView140"
android:textStyle="bold" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="#string/bf_tekst_upute"
android:id="#+id/textView141"
android:textColor="#424242" />
</LinearLayout>
</android.support.v7.widget.CardView>
<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:foreground="?android:attr/selectableItemBackground"
android:clickable="false"
android:id="#+id/Drugo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
card_view:cardCornerRadius="2dp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_weight="1"
android:layout_marginBottom="10dp"
android:elevation="50dp"
card_view:cardBackgroundColor="#ffffff">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/linearLayout27"
android:layout_below="#+id/linearLayout28"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:padding="7dp">
<LinearLayout
android:orientation="horizontal"
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/textAppearanceLarge"
android:text="#string/bf_radio_naslov"
android:id="#+id/textView3"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RadioGroup
android:layout_width="match_parent"
android:layout_height="match_parent">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/radioButtonBFZeneCmKg"
android:text="#string/bf_radio2"
android:onClick="radioZeneCmKgClicked"
android:checked="false" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/radioButtonBFZeneFtLb"
android:text="#string/bf_radio1"
android:onClick="radioZeneBFFtLbClicked"
android:checked="false" />
</RadioGroup>
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="#string/bf_edit_tezina"
android:id="#+id/textView133"
android:textStyle="bold" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="numberDecimal"
android:ems="10"
android:id="#+id/editTezinaBF"
android:hint="#string/bf_edit_tezina_hint_z"
android:layout_marginBottom="5dp" />
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/LayoutVisinaMetric">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="#string/bf_edit_visina"
android:id="#+id/textViewVisinaMetric"
android:textStyle="bold" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="numberDecimal"
android:ems="10"
android:id="#+id/editVisinaBF"
android:hint="#string/bf_edit_visina_hint_z"
android:layout_marginBottom="5dp" />
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/LayoutVisinaImperial">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="#string/bf_feetinch_naslov"
android:id="#+id/textViewVisinaImperial"
android:textStyle="bold" />
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="numberDecimal"
android:ems="10"
android:id="#+id/editTextVisinaFT"
android:layout_weight="1"
android:hint="#string/bf_radio1_hint1" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="numberDecimal"
android:ems="10"
android:id="#+id/editTextVisinaINC"
android:layout_weight="1"
android:hint="#string/bf_radio1_hint2" />
</LinearLayout>
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="#string/bf_edit_struk"
android:id="#+id/textView135"
android:textStyle="bold" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="numberDecimal"
android:ems="10"
android:id="#+id/editStrukBF"
android:hint="#string/bf_edit_struk_hint_z"
android:layout_marginBottom="5dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="#string/bf_edit_bokovi"
android:id="#+id/textView136"
android:textStyle="bold" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="numberDecimal"
android:ems="10"
android:id="#+id/editBokoviBF"
android:hint="#string/bf_edit_bokovi_hint_z"
android:layout_marginBottom="5dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="#string/bf_edit_vrat"
android:id="#+id/textView137"
android:textStyle="bold" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="numberDecimal"
android:ems="10"
android:id="#+id/editVratBF"
android:hint="#string/bf_edit_vrat_hint_z"
android:layout_marginBottom="5dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="#string/bf_edit_godine"
android:id="#+id/textView138"
android:textStyle="bold" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="numberDecimal"
android:ems="10"
android:id="#+id/editGodineBF"
android:hint="#string/bf_edit_godine_hint_z"
android:layout_marginBottom="15dp" />
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/linearLayout27"
android:layout_marginBottom="20dp"
android:id="#+id/linearLayout29">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/bf_btn_izracunaj"
android:id="#+id/btnIzracunajBF"
android:layout_gravity="center_horizontal"
android:background="#424242"
android:textColor="#ffffff"
android:layout_marginBottom="10dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="#string/bf_tekst_rezultat"
android:id="#+id/textView139"
android:layout_gravity="center_horizontal" />
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:id="#+id/textRezultatBF"
android:layout_gravity="center_horizontal" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="#string/bf_rezultat"
android:id="#+id/textView152" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
<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:foreground="?android:attr/selectableItemBackground"
android:clickable="false"
android:id="#+id/Osnove3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
card_view:cardCornerRadius="2dp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_weight="1"
android:elevation="50dp"
card_view:cardBackgroundColor="#ffffff">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:layout_below="#+id/linearLayout27"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:padding="7dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="#string/bf_tekst_tumacenje"
android:id="#+id/textView142"
android:gravity="center_horizontal"
android:layout_marginBottom="5dp"
android:textStyle="bold" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="#string/bf_tumacenje_rezultat_z"
android:id="#+id/textView143"
android:gravity="center_horizontal"
android:textColor="#424242" />
</LinearLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
</ScrollView>
java
public class KalkulatoriBFMan extends AppCompatActivity {
EditText editTezina, editVisina, editStruk, editBokovi, editVrat, editGodine, editVisinaFT, editVisinaINC;
Button btnIzracunajBF;
TextView textRezultatBF, textVisinaMetric, textVisinaImperial;
LinearLayout imperial, metric;
public void radioZeneCmKgClicked(View view){
boolean checked = ((RadioButton) view).isChecked();
switch (view.getId()){
case R.id.radioButtonBFZeneCmKg:
if (checked)
editVisina.setVisibility(View.VISIBLE);
imperial.setVisibility(View.GONE);
btnIzracunajBF.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
// IMPERIAL FORMULA
//Body Fat (women) = 495 / (1.29579 - .35004 * log10(Waist + Hip - Neck) + 0.22100 * log10(Height)) - 450
double prvoPolje = Double.parseDouble(editTezina.getText().toString());
double drugoPolje = Double.parseDouble(editVisina.getText().toString());
double trecePolje = Double.parseDouble(editStruk.getText().toString());
double cetvrtoPolje = Double.parseDouble(editBokovi.getText().toString());
double petoPolje = Double.parseDouble(editVrat.getText().toString());
double sestoPolje = Double.parseDouble(editGodine.getText().toString());
double rezultat = 0;
rezultat = 495 / (1.0324 - 0.19077 * Math.log10(trecePolje - petoPolje) + 0.15456 * Math.log10(drugoPolje)) - 450;
rezultat = Math.round(rezultat * 100.0) / 100.0;
textRezultatBF.setText(Double.toString(rezultat) + " %");
}
});
break;
}
}
public void radioZeneBFFtLbClicked(View view) {
boolean checked = ((RadioButton) view).isChecked();
switch (view.getId()) {
case R.id.radioButtonBFZeneFtLb:
if (checked)
editVisinaFT.setVisibility(View.VISIBLE);
metric.setVisibility(View.GONE);
btnIzracunajBF.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
// IMPERIAL FORMULA
//Body Fat (men) = 495 / (1.29579 - .35004 * log10(Waist - Neck) + 0.22100 * log10(Height)) - 450
double prvoPolje = Double.parseDouble(editTezina.getText().toString());
double drugoPolje = Double.parseDouble(editVisinaFT.getText().toString());
double trecePolje = Double.parseDouble(editStruk.getText().toString());
double cetvrtoPolje = Double.parseDouble(editBokovi.getText().toString());
double petoPolje = Double.parseDouble(editVrat.getText().toString());
double sestoPolje = Double.parseDouble(editGodine.getText().toString());
double sedmoPolje = Double.parseDouble(editVisinaINC.getText().toString());
double rezultat = 0;
rezultat = 495 / (1.29579 - 0.35004 * Math.log10(trecePolje - petoPolje) + 0.22100 * Math.log10(((drugoPolje * 12) + sedmoPolje))) - 450;
rezultat = Math.round(rezultat * 100.0) / 100.0;
textRezultatBF.setText(Double.toString(rezultat) + " %");
}
});
break;
}
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_kalkulatori_bfwoman);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
Intent intent = getIntent();
editTezina = (EditText) findViewById(R.id.editTezinaBF);
editVisina = (EditText) findViewById(R.id.editVisinaBF);
editStruk = (EditText) findViewById(R.id.editStrukBF);
editBokovi = (EditText) findViewById(R.id.editBokoviBF);
editVrat = (EditText) findViewById(R.id.editVratBF);
editGodine = (EditText) findViewById(R.id.editGodineBF);
editVisinaFT = (EditText) findViewById(R.id.editTextVisinaFT);
editVisinaINC = (EditText) findViewById(R.id.editTextVisinaINC);
textVisinaImperial = (TextView) findViewById(R.id.textViewVisinaImperial);
textVisinaMetric = (TextView) findViewById(R.id.textViewVisinaMetric);
imperial = (LinearLayout) findViewById(R.id.LayoutVisinaImperial);
metric = (LinearLayout) findViewById(R.id.LayoutVisinaMetric);
btnIzracunajBF = (Button) findViewById(R.id.btnIzracunajBF);
textRezultatBF = (TextView) findViewById(R.id.textRezultatBF);
}
}
Note that I solved blank space problem with setting visibility to GONE, but when I open activity both EditTexts appear, imperial and metric. I want them both to be invisible until user selects one RadioButton
To define the click event handler for a button, add the android:onClick attribute to the <RadioButton> element in your XML layout. The value for this attribute must be the name of the method you want to call in response to a click event. The Activity hosting the layout must then implement the corresponding method.
For example, here are a couple RadioButton objects:
<?xml version="1.0" encoding="utf-8"?>
<RadioGroup xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<RadioButton android:id="#+id/radio_pirates"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/pirates"
android:onClick="onRadioButtonClicked"/>
<RadioButton android:id="#+id/radio_ninjas"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/ninjas"
android:onClick="onRadioButtonClicked"/>
</RadioGroup>
Within the Activity that hosts this layout, the following method handles the click event for both radio buttons:
public void onRadioButtonClicked(View view) {
// Is the button now checked?
boolean checked = ((RadioButton) view).isChecked();
// Check which radio button was clicked
switch(view.getId()) {
case R.id.imperial:
if (checked)
EditBox1.setVisibility(Visible)
break;
case R.id.metrics:
if (checked){
EditBox1.setVisibility(Visible)
EditBox2.setVisibility(Visible)
}
break;
}
}
Source: http://developer.android.com/guide/topics/ui/controls/radiobutton.html

Inflated items are gone when device orientation changes

Hi i'm using eclipse for deveoping this android app. I'm inflating a layout using inflator and everything works fine.. I'm adding those inflated items one by one to another layout, so when the device's orientation is changed, all those inflated items are gone. The application seems like it has been restarted. But some values are still remaining.. Please help..
public void addNewItem() {
LayoutInflater li = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View item = li.inflate(R.layout.activity_order_items, null);
orderItemFrameView.add(item);
itemsLayout.addView(item);
setAdditionalFonts(item);
}
private void setAdditionalFonts(View view) {
TextView lblNameTak = (TextView)view.findViewById(R.id.tak_itemNo);
AutoCompleteTextView txtNameTak = (AutoCompleteTextView)view.findViewById(R.id.tak_itemName);
ImageView barScan = (ImageView)view.findViewById(R.id.barcode_scan);
TextView lblRate = (TextView)view.findViewById(R.id.tak_lblRate);
TextView lblQty = (TextView)view.findViewById(R.id.tak_lblQty);
TextView lblTotal = (TextView)view.findViewById(R.id.tak_lblTotal);
TextView rate = (TextView)view.findViewById(R.id.tak_Rate);
EditText2 qty = (EditText2)view.findViewById(R.id.tak_Qty);
TextView total = (TextView)view.findViewById(R.id.tak_Total);
qty.totTextView = total;
qty.rateTextView = rate;
qty.orderTake = this;
qty.index = orderItemFrameView.size() - 1;
qty.sqlDb = SqlDb;
qty.itemName = txtNameTak;
setupBarcodeScan(barScan);
Typeface tf = Typeface.createFromAsset(getAssets(), "fonts/ubuntu-l.ttf");
Typeface tf2 = Typeface.createFromAsset(getAssets(), "fonts/calibri.ttf");
Typeface tf3 = Typeface.createFromAsset(getAssets(), "fonts/ubu-r.ttf");
Typeface tf4 = Typeface.createFromAsset(getAssets(), "fonts/tahoma.ttf");
lblNameTak.setTypeface(tf3);
txtNameTak.setTypeface(tf2);
lblRate.setTypeface(tf2);
lblQty.setTypeface(tf2);
lblTotal.setTypeface(tf2);
lblNameTak.setText("Item " + (qty.index + 1));
DbAdapterItem dbItem;
TextAdapterItem txtItem;
dbItem = new DbAdapterItem(this, SqlDb);
txtItem = new TextAdapterItem(dbItem, this, qty);
txtNameTak.setAdapter(txtItem);
txtNameTak.setOnItemClickListener(txtItem);
txtNameTak.requestFocus();
}
all these items created by addNewItem() are vanished...
here is the layout from which i'm inflating...
<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:paddingBottom ="22dp"
tools:context=".OrderItems" >
<FrameLayout
android:layout_width="match_parent"
android:background= "#888888"
android:layout_height="98dp" >
<TextView
android:id="#+id/tak_itemNo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="15dp"
android:layout_marginTop="2.7dp"
android:text="Item 1"
android:layout_gravity="right"
android:textColor="#DDDDDD" />
<LinearLayout
android:layout_width="match_parent"
android:layout_marginTop="22dp"
android:orientation="vertical"
android:background= "#FFFFFF"
android:layout_height="75dp" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<AutoCompleteTextView
android:id="#+id/tak_itemName"
android:imeOptions="actionUnspecified"
android:imeActionLabel="search"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginRight="0dp"
android:layout_weight="1.06"
android:completionThreshold="1"
android:ems="10"
android:hint="Item Name / Barcode"
android:inputType="textCapWords"
android:singleLine="true"
android:textColorHint="#DDDDDD" />
<ImageView
android:id="#+id/barcode_scan"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="7dp"
android:paddingRight="7dp"
android:paddingTop="6dp"
android:src="#android:drawable/ic_menu_camera" />
</LinearLayout>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<FrameLayout
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_gravity="left"
>
<TextView
android:id="#+id/tak_lblRate"
android:layout_width="wrap_content"
android:layout_marginLeft="7dp"
android:layout_height="wrap_content"
android:textColor="#555555"
android:textSize="10sp"
android:text="Rate" />
<TextView
android:id="#+id/tak_Rate"
android:layout_width="match_parent"
android:layout_marginTop="12dp"
android:layout_marginRight="15dp"
android:layout_height="wrap_content"
android:gravity="right"
android:textSize="16sp"
android:text="0.00" />
</FrameLayout>
<FrameLayout
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
>
<TextView
android:id="#+id/tak_lblQty"
android:layout_width="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginRight="7dp"
android:layout_height="wrap_content"
android:textColor="#555555"
android:textSize="10sp"
android:text="Qty" />
<com.sayka.ordergadget.EditText2
android:id="#+id/tak_Qty"
android:layout_width="match_parent"
android:layout_marginBottom="-4dp"
android:layout_marginTop="4dp"
android:inputType="numberDecimal"
android:imeOptions="actionNext"
android:layout_marginLeft="12dp"
android:layout_height="wrap_content"
android:gravity="center"
android:textSize="16sp"
android:text="0.0" />
</FrameLayout>
<FrameLayout
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_gravity="right"
>
<TextView
android:id="#+id/tak_lblTotal"
android:layout_width="wrap_content"
android:layout_marginLeft="7dp"
android:layout_gravity="left"
android:layout_height="wrap_content"
android:textColor="#555555"
android:textSize="10sp"
android:text="Total" />
<TextView
android:id="#+id/tak_Total"
android:layout_width="match_parent"
android:layout_gravity="right"
android:layout_marginTop="12dp"
android:layout_marginRight="15dp"
android:layout_height="wrap_content"
android:gravity="right"
android:textSize="16sp"
android:text="0.00" />
</FrameLayout>
</FrameLayout>
</LinearLayout>
</FrameLayout>
It's normal behaviour and you are correct. the activity is recreated from scratch on rotation.
If you want something to persist. store it during onSaveInstanceState in the provided bundle.
then during onCreate check to see if the bundle is null, if it isn't, pull your data out and add the view elements again.
Alternatively you can fudge it and state in the manifest that your activity will handle orientation/configuration changes.

Categories