I'm using android 2.3.3. I've created two tab indicators using custom layout, but there is always a black line between them. Below is the result screen.
I want to delete or hide the two black lines. I think those two are tab dividers, so I using
tabHost.getTabWidget().setDividerDrawable(null)
to delete divider, but nothing happens.
Below is my code:
package com.intasect.htfutures.activities;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.TabWidget;
import com.intasect.core.tab.TabMainActivity;
import com.intasect.htfutures.activities.competitionpk.PkActivityGroup;
import com.intasect.htfutures.activities.home.HomeActivityGroup;
import com.intasect.htfutures.utils.Const;
public class MainActivity extends TabMainActivity {
int currentTabId = 0;
public static TabHost tabHost;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tabHost = getTabHost();
final TabWidget tabwidges = tabHost.getTabWidget();
Intent intent;
// 首页
intent = new Intent(this, HomeActivityGroup.class);
tabHost.addTab(tabHost.newTabSpec(String.valueOf(TAB_ID_HOME))
.setIndicator(createView(TAB_ID_HOME, true)).setContent(intent));
// 业绩PK
intent = new Intent(this, PkActivityGroup.class);
tabHost.addTab(tabHost.newTabSpec(String.valueOf(TAB_ID_PK))
.setIndicator(createView(TAB_ID_PK)).setContent(intent));
// 设置
// intent = new Intent(this, SettingsActivityGroup.class);
// tabHost.addTab(tabHost.newTabSpec(String.valueOf(TAB_ID_SETTINGS))
// .setIndicator(createView(TAB_ID_SETTINGS)).setContent(intent));
/*
* 设置tab监听事件,改变背景颜色
*/
tabHost.setOnTabChangedListener(new OnTabChangeListener() {
#Override
public void onTabChanged(String tabId) {
currentTabId = tabHost.getCurrentTab();
chooseTab(tabwidges, currentTabId);
}
});
tabHost.setCurrentTab(currentTabId);
Const.initApp();
}
private static final int TAB_ID_HOME = 0;
private static final int TAB_ID_PK = 1;
// private static final int TAB_ID_SETTINGS = 2;
private static final int TOTAL_TAB_COUNT = 2;
private static final int[] IMAGE_IDS = { R.drawable.btn_tab_home,
R.drawable.btn_tab_home_selected, R.drawable.btn_tab_query,
R.drawable.btn_tab_query_selected };
private View createView(int tabId) {
return createView(tabId, false);
}
private View createView(int tabId, boolean choosed) {
ImageButton image = new ImageButton(this);
changeTabImage(image, tabId, choosed);
return image;
}
private void changeTabImage(ImageButton image, int tabId, boolean choosed) {
image.setImageResource(choosed ? IMAGE_IDS[tabId * 2 + 1]
: IMAGE_IDS[tabId * 2]);
image.setBackgroundColor(choosed ? Color.WHITE : getResources().getColor(R.color.tab_item_gray));
}
private void chooseTab(TabWidget tabwidges, int tabId) {
resetAllTabToUnselected(tabwidges);
ImageButton image = (ImageButton) tabwidges.getChildAt(tabId);
changeTabImage(image, tabId, true);
}
private void resetAllTabToUnselected(TabWidget tabwidges) {
for (int i = 0; i < TOTAL_TAB_COUNT; i++) {
ImageButton image = (ImageButton) tabwidges.getChildAt(i);
changeTabImage(image, i, false);
}
}
}
my layout xml:
<?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"
android:background="#android:color/white" >
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingBottom="66dip" />
<TabWidget
android:id="#android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:layout_margin="0dp"
android:divider="#null"
android:fadingEdge="none"
android:fadingEdgeLength="0dp"
android:padding="0dp"
android:tabStripEnabled="false" />
</TabHost>
I've tried many ways to delete those two lines, but there is no miracle. How can I remove them?
i figured out by myself.i'm really sorry to ask such a stupid question.that black line is not divider,it is on my picture.i delete that line in my picture,every thing goes perfect.
add these lines in xml for Tabhost|TabWidget
android:divider="#null"
android:fadingEdge="none"
android:fadingEdgeLength="0dp"
Related
Does anyone have any idea how I can save the dynamically generated Textviews? So, the next time I open the app, the TextViews will display the same content that I previously put into them.
I have looked into onSaveInstanceState, onRestoreInstanceState but None of them work. Are there any libraries I can use to save generated TextViews? Or maybe any advice on a better method of doing this. I've researched this stuff for a couple of days and have found outdated information and information that doesn't seem relevant to me. Code solutions will be very much appreciated
MainActivity.java:
package com.example.savingdynamiccontent;
import android.app.Activity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioGroup;
import android.widget.TextView;
import java.util.ArrayList;
public class MainActivity extends Activity {
private LinearLayout mLayout;
private LinearLayout ListItems;
static final String counter_value = "int_value";
static final String toDoList_value = "toDolist";
private EditText mEditText;
private Button mButton;
private int counter;
private ArrayList<String> toDoList;
private ArrayList<String> keys;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
if (toDoList != null){
System.out.println("Success!");
for(int i = 0; i< toDoList.size(); i++){
System.out.println(toDoList.get(i));
ListItems.addView(createNewTextView(toDoList.get(i)));
}
}
else{
System.out.println("Nope!");
toDoList = new ArrayList<String>();
}
counter = 1;
mLayout = (LinearLayout) findViewById(R.id.linearLayout);
ListItems = (LinearLayout) findViewById(R.id.listItems);
mEditText = (EditText) findViewById(R.id.editText);
mButton = (Button) findViewById(R.id.button);
mButton.setOnClickListener(onClick());
TextView textView = new TextView(this);
textView.setText("New text");
}
private View.OnClickListener onClick() {
return new View.OnClickListener() {
#Override
public void onClick(View v) {
ListItems.addView(createNewTextView(mEditText.getText().toString()));
}
};
}
private TextView createNewTextView(String text) {
final RadioGroup.LayoutParams lparams = new RadioGroup.LayoutParams(RadioGroup.LayoutParams.WRAP_CONTENT, RadioGroup.LayoutParams.WRAP_CONTENT);
final TextView textView = new TextView(this);
textView.setLayoutParams(lparams);
textView.setFreezesText(true);
textView.setText(counter + ":" + text);
textView.setId(counter);
System.out.println(textView.getId());
toDoList.add(text);
counter++;
return textView;
}
#Override
protected void onRestoreInstanceState(Bundle savedInstanceState){
super.onRestoreInstanceState(savedInstanceState);
counter = savedInstanceState.getInt(counter_value);
toDoList = savedInstanceState.getStringArrayList("key");
// Add this for-loop to restoring your list
for(String str : toDoList){
ListItems.addView(createNewTextView(str));
}
//REad values from the savedInstanceState" -object and put them in your textview
}
#Override
protected void onSaveInstanceState(Bundle outState){
// Save the values you need from your textview into "outSTate" -object
// outState.putParcelableArrayList("key", toDoList);
outState.putInt(counter_value, counter);
outState.putStringArrayList("key", toDoList);
super.onSaveInstanceState(outState);
}
}
Activit_Main.xml:
<?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/linearLayout"
android:weightSum="1">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:id="#+id/listItems"></LinearLayout>
<EditText
android:id="#+id/editText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add+"
/>
</LinearLayout>
I have viewpager in which each fragment has videoview now I want to show buttons over that view pager. I am using frame layout for that. but still not able to make those buttons visible overview pager. only fragments in my view pager are visible but the buttons which I want to show over viewpager pager are not getting visible.
when activity starts at that time buttons are shown for 2.3 secs when as soon as video in fragment become visible that buttons are not visible
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:custom="http://schemas.android.com/apk/res-auto"
tools:context=".Activities.MenuButtonActivity">
<com.abp.app.ViewPager.NonSwipeableViewPager
android:id="#+id/viewpager"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<RelativeLayout
android:id="#+id/rl_bottom_overlay"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:text="yyyyyyyy"
android:visibility="visible" >
<Button
android:id="#+id/btn_left_arrow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="35dp"
android:text="yyyyyyyy" />
<Button
android:id="#+id/btn_below_share"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="35dp"
android:text="yyyyyyyy"
android:visibility="visible" />
<Button
android:id="#+id/btn_right_arrow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_marginLeft="50dp"
android:text="yyyyyyyy"
android:layout_toRightOf="#id/btn_left_arrow" />
</RelativeLayout>
</FrameLayout>
here is the code of acitivity
package com.abp.app.Activities;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.abp.app.Adapter.MyPageAdapter;
import com.abp.app.CallerFuntionClasses.FunctionsCallApiFromServer;
import com.abp.app.Fragments.ConnectivityFragment;
import com.abp.app.Fragments.LondonRoyalDocksFragment;
import com.abp.app.Fragments.MasterPlanFragment;
import com.abp.app.Fragments.OfficesFragment;
import com.abp.app.Fragments.ProjectTeamFragment;
import com.abp.app.Fragments.VideosFragment;
import com.abp.app.R;
import com.abp.app.Utilities.AppVeriableClass;
import com.abp.app.Utilities.CustomSharePreferences;
import com.abp.app.Utilities.UtilsForDownloadDataFromServer;
import com.abp.app.ViewPager.NonSwipeableViewPager;
import java.util.ArrayList;
import java.util.List;
import static android.widget.RelativeLayout.ABOVE;
public class MenuButtonActivity extends AppCompatActivity {
private Context context;
private Activity activity;
private int BtnHeight = 0;
private ProgressBar progressBar;
private TextView tv_progressCounting;
private static String dirPath;
boolean doubleBackToExit = false;
FrameLayout frameLayout;
RelativeLayout.LayoutParams params;
FunctionsCallApiFromServer apiFromServer;
String accessTokenType,accessToken;
SharedPreferences sharedPreferences;
CustomSharePreferences customSharePreferences = new CustomSharePreferences();
ConnectivityManager conMgr;
NetworkInfo netInfo;
ArrayList<String> test;
///////////// Fragments //////////
MasterPlanFragment masterPlanFragment ;
OfficesFragment officesFragment;
ConnectivityFragment connectivityFragment ;
VideosFragment videosFragment ;
LondonRoyalDocksFragment londonRoyalDocksFragment;
ProjectTeamFragment projectTeamFragment ;
MyPageAdapter pageAdapter;
com.abp.app.ViewPager.NonSwipeableViewPager viewpager;
////////////////////////////////////
private android.support.v4.app.FragmentTransaction fragmentTransaction;
final Handler handler = new Handler();
private static final int[] menuBtnIdsArray = {R.id.btn_masterplan,R.id.btn_Offices,R.id.btn_connectivity,R.id.btn_londonroyaldock,R.id.btn_videos,R.id.btn_projectteam};
private ArrayList<Button> menuBtnArrayList = new ArrayList<Button>();
private int previousClickedButton = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_manu_buttons);
viewpager = (com.abp.app.ViewPager.NonSwipeableViewPager)findViewById(R.id.viewpager);
viewpager.setPagingEnabled(false);
List fragments = getFragmetns();
pageAdapter = new MyPageAdapter( getSupportFragmentManager(),getFragmetns());
viewpager.setAdapter(pageAdapter);
VariableInitialize();
// CallAPIFromServer();
hideStatusBar();
// onclickBtnFrangment();
// firstTimeAlwaysCallFragment();
}
private void firstTimeAlwaysCallFragment() {
frameLayout.setLayoutParams(new RelativeLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT,FrameLayout.LayoutParams.MATCH_PARENT));
setFragment(masterPlanFragment);
menuBtnArrayList.get(0).setBackgroundColor(menuBtnArrayList.get(0).getContext().getResources().getColor(R.color.button_pressed));
}
private void VariableInitialize(){
context = MenuButtonActivity.this;
activity = MenuButtonActivity.this;
////////////// Fragment Initialize/////////////////
masterPlanFragment = new MasterPlanFragment();
officesFragment = new OfficesFragment();
connectivityFragment = new ConnectivityFragment();
londonRoyalDocksFragment = new LondonRoyalDocksFragment();
videosFragment = new VideosFragment();
projectTeamFragment = new ProjectTeamFragment();
////////////////////////////////
conMgr = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
netInfo= conMgr.getActiveNetworkInfo();
apiFromServer = new FunctionsCallApiFromServer();
sharedPreferences = context.getSharedPreferences("AccessToken",Context.MODE_PRIVATE);
accessToken = sharedPreferences.getString("Token",null);
accessTokenType = sharedPreferences.getString("Type",null);
dirPath = UtilsForDownloadDataFromServer.getRootDirPath(getApplicationContext());
// frameLayout = (FrameLayout)findViewById(R.id.mainContainter_fragment);
params = new RelativeLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT,FrameLayout.LayoutParams.MATCH_PARENT);
//tv_progressCounting = (TextView)findViewById(R.id.textViewProgressCount);
//progressBar = (ProgressBar)findViewById(R.id.progressbar);
}
private void onclickBtnFrangment(){
for (int i = 0; i< menuBtnIdsArray.length; i++){
final int currentBtnSelected = i;
menuBtnArrayList.add((Button)findViewById( menuBtnIdsArray[i]));
try {
menuBtnArrayList.get(currentBtnSelected).setText(AppVeriableClass.getInstance().menuDisplayNames.get(i));
} catch (Exception e) {
e.printStackTrace();
}
menuBtnArrayList.get(i).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
switch (menuBtnArrayList.get(currentBtnSelected).getId()) {
case R.id.btn_masterplan:
setFragment(masterPlanFragment);
frameLayout.setLayoutParams(new RelativeLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT,FrameLayout.LayoutParams.MATCH_PARENT));
selectedBtnColorChange(currentBtnSelected);
break;
case R.id.btn_Offices:
setFragment(officesFragment);
frameLayout.setLayoutParams(new RelativeLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT,FrameLayout.LayoutParams.MATCH_PARENT));
selectedBtnColorChange(currentBtnSelected);
break;
case R.id.btn_connectivity:
setFragment(connectivityFragment);
frameLayout.setLayoutParams(new RelativeLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT,FrameLayout.LayoutParams.MATCH_PARENT));// display full screen
selectedBtnColorChange(currentBtnSelected);
break;
case R.id.btn_londonroyaldock:
setFragment(londonRoyalDocksFragment);
// params.addRule(ABOVE,R.id.linearlayout); // display show above on the menu bar as per the requirement.
frameLayout.setLayoutParams(params);
selectedBtnColorChange(currentBtnSelected);
break;
case R.id.btn_videos:
setFragment(videosFragment);
// params.addRule(ABOVE,R.id.linearlayout); // display show above on the menu bar as per the requirement.
frameLayout.setLayoutParams(params);
selectedBtnColorChange(currentBtnSelected);
break;
case R.id.btn_projectteam:
setFragment(projectTeamFragment);
frameLayout.setLayoutParams(new RelativeLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT,FrameLayout.LayoutParams.MATCH_PARENT));
selectedBtnColorChange(currentBtnSelected);
break;
}
}
});
}
}
// After click the menu button this function change the color of button until you press the other button.
private void selectedBtnColorChange(int current) {
menuBtnArrayList.get(previousClickedButton).setBackgroundColor(menuBtnArrayList.get(previousClickedButton).getContext().getResources().getColor(R.color.button_default));
menuBtnArrayList.get(previousClickedButton).setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.MATCH_PARENT));
menuBtnArrayList.get(current).setBackgroundColor(menuBtnArrayList.get(current).getContext().getResources().getColor(R.color.button_pressed));
if (BtnHeight == menuBtnArrayList.get(current).getHeight()){
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, BtnHeight);
menuBtnArrayList.get(current).setLayoutParams(params);
}
else{
BtnHeight = menuBtnArrayList.get(current).getHeight() + 10;
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, BtnHeight);
menuBtnArrayList.get(current).setLayoutParams(params);
}
previousClickedButton = current;
}
private void hideStatusBar(){
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
}
#Override
public void onBackPressed(){
if (doubleBackToExit){
super.onBackPressed();
return;
}
else
Toast.makeText(context, "Click again to exit", Toast.LENGTH_SHORT).show();
this.doubleBackToExit = true;
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
doubleBackToExit = false;
}
},2000);
}
private void CallAPIFromServer(){
if (netInfo != null) {
apiFromServer.getMediaResourceOffices(context, activity, accessTokenType, accessToken);
apiFromServer.getMediaResourceConnectivity(context, accessTokenType, accessToken);
apiFromServer.getMediaResourceLondonRoyalDock(context, accessTokenType, accessToken);
apiFromServer.getMediaResouceVideo(context, accessTokenType, accessToken);
}
}
public void onResume() {
super.onResume();
conMgr = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
netInfo= conMgr.getActiveNetworkInfo();
}
private void setFragment(Fragment fragment){
fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.mainContainter_fragment,fragment);
fragmentTransaction.commit();
}
private List<Fragment> getFragmetns()
{
List<Fragment> flist = new ArrayList<>();
flist.add(MasterPlanFragment.NewIntance());
flist.add(OfficesFragment.NewIntance());
flist.add(ConnectivityFragment.NewIntance());
flist.add(VideosFragment.NewIntance());
flist.add(ProjectTeamFragment.NewIntance());
return flist;
}
}
Try this solution.
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Activities.MenuButtonActivity">
<com.abp.app.ViewPager.NonSwipeableViewPager
android:id="#+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<LinearLayout
android:id="#+id/rl_bottom_overlay"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:orientation="horizontal"
android:text="yyyyyyyy"
android:visibility="visible">
<Button
android:id="#+id/btn_left_arrow"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="yyyyyyyy"/>
<Button
android:id="#+id/btn_below_share"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="yyyyyyyy"
android:visibility="visible"/>
<Button
android:id="#+id/btn_right_arrow"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="yyyyyyyy"/>
</LinearLayout>
</FrameLayout>
And the result is.
I'm working with tabs in android, according to my requirement I need to open a popup containing 5 tabs. That is, I have a Fragment where I have to open a DialogFragment and this DialogFragment need to show my 5 tabs. So far so quiet! Could enter each content on their respective tabs.
But the problem is that when I change the tab, the values that were entered in another tab are clean. example:
I go into tab1 fills any value in a text field and then when I switch to tab2 tab1 back to the value it had before entered is lost.
Given this scenario, how do I retain the values that were filled to the brim change? Follow the code below to explain further.
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.TabHost.TabSpec;
import android.widget.TextView;
import br.com.company.R;
import br.com.company.beans.Ordem;
import br.com.company.dispatcher.IDispacher;
import br.com.company.fragment.helper.IFragmentCo;
import br.com.company.bo.IClasseBO;
public class TabsFragment extends DialogFragment implements OnTabChangeListener {
private static final String TAG = "FragmentTabs";
public static final String TAB_DADOS_CLIENTE = "dadosCliente";
public static final String TAB_DEFEITO_FALHA = "defeitoFalha";
public static final String TAB_SERVICOS = "servico";
public static final String TAB_MATERIAIS = "material";
public static final String TAB_OBSERVACAO = "observacao";
private View mRoot;
private Button enviar;
private Button cancelar;
private Spinner classe;
private TabHost mTabHost;
private Ordem ordem;
private IClasseBO classeBO;
private IDispacher dispatch;
private IFragmentCo ifrag;
private int mCurrentTab;
public TabsFragment(IFragmentCo ifrag, Ordem ordem) {
this.ordem = ordem;
this.ifrag = ifrag;
}
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
mRoot = inflater.inflate(R.layout.ordem_encerramento, null);
enviar = (Button) mRoot.findViewById(R.ordem_encerramento.enviar);
cancelar = (Button) mRoot.findViewById(R.ordem_encerramento.cancelar);
cancelar.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
TabsFragment.this.dismiss();
}
});
mTabHost = (TabHost) mRoot.findViewById(android.R.id.tabhost);
setupTabs();
return mRoot;
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
setRetainInstance(true);
mTabHost.setOnTabChangedListener(this);
mTabHost.setCurrentTab(mCurrentTab);
mTabHost.getTabContentView().addView(addView(R.layout.dados_cliente));
}
private void setupTabs() {
mTabHost.setup(); // importante!
mTabHost.addTab(newTab(TAB_DADOS_CLIENTE, R.string.tab_dados_cliente, R.ordem_encerramento.dados_cliente));
mTabHost.addTab(newTab(TAB_DEFEITO_FALHA, R.string.tab_defeito_falha, R.ordem_encerramento.defeito_falha));
mTabHost.addTab(newTab(TAB_SERVICOS, R.string.tab_servico, R.ordem_encerramento.servico));
mTabHost.addTab(newTab(TAB_MATERIAIS, R.string.tab_material, R.ordem_encerramento.material));
mTabHost.addTab(newTab(TAB_OBSERVACAO, R.string.tab_observacao, R.ordem_encerramento.observacao));
}
private View addView(int resource) {
LayoutInflater layoutInflater = (LayoutInflater)getActivity().getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = layoutInflater.inflate(resource, null);
return view;
}
private TabSpec newTab(String tag, int labelId, int tabContentId) {
Log.d(TAG, "buildTab(): tag=" + tag);
View view = LayoutInflater.from(mTabHost.getContext()).inflate(R.layout.tabs_bg_view, null);
TextView tv = (TextView) view.findViewById(R.id.tabsText);
tv.setText(labelId);
TabSpec tabSpec = mTabHost.newTabSpec(tag);
tabSpec.setIndicator(view);
tabSpec.setContent(tabContentId);
return tabSpec;
}
#Override
public void onResume() {
super.onResume();
}
#Override
public void onTabChanged(String tabId) {
Log.d(TAG, "onTabChanged(): tabId=" + tabId);
View view = null;
if (TAB_DADOS_CLIENTE.equals(tabId)) {
mTabHost.getTabContentView().removeAllViews();
mTabHost.getTabContentView().addView(addView(R.layout.dados_cliente));
mTabHost.setCurrentTab(0);
} else if (TAB_DEFEITO_FALHA.equals(tabId)) {
view = new DefeitoFalhaView().createView(getActivity());
mTabHost.getTabContentView().addView(view);
mTabHost.setCurrentTab(1);
} else if (TAB_SERVICOS.equals(tabId)) {
mTabHost.getTabContentView().removeAllViews();
mTabHost.getTabContentView().addView(addView(R.layout.servico));
mCurrentTab = 2;
} else if (TAB_MATERIAIS.equals(tabId)) {
mTabHost.getTabContentView().removeAllViews();
mTabHost.getTabContentView().addView(addView(R.layout.materiais));
mCurrentTab = 3;
} else if (TAB_OBSERVACAO.equals(tabId)) {
mTabHost.getTabContentView().removeAllViews();
mTabHost.getTabContentView().addView(addView(R.layout.observacao));
mCurrentTab = 4;
}
}
public View getmRoot() {
return mRoot;
}
}
File xml.
<?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:background="#color/White"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="30dip"
android:layout_gravity="center_vertical"
android:orientation="horizontal"
android:paddingRight="10dip" >
<TextView
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:gravity="center_vertical"
android:paddingLeft="10dip"
android:text="Devolver OS"
android:textColor="#color/Black" />
<View
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" />
<Button
android:id="#+ordem_encerramento/cancelar"
style="#style/ButtonNovo"
android:layout_marginLeft="10dip"
android:text="Cancelar" />
<Button
android:id="#+ordem_encerramento/enviar"
style="#style/ButtonNovo"
android:layout_marginLeft="10dip"
android:text="Enviar" />
</LinearLayout>
<View
android:layout_width="fill_parent"
android:layout_height="1dip"
android:background="#xml/menu_bg" />
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginBottom="40dip" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:padding="10dip" >
<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"
android:background="#EFEFEF" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<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" >
<FrameLayout
android:id="#+ordem_encerramento/dados_cliente"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<FrameLayout
android:id="#+ordem_encerramento/defeito_falha"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<FrameLayout
android:id="#+ordem_encerramento/servico"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<FrameLayout
android:id="#+ordem_encerramento/material"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<FrameLayout
android:id="#+ordem_encerramento/observacao"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</FrameLayout>
</LinearLayout>
</TabHost>
</LinearLayout>
</ScrollView>
</LinearLayout>
Try storing those values somewhere e.g.SharedPreferences or static variable and setting them back in onTabChanged().
TabGroupActivity.java
import java.util.ArrayList;
import com.data.DataClass;
import android.app.Activity;
import android.app.ActivityGroup;
import android.app.LocalActivityManager;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Window;
/**
* The purpose of this Activity is to manage the activities in a tab. Note:
* Child Activities can handle Key Presses before they are seen here.
*
* #author Eric Harlow
*/
public class TabGroupActivity extends ActivityGroup {
private ArrayList<String> mIdList;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (mIdList == null)
mIdList = new ArrayList<String>();
}
/**
* This is called when a child activity of this one calls its finish method.
* This implementation calls {#link LocalActivityManager#destroyActivity} on
* the child activity and starts the previous activity. If the last child
* activity just called finish(),this activity (the parent), calls finish to
* finish the entire group.
*/
#Override
public void finishFromChild(Activity child) {
LocalActivityManager manager = getLocalActivityManager();
int index = mIdList.size() - 1;
if (index < 1) {
finish();
return;
}
manager.destroyActivity(mIdList.get(index), true);
mIdList.remove(index);
index--;
String lastId = mIdList.get(index);
Intent lastIntent = manager.getActivity(lastId).getIntent();
Window newWindow = manager.startActivity(lastId, lastIntent);
setContentView(newWindow.getDecorView());
}
/**
* Starts an Activity as a child Activity to this.
*
* #param Id
* Unique identifier of the activity to be started.
* #param intent
* The Intent describing the activity to be started.
* #throws android.content.ActivityNotFoundException.
*/
public void startChildActivity(String Id, Intent intent) {
Window window = getLocalActivityManager().startActivity(Id,
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
if (window != null) {
mIdList.add(Id);
setContentView(window.getDecorView());
}
}
/**
* The primary purpose is to prevent systems before
* android.os.Build.VERSION_CODES.ECLAIR from calling their default
* KeyEvent.KEYCODE_BACK during onKeyDown.
*/
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
// preventing default implementation previous to
// android.os.Build.VERSION_CODES.ECLAIR
// onBackPressed();//Added after
return true;
}
return super.onKeyDown(keyCode, event);
}
/**
* Overrides the default implementation for KeyEvent.KEYCODE_BACK so that
* all systems call onBackPressed().
*/
#Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
onBackPressed();
return true;
}
return super.onKeyUp(keyCode, event);
}
/**
* If a Child Activity handles KeyEvent.KEYCODE_BACK. Simply override and
* add this method.
*/
public void onBackPressed() {
int length = mIdList.size();
if (length > 1) {
Activity current = getLocalActivityManager().getActivity(
mIdList.get(length - 1));
if (DataClass.isTemp()) {
overridePendingTransition(R.anim.slide_in_up,
R.anim.slide_in_up);
}
current.finish();
}
}
}
MainTab.java
import com.data.DataClass;
import android.app.TabActivity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.StateListDrawable;
import android.os.Bundle;
import android.view.Gravity;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TabHost;
import android.widget.LinearLayout.LayoutParams;
public class MainTabInfoMe extends TabActivity {
public final static int HOME = 1;
public final static int DIARY = 2;
public final static int PROGRESS = 3;
long transactionID = -1;
public static TabHost tabHost;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mytabs);
MyView view = null;
tabHost = getTabHost();
TabHost.TabSpec spec;
Intent intent;
intent = new Intent().setClass(this, TabGroup1Activity.class);
view = new MyView(this, R.drawable.home_select, R.drawable.home, "");
view.setFocusable(true);
spec = tabHost.newTabSpec("Home").setIndicator(view).setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this, TabGroup2Activity.class);
view = new MyView(this, R.drawable.prof_select, R.drawable.prof, "");
view.setFocusable(true);
spec = tabHost.newTabSpec("Diary").setIndicator(view)
.setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this, TabGroup3Activity.class);
view = new MyView(this, R.drawable.bus_select, R.drawable.bus, "");
view.setFocusable(true);
spec = tabHost.newTabSpec("progress").setIndicator(view)
.setContent(intent);
tabHost.addTab(spec);
tabHost.setCurrentTab(0);
tabHost.getTabWidget().getChildAt(0).getLayoutParams().height =
LayoutParams.WRAP_CONTENT;
tabHost.getTabWidget().getChildAt(1).getLayoutParams().height =
LayoutParams.WRAP_CONTENT;
tabHost.getTabWidget().getChildAt(2).getLayoutParams().height =
LayoutParams.WRAP_CONTENT;
if (MyApplication.getFrom().equals("Home")) {
tabHost.setCurrentTab(0);
} else if (MyApplication.getFrom().equals("Diary")) {
tabHost.setCurrentTab(1);
} else if (MyApplication.getFrom().equals("progress")) {
tabHost.setCurrentTab(2);
}
int type = 0;
if (getIntent().getExtras() != null) {
if (getIntent().getExtras().containsKey("from")) {
type = getIntent().getExtras().getInt("from");
switch (type) {
case HOME:
tabHost.setCurrentTab(0);
case DIARY:
tabHost.setCurrentTab(1);
case PROGRESS:
tabHost.setCurrentTab(2);
default:
tabHost.setCurrentTab(0);
}
}
}
}
public long getTransactionID() {
return transactionID;
}
public void setTransactionID(long l) {
transactionID = l;
}
public void switchTabSpecial(int tab) {
tabHost.setCurrentTab(tab);
}
class ChangeTabReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
tabHost = getTabHost();
tabHost.setCurrentTab(1);
}
}
private class MyView extends LinearLayout {
ImageView iv;
public MyView(Context c, int drawable, int drawableselec, String label) {
super(c);
iv = new ImageView(c);
StateListDrawable listDrawable = new StateListDrawable();
listDrawable.addState(SELECTED_STATE_SET, this.getResources()
.getDrawable(drawable));
listDrawable.addState(ENABLED_STATE_SET, this.getResources()
.getDrawable(drawableselec));
iv.setImageDrawable(listDrawable);
iv.setBackgroundColor(Color.TRANSPARENT);
iv.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT, (float) 0.0));
iv.setPadding(0, 0, 0, 5);
setGravity(Gravity.CENTER);
addView(iv);
}
}
#Override
protected void onPause() {
super.onPause();
if( DataClass.isTemp()){
overridePendingTransition(R.anim.slide_in_up, R.anim.slide_in_up);
}
}
#Override
protected void onDestroy() {
super.onDestroy();
finish();
}
}
FirstTab of class
public class TabGroup1Activity extends TabGroupActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
startChildActivity("OptionsActivity", new Intent(this,
MainActivity.class));
}
#Override
public void onBackPressed() {
super.onBackPressed();
DataClass.setTemp(false);
finish();
}
}
SecondTab of class
public class TabGroup2Activity extends TabGroupActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
startChildActivity("OptionsActivity", new Intent(this,
MainActivity.class));
}
#Override
public void onBackPressed() {
super.onBackPressed();
DataClass.setTemp(false);
finish();
}
}
thirdTab of class
public class TabGroup1Activity extends TabGroupActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
startChildActivity("OptionsActivity", new Intent(this,
MainActivity.class));
}
#Override
public void onBackPressed() {
super.onBackPressed();
DataClass.setTemp(false);
finish();
}
}
static variable class
import android.app.Application;
public class MyApplication extends Application {
private static String from = "Home";
public static String getFrom() {
return from;
}
public static void setFrom(String fromPage) {
from = fromPage;
}
}
xml file
<?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" >
<TabHost android:id="#android:id/tabhost" android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<RelativeLayout android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<FrameLayout android:id="#android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<TabWidget android:id="#android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="#drawable/topbarbck"/>
</RelativeLayout>
</TabHost>
</LinearLayout>
put all file and check it
Thanks to all who tried to help me!
I do not have much experience with android but from what I saw, the classes must be executed according to the hierarchy proposed by android. That is, in my case I have a main activity which calls the mine fragments and through one of the fragments I had to call a DialogFragment and this would inflate DialogFragment several Views (my tabs), so I have the following structure:
Activity > Fragment > DialogFragment > Views
Based on this solution for my case was thus:
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.TabHost.TabSpec;
import android.widget.TextView;
public class OrdemEncerramentoFragment extends DialogFragment implements OnTabChangeListener {
private static final String TAG = "FragmentTabs";
public static final String TAB_DADOS_CLIENTE = "dadosCliente";
public static final String TAB_DEFEITO_FALHA = "defeitoFalha";
public static final String TAB_SERVICOS = "servico";
public static final String TAB_MATERIAIS = "material";
public static final String TAB_OBSERVACAO = "observacao";
private View mRoot;
private Button enviar;
private Button cancelar;
private TabHost mTabHost;
private Ordem ordem;
private IClasseBO classeBO;
private IDispacher dispatch;
private IFragmentCo ifrag;
private int mCurrentTab;
public OrdemEncerramentoFragment(IFragmentCo ifrag, Ordem ordem) {
this.ordem = ordem;
this.ifrag = ifrag;
}
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
mRoot = inflater.inflate(R.layout.ordem_encerramento, null);
enviar = (Button) mRoot.findViewById(R.ordem_encerramento.enviar);
cancelar = (Button) mRoot.findViewById(R.ordem_encerramento.cancelar);
cancelar.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
OrdemEncerramentoFragment.this.dismiss();
}
});
enviar.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
View view = mTabHost.getChildAt(0);
new DefeitoFalhaView().getDadosDefeitoFalha(view);
}
});
mTabHost = (TabHost) mRoot.findViewById(android.R.id.tabhost);
setupTabs();
return mRoot;
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
setRetainInstance(true);
mTabHost.setOnTabChangedListener(this);
mTabHost.setCurrentTab(0);
}
private void setupTabs() {
mTabHost.setup(); // importante!
mTabHost.addTab(inserirAba(TAB_DADOS_CLIENTE, R.string.tab_dados_cliente, new DadosClienteView().createView(getActivity())));
mTabHost.addTab(inserirAba(TAB_DEFEITO_FALHA, R.string.tab_defeito_falha, new DefeitoFalhaView().createView(getActivity())));
mTabHost.addTab(inserirAba(TAB_SERVICOS, R.string.tab_servico, new ServicoView().createView(getActivity())));
mTabHost.addTab(inserirAba(TAB_MATERIAIS, R.string.tab_material, new MaterialView().createView(getActivity())));
mTabHost.addTab(inserirAba(TAB_OBSERVACAO, R.string.tab_observacao, new ObservacaoView().createView(getActivity())));
}
private TabSpec inserirAba(String tag, int labelId, final View view) {
TabHost.TabSpec spec = mTabHost.newTabSpec(tag);
spec.setContent(new TabHost.TabContentFactory() {
public View createTabContent(String tag) {
return(view);
}
});
spec.setIndicator(buildTabIndicator(labelId));
return spec;
}
private View buildTabIndicator(int msg) {
View indicator=LayoutInflater.from(mTabHost.getContext()).inflate(R.layout.tabs_bg_view, null);
TextView tv=(TextView)indicator.findViewById(R.id.tabsText);
tv.setText(msg);
return(indicator);
}
#Override
public void onTabChanged(String tabId) {
Log.d(TAG, "onTabChanged(): tabId=" + tabId);
}
public View getmRoot() {
return mRoot;
}
}
This question already has answers here:
Focusable EditText inside ListView
(13 answers)
Closed 9 years ago.
I have an EditText inside each row of a ListView. For some reason, when I tap the EditText, it briefly gains focus but then immediately loses it.
I have tried these things:
listView.setItemsCanFocus(true);
editText.setFocusable(true);
While the EditText is (briefly) focused, the Enter key says Next and the auto-correct bar is present. When it loses focus, the soft keyboard stays up, but the Enter key becomes a Return Arrow, and the auto-correct bar disappears.
If you want list items to take focus, its pretty straight forward:
Make the listview not focusable, and say that its focus is afterDescendants which lets EditTexts and other focusable items take focus.
<ListView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:descendantFocusability="afterDescendants"
android:focusable="false" />
EditTexts within ListViews are extremely tricky. I'd suggest you avoid them like the plague if possible. When I was messing with them, this answer was helpful though. If you only have a few items you can always just use a ScrollView.
I know this question is old, but I was also fighting with EditText and ListView today. I think lose time with focus problem or updating dataset is the end of the line.
So, I created an Activity and dynamically added the controls I need. In this example I added two controls: a textview and edittext.
First I create a class to be a "container" of the controls and them I put the controls on a ScrollView.
The layout.xml file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/llMain"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/tvHeader"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
<ScrollView
android:id="#+id/svMain"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:id="#+id/llForm"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
</LinearLayout>
</ScrollView>
</LinearLayout>
The "containter" class:
import android.widget.EditText;
public class RespostasArray {
private int pergunta;
private int formulario;
private int form_coord;
private int coordenada;
private int form_resposta;
private String questao;
private String resposta;
private EditText respostaEdit;
public RespostasArray() {
respostaEdit = null;
}
public int getPergunta() {
return pergunta;
}
public void setPergunta(int pergunta) {
this.pergunta = pergunta;
}
public int getFormulario() {
return formulario;
}
public void setFormulario(int formulario) {
this.formulario = formulario;
}
public int getForm_coord() {
return form_coord;
}
public void setForm_coord(int form_coord) {
this.form_coord = form_coord;
}
public int getCoordenada() {
return coordenada;
}
public void setCoordenada(int coordenada) {
this.coordenada = coordenada;
}
public int getForm_resposta() {
return form_resposta;
}
public void setForm_resposta(int form_resposta) {
this.form_resposta = form_resposta;
}
public String getQuestao() {
return questao;
}
public void setQuestao(String questao) {
this.questao = questao;
}
public String getResposta() {
return resposta;
}
public void setResposta(String resposta) {
this.resposta = resposta;
}
public EditText getRespostaEdit() {
return respostaEdit;
}
public void setRespostaEdit(EditText respostaEdit) {
this.respostaEdit = respostaEdit;
}
}
So, the main activity:
import java.util.ArrayList;
import java.util.List;
import jv.android.getpoint.R;
import jv.android.getpointlib.data.Formularios;
import jv.android.getpointlib.data.GetPointDataHelper;
import jv.android.getpointlib.data.Respostas;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.WindowManager.LayoutParams;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
public class RespostasActivity extends Activity {
private Intent intent;
private ArrayList<RespostasArray> listItems = null;
private GetPointDataHelper db = null;
private int coordenada = -1;
private int formulario = -1;
private LinearLayout llRespostas;
private TextView tvRespostasHeader;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout.xml);
llRespostas = (LinearLayout)findViewById(R.id.llRespostas);
tvHeader = (TextView)findViewById(R.id.tvHeader);
listItems = new ArrayList<RespostasArray>();
db = new GetPointDataHelper(this, false);
intent = getIntent();
if (intent != null)
{
Bundle params = intent.getExtras();
if (params != null) {
coordenada = params.getInt("coordenada");
formulario = params.getInt("formulario");
tvHeader.setText("Some header");
// Load the fields I need from SQLite. Change it to your needs.
List<Respostas> respostas = db.selectAllRespostaDaCoordenada (coordenada, formulario);
if (respostas != null && respostas.size() > 0) {
for (int i = 0; i < respostas.size(); i++) {
TextView tv = new TextView(getApplicationContext());
tv.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
tv.setText(respostas.get(i).getQuestao().trim() + (respostas.get(i).getQuestao().trim().endsWith(":") ? "" : ":"));
llRespostas.addView(tv);
EditText et = new EditText(getApplicationContext());
et.setText(respostas.get(i).getResposta().trim());
et.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
llRespostas.addView(et);
RespostasArray ra = new RespostasArray();
ra.setCoordenada(respostas.get(i).getCoordenada());
ra.setForm_coord(respostas.get(i).getForm_coord());
ra.setForm_resposta(respostas.get(i).getForm_resposta());
ra.setFormulario(respostas.get(i).getFormulario());
ra.setPergunta(respostas.get(i).getPergunta());
ra.setQuestao(respostas.get(i).getQuestao());
ra.setResposta(respostas.get(i).getResposta());
ra.setRespostaEdit(et);
listItems.add(ra);
}
}
}
}
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
doProcessResult();
}
return super.onKeyDown(keyCode, event);
}
private void doProcessResult() {
for (int i = 0; i < listItems.size(); i++) {
if (listItems.get(i).getForm_resposta() == 0) {
db.insertResposta(listItems.get(i).getFormulario(), listItems.get(i).getForm_coord(), listItems.get(i).getPergunta(), listItems.get(i).getRespostaEdit().getText().toString());
} else {
db.updateResposta(listItems.get(i).getForm_resposta(), listItems.get(i).getRespostaEdit().getText().toString());
}
}
}
}
I hope it helps someone.
I've made a full working three TAB-GUI in Android SDK (2.3.3) but I want to integrate several radiobuttons and one radiogroup into a TAB. I'm a android-newbe so I hope somebody can help me?
Hereby the main-code and the according Class-code:
MAIN
package com.example.androidtablayout;
import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;
public class AndroidTabLayoutActivity extends TabActivity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TabHost tabHost = getTabHost();
// Tab for Dag
TabSpec dagspec = tabHost.newTabSpec("Dag");
dagspec.setIndicator("DagRooster", getResources().getDrawable(R.drawable.icon_dag_tab));
Intent dagIntent = new Intent(this, DagActivity.class);
dagspec.setContent(dagIntent);
// Tab for Norm
TabSpec normspec = tabHost.newTabSpec("Norm");
// setting Title and Icon for the Tab
normspec.setIndicator("Normaal", getResources().getDrawable(R.drawable.icon_norm_tab));
Intent normIntent = new Intent(this, NormActivity.class);
normspec.setContent(normIntent);
// Tab for Instel
TabSpec instelspec = tabHost.newTabSpec("Instel");
instelspec.setIndicator("Info", getResources().getDrawable(R.drawable.icon_setting_tab));
Intent instelIntent = new Intent(this, InstelActivity.class);
instelspec.setContent(instelIntent);
// Adding all TabSpec to TabHost
tabHost.addTab(dagspec); // Adding photos tab
tabHost.addTab(normspec); // Adding songs tab
tabHost.addTab(instelspec); // Adding videos tab
}
}
NORM-CLASS code
package com.example.androidtablayout;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;
public class NormActivity extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.instel_layout);
}
}
In the activity you want the radioButtons, .. Not TabActivty.. try something like this below. NB: pay atention to the radiobutton stuff.. I ripped some code for location management out before posting. But the radioButton logic will get you what you want.
public class Distribute extends Activity implements OnClickListener {
#SuppressWarnings("unused")
private static final String DEBUG_FLAG = "DEBUG_FLAG";
private static final String SAVE_PREFS = "save_prefs";
private static RadioButton mUserTypeRadioButton1;
private static RadioButton mUserTypeRadioButton2;
private static RadioButton mUserTypeRadioButton3;
private static RadioButton mUserTypeRadioButton4;
private static CheckBox mSavePrefs;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView textview = new TextView(this);
textview.setText("Distribution");
setContentView(R.drawable.configup);
Button button = (Button)findViewById(R.id.btn_configup1);
mUserTypeRadioButton1 = (RadioButton) findViewById(R.id.rb_configup1);
mUserTypeRadioButton2 = (RadioButton) findViewById(R.id.rb_configup2);
mUserTypeRadioButton3 = (RadioButton) findViewById(R.id.rb_configup3);
mUserTypeRadioButton4 = (RadioButton) findViewById(R.id.rb_configup4);
mSavePrefs = (CheckBox) findViewById(R.id.cb_config1);
button.setOnClickListener(this);
}
public void onClick(View v) {
Intent i = new Intent();
Bundle extras = new Bundle();
int userType = 0;
int savePrefs = 0;
//i.putExtra("passLoc", currLoc);
i.setClass(getApplicationContext(), Clevel.class);
i.putExtras(extras);
if (mUserTypeRadioButton1.isChecked()) {
userType = 1;
}
else if (mUserTypeRadioButton2.isChecked()) {
userType = 2;
}
else if (mUserTypeRadioButton3.isChecked()) {
userType = 3;
}
else if (mUserTypeRadioButton4.isChecked()) {
userType = 4;
}
if (mSavePrefs.isChecked()) {
savePrefs = 1;
}
else {
savePrefs = 0;
}
if (userType == 4){
Toast.makeText(this, "Ad-Hoc Reporting is not Yet Implemented", 5).show();
i.putExtra("SetTab", 1);
}
else if (userType == 0){
Toast.makeText(this, "Please Select a Report Group", 5).show();
i.putExtra("SetTab", 1);
}
else {
i.putExtra("ds", userType);
i.putExtra("prefs", savePrefs);
i.putExtra("SetTab", 2);
}
startActivity(i);
}
}
In your TabHost you will need something like this to manage the Intent/Bundle
/**
* Distribute TabHost--------------------------------
*/
intent = new Intent().setClass(this, Distribute.class);
if(mTab == 1){
mPos = extras.getInt("lvPos");
mPrefs = extras.getInt("prefs");
intent.putExtra("lvPos", mPos);
intent.putExtra("prefs", mPrefs);
}
spec = tabHost.newTabSpec("Config").setIndicator("Distribute-It!",
res.getDrawable(R.drawable.gixconfig))
.setContent(intent);
tabHost.addTab(spec);
And the needed XML.. Just a start, you'll need to wrap this inside your XML for the screen you want radioButtons to show up
<RadioGroup
android:id="#+id/rg_configup1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
>
<RadioButton
android:id="#+id/rb_configup1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Sales"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
>
</RadioButton>
<RadioButton
android:id="#+id/rb_configup2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Regional"
android:layout_above="#+id/rb_configup1"
android:layout_alignLeft="#+id/rb_configup1"
>
</RadioButton>
<RadioButton
android:id="#+id/rb_configup3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Global"
android:layout_above="#+id/rb_configup2"
android:layout_alignLeft="#+id/rb_configup1"
>
</RadioButton>
<RadioButton
android:id="#+id/rb_configup4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Ad-hoc"
android:layout_above="#+id/rb_configup3"
android:layout_alignLeft="#+id/rb_configup1"
>
</RadioButton>
</RadioGroup>