Android : how to divide JSON into two parts - java

i have json as below :
{
"pp": {
"status_marital_pmg": "BELUM MENIKAH",
"nama_pmg": "tomy",
"kerjab_pmg": "-",
"pendidikan_pmg": "S1",
"agama_pmg": "KRISTENKATOLIK",
"bidang_industri_pmg": "JASAKEUANGAN",
"warga_pmg": "-",
"hubungan_pmg_ttg": "DiriSendiri",
"pendanaana_pmg": "GAJI",
"pendanaan_pmg": "GAJI",
"usia_pmg": 33,
"penghasilan_pmg": "-",
"kelamin_pmg": "Pria",
"tujuan_pmg": "INVESTASI",
"tujuana_pmg": "INVESTASI",
"kerja_pmg": "KARYAWAN",
"bidang_industria_pmg": "JASAKEUANGAN"
}
}
i want to divide and save my json into two parts, they are : listPP and listPPL and in my layout, i want to divide it into two parts first part for listPP and other part for listPPL, but when i run it, there is just one part: ListPPL and it show in first part of layout, not second part of layout.
ListPP doesn't show anything, this is my code :
try {
JSONObject jsonObject = new JSONObject(result);
PPVariabel varpp = null;
PPPelengkapvariabel varppl=null;
JSONObject pp = jsonObject.getJSONObject("pp");
{
for (int i=0; i<PP.length;i++){
varpp= new PPVariabel(pp.optString("nama_pmg"),
pp.optString("kerjab_pmg"),
pp.optString("warga_pmg"),
pp.optString("usia_pmg"),
pp.optString("status_marital_pmg"),
pp.optString("kelamin_pmg"),
pp.optString("agama_pmg"),
pp.optString("pendidikan_pmg"));
listPP.add(varpp);
LinearLayout linear1=(LinearLayout)findViewById(R.id.pp1);
list=(ListView)findViewById(android.R.id.list);
setListAdapter(new PPViewerAdapter(this, listPP,PP));
}
for (int j=0; j<Pelengkap.length;j++){
varppl= new PPPelengkapvariabel (pp.optString("tujuan_pmg"),
pp.optString("tujuana_pmg"),
pp.optString("penghasilan_pmg"),
pp.optString("pendanaan_pmg"),
pp.optString("pendanaana_pmg"),
pp.optString("kerja_pmg"),
pp.optString("bidang_industri_pmg"),
pp.optString("bidang_industria_pmg"),
pp.optString("hubungan_pmg_ttg"));
listPPL.add(varppl);
LinearLayout linear2=(LinearLayout)findViewById(R.id.dpl2);
list=(ListView)findViewById(R.id.ppl);
setListAdapter(new PPDataPelengkapAdapter(this, listPPL,Pelengkap));
}
this is my layout :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="#color/medium_gray">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fadingEdge="vertical"
android:fadeScrollbars="true"
android:fadingEdgeLength="1dp"
android:scrollbars="none">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<LinearLayout
android:id="#+id/pp1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="23sp"
android:textColor="#FF0000"
android:text="DATA DIRI"
android:textStyle="bold"
android:divider="#000000"
android:dividerHeight="1dp"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:typeface="sans"/>
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="470dp"
android:divider="#000000"
android:dividerHeight="1dp"
android:focusable="false"
android:clickable="false"/>
</LinearLayout>
<LinearLayout
android:id="#+id/dpl2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="23sp"
android:textColor="#FF0000"
android:text="DATA PELENGKAP"
android:textStyle="bold"
android:divider="#000000"
android:dividerHeight="1dp"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:typeface="sans"/>
<ListView
android:id="#+id/ppl"
android:layout_width="fill_parent"
android:layout_height="1000dp"
android:divider="#000000"
android:dividerHeight="1dp"
android:focusable="false"
android:clickable="false"/>
</LinearLayout>
</LinearLayout>
</ScrollView>
</LinearLayout>
is there any wrong with my code? if yes, is there anyone can help me to solve my problem? thank you

Related

layout weight not working CardView

I want my screen to show a total of 3 cards distributed evenly taking up the whole space but nothing I try seems to be working. The cards are being loaded with the use of a RecyclerView/viewHolder.
I've tried two solutions.
Solution 1:
<?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:id="#+id/card_view_disciplines"
card_view:cardCornerRadius="4dp"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="3"
>
<TextView
android:layout_width="match_parent"
android:layout_height="0dp"
android:id="#+id/disciplineName"
android:textSize="25sp"
android:layout_weight="1"
/>
</LinearLayout>
</RelativeLayout>
</android.support.v7.widget.CardView>
and Solution 2:
<?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:id="#+id/card_view_disciplines"
card_view:cardCornerRadius="4dp"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:weightSum="3"
>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="#+id/disciplineName"
android:textSize="25sp"
android:layout_weight="1"
/>
</LinearLayout>
</RelativeLayout>
</android.support.v7.widget.CardView>
can someone please help?
Thanks in advance!
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:weight_sum="3"
android:padding="16dp">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
app:cardBackgroundColor="#000">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="40sp"
android:text="Card 1"
android:textColor="#FFF"
android:layout_gravity="center"/>
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:padding="16dp"
android:background="#000">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="40sp"
android:text="Card 2"
android:layout_gravity="center"/>
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
app:cardBackgroundColor="#000">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="40sp"
android:text="Card 3"
android:textColor="#FFF"
android:layout_gravity="center"/>
</android.support.v7.widget.CardView>
</LinearLayout>
I hope this will work as you expected:
<LinearLayout
android:id="#+id/layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#888888"
android:orientation="vertical"
android:weightSum="3">
<android.support.v7.widget.CardView
android:id="#+id/cardView1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_margin="2dp"
android:layout_weight="1"
card_view:cardBackgroundColor="#000088"
card_view:cardCornerRadius="5dp"
card_view:cardElevation="5dp">
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
android:id="#+id/cardView2"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_margin="2dp"
android:layout_weight="1"
card_view:cardBackgroundColor="#008800"
card_view:cardCornerRadius="4dp"
card_view:cardElevation="5dp">
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
android:id="#+id/cardView3"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_margin="2dp"
android:layout_weight="1"
card_view:cardBackgroundColor="#880000"
card_view:cardCornerRadius="4dp"
card_view:cardElevation="5dp">
</android.support.v7.widget.CardView>
</LinearLayout>
MainActivity.java
CardView cardView1 = (CardView) findViewById(R.id.cardView1);
CardView cardView2 = (CardView) findViewById(R.id.cardView2);
CardView cardView3 = (CardView) findViewById(R.id.cardView3);
cardView1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "CardView1", Toast.LENGTH_SHORT).show();
}
});
cardView2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "CardView2", Toast.LENGTH_SHORT).show();
}
});
cardView3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "CardView3", Toast.LENGTH_SHORT).show();
}
});

Disabling all child views inside the layout

I saw many threads related to this, before posting my question. But none worked for me. I have a RelativeLayout with many other layouts and fragments as children. I want to disable all the children of "content_view" as well as the content_view itself on a button click. I tried
contentView.setDisabled(false);
This didn't work. I've also tried
for (int i = 0; i < layout.getChildCount(); i++) {
View child = layout.getChildAt(i);
child.setEnabled(false);
}
Even this didn't work. What am I doing wrong? Please find my .xml code below.
I even tried placing a view above all views. Even that didn't solve my problem.
<RelativeLayout
android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="vertical"
android:id="#+id/content_view"
android:background="#ffffff">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="#+id/channel_actionbar"
android:layout_marginLeft="0dp"
android:layout_marginTop="0dp"
android:duplicateParentState="true">
<FrameLayout
android:layout_height="match_parent"
android:layout_width="match_parent"
android:id="#+id/body_container"
android:layout_below="#+id/channel_actionbar"
android:duplicateParentState="true">
<RelativeLayout
android:layout_height="match_parent"
android:layout_width="match_parent"
android:id="#+id/channelList"
android:duplicateParentState="true">
<com.mobile.subview.ScrollViewWithScrollListener
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/scrollView"
android:layout_alignParentTop="false"
android:duplicateParentState="true">
<RelativeLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:duplicateParentState="true">
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/imagePlaceHolder"
android:duplicateParentState="true"></FrameLayout>
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/channelTable"
android:layout_below="#+id/imagePlaceHolder"
android:duplicateParentState="true"></TableLayout>
<com.mobile.subview.CustomTextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="You do not qualify for any engagements or channels. Please check back later."
app:typeface="fonts/HelveticaNeue"
app:customStyle="Regular"
android:id="#+id/noChannelsMessage"
android:textColor="#000"
android:textSize="#dimen/contentTextSize"
android:visibility="gone"
android:duplicateParentState="true"/>
</RelativeLayout>
</com.mobile.subview.ScrollViewWithScrollListener>
<com.mobile.subview.ParallaxImage
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/header"
android:adjustViewBounds="true"
android:layout_alignParentTop="true"
android:scaleType="fitStart"
android:visibility="invisible"
android:duplicateParentState="true"/>
</RelativeLayout>
<RelativeLayout
android:layout_height="match_parent"
android:layout_width="match_parent"
android:layout_below="#+id/channel_actionbar"
android:id="#+id/sibling_view"
android:visibility="gone"
android:duplicateParentState="true"></RelativeLayout>
</FrameLayout>
</LinearLayout>
<RelativeLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:visibility="invisible"
android:id="#+id/retailersContainer"
android:layout_marginLeft="0dp"
android:duplicateParentState="true"
android:layout_below="#+id/channel_actionbar">
<fragment
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:name="com.mobile.subview.List"
android:id="#+id/retailers"
android:duplicateParentState="true"/>
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="#+id/retailers"
android:id="#+id/retailerClickBlocker"
android:duplicateParentState="true"></FrameLayout>
</RelativeLayout>
<RelativeLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="#dimen/actionBarHeight"
android:layout_alignParentTop="true"
android:id="#+id/channel_actionbar"
android:background="#F8F8F8"
android:layout_marginLeft="0dp"
android:duplicateParentState="true">
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/shared_navmenu_button"
android:id="#+id/show_menu_button"
android:layout_centerVertical="true"
android:background="#null"
android:scaleType="fitCenter"
android:layout_marginLeft='5px'
android:duplicateParentState="true"/>
<com.mobile.subview.CustomTextView
android:id="#+id/channel_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:textSize="#dimen/titleTextSize"
android:textColor="#000"
android:text="Test Title"
app:typeface="fonts/HelveticaNeue"
app:customStyle="Medium"
android:visibility="gone"
android:duplicateParentState="true"/>
<ImageView
android:id="#+id/logo"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_centerInParent="true"
android:src="#drawable/shared_navbar_logo2x"
android:visibility="gone"
android:scaleType="fitCenter"
android:duplicateParentState="true"/>
<com.mobile.subview.CustomButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:id="#+id/channel_done_btn"
app:typeface="fonts/HelveticaNeue"
app:customStyle="Regular"
android:visibility="gone"
android:duplicateParentState="true"/>
<com.mobile.subview.CustomButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
app:typeface="fonts/HelveticaNeue"
app:customStyle="Regular"
android:id="#+id/channel_share_btn"
android:visibility="gone" />
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="1dp"
android:background="#D6D6D6"
android:id="#+id/divider"
android:layout_alignParentBottom="true"
android:duplicateParentState="true"/>
</RelativeLayout>
<View
android:visibility="gone"
android:id="#+id/click_preventing_view"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#66000000"
android:clickable="false" />
</RelativeLayout>
Because your layouts are so heavily nested, you need to recursively disable the views. Instead of using your method, try something like this:
private static void disable(ViewGroup layout) {
layout.setEnabled(false);
for (int i = 0; i < layout.getChildCount(); i++) {
View child = layout.getChildAt(i);
if (child instanceof ViewGroup) {
disable((ViewGroup) child);
} else {
child.setEnabled(false);
}
}
}
then call:
disable(content_view);
Even though the answer is expected instead of using recursion I think the below code will do the trick.This is what I used to disbale it. I just passed parentlayout and whether to show or hide as a boolean parameter
private void disable(LinearLayout layout, boolean enable) {
for (int i = 0; i < layout.getChildCount(); i++) {
View child = layout.getChildAt(i);
child.setEnabled(enable);
if (child instanceof ViewGroup) {
ViewGroup group = (ViewGroup) child;
for (int j = 0; j < group.getChildCount(); j++) {
group.getChildAt(j).setEnabled(enable);
}
}
}
Here are Kotlin versions:
Using Android KTX
fun View.changeEnableChildren(enable: Boolean) {
isEnabled = enable
(this as? ViewGroup)?.let {
forEach {
changeEnableChildren(enable)
}
}
}
Without Android KTX
fun View.changeEnableChildren(enable: Boolean) {
isEnabled = enable
(this as? ViewGroup)?.let {
for (i in 0..it.childCount) {
changeEnableChildren(enable)
}
}
}
Note: recursive method may cause StackOverFlowError in that case you should replace changeEnableChildren(enable) with isEnabled=enable
apply android:duplicateParentState="true" to the children

After take a picture and save it, sometimes the program crashes, doesn't return to the activity

I'm calling the function dispatchTakePictureIntent to take a picture. But after I take the picture and save it, the application doesn't returns correctly to the actual activity, the application crashes.
When my button is pressed I call
dispatchTakePictureIntent(1);
then:
public void dispatchTakePictureIntent(int actionCode) {
try {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File f = createImageFile();
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f));
startActivityForResult(takePictureIntent, actionCode);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
/* Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(takePictureIntent, actionCode);*/
}
public File createImageFile() throws IOException {
// Create an image file name
final String nomeForm = "teste";
String imageFileName = nomeForm +".jpeg";
File path = getExternalFilesDir(Environment.DIRECTORY_DCIM);
File image = new File(path,imageFileName );
// String getAbsolutePath = image.getAbsolutePath();
Toast.makeText(this, "Image saved to:\n" + image, Toast.LENGTH_LONG).show();
return image;
}
my logcat error report:
java.lang.RuntimeException: Unable to stop activity {com.example.sform/com.example.sform.mainRisco}: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.sform/com.example.sform.risco1}: java.lang.IllegalArgumentException: Wrong state class, expecting View State but received class android.widget.CompoundButton$SavedState instead. This usually happens when two views of different type have the same id in the same hierarchy. This view's id is id/0x1. Make sure other views do not use the same id.
Here all my layouts:
Tab Host Layout:
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/layout_riscos">
<TabWidget
android:id="#android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
</LinearLayout>
</TabHost>
The Tab1:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="#+id/risco1"
>
<RelativeLayout
android:id="#+id/geral_risco1"
android:layout_width="match_parent"
android:layout_height="739dp"
android:orientation="vertical"
android:screenOrientation="nosensor" >
<Button
android:id="#+id/pelicula_risco1"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="22dp"
android:layout_marginTop="36dp"
android:text="Take a Picture ;)"
android:textSize="30dp" />
<ImageView
android:id="#+id/imageView_risco1"
android:layout_width="9000dp"
android:layout_height="500dp"
android:layout_alignLeft="#+id/pelicula"
android:layout_marginTop="100dp"
android:layout_alignParentTop="true" >
</ImageView>
<RelativeLayout
android:id="#+id/riscoQ1_risco1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true" >
</RelativeLayout>
</RelativeLayout>
</ScrollView>
The Tab2:
<TextView android:text="risco 2"
android:padding="15dip"
android:textSize="18dip"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/text_risco2"/>
</RelativeLayout>
The Tab3:
<TextView android:text="risco 3"
android:padding="15dip"
android:textSize="18dip"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/text_risco3"/>
</RelativeLayout>
The Tab4:
<?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:id="#+id/geral_risco4">
<TextView android:text="risco 4"
android:padding="15dip"
android:textSize="18dip"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/text_risco4"/>
</RelativeLayout>
The Tab5:
<?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:id="#+id/geral_risco5" >
<TextView android:text="risco 5"
android:padding="15dip"
android:textSize="18dip"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/text_risco5"/>
</RelativeLayout>
Main Activity:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/info"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:screenOrientation="nosensor"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity"
>
</RelativeLayout>
Another screen:
<?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"
android:orientation="vertical"
android:screenOrientation="nosensor"
android:id="#+id/scroll_tela_form"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/info2"
>
<EditText
android:id="#+id/dataAnalise"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="false"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="false"
android:layout_alignWithParentIfMissing="false"
android:layout_below="#+id/textView3"
android:layout_centerInParent="false"
android:layout_centerVertical="false"
android:layout_toLeftOf="#+id/getDate"
android:ems="10" />
<TextView
android:id="#+id/horaAnaliseFixo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/horaAnalise"
android:layout_below="#+id/dataAnalise"
android:layout_marginTop="8dp"
android:text="Hora de Inicio da AnĂ¡lise"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/nomeForm"
android:layout_marginTop="40dp"
android:text="Data da AnĂ¡lise"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/nomeForm"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginTop="35dp"
android:layout_toLeftOf="#+id/getDate"
android:layout_toRightOf="#+id/textView3"
android:text="NomeForm"
android:textSize="34dp"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="#+id/horaAnalise"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/horaAnaliseFixo"
android:layout_toLeftOf="#+id/getDate"
android:ems="10" >
<requestFocus />
</EditText>
<Button
android:id="#+id/getDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/horaAnaliseFixo"
android:layout_alignParentRight="true"
android:layout_marginRight="41dp"
android:text="Get Date" />
<Button
android:id="#+id/getTime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/getDate"
android:layout_alignParentBottom="true"
android:text="Get Time" />
</RelativeLayout>
</ScrollView>
Another reason for this issue can be two (or more) layout files for the same layout stored under layout, layout-land, layout-w600dp etc. If these layouts have views of different types sharing same id, then you can get this exception two. To reproduce the exception you need to call function dispatchTakePictureIntent(), then rotate your device, then take picture and return back. Does it help?
You have used same id for two different views.
Check your layout XML & use different ids for different views.
It says that you are using the same id for different views and that the id is 0x1. That looks suspiciously like
a) somewhere a view id is expected, something else is given or
b) the R.java is generated incorrectly. Please check if theres an id of 1 in there somewhere.
Did you check the value of
#android:id/tabs
#android:id/tabcontent
?

Change Order Of FrameLayout With Java Code

By default, in my current layout, there are two TextViews at the top and one ImageView at the bottom. I would like to change this by having the ImageView to be at the top and the TextViews be at the bottom using Java. Please help.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="2" >
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:weightSum="2" >
<TextView
android:id="#+id/tvText"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#drawable/text_view"
android:gravity="center"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:textSize="13dp"
android:textStyle="bold" />
<TextView
android:id="#+id/tvText2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:layout_weight="1"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:background="#drawable/text_view"
android:textSize="13dp"
android:textStyle="bold"
/>
</LinearLayout>
</FrameLayout>
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<ImageView
android:id="#+id/ivImage"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:background="#drawable/text_view"
android:scaleType="fitXY" />
</LinearLayout>
</FrameLayout>
</LinearLayout>
Here is what i have done :i have added 2 more frame layout in xml and
when u click the first text view it will change the view i mean the
above frame layout and will show next two.Hope this helps u
public class MainActivity extends Activity
{
LinearLayout ll;
TextView tv;
FrameLayout ff2, ff1, ff3, ff4;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ff1 = (FrameLayout) findViewById(R.id.lay1);
ff2 = (FrameLayout) findViewById(R.id.lay2);
ff3 = (FrameLayout) findViewById(R.id.lay3);
ff4 = (FrameLayout) findViewById(R.id.lay4);
ff3.setVisibility(View.GONE);
ff4.setVisibility(View.GONE);
tv = (TextView) findViewById(R.id.tvText);
tv.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
ff1.setVisibility(View.GONE);
ff2.setVisibility(View.GONE);
ff3.setVisibility(View.VISIBLE);
ff4.setVisibility(View.VISIBLE);
Toast.makeText(getApplicationContext(), "layout change",
Toast.LENGTH_LONG).show();
}
});
}
}
Here is the layout file :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="2" >
<FrameLayout
android:id="#+id/lay1"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:weightSum="2" >
<TextView
android:id="#+id/tvText"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#drawable/weed2"
android:gravity="center"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:textSize="13dp"
android:textStyle="bold" />
<TextView
android:id="#+id/tvText2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#drawable/weed2"
android:gravity="center"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:textSize="13dp"
android:textStyle="bold" />
</LinearLayout>
</FrameLayout>
<FrameLayout
android:id="#+id/lay2"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<ImageView
android:id="#+id/ivImage"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:background="#drawable/ic_launcher"
android:scaleType="fitXY" />
</LinearLayout>
</FrameLayout>
<FrameLayout
android:id="#+id/lay3"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<ImageView
android:id="#+id/ivImage"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:background="#drawable/ic_launcher"
android:scaleType="fitXY" />
</LinearLayout>
</FrameLayout>
<FrameLayout
android:id="#+id/lay4"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:weightSum="2" >
<TextView
android:id="#+id/tvText"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#drawable/weed2"
android:gravity="center"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:textSize="13dp"
android:textStyle="bold" />
<TextView
android:id="#+id/tvText2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#drawable/weed2"
android:gravity="center"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:textSize="13dp"
android:textStyle="bold" />
</LinearLayout>
</FrameLayout>
</LinearLayout>

Android: Layout short within a ScrollView

I recently just overhauled my application to use ListView instead of using the classic TextView.setText and looping the inflation of a layout. I was able to acheive this very well but now I'm having a problem having it fill the screen. Heres the section of my code that does the work.
//Place JSON data into array one item at a time
JSONArray jArray = new JSONArray(result);
//Loop through each record in the database
//Get ListView
ListView lv= (ListView)findViewById(R.id.listview);
// create the grid item mapping
String[] from = new String[] {"lblQuote", "lblBuzzed", "lblShared", "lblSaid", "lblLikes", "lblHates", "lblLocation", "lblDate"};
int[] to = new int[] { R.id.lblQuote, R.id.lblBuzzed, R.id.lblShared, R.id.lblSaid, R.id.lblLikes, R.id.lblHates, R.id.lblLocation, R.id.lblDate };
for(int i=0;i<jArray.length();i++){
Log.i("uDrew Debug", "Made it into JSONArray Loop");
//Get this record
JSONObject json_data = jArray.getJSONObject(i);
//Put each result into variables for later handling
strFName = json_data.getString("FName");
strLInitial = json_data.getString("LInitial");
strCity = json_data.getString("City");
strState = json_data.getString("State");
strDate = json_data.getString("Date");
strQuote = json_data.getString("Quote");
intLikes = Integer.parseInt(json_data.getString("Likes"));
intHates = Integer.parseInt(json_data.getString("Hates"));
strFNameSaid = json_data.getString("FNameSaid");
strLInitialSaid = json_data.getString("LInitialSaid");
intBuzz = Integer.parseInt(json_data.getString("Buzz"));
Log.i("uDrew Debug", "Made it past JSON Parsing");
switch(intBuzz){
case 1:
strBuzzed = ("One Beer\nSyndrome");
break;
case 2:
strBuzzed = ("Buzzed");
break;
case 3:
strBuzzed = ("Drunk");
break;
case 4:
strBuzzed = ("Trashed");
break;
case 5:
strBuzzed = "Retarded";
break;
}
HashMap<String, String> map = new HashMap<String, String>();
map.put("lblQuote", strQuote);
map.put("lblBuzzed", strBuzzed);
map.put("lblShared", strFName + " " + strLInitial);
map.put("lblSaid",strFNameSaid + " " + strLInitialSaid);
map.put("lblDate", strDate);
map.put("lblLocation", strCity + ", " + strState);
map.put("lblLikes", intLikes.toString());
map.put("lblHates", intHates.toString());
fillMaps.add(map);
}//End For loop
// fill in the grid_item layout
SimpleAdapter adapter = new SimpleAdapter(this, fillMaps, R.layout.myviews, from, to);
lv.setAdapter(adapter);
main.xml:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:fillViewport="true"
android:scrollbars="vertical">
<LinearLayout
android:orientation="vertical"
android:id="#+id/myMainLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<com.google.ads.AdView android:id="#+id/adViewer"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
ads:adUnitId="a14de539f600385"
ads:adSize="BANNER"
ads:loadAdOnCreate="true"/>
<TextView
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:text="RandomDrunkQuotes.com"
android:id="#+id/lblTitle"
android:textSize="16px"
android:padding="5px"
android:textStyle="bold"
android:gravity="center_horizontal"/>
<!-- List Divider -->
<View android:layout_width="fill_parent"
android:layout_height="1dip"
android:background="?android:attr/listDivider" />
<!-- ListView (grid_items) -->
<LinearLayout android:id="#+id/layout"
android:layout_width="wrap_content"
android:layout_height="fill_parent">
<ListView android:id="#+id/listview"
android:layout_height="fill_parent"
android:layout_width="fill_parent">
</ListView>
</LinearLayout>
</LinearLayout>
</ScrollView>
myviews.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:id="#+id/LinearLayout01"
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/lblQuote"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Filler Text"/>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="left">
<ImageView
android:id="#+id/imgUp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0"
android:paddingLeft="5px"
android:paddingRight="5px"
android:src="#drawable/thumbsup"
android:layout_gravity="left"/>
<TextView
android:id="#+id/lblLikes"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0"
android:paddingLeft="5px"
android:paddingRight="5px"
android:textSize="4pt"
android:text="10"
android:layout_gravity="left"/>
<TextView
android:id="#+id/lblBuzzedTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="2"
android:textSize="4pt"
android:text="Buzz Level:"/>
<TextView
android:id="#+id/lblShared"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="2"
android:text="Shared By: Filler Name"
android:textSize="4pt"/>
<TextView
android:id="#+id/lblSaid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="2"
android:text="Said By: Filler Name"
android:textSize="4pt"/>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="left">
<ImageView
android:id="#+id/imgDown"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0"
android:paddingLeft="5px"
android:paddingRight="5px"
android:src="#drawable/thumbsdn"
android:layout_gravity="center_vertical"/>
<TextView
android:id="#+id/lblHates"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0"
android:paddingLeft="5px"
android:paddingRight="5px"
android:textSize="4pt"
android:text="2"
android:layout_gravity="center_vertical"/>
<TextView
android:id="#+id/lblBuzzed"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="2"
android:text="High"
android:textSize="4pt"
android:layout_gravity="center_vertical"/>
<TextView
android:id="#+id/lblDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="2"
android:textSize="4pt"
android:text="04/04/1987 4:32 PM"
android:layout_gravity="center_vertical"/>
<TextView
android:id="#+id/lblLocation"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="2"
android:textSize="4pt"
android:text="Broomfield, CO"
android:layout_gravity="center_vertical"/>
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:weightSum="1">
<TextView
android:text=" "
android:textSize="1pt"
android:background="#6F7285"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"/>
</LinearLayout>
</LinearLayout>
Heres what the App looks like:
There are also no scrollbars and if I change the main.xml ScrolView layout_height="fill_parent" it extends to the end of the screen but the scrollable option isn't there.
Use android:layout_height="fill_parent" in your ScrollView and in the first LinearLayout.
Don't wrap the ListView inside a LinearLayout (the one below the divider View). There's absolutely no need for that.
(Optional) you might want to move the AdView outside the ScollView so it is always visible and not scrolled away
The following code fixed my problem:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
android:orientation="vertical"
android:id="#+id/BigLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fillViewport="true"
android:scrollbars="vertical">
<LinearLayout
android:orientation="vertical"
android:id="#+id/myMainLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<com.google.ads.AdView android:id="#+id/adViewer"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
ads:adUnitId="a14de539f600385"
ads:adSize="BANNER"
ads:loadAdOnCreate="true"/>
<TextView
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:text="RandomDrunkQuotes.com"
android:id="#+id/lblTitle"
android:textSize="16px"
android:padding="5px"
android:textStyle="bold"
android:gravity="center_horizontal"/>
<!-- List Divider -->
<View
android:layout_width="fill_parent"
android:layout_height="1dip"
android:background="?android:attr/listDivider" />
</LinearLayout>
<!-- ListView (grid_items) -->
<ListView
android:id="#+id/listview"
android:layout_height="fill_parent"
android:layout_width="fill_parent">
</ListView>
</LinearLayout>

Categories