Error in MainActivity when implementing TabActivity - android

i want a page with text on top(EditText) then image (in FragmentLayout) and then two tabs below that image.
my output is not showing any tab in output but i have written code to implement tabs.
this code is notshowing any error and in output i mam not getting any tab in output
i am not getting correct output and this is not even showing any error
i tried it without extending TabActivity also (only with extens Activity) but still output is same whose image is loaded above
Code of activity_main.xml is
<LinearLayout 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:orientation="vertical"
tools:context=".MainActivity" >
<TextView
android:id="#+id/tbol"
android:layout_width="250dp"
android:layout_height="30dp"
android:text="#string/heading"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#F00"
android:textSize="25sp" />
<FrameLayout
android:layout_width="match_parent"
android:layout_height="160dp"
android:background="#drawable/leaf" >
</FrameLayout>
<TabHost
android:id="#android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TabWidget
android:id="#android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</TabWidget>
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</FrameLayout>
</LinearLayout>
</TabHost>
</LinearLayout>
code of MainActivity.java is
package com.example.tabhost;
import android.os.Bundle;
import android.app.TabActivity;
import android.content.Intent;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;
#SuppressWarnings("deprecation")
public class MainActivity extends TabActivity {
TabHost th;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
th=getTabHost();
//For desc tab
TabSpec descspec=th.newTabSpec("Description");
descspec.setIndicator("Description", null);
Intent firstintent=new Intent(this, DecsriptionTab.class);
descspec.setContent(firstintent);
//For Information Tab
TabSpec infospec=th.newTabSpec("Information");
infospec.setIndicator("Information", null);
Intent secondintent=new Intent(this, Info_Tab.class);
descspec.setContent(secondintent);
}
}
this is code of 2 .xml files
desc_layout.xml
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android">
<TextView android:text="This is desc tab"
android:padding="15dp"
android:textSize="18sp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
info_layout
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android">
<TextView android:text="This is info tab"
android:padding="15dp"
android:textSize="18sp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
This is code of corresponding java files
DecsriptionTab.java
package com.example.tabhost;
import android.app.Activity;
import android.os.Bundle;
public class DecsriptionTab extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.desc_layout);
}
}
Info_Tab.java
package com.example.tabhost;
import android.app.Activity;
import android.os.Bundle;
public class Info_Tab extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.info_layout);
}
}

Related

Probelm with spinner and context - Android Java

I'm having trouble launching the app (the app freezes) after adding a Spinner to it.
It probably doesn't get me "Context" properly - probably.
Maybe the problem lies elsewhere, but I cannot verify it at my stage
Before adding Spinner everything worked ok.
Git app code: https://github.com/RemekLago/project_Factory2.git
I will be grateful for any help.
code of main activity:
package com.example.projecttech_v4;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentManager;
import androidx.viewpager2.widget.ViewPager2;
import android.os.Bundle;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import com.google.android.material.tabs.TabLayout;
public class MainActivity extends AppCompatActivity {
private TabLayout tabLayout;
private ViewPager2 viewPager2;
private PagerAdapter adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tabLayout = findViewById(R.id.tabLayout);
viewPager2 = findViewById(R.id.viewPager2);
tabLayout.addTab(tabLayout.newTab().setText("Maszyna01"));
tabLayout.addTab(tabLayout.newTab().setText("Maszyna02"));
tabLayout.addTab(tabLayout.newTab().setText("Maszyna03"));
tabLayout.addTab(tabLayout.newTab().setText("Maszyna04"));
tabLayout.addTab(tabLayout.newTab().setText("Maszyna05"));
tabLayout.addTab(tabLayout.newTab().setText("Maszyna06"));
FragmentManager fragmentManager = getSupportFragmentManager();
adapter = new PagerAdapter(fragmentManager, getLifecycle());
viewPager2.setAdapter(adapter);
tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
#Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager2.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(TabLayout.Tab tab) {
}
#Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
viewPager2.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
#Override
public void onPageSelected(int position){
tabLayout.selectTab(tabLayout.getTabAt(position));
}
});
Spinner spinner1 = findViewById(R.id.spinner1_operator);
ArrayAdapter<CharSequence> adapter1 = ArrayAdapter.createFromResource
(this, R.array.operator, android.R.layout.simple_spinner_item);
adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner1.setAdapter(adapter1);
spinner1.setOnItemSelectedListener((AdapterView.OnItemSelectedListener) this);
}
}
code: XML mainactivity:
<?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">
<TableRow
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginTop="10dp"
android:layout_marginLeft="6dp"
android:layout_marginRight="6dp"
android:orientation="vertical">
<Spinner
android:id="#+id/spinner1_operator"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" />
<Spinner
android:id="#+id/spinner2_data"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" />
<Spinner
android:id="#+id/spinner3_godzina"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" />
<Button
android:id="#+id/button_zatwierdz_operator"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Zatwierdź"
android:textSize="11sp"
android:textColor="#color/black"
android:layout_weight="1"/>
</TableRow>
<com.google.android.material.tabs.TabLayout
android:id="#+id/tabLayout"
android:layout_width="match_parent"
android:layout_height="50dp"
app:tabGravity="fill"
app:tabIndicatorAnimationMode="elastic"
app:tabIndicatorGravity="stretch"
app:tabMode="scrollable"
app:tabSelectedTextColor="#color/white" />
<androidx.viewpager2.widget.ViewPager2
android:id="#+id/viewPager2"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</LinearLayout>
TableRow should be a child of TableLayout only. exchange <TableRow to <LinearLayout with android:orientation="horizontal" attribute in your XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<!-- TableRow becomes LinearLayout -->
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginTop="10dp"
android:layout_marginLeft="6dp"
android:layout_marginRight="6dp">
<!-- spinners and rest of code -->
powodzenia

How do i display a ListView with a header below another ListView all in Tab1 of TabHost?

I'm trying to put a ListView below another ListView in the first tab using tabhost.
The reason i want it like this is because i want to divide a list of trading cards by type so i guess creating sections or whatever.
Here is my .xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical" >
<TextView
android:id="#+id/banlistdate"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#000000"
android:text="Banlist Date: March, 2013"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#FFFFFF" />
<TabHost
android:id="#+id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#id/banlistdate" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TabWidget
android:id="#android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</TabWidget>
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:id="#+id/tab1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<ListView
android:id="#+id/fLV1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FF8B53"
tools:listheader="#layout/banlist_header_effectmonsters" >
</ListView>
<ListView
android:id="#+id/fLV2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#a086b7"
tools:listheader="#layout/banlist_header_fusionmonsters" >
</ListView>
</LinearLayout>
<LinearLayout
android:id="#+id/tab2"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ListView
android:id="#+id/flv3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" >
</ListView>
</LinearLayout>
<LinearLayout
android:id="#+id/tab3"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ListView
android:id="#+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1" >
</ListView>
</LinearLayout>
</FrameLayout>
</LinearLayout>
</TabHost>
</LinearLayout>
Here is my the main java file to start the tabs and setup the ListViews:
package cybertech.productions.yugiohlibrary;
import java.util.ArrayList;
import java.util.Arrays;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;
import android.widget.TextView;
public class Banlist extends Activity implements OnClickListener {
TabHost th;
Initializers initialMngr;
ArrayAdapter<String> listAdapter;
ListView forbiddenEMListView, forbiddenFListView;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.banlist);
th = (TabHost) findViewById(R.id.tabhost);
th.setup();
TabSpec specs = th.newTabSpec("tag1");
specs.setContent(R.id.tab1);
specs.setIndicator("Forbidden");
th.addTab(specs);
specs = th.newTabSpec("tag2");
specs.setContent(R.id.tab2);
specs.setIndicator("Semi-Limited");
th.addTab(specs);
specs = th.newTabSpec("tag3");
specs.setContent(R.id.tab3);
specs.setIndicator("Limited");
th.addTab(specs);
th.setCurrentTab(0);
// #BEGIN: Tab 1 ListViews
//Setup the "forbidden" ListView(s);
// ListView 1: Effect Monsters
forbiddenEMListView = (ListView) findViewById(R.id.fLV1);
ArrayList<String> forbiddenEMList = new ArrayList<String>();
forbiddenEMList.addAll(Arrays.asList(initialMngr.forbiddenEM));
listAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, forbiddenEMList);
View effectMon_header = View.inflate(this, R.layout.banlist_header_effectmonsters, null);
forbiddenEMListView.addHeaderView(effectMon_header);
forbiddenEMListView.setAdapter(listAdapter);
// ListView 2: Fusion Monsters
forbiddenFListView = (ListView) findViewById(R.id.fLV2);
ArrayList<String> forbiddenFList = new ArrayList<String>();
forbiddenFList.addAll(Arrays.asList(initialMngr.forbiddenF));
listAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, forbiddenFList);
View fusion_header = View.inflate(this, R.layout.banlist_header_fusionmonsters, null);
forbiddenFListView.addHeaderView(fusion_header);
forbiddenFListView.setAdapter(listAdapter);
// #END: Tab 1 ListViews
}
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
}
}
Thanks for any help.
You want to use an ExpandableListView. Use the group views to label the different categories or sections. Since you are using Arrays to back your lists, SimpleExpandableListAdapter should fit the bill nicely.

Android code to zoom and pan a Relative layout which contains text boxes, buttons, spinners, etc

I have zoomed into the layout easily but I also want to scroll the zoomed layout. I have been able to zoom but I want to also have scrolling after I zoom so that I can edit the values of the Widgets
Is there any way to do this ?
import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.RelativeLayout;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;
public class MainActivity extends TabActivity {
View mainview;
int zoomed = 0;
TabHost th;
TabSpec one;
TabSpec two;
TabSpec three;
TabSpec four;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mainview = (RelativeLayout) findViewById(R.id.relLayout1);
th = getTabHost();
th.setup();
one = th.newTabSpec("One");
two = th.newTabSpec("Two");
three = th.newTabSpec("Three");
four = th.newTabSpec("Four");
one.setIndicator("One");
two.setIndicator("Two");
three.setIndicator("Three");
four.setIndicator("Four");
Intent a = new Intent(this, first.class);
Intent b = new Intent(this, second.class);
Intent c = new Intent(this, third.class);
Intent d = new Intent(this, fourth.class);
one.setContent(a);
two.setContent(b);
three.setContent(c);
four.setContent(d);
th.addTab(one);
th.addTab(two);
th.addTab(three);
th.addTab(four);
mainview.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
mainview.setPivotX(100);
mainview.setPivotY(100);
if (zoomed == 0) {
mainview.setScaleX(2f);
mainview.setScaleY(2f);
zoomed=1;
}
else{
mainview.setScaleX(1f);
mainview.setScaleY(1f);
zoomed=0;
}
}
});
}
}
And this is my layout :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/relLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<TabHost
android:id="#android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="220dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TabWidget
android:id="#android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</TabWidget>
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:id="#+id/tab1"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</LinearLayout>
<LinearLayout
android:id="#+id/tab2"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</LinearLayout>
<LinearLayout
android:id="#+id/tab3"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</LinearLayout>
<LinearLayout
android:id="#+id/tab4"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</LinearLayout>
</FrameLayout>
</LinearLayout>
</TabHost>
</RelativeLayout>

Android Fragment behaving weird

The activity_main.xml is like this
<LinearLayout 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" >
<Button
android:id="#+id/button_one_activity_one"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="First Button"
/>
<fragment
android:name="fragments.FirstFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/first_fragment" />
<Button
android:id="#+id/button_two_activity_one"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Second Button"
/>
</LinearLayout>
The main activity class is like this
package com.example.testfragmentshoneycomb;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
public class MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
first_fragment.xml is like this
<?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="wrap_content"
android:orientation="vertical"
android:background="#color/grey" >"
<TextView
android:id="#+id/text_view_one_fragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Text View one" />
<TextView
android:id="#+id/text_view_two_fragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Text View two" />
<TextView
android:id="#+id/text_view_three_fragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Text View three" />
</LinearLayout>
FirstFragment class is like this
package fragments;
import com.example.testfragmentshoneycomb.R;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class FirstFragment extends Fragment{
#Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.first_fragment, container, false);
return view;
}
}
It shows only first button and nothing else on the screen. If I remove the first button from activity_main.xml it shows the fragment but doesn't show the second button.
Min SDK version is 11 and build target is android 4.1
set android:orientation="vertical" in your activity layout.
Its because by default LinearLayout's orientation is horizontal. Therefore the whole screens width is captured by the First Button and Fragment.
Are you sure you want to see it like?
First_Button Fragment Second_Button
If Yes the use layout_weight. If No then give orientation=vertical to LinearLayout which will show your layout output as
First_Button
Fragment
Second_Button
set LinearLayout orientation to vertical, it's horizontal by default.
read docs carefully
Use the following layout :
<LinearLayout 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:orientation="vertical" >
<Button
android:id="#+id/button_one_activity_one"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="First Button"
/>
<fragment
android:name="fragments.FirstFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/first_fragment" />
<Button
android:id="#+id/button_two_activity_one"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Second Button"
/>
</LinearLayout>

Android Activity not working

I have this activity,
package org.dewsworld.ui;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class Main extends Activity {
TextView textView ;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
textView = (TextView) findViewById(R.id.textView1) ;
textView.setText("hello ") ;
}
}
And main.xml is
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:id="#+id/textView1">
<TextView android:layout_height="wrap_content" android:id="#+id/textView1"
android:layout_width="wrap_content" android:text="TextView" />
</LinearLayout>
And the logcat is
And I can't find the error... :(
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView android:layout_height="wrap_content" android:id="#+id/textView1"
android:layout_width="wrap_content" android:text="TextView" />
</LinearLayout>
try now
You have 2 views with the same ids Linearlayout and TextView. remove the id for the LinearLayout and try it again. It will work.

Categories

Resources